blob: 27ca676a7092e1fe98288e8757489b95131e8951 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -030025#define TPACPI_SYSFS_VERSION 0x020400
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes 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 Holschuhbee4cd9b2009-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;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300267 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300268 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269 u32 input_device_registered:1;
270 u32 platform_drv_registered:1;
271 u32 platform_drv_attrs_registered:1;
272 u32 sensors_pdrv_registered:1;
273 u32 sensors_pdrv_attrs_registered:1;
274 u32 sensors_pdev_attrs_registered:1;
275 u32 hotkey_poll_active:1;
276} tp_features;
277
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300278static struct {
279 u16 hotkey_mask_ff:1;
280} tp_warned;
281
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200282struct thinkpad_id_data {
283 unsigned int vendor; /* ThinkPad vendor:
284 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
285
286 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
287 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
288
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300289 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300291 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
292 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300294 char *model_str; /* ThinkPad T43 */
295 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200296};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200297static struct thinkpad_id_data thinkpad_id;
298
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300299static enum {
300 TPACPI_LIFE_INIT = 0,
301 TPACPI_LIFE_RUNNING,
302 TPACPI_LIFE_EXITING,
303} tpacpi_lifecycle;
304
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200305static int experimental;
306static u32 dbg_level;
307
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300308static struct workqueue_struct *tpacpi_wq;
309
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000310enum led_status_t {
311 TPACPI_LED_OFF = 0,
312 TPACPI_LED_ON,
313 TPACPI_LED_BLINK,
314};
315
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300316/* Special LED class that can defer work */
317struct tpacpi_led_classdev {
318 struct led_classdev led_classdev;
319 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000320 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300321 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300322};
323
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200324#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
325static int dbg_wlswemul;
326static int tpacpi_wlsw_emulstate;
327static int dbg_bluetoothemul;
328static int tpacpi_bluetooth_emulstate;
329static int dbg_wwanemul;
330static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200331static int dbg_uwbemul;
332static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200333#endif
334
335
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000336/*************************************************************************
337 * Debugging helpers
338 */
339
340#define dbg_printk(a_dbg_level, format, arg...) \
341 do { if (dbg_level & (a_dbg_level)) \
342 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
343 } while (0)
344
345#ifdef CONFIG_THINKPAD_ACPI_DEBUG
346#define vdbg_printk dbg_printk
347static const char *str_supported(int is_supported);
348#else
349#define vdbg_printk(a_dbg_level, format, arg...) \
350 do { } while (0)
351#endif
352
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000353static void tpacpi_log_usertask(const char * const what)
354{
355 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
356 what, task_tgid_vnr(current));
357}
358
359#define tpacpi_disclose_usertask(what, format, arg...) \
360 do { \
361 if (unlikely( \
362 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
363 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
364 printk(TPACPI_DEBUG "%s: PID %d: " format, \
365 what, task_tgid_vnr(current), ## arg); \
366 } \
367 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000368
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300369/*
370 * Quirk handling helpers
371 *
372 * ThinkPad IDs and versions seen in the field so far
373 * are two-characters from the set [0-9A-Z], i.e. base 36.
374 *
375 * We use values well outside that range as specials.
376 */
377
378#define TPACPI_MATCH_ANY 0xffffU
379#define TPACPI_MATCH_UNKNOWN 0U
380
381/* TPID('1', 'Y') == 0x5931 */
382#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
383
384#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_IBM, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
389
390#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
391 { .vendor = PCI_VENDOR_ID_LENOVO, \
392 .bios = TPID(__id1, __id2), \
393 .ec = TPACPI_MATCH_ANY, \
394 .quirks = (__quirk) }
395
396struct tpacpi_quirk {
397 unsigned int vendor;
398 u16 bios;
399 u16 ec;
400 unsigned long quirks;
401};
402
403/**
404 * tpacpi_check_quirks() - search BIOS/EC version on a list
405 * @qlist: array of &struct tpacpi_quirk
406 * @qlist_size: number of elements in @qlist
407 *
408 * Iterates over a quirks list until one is found that matches the
409 * ThinkPad's vendor, BIOS and EC model.
410 *
411 * Returns 0 if nothing matches, otherwise returns the quirks field of
412 * the matching &struct tpacpi_quirk entry.
413 *
414 * The match criteria is: vendor, ec and bios much match.
415 */
416static unsigned long __init tpacpi_check_quirks(
417 const struct tpacpi_quirk *qlist,
418 unsigned int qlist_size)
419{
420 while (qlist_size) {
421 if ((qlist->vendor == thinkpad_id.vendor ||
422 qlist->vendor == TPACPI_MATCH_ANY) &&
423 (qlist->bios == thinkpad_id.bios_model ||
424 qlist->bios == TPACPI_MATCH_ANY) &&
425 (qlist->ec == thinkpad_id.ec_model ||
426 qlist->ec == TPACPI_MATCH_ANY))
427 return qlist->quirks;
428
429 qlist_size--;
430 qlist++;
431 }
432 return 0;
433}
434
435
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436/****************************************************************************
437 ****************************************************************************
438 *
439 * ACPI Helpers and device model
440 *
441 ****************************************************************************
442 ****************************************************************************/
443
444/*************************************************************************
445 * ACPI basic handles
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300448static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200450#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 static acpi_handle object##_handle; \
452 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 static char *object##_paths[] = { paths }
455
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200456TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 "\\_SB.PCI.ISA.EC", /* 570 */
458 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
459 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
460 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
461 "\\_SB.PCI0.ICH3.EC0", /* R31 */
462 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300463 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200465TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
466TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200468TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
469 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400470 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
471 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200474TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400475 "^HKEY", /* R30, R31 */
476 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300477 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400478
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200479TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
480 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
481 "\\_SB.PCI0.VID0", /* 770e */
482 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
483 "\\_SB.PCI0.AGP.VID", /* all others */
484 ); /* R30, R31 */
485
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400486
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300487/*************************************************************************
488 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200489 */
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static int acpi_evalf(acpi_handle handle,
492 void *res, char *method, char *fmt, ...)
493{
494 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400495 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200496 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400497 struct acpi_buffer result, *resultp;
498 union acpi_object out_obj;
499 acpi_status status;
500 va_list ap;
501 char res_type;
502 int success;
503 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200506 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508 }
509
510 if (*fmt == 'q') {
511 quiet = 1;
512 fmt++;
513 } else
514 quiet = 0;
515
516 res_type = *(fmt++);
517
518 params.count = 0;
519 params.pointer = &in_objs[0];
520
521 va_start(ap, fmt);
522 while (*fmt) {
523 char c = *(fmt++);
524 switch (c) {
525 case 'd': /* int */
526 in_objs[params.count].integer.value = va_arg(ap, int);
527 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
528 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400529 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200531 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 "with invalid format character '%c'\n", c);
533 return 0;
534 }
535 }
536 va_end(ap);
537
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538 if (res_type != 'v') {
539 result.length = sizeof(out_obj);
540 result.pointer = &out_obj;
541 resultp = &result;
542 } else
543 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400545 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (res)
550 *(int *)res = out_obj.integer.value;
551 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
552 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 success = status == AE_OK;
555 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 "with invalid format character '%c'\n", res_type);
560 return 0;
561 }
562
563 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200564 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 method, fmt0, status);
566
567 return success;
568}
569
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200570static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300571{
572 int v;
573
574 if (ecrd_handle) {
575 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
576 return 0;
577 *p = v;
578 } else {
579 if (ec_read(i, p) < 0)
580 return 0;
581 }
582
583 return 1;
584}
585
586static int acpi_ec_write(int i, u8 v)
587{
588 if (ecwr_handle) {
589 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
590 return 0;
591 } else {
592 if (ec_write(i, v) < 0)
593 return 0;
594 }
595
596 return 1;
597}
598
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200599#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300600static int _sta(acpi_handle handle)
601{
602 int status;
603
604 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
605 status = 0;
606
607 return status;
608}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200609#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300610
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300611static int issue_thinkpad_cmos_command(int cmos_cmd)
612{
613 if (!cmos_handle)
614 return -ENXIO;
615
616 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
617 return -EIO;
618
619 return 0;
620}
621
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300622/*************************************************************************
623 * ACPI device model
624 */
625
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200626#define TPACPI_ACPIHANDLE_INIT(object) \
627 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200628 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
629
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300630static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300631 acpi_handle *handle, acpi_handle parent,
632 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300633{
634 int i;
635 acpi_status status;
636
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300637 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
638 name);
639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300640 for (i = 0; i < num_paths; i++) {
641 status = acpi_get_handle(parent, paths[i], handle);
642 if (ACPI_SUCCESS(status)) {
643 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300644 dbg_printk(TPACPI_DBG_INIT,
645 "Found ACPI handle %s for %s\n",
646 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300647 return;
648 }
649 }
650
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300651 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
652 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300653 *handle = NULL;
654}
655
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300656static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300657{
658 struct ibm_struct *ibm = data;
659
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300660 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
661 return;
662
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300663 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300664 return;
665
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300666 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300667}
668
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300669static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300670{
671 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300672 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300674 BUG_ON(!ibm->acpi);
675
676 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677 return 0;
678
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300679 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300680 "setting up ACPI notify for %s\n", ibm->name);
681
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300682 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
683 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200684 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300685 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686 return -ENODEV;
687 }
688
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700689 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300690 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200691 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300692 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300694 status = acpi_install_notify_handler(*ibm->acpi->handle,
695 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300696 if (ACPI_FAILURE(status)) {
697 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200698 printk(TPACPI_NOTICE
699 "another device driver is already "
700 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200702 printk(TPACPI_ERR
703 "acpi_install_notify_handler(%s) failed: %d\n",
704 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300705 }
706 return -ENODEV;
707 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300708 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300709 return 0;
710}
711
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300712static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300713{
714 return 0;
715}
716
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300717static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300718{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300719 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300720
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300721 dbg_printk(TPACPI_DBG_INIT,
722 "registering %s as an ACPI driver\n", ibm->name);
723
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300724 BUG_ON(!ibm->acpi);
725
726 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
727 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300728 printk(TPACPI_ERR
729 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300730 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300731 }
732
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200733 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200735
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300736 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300737
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300738 rc = acpi_bus_register_driver(ibm->acpi->driver);
739 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200740 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200741 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300742 kfree(ibm->acpi->driver);
743 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300744 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300745 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300746
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300747 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748}
749
750
751/****************************************************************************
752 ****************************************************************************
753 *
754 * Procfs Helpers
755 *
756 ****************************************************************************
757 ****************************************************************************/
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static int dispatch_procfs_read(char *page, char **start, off_t off,
760 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300761{
762 struct ibm_struct *ibm = data;
763 int len;
764
765 if (!ibm || !ibm->read)
766 return -EINVAL;
767
768 len = ibm->read(page);
769 if (len < 0)
770 return len;
771
772 if (len <= off + count)
773 *eof = 1;
774 *start = page + off;
775 len -= off;
776 if (len > count)
777 len = count;
778 if (len < 0)
779 len = 0;
780
781 return len;
782}
783
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300784static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200785 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787{
788 struct ibm_struct *ibm = data;
789 char *kernbuf;
790 int ret;
791
792 if (!ibm || !ibm->write)
793 return -EINVAL;
794
795 kernbuf = kmalloc(count + 2, GFP_KERNEL);
796 if (!kernbuf)
797 return -ENOMEM;
798
799 if (copy_from_user(kernbuf, userbuf, count)) {
800 kfree(kernbuf);
801 return -EFAULT;
802 }
803
804 kernbuf[count] = 0;
805 strcat(kernbuf, ",");
806 ret = ibm->write(kernbuf);
807 if (ret == 0)
808 ret = count;
809
810 kfree(kernbuf);
811
812 return ret;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static char *next_cmd(char **cmds)
816{
817 char *start = *cmds;
818 char *end;
819
820 while ((end = strchr(start, ',')) && end == start)
821 start = end + 1;
822
823 if (!end)
824 return NULL;
825
826 *end = 0;
827 *cmds = end + 1;
828 return start;
829}
830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300831
832/****************************************************************************
833 ****************************************************************************
834 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300835 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300836 *
837 ****************************************************************************
838 ****************************************************************************/
839
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300840static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300841static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700842static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300843static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300844static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200845static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300846
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200847static int tpacpi_suspend_handler(struct platform_device *pdev,
848 pm_message_t state)
849{
850 struct ibm_struct *ibm, *itmp;
851
852 list_for_each_entry_safe(ibm, itmp,
853 &tpacpi_all_drivers,
854 all_drivers) {
855 if (ibm->suspend)
856 (ibm->suspend)(state);
857 }
858
859 return 0;
860}
861
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300862static int tpacpi_resume_handler(struct platform_device *pdev)
863{
864 struct ibm_struct *ibm, *itmp;
865
866 list_for_each_entry_safe(ibm, itmp,
867 &tpacpi_all_drivers,
868 all_drivers) {
869 if (ibm->resume)
870 (ibm->resume)();
871 }
872
873 return 0;
874}
875
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200876static void tpacpi_shutdown_handler(struct platform_device *pdev)
877{
878 struct ibm_struct *ibm, *itmp;
879
880 list_for_each_entry_safe(ibm, itmp,
881 &tpacpi_all_drivers,
882 all_drivers) {
883 if (ibm->shutdown)
884 (ibm->shutdown)();
885 }
886}
887
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300888static struct platform_driver tpacpi_pdriver = {
889 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200890 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300891 .owner = THIS_MODULE,
892 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200893 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300894 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200895 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300896};
897
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300898static struct platform_driver tpacpi_hwmon_pdriver = {
899 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200900 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300901 .owner = THIS_MODULE,
902 },
903};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300904
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300905/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300906 * sysfs support helpers
907 */
908
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200909struct attribute_set {
910 unsigned int members, max_members;
911 struct attribute_group group;
912};
913
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300914struct attribute_set_obj {
915 struct attribute_set s;
916 struct attribute *a;
917} __attribute__((packed));
918
919static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200920 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300921{
922 struct attribute_set_obj *sobj;
923
924 if (max_members == 0)
925 return NULL;
926
927 /* Allocates space for implicit NULL at the end too */
928 sobj = kzalloc(sizeof(struct attribute_set_obj) +
929 max_members * sizeof(struct attribute *),
930 GFP_KERNEL);
931 if (!sobj)
932 return NULL;
933 sobj->s.max_members = max_members;
934 sobj->s.group.attrs = &sobj->a;
935 sobj->s.group.name = name;
936
937 return &sobj->s;
938}
939
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200940#define destroy_attr_set(_set) \
941 kfree(_set);
942
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300943/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200944static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300945{
946 if (!s || !attr)
947 return -EINVAL;
948
949 if (s->members >= s->max_members)
950 return -ENOMEM;
951
952 s->group.attrs[s->members] = attr;
953 s->members++;
954
955 return 0;
956}
957
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200958static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300959 struct attribute **attr,
960 unsigned int count)
961{
962 int i, res;
963
964 for (i = 0; i < count; i++) {
965 res = add_to_attr_set(s, attr[i]);
966 if (res)
967 return res;
968 }
969
970 return 0;
971}
972
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200973static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300974{
975 sysfs_remove_group(kobj, &s->group);
976 destroy_attr_set(s);
977}
978
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200979#define register_attr_set_with_sysfs(_attr_set, _kobj) \
980 sysfs_create_group(_kobj, &_attr_set->group)
981
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300982static int parse_strtoul(const char *buf,
983 unsigned long max, unsigned long *value)
984{
985 char *endp;
986
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300987 while (*buf && isspace(*buf))
988 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300989 *value = simple_strtoul(buf, &endp, 0);
990 while (*endp && isspace(*endp))
991 endp++;
992 if (*endp || *value > max)
993 return -EINVAL;
994
995 return 0;
996}
997
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -0300998static void tpacpi_disable_brightness_delay(void)
999{
1000 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1001 printk(TPACPI_NOTICE
1002 "ACPI backlight control delay disabled\n");
1003}
1004
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001005static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1006{
1007 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1008 union acpi_object *obj;
1009 int rc;
1010
1011 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1012 obj = (union acpi_object *)buffer.pointer;
1013 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1014 printk(TPACPI_ERR "Unknown _BCL data, "
1015 "please report this to %s\n", TPACPI_MAIL);
1016 rc = 0;
1017 } else {
1018 rc = obj->package.count;
1019 }
1020 } else {
1021 return 0;
1022 }
1023
1024 kfree(buffer.pointer);
1025 return rc;
1026}
1027
1028static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1029 u32 lvl, void *context, void **rv)
1030{
1031 char name[ACPI_PATH_SEGMENT_LENGTH];
1032 struct acpi_buffer buffer = { sizeof(name), &name };
1033
1034 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1035 !strncmp("_BCL", name, sizeof(name) - 1)) {
1036 BUG_ON(!rv || !*rv);
1037 **(int **)rv = tpacpi_query_bcl_levels(handle);
1038 return AE_CTRL_TERMINATE;
1039 } else {
1040 return AE_OK;
1041 }
1042}
1043
1044/*
1045 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1046 */
1047static int __init tpacpi_check_std_acpi_brightness_support(void)
1048{
1049 int status;
1050 int bcl_levels = 0;
1051 void *bcl_ptr = &bcl_levels;
1052
1053 if (!vid_handle) {
1054 TPACPI_ACPIHANDLE_INIT(vid);
1055 }
1056 if (!vid_handle)
1057 return 0;
1058
1059 /*
1060 * Search for a _BCL method, and execute it. This is safe on all
1061 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1062 * BIOS in ACPI backlight control mode. We do NOT have to care
1063 * about calling the _BCL method in an enabled video device, any
1064 * will do for our purposes.
1065 */
1066
1067 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1068 tpacpi_acpi_walk_find_bcl, NULL,
1069 &bcl_ptr);
1070
1071 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1072 tp_features.bright_acpimode = 1;
1073 return (bcl_levels - 2);
1074 }
1075
1076 return 0;
1077}
1078
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001079static int __init tpacpi_new_rfkill(const unsigned int id,
1080 struct rfkill **rfk,
1081 const enum rfkill_type rfktype,
1082 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001083 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001084 int (*toggle_radio)(void *, enum rfkill_state),
1085 int (*get_state)(void *, enum rfkill_state *))
1086{
1087 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001088 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1089
1090 res = get_state(NULL, &initial_state);
1091 if (res < 0) {
1092 printk(TPACPI_ERR
1093 "failed to read initial state for %s, error %d; "
1094 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001095 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001096 /* try to set the initial state as the default for the rfkill
1097 * type, since we ask the firmware to preserve it across S5 in
1098 * NVRAM */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00001099 if (rfkill_set_default(rfktype,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001100 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1101 RFKILL_STATE_UNBLOCKED :
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00001102 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1103 vdbg_printk(TPACPI_DBG_RFKILL,
1104 "Default state for %s cannot be changed\n",
1105 name);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001106 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001107
1108 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1109 if (!*rfk) {
1110 printk(TPACPI_ERR
1111 "failed to allocate memory for rfkill class\n");
1112 return -ENOMEM;
1113 }
1114
1115 (*rfk)->name = name;
1116 (*rfk)->get_state = get_state;
1117 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001118 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001119
1120 res = rfkill_register(*rfk);
1121 if (res < 0) {
1122 printk(TPACPI_ERR
1123 "failed to register %s rfkill switch: %d\n",
1124 name, res);
1125 rfkill_free(*rfk);
1126 *rfk = NULL;
1127 return res;
1128 }
1129
1130 return 0;
1131}
1132
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001133static void printk_deprecated_attribute(const char * const what,
1134 const char * const details)
1135{
1136 tpacpi_log_usertask("deprecated sysfs attribute");
1137 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1138 "will be removed. %s\n",
1139 what, details);
1140}
1141
1142static void printk_deprecated_rfkill_attribute(const char * const what)
1143{
1144 printk_deprecated_attribute(what,
1145 "Please switch to generic rfkill before year 2010");
1146}
1147
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001148/*************************************************************************
1149 * thinkpad-acpi driver attributes
1150 */
1151
1152/* interface_version --------------------------------------------------- */
1153static ssize_t tpacpi_driver_interface_version_show(
1154 struct device_driver *drv,
1155 char *buf)
1156{
1157 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1158}
1159
1160static DRIVER_ATTR(interface_version, S_IRUGO,
1161 tpacpi_driver_interface_version_show, NULL);
1162
1163/* debug_level --------------------------------------------------------- */
1164static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1165 char *buf)
1166{
1167 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1168}
1169
1170static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1171 const char *buf, size_t count)
1172{
1173 unsigned long t;
1174
1175 if (parse_strtoul(buf, 0xffff, &t))
1176 return -EINVAL;
1177
1178 dbg_level = t;
1179
1180 return count;
1181}
1182
1183static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1184 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1185
1186/* version ------------------------------------------------------------- */
1187static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1188 char *buf)
1189{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001190 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1191 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001192}
1193
1194static DRIVER_ATTR(version, S_IRUGO,
1195 tpacpi_driver_version_show, NULL);
1196
1197/* --------------------------------------------------------------------- */
1198
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001199#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1200
1201static void tpacpi_send_radiosw_update(void);
1202
1203/* wlsw_emulstate ------------------------------------------------------ */
1204static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1205 char *buf)
1206{
1207 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1208}
1209
1210static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1211 const char *buf, size_t count)
1212{
1213 unsigned long t;
1214
1215 if (parse_strtoul(buf, 1, &t))
1216 return -EINVAL;
1217
1218 if (tpacpi_wlsw_emulstate != t) {
1219 tpacpi_wlsw_emulstate = !!t;
1220 tpacpi_send_radiosw_update();
1221 } else
1222 tpacpi_wlsw_emulstate = !!t;
1223
1224 return count;
1225}
1226
1227static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1228 tpacpi_driver_wlsw_emulstate_show,
1229 tpacpi_driver_wlsw_emulstate_store);
1230
1231/* bluetooth_emulstate ------------------------------------------------- */
1232static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1233 struct device_driver *drv,
1234 char *buf)
1235{
1236 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1237}
1238
1239static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1240 struct device_driver *drv,
1241 const char *buf, size_t count)
1242{
1243 unsigned long t;
1244
1245 if (parse_strtoul(buf, 1, &t))
1246 return -EINVAL;
1247
1248 tpacpi_bluetooth_emulstate = !!t;
1249
1250 return count;
1251}
1252
1253static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1254 tpacpi_driver_bluetooth_emulstate_show,
1255 tpacpi_driver_bluetooth_emulstate_store);
1256
1257/* wwan_emulstate ------------------------------------------------- */
1258static ssize_t tpacpi_driver_wwan_emulstate_show(
1259 struct device_driver *drv,
1260 char *buf)
1261{
1262 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1263}
1264
1265static ssize_t tpacpi_driver_wwan_emulstate_store(
1266 struct device_driver *drv,
1267 const char *buf, size_t count)
1268{
1269 unsigned long t;
1270
1271 if (parse_strtoul(buf, 1, &t))
1272 return -EINVAL;
1273
1274 tpacpi_wwan_emulstate = !!t;
1275
1276 return count;
1277}
1278
1279static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1280 tpacpi_driver_wwan_emulstate_show,
1281 tpacpi_driver_wwan_emulstate_store);
1282
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001283/* uwb_emulstate ------------------------------------------------- */
1284static ssize_t tpacpi_driver_uwb_emulstate_show(
1285 struct device_driver *drv,
1286 char *buf)
1287{
1288 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1289}
1290
1291static ssize_t tpacpi_driver_uwb_emulstate_store(
1292 struct device_driver *drv,
1293 const char *buf, size_t count)
1294{
1295 unsigned long t;
1296
1297 if (parse_strtoul(buf, 1, &t))
1298 return -EINVAL;
1299
1300 tpacpi_uwb_emulstate = !!t;
1301
1302 return count;
1303}
1304
1305static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1306 tpacpi_driver_uwb_emulstate_show,
1307 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001308#endif
1309
1310/* --------------------------------------------------------------------- */
1311
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001312static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001313 &driver_attr_debug_level, &driver_attr_version,
1314 &driver_attr_interface_version,
1315};
1316
1317static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1318{
1319 int i, res;
1320
1321 i = 0;
1322 res = 0;
1323 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1324 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1325 i++;
1326 }
1327
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001328#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1329 if (!res && dbg_wlswemul)
1330 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1331 if (!res && dbg_bluetoothemul)
1332 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1333 if (!res && dbg_wwanemul)
1334 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001335 if (!res && dbg_uwbemul)
1336 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001337#endif
1338
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001339 return res;
1340}
1341
1342static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1343{
1344 int i;
1345
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001346 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001347 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001348
1349#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1350 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1351 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1352 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001353 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001354#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001355}
1356
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001357/****************************************************************************
1358 ****************************************************************************
1359 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001360 * Subdrivers
1361 *
1362 ****************************************************************************
1363 ****************************************************************************/
1364
1365/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001366 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001367 */
1368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001369static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001371 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1372 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001374 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001375 (thinkpad_id.bios_version_str) ?
1376 thinkpad_id.bios_version_str : "unknown",
1377 (thinkpad_id.ec_version_str) ?
1378 thinkpad_id.ec_version_str : "unknown");
1379
1380 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001381 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001382 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1383 "IBM" : ((thinkpad_id.vendor ==
1384 PCI_VENDOR_ID_LENOVO) ?
1385 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001386 thinkpad_id.model_str,
1387 (thinkpad_id.nummodel_str) ?
1388 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
1391}
1392
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001393static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 int len = 0;
1396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001397 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1398 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 return len;
1401}
1402
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001403static struct ibm_struct thinkpad_acpi_driver_data = {
1404 .name = "driver",
1405 .read = thinkpad_acpi_driver_read,
1406};
1407
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001408/*************************************************************************
1409 * Hotkey subdriver
1410 */
1411
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001412enum { /* hot key scan codes (derived from ACPI DSDT) */
1413 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1414 TP_ACPI_HOTKEYSCAN_FNF2,
1415 TP_ACPI_HOTKEYSCAN_FNF3,
1416 TP_ACPI_HOTKEYSCAN_FNF4,
1417 TP_ACPI_HOTKEYSCAN_FNF5,
1418 TP_ACPI_HOTKEYSCAN_FNF6,
1419 TP_ACPI_HOTKEYSCAN_FNF7,
1420 TP_ACPI_HOTKEYSCAN_FNF8,
1421 TP_ACPI_HOTKEYSCAN_FNF9,
1422 TP_ACPI_HOTKEYSCAN_FNF10,
1423 TP_ACPI_HOTKEYSCAN_FNF11,
1424 TP_ACPI_HOTKEYSCAN_FNF12,
1425 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1426 TP_ACPI_HOTKEYSCAN_FNINSERT,
1427 TP_ACPI_HOTKEYSCAN_FNDELETE,
1428 TP_ACPI_HOTKEYSCAN_FNHOME,
1429 TP_ACPI_HOTKEYSCAN_FNEND,
1430 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1431 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1432 TP_ACPI_HOTKEYSCAN_FNSPACE,
1433 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1434 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1435 TP_ACPI_HOTKEYSCAN_MUTE,
1436 TP_ACPI_HOTKEYSCAN_THINKPAD,
1437};
1438
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001439enum { /* Keys available through NVRAM polling */
1440 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1441 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1442};
1443
1444enum { /* Positions of some of the keys in hotkey masks */
1445 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1446 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1447 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1448 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1449 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1450 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1451 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1452 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1453 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1454 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1455 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1456};
1457
1458enum { /* NVRAM to ACPI HKEY group map */
1459 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1460 TP_ACPI_HKEY_ZOOM_MASK |
1461 TP_ACPI_HKEY_DISPSWTCH_MASK |
1462 TP_ACPI_HKEY_HIBERNATE_MASK,
1463 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1464 TP_ACPI_HKEY_BRGHTDWN_MASK,
1465 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1466 TP_ACPI_HKEY_VOLDWN_MASK |
1467 TP_ACPI_HKEY_MUTE_MASK,
1468};
1469
1470#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1471struct tp_nvram_state {
1472 u16 thinkpad_toggle:1;
1473 u16 zoom_toggle:1;
1474 u16 display_toggle:1;
1475 u16 thinklight_toggle:1;
1476 u16 hibernate_toggle:1;
1477 u16 displayexp_toggle:1;
1478 u16 display_state:1;
1479 u16 brightness_toggle:1;
1480 u16 volume_toggle:1;
1481 u16 mute:1;
1482
1483 u8 brightness_level;
1484 u8 volume_level;
1485};
1486
1487static struct task_struct *tpacpi_hotkey_task;
1488static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1489static int hotkey_poll_freq = 10; /* Hz */
1490static struct mutex hotkey_thread_mutex;
1491static struct mutex hotkey_thread_data_mutex;
1492static unsigned int hotkey_config_change;
1493
1494#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1495
1496#define hotkey_source_mask 0U
1497
1498#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1499
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001500static struct mutex hotkey_mutex;
1501
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001502static enum { /* Reasons for waking up */
1503 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1504 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1505 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1506} hotkey_wakeup_reason;
1507
1508static int hotkey_autosleep_ack;
1509
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001510static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001511static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001512static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001513static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001514
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001515static unsigned int hotkey_report_mode;
1516
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001517static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001518
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001519static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001520
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001521#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1522#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001523 do { \
1524 mutex_lock(&hotkey_thread_data_mutex); \
1525 hotkey_config_change++; \
1526 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001527#define HOTKEY_CONFIG_CRITICAL_END \
1528 mutex_unlock(&hotkey_thread_data_mutex);
1529#else
1530#define HOTKEY_CONFIG_CRITICAL_START
1531#define HOTKEY_CONFIG_CRITICAL_END
1532#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1533
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001534/* HKEY.MHKG() return bits */
1535#define TP_HOTKEY_TABLET_MASK (1 << 3)
1536
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001537static int hotkey_get_wlsw(int *status)
1538{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001539#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1540 if (dbg_wlswemul) {
1541 *status = !!tpacpi_wlsw_emulstate;
1542 return 0;
1543 }
1544#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001545 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1546 return -EIO;
1547 return 0;
1548}
1549
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001550static int hotkey_get_tablet_mode(int *status)
1551{
1552 int s;
1553
1554 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1555 return -EIO;
1556
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001557 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1558 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001559}
1560
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001561/*
1562 * Call with hotkey_mutex held
1563 */
1564static int hotkey_mask_get(void)
1565{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001566 u32 m = 0;
1567
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001568 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001569 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001570 return -EIO;
1571 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001572 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001573
1574 return 0;
1575}
1576
1577/*
1578 * Call with hotkey_mutex held
1579 */
1580static int hotkey_mask_set(u32 mask)
1581{
1582 int i;
1583 int rc = 0;
1584
1585 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001586 if (!tp_warned.hotkey_mask_ff &&
1587 (mask == 0xffff || mask == 0xffffff ||
1588 mask == 0xffffffff)) {
1589 tp_warned.hotkey_mask_ff = 1;
1590 printk(TPACPI_NOTICE
1591 "setting the hotkey mask to 0x%08x is likely "
1592 "not the best way to go about it\n", mask);
1593 printk(TPACPI_NOTICE
1594 "please consider using the driver defaults, "
1595 "and refer to up-to-date thinkpad-acpi "
1596 "documentation\n");
1597 }
1598
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001599 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001600 for (i = 0; i < 32; i++) {
1601 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001602 /* enable in firmware mask only keys not in NVRAM
1603 * mode, but enable the key in the cached hotkey_mask
1604 * regardless of mode, or the key will end up
1605 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001606 if (!acpi_evalf(hkey_handle,
1607 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001608 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001609 rc = -EIO;
1610 break;
1611 } else {
1612 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1613 }
1614 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001615 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001616
1617 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001618 if (!hotkey_mask_get() && !rc &&
1619 (hotkey_mask & ~hotkey_source_mask) !=
1620 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001621 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001622 "requested hot key mask 0x%08x, but "
1623 "firmware forced it to 0x%08x\n",
1624 mask, hotkey_mask);
1625 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001626 } else {
1627#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1628 HOTKEY_CONFIG_CRITICAL_START
1629 hotkey_mask = mask & hotkey_source_mask;
1630 HOTKEY_CONFIG_CRITICAL_END
1631 hotkey_mask_get();
1632 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001633 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001634 "requested hot key mask 0x%08x, "
1635 "forced to 0x%08x (NVRAM poll mask is "
1636 "0x%08x): no firmware mask support\n",
1637 mask, hotkey_mask, hotkey_source_mask);
1638 }
1639#else
1640 hotkey_mask_get();
1641 rc = -ENXIO;
1642#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001643 }
1644
1645 return rc;
1646}
1647
1648static int hotkey_status_get(int *status)
1649{
1650 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1651 return -EIO;
1652
1653 return 0;
1654}
1655
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001656static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001657{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001658 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001659 return -EIO;
1660
1661 return 0;
1662}
1663
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001664static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001665{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001666 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001667
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001668 if (tp_features.hotkey_tablet &&
1669 !hotkey_get_tablet_mode(&state)) {
1670 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001671
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001672 input_report_switch(tpacpi_inputdev,
1673 SW_TABLET_MODE, !!state);
1674 input_sync(tpacpi_inputdev);
1675
1676 mutex_unlock(&tpacpi_inputdev_send_mutex);
1677 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001678}
1679
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001680static void tpacpi_input_send_key(unsigned int scancode)
1681{
1682 unsigned int keycode;
1683
1684 keycode = hotkey_keycode_map[scancode];
1685
1686 if (keycode != KEY_RESERVED) {
1687 mutex_lock(&tpacpi_inputdev_send_mutex);
1688
1689 input_report_key(tpacpi_inputdev, keycode, 1);
1690 if (keycode == KEY_UNKNOWN)
1691 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1692 scancode);
1693 input_sync(tpacpi_inputdev);
1694
1695 input_report_key(tpacpi_inputdev, keycode, 0);
1696 if (keycode == KEY_UNKNOWN)
1697 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1698 scancode);
1699 input_sync(tpacpi_inputdev);
1700
1701 mutex_unlock(&tpacpi_inputdev_send_mutex);
1702 }
1703}
1704
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001705#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1707
1708static void tpacpi_hotkey_send_key(unsigned int scancode)
1709{
1710 tpacpi_input_send_key(scancode);
1711 if (hotkey_report_mode < 2) {
1712 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1713 0x80, 0x1001 + scancode);
1714 }
1715}
1716
1717static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1718{
1719 u8 d;
1720
1721 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1722 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1723 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1724 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1725 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1726 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1727 }
1728 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1729 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1730 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1731 }
1732 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1733 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1734 n->displayexp_toggle =
1735 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1736 }
1737 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1738 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1739 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1741 n->brightness_toggle =
1742 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1743 }
1744 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1745 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1746 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1747 >> TP_NVRAM_POS_LEVEL_VOLUME;
1748 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1749 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1750 }
1751}
1752
1753#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001754 do { \
1755 if ((mask & (1 << __scancode)) && \
1756 oldn->__member != newn->__member) \
1757 tpacpi_hotkey_send_key(__scancode); \
1758 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001759
1760#define TPACPI_MAY_SEND_KEY(__scancode) \
1761 do { if (mask & (1 << __scancode)) \
1762 tpacpi_hotkey_send_key(__scancode); } while (0)
1763
1764static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001765 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001766 u32 mask)
1767{
1768 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1769 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1770 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1771 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1772
1773 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1774
1775 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1776
1777 /* handle volume */
1778 if (oldn->volume_toggle != newn->volume_toggle) {
1779 if (oldn->mute != newn->mute) {
1780 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1781 }
1782 if (oldn->volume_level > newn->volume_level) {
1783 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1784 } else if (oldn->volume_level < newn->volume_level) {
1785 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1786 } else if (oldn->mute == newn->mute) {
1787 /* repeated key presses that didn't change state */
1788 if (newn->mute) {
1789 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1790 } else if (newn->volume_level != 0) {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1792 } else {
1793 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1794 }
1795 }
1796 }
1797
1798 /* handle brightness */
1799 if (oldn->brightness_toggle != newn->brightness_toggle) {
1800 if (oldn->brightness_level < newn->brightness_level) {
1801 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1802 } else if (oldn->brightness_level > newn->brightness_level) {
1803 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1804 } else {
1805 /* repeated key presses that didn't change state */
1806 if (newn->brightness_level != 0) {
1807 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1808 } else {
1809 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1810 }
1811 }
1812 }
1813}
1814
1815#undef TPACPI_COMPARE_KEY
1816#undef TPACPI_MAY_SEND_KEY
1817
1818static int hotkey_kthread(void *data)
1819{
1820 struct tp_nvram_state s[2];
1821 u32 mask;
1822 unsigned int si, so;
1823 unsigned long t;
1824 unsigned int change_detector, must_reset;
1825
1826 mutex_lock(&hotkey_thread_mutex);
1827
1828 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1829 goto exit;
1830
1831 set_freezable();
1832
1833 so = 0;
1834 si = 1;
1835 t = 0;
1836
1837 /* Initial state for compares */
1838 mutex_lock(&hotkey_thread_data_mutex);
1839 change_detector = hotkey_config_change;
1840 mask = hotkey_source_mask & hotkey_mask;
1841 mutex_unlock(&hotkey_thread_data_mutex);
1842 hotkey_read_nvram(&s[so], mask);
1843
1844 while (!kthread_should_stop() && hotkey_poll_freq) {
1845 if (t == 0)
1846 t = 1000/hotkey_poll_freq;
1847 t = msleep_interruptible(t);
1848 if (unlikely(kthread_should_stop()))
1849 break;
1850 must_reset = try_to_freeze();
1851 if (t > 0 && !must_reset)
1852 continue;
1853
1854 mutex_lock(&hotkey_thread_data_mutex);
1855 if (must_reset || hotkey_config_change != change_detector) {
1856 /* forget old state on thaw or config change */
1857 si = so;
1858 t = 0;
1859 change_detector = hotkey_config_change;
1860 }
1861 mask = hotkey_source_mask & hotkey_mask;
1862 mutex_unlock(&hotkey_thread_data_mutex);
1863
1864 if (likely(mask)) {
1865 hotkey_read_nvram(&s[si], mask);
1866 if (likely(si != so)) {
1867 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001868 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001869 }
1870 }
1871
1872 so = si;
1873 si ^= 1;
1874 }
1875
1876exit:
1877 mutex_unlock(&hotkey_thread_mutex);
1878 return 0;
1879}
1880
1881static void hotkey_poll_stop_sync(void)
1882{
1883 if (tpacpi_hotkey_task) {
1884 if (frozen(tpacpi_hotkey_task) ||
1885 freezing(tpacpi_hotkey_task))
1886 thaw_process(tpacpi_hotkey_task);
1887
1888 kthread_stop(tpacpi_hotkey_task);
1889 tpacpi_hotkey_task = NULL;
1890 mutex_lock(&hotkey_thread_mutex);
1891 /* at this point, the thread did exit */
1892 mutex_unlock(&hotkey_thread_mutex);
1893 }
1894}
1895
1896/* call with hotkey_mutex held */
1897static void hotkey_poll_setup(int may_warn)
1898{
1899 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1900 hotkey_poll_freq > 0 &&
1901 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1902 if (!tpacpi_hotkey_task) {
1903 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001904 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001905 if (IS_ERR(tpacpi_hotkey_task)) {
1906 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001907 printk(TPACPI_ERR
1908 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001909 "for hotkey polling\n");
1910 }
1911 }
1912 } else {
1913 hotkey_poll_stop_sync();
1914 if (may_warn &&
1915 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001916 printk(TPACPI_NOTICE
1917 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001918 "which is currently disabled\n",
1919 hotkey_source_mask);
1920 }
1921 }
1922}
1923
1924static void hotkey_poll_setup_safe(int may_warn)
1925{
1926 mutex_lock(&hotkey_mutex);
1927 hotkey_poll_setup(may_warn);
1928 mutex_unlock(&hotkey_mutex);
1929}
1930
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001931#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1932
1933static void hotkey_poll_setup_safe(int __unused)
1934{
1935}
1936
1937#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1938
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001939static int hotkey_inputdev_open(struct input_dev *dev)
1940{
1941 switch (tpacpi_lifecycle) {
1942 case TPACPI_LIFE_INIT:
1943 /*
1944 * hotkey_init will call hotkey_poll_setup_safe
1945 * at the appropriate moment
1946 */
1947 return 0;
1948 case TPACPI_LIFE_EXITING:
1949 return -EBUSY;
1950 case TPACPI_LIFE_RUNNING:
1951 hotkey_poll_setup_safe(0);
1952 return 0;
1953 }
1954
1955 /* Should only happen if tpacpi_lifecycle is corrupt */
1956 BUG();
1957 return -EBUSY;
1958}
1959
1960static void hotkey_inputdev_close(struct input_dev *dev)
1961{
1962 /* disable hotkey polling when possible */
1963 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1964 hotkey_poll_setup_safe(0);
1965}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001967/* sysfs hotkey enable ------------------------------------------------- */
1968static ssize_t hotkey_enable_show(struct device *dev,
1969 struct device_attribute *attr,
1970 char *buf)
1971{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001972 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001973
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001974 printk_deprecated_attribute("hotkey_enable",
1975 "Hotkey reporting is always enabled");
1976
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001977 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001978 if (res)
1979 return res;
1980
1981 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1982}
1983
1984static ssize_t hotkey_enable_store(struct device *dev,
1985 struct device_attribute *attr,
1986 const char *buf, size_t count)
1987{
1988 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001989
1990 printk_deprecated_attribute("hotkey_enable",
1991 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001992
1993 if (parse_strtoul(buf, 1, &t))
1994 return -EINVAL;
1995
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001996 if (t == 0)
1997 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001998
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001999 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002000}
2001
2002static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002003 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002004 hotkey_enable_show, hotkey_enable_store);
2005
2006/* sysfs hotkey mask --------------------------------------------------- */
2007static ssize_t hotkey_mask_show(struct device *dev,
2008 struct device_attribute *attr,
2009 char *buf)
2010{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002011 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002012
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002013 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002014 return -ERESTARTSYS;
2015 res = hotkey_mask_get();
2016 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002017
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002018 return (res)?
2019 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002020}
2021
2022static ssize_t hotkey_mask_store(struct device *dev,
2023 struct device_attribute *attr,
2024 const char *buf, size_t count)
2025{
2026 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002027 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002028
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002029 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002030 return -EINVAL;
2031
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002032 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002033 return -ERESTARTSYS;
2034
2035 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002036
2037#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2038 hotkey_poll_setup(1);
2039#endif
2040
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002041 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002042
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002043 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2044
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002045 return (res) ? res : count;
2046}
2047
2048static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002049 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002050 hotkey_mask_show, hotkey_mask_store);
2051
2052/* sysfs hotkey bios_enabled ------------------------------------------- */
2053static ssize_t hotkey_bios_enabled_show(struct device *dev,
2054 struct device_attribute *attr,
2055 char *buf)
2056{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002057 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002058}
2059
2060static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002061 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002062
2063/* sysfs hotkey bios_mask ---------------------------------------------- */
2064static ssize_t hotkey_bios_mask_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2067{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002068 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002069}
2070
2071static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002072 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002073
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002074/* sysfs hotkey all_mask ----------------------------------------------- */
2075static ssize_t hotkey_all_mask_show(struct device *dev,
2076 struct device_attribute *attr,
2077 char *buf)
2078{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002079 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2080 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002081}
2082
2083static struct device_attribute dev_attr_hotkey_all_mask =
2084 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2085
2086/* sysfs hotkey recommended_mask --------------------------------------- */
2087static ssize_t hotkey_recommended_mask_show(struct device *dev,
2088 struct device_attribute *attr,
2089 char *buf)
2090{
2091 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002092 (hotkey_all_mask | hotkey_source_mask)
2093 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002094}
2095
2096static struct device_attribute dev_attr_hotkey_recommended_mask =
2097 __ATTR(hotkey_recommended_mask, S_IRUGO,
2098 hotkey_recommended_mask_show, NULL);
2099
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002100#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2101
2102/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2103static ssize_t hotkey_source_mask_show(struct device *dev,
2104 struct device_attribute *attr,
2105 char *buf)
2106{
2107 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2108}
2109
2110static ssize_t hotkey_source_mask_store(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t count)
2113{
2114 unsigned long t;
2115
2116 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2117 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2118 return -EINVAL;
2119
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002120 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002121 return -ERESTARTSYS;
2122
2123 HOTKEY_CONFIG_CRITICAL_START
2124 hotkey_source_mask = t;
2125 HOTKEY_CONFIG_CRITICAL_END
2126
2127 hotkey_poll_setup(1);
2128
2129 mutex_unlock(&hotkey_mutex);
2130
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002131 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2132
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002133 return count;
2134}
2135
2136static struct device_attribute dev_attr_hotkey_source_mask =
2137 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2138 hotkey_source_mask_show, hotkey_source_mask_store);
2139
2140/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2141static ssize_t hotkey_poll_freq_show(struct device *dev,
2142 struct device_attribute *attr,
2143 char *buf)
2144{
2145 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2146}
2147
2148static ssize_t hotkey_poll_freq_store(struct device *dev,
2149 struct device_attribute *attr,
2150 const char *buf, size_t count)
2151{
2152 unsigned long t;
2153
2154 if (parse_strtoul(buf, 25, &t))
2155 return -EINVAL;
2156
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002157 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002158 return -ERESTARTSYS;
2159
2160 hotkey_poll_freq = t;
2161
2162 hotkey_poll_setup(1);
2163 mutex_unlock(&hotkey_mutex);
2164
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002165 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2166
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002167 return count;
2168}
2169
2170static struct device_attribute dev_attr_hotkey_poll_freq =
2171 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2172 hotkey_poll_freq_show, hotkey_poll_freq_store);
2173
2174#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2175
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002176/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002177static ssize_t hotkey_radio_sw_show(struct device *dev,
2178 struct device_attribute *attr,
2179 char *buf)
2180{
2181 int res, s;
2182 res = hotkey_get_wlsw(&s);
2183 if (res < 0)
2184 return res;
2185
2186 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2187}
2188
2189static struct device_attribute dev_attr_hotkey_radio_sw =
2190 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2191
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002192static void hotkey_radio_sw_notify_change(void)
2193{
2194 if (tp_features.hotkey_wlsw)
2195 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2196 "hotkey_radio_sw");
2197}
2198
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002199/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2200static ssize_t hotkey_tablet_mode_show(struct device *dev,
2201 struct device_attribute *attr,
2202 char *buf)
2203{
2204 int res, s;
2205 res = hotkey_get_tablet_mode(&s);
2206 if (res < 0)
2207 return res;
2208
2209 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2210}
2211
2212static struct device_attribute dev_attr_hotkey_tablet_mode =
2213 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2214
2215static void hotkey_tablet_mode_notify_change(void)
2216{
2217 if (tp_features.hotkey_tablet)
2218 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2219 "hotkey_tablet_mode");
2220}
2221
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002222/* sysfs hotkey report_mode -------------------------------------------- */
2223static ssize_t hotkey_report_mode_show(struct device *dev,
2224 struct device_attribute *attr,
2225 char *buf)
2226{
2227 return snprintf(buf, PAGE_SIZE, "%d\n",
2228 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2229}
2230
2231static struct device_attribute dev_attr_hotkey_report_mode =
2232 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2233
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002234/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002235static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2236 struct device_attribute *attr,
2237 char *buf)
2238{
2239 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2240}
2241
2242static struct device_attribute dev_attr_hotkey_wakeup_reason =
2243 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2244
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002245static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002246{
2247 if (tp_features.hotkey_mask)
2248 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2249 "wakeup_reason");
2250}
2251
2252/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002253static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2254 struct device_attribute *attr,
2255 char *buf)
2256{
2257 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2258}
2259
2260static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2261 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2262 hotkey_wakeup_hotunplug_complete_show, NULL);
2263
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002264static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002265{
2266 if (tp_features.hotkey_mask)
2267 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2268 "wakeup_hotunplug_complete");
2269}
2270
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002271/* --------------------------------------------------------------------- */
2272
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002273static struct attribute *hotkey_attributes[] __initdata = {
2274 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002275 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002276 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002277#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2278 &dev_attr_hotkey_mask.attr,
2279 &dev_attr_hotkey_all_mask.attr,
2280 &dev_attr_hotkey_recommended_mask.attr,
2281 &dev_attr_hotkey_source_mask.attr,
2282 &dev_attr_hotkey_poll_freq.attr,
2283#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002284};
2285
2286static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002287 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002288#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2289 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002290 &dev_attr_hotkey_all_mask.attr,
2291 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002292#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002293 &dev_attr_hotkey_wakeup_reason.attr,
2294 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002295};
2296
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002297static void bluetooth_update_rfk(void);
2298static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002299static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002300static void tpacpi_send_radiosw_update(void)
2301{
2302 int wlsw;
2303
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002304 /* Sync these BEFORE sending any rfkill events */
2305 if (tp_features.bluetooth)
2306 bluetooth_update_rfk();
2307 if (tp_features.wan)
2308 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002309 if (tp_features.uwb)
2310 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002311
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002312 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex);
2314
2315 input_report_switch(tpacpi_inputdev,
2316 SW_RFKILL_ALL, !!wlsw);
2317 input_sync(tpacpi_inputdev);
2318
2319 mutex_unlock(&tpacpi_inputdev_send_mutex);
2320 }
2321 hotkey_radio_sw_notify_change();
2322}
2323
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002324static void hotkey_exit(void)
2325{
2326#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327 hotkey_poll_stop_sync();
2328#endif
2329
2330 if (hotkey_dev_attributes)
2331 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2332
2333 kfree(hotkey_keycode_map);
2334
2335 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002336 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002337 "restoring original hot key mask\n");
2338 /* no short-circuit boolean operator below! */
2339 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002340 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002341 printk(TPACPI_ERR
2342 "failed to restore hot key mask "
2343 "to BIOS defaults\n");
2344 }
2345}
2346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002347static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002348{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002349 /* Requirements for changing the default keymaps:
2350 *
2351 * 1. Many of the keys are mapped to KEY_RESERVED for very
2352 * good reasons. Do not change them unless you have deep
2353 * knowledge on the IBM and Lenovo ThinkPad firmware for
2354 * the various ThinkPad models. The driver behaves
2355 * differently for KEY_RESERVED: such keys have their
2356 * hot key mask *unset* in mask_recommended, and also
2357 * in the initial hot key mask programmed into the
2358 * firmware at driver load time, which means the firm-
2359 * ware may react very differently if you change them to
2360 * something else;
2361 *
2362 * 2. You must be subscribed to the linux-thinkpad and
2363 * ibm-acpi-devel mailing lists, and you should read the
2364 * list archives since 2007 if you want to change the
2365 * keymaps. This requirement exists so that you will
2366 * know the past history of problems with the thinkpad-
2367 * acpi driver keymaps, and also that you will be
2368 * listening to any bug reports;
2369 *
2370 * 3. Do not send thinkpad-acpi specific patches directly to
2371 * for merging, *ever*. Send them to the linux-acpi
2372 * mailinglist for comments. Merging is to be done only
2373 * through acpi-test and the ACPI maintainer.
2374 *
2375 * If the above is too much to ask, don't change the keymap.
2376 * Ask the thinkpad-acpi maintainer to do it, instead.
2377 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002378 static u16 ibm_keycode_map[] __initdata = {
2379 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2380 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2381 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2382 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002383
2384 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002385 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2386 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2387 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002388
2389 /* brightness: firmware always reacts to them, unless
2390 * X.org did some tricks in the radeon BIOS scratch
2391 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002392 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002393 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002394
2395 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002396 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002397
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002398 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2399 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002400
2401 /* Volume: firmware always react to it and reprograms
2402 * the built-in *extra* mixer. Never map it to control
2403 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002404 KEY_RESERVED, /* 0x14: VOLUME UP */
2405 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2406 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002407
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002408 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002409
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002410 /* (assignments unknown, please report if found) */
2411 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2412 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2413 };
2414 static u16 lenovo_keycode_map[] __initdata = {
2415 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2416 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2417 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2418 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002419
2420 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002421 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2422 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2423 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002424
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002425 /* These either have to go through ACPI video, or
2426 * act like in the IBM ThinkPads, so don't ever
2427 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002428 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002429 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002430
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002431 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002432
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002433 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2434 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002435
2436 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2437 * react to it and reprograms the built-in *extra* mixer.
2438 * Never map it to control another mixer by default.
2439 *
2440 * T60?, T61, R60?, R61: firmware and EC tries to send
2441 * these over the regular keyboard, so these are no-ops,
2442 * but there are still weird bugs re. MUTE, so do not
2443 * change unless you get test reports from all Lenovo
2444 * models. May cause the BIOS to interfere with the
2445 * HDA mixer.
2446 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002447 KEY_RESERVED, /* 0x14: VOLUME UP */
2448 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2449 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002450
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002451 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002452
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002453 /* (assignments unknown, please report if found) */
2454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2455 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2456 };
2457
2458#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2459#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2460#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2461
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002462 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002463 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002464 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002465
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002466 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2467 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002468
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002469 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002470 BUG_ON(tpacpi_inputdev->open != NULL ||
2471 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002472
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002473 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002474 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002475
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002476#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2477 mutex_init(&hotkey_thread_mutex);
2478 mutex_init(&hotkey_thread_data_mutex);
2479#endif
2480
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002481 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002482 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002483
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002484 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2485 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002486 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002487
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002488 if (!tp_features.hotkey)
2489 return 1;
2490
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03002491 tpacpi_disable_brightness_delay();
2492
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002493 hotkey_dev_attributes = create_attr_set(13, NULL);
2494 if (!hotkey_dev_attributes)
2495 return -ENOMEM;
2496 res = add_many_to_attr_set(hotkey_dev_attributes,
2497 hotkey_attributes,
2498 ARRAY_SIZE(hotkey_attributes));
2499 if (res)
2500 goto err_exit;
2501
2502 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2503 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2504 for HKEY interface version 0x100 */
2505 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2506 if ((hkeyv >> 8) != 1) {
2507 printk(TPACPI_ERR "unknown version of the "
2508 "HKEY interface: 0x%x\n", hkeyv);
2509 printk(TPACPI_ERR "please report this to %s\n",
2510 TPACPI_MAIL);
2511 } else {
2512 /*
2513 * MHKV 0x100 in A31, R40, R40e,
2514 * T4x, X31, and later
2515 */
2516 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002517 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2518 "firmware HKEY interface version: 0x%x\n",
2519 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002520 }
2521 }
2522
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002523 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2524 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002525 str_supported(tp_features.hotkey_mask));
2526
2527 if (tp_features.hotkey_mask) {
2528 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2529 "MHKA", "qd")) {
2530 printk(TPACPI_ERR
2531 "missing MHKA handler, "
2532 "please report this to %s\n",
2533 TPACPI_MAIL);
2534 /* FN+F12, FN+F4, FN+F3 */
2535 hotkey_all_mask = 0x080cU;
2536 }
2537 }
2538
2539 /* hotkey_source_mask *must* be zero for
2540 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002541 if (tp_features.hotkey_mask) {
2542 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002543 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002544 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002545
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002546 hotkey_orig_mask = hotkey_mask;
2547 res = add_many_to_attr_set(
2548 hotkey_dev_attributes,
2549 hotkey_mask_attributes,
2550 ARRAY_SIZE(hotkey_mask_attributes));
2551 if (res)
2552 goto err_exit;
2553 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002554
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002555#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002556 if (tp_features.hotkey_mask) {
2557 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2558 & ~hotkey_all_mask;
2559 } else {
2560 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2561 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002563 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002564 "hotkey source mask 0x%08x, polling freq %d\n",
2565 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002566#endif
2567
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002568#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2569 if (dbg_wlswemul) {
2570 tp_features.hotkey_wlsw = 1;
2571 printk(TPACPI_INFO
2572 "radio switch emulation enabled\n");
2573 } else
2574#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002575 /* Not all thinkpads have a hardware radio switch */
2576 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2577 tp_features.hotkey_wlsw = 1;
2578 printk(TPACPI_INFO
2579 "radio switch found; radios are %s\n",
2580 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002581 }
2582 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002583 res = add_to_attr_set(hotkey_dev_attributes,
2584 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002585
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002586 /* For X41t, X60t, X61t Tablets... */
2587 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2588 tp_features.hotkey_tablet = 1;
2589 printk(TPACPI_INFO
2590 "possible tablet mode switch found; "
2591 "ThinkPad in %s mode\n",
2592 (status & TP_HOTKEY_TABLET_MASK)?
2593 "tablet" : "laptop");
2594 res = add_to_attr_set(hotkey_dev_attributes,
2595 &dev_attr_hotkey_tablet_mode.attr);
2596 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002597
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002598 if (!res)
2599 res = register_attr_set_with_sysfs(
2600 hotkey_dev_attributes,
2601 &tpacpi_pdev->dev.kobj);
2602 if (res)
2603 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002604
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002605 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002606
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002607 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2608 GFP_KERNEL);
2609 if (!hotkey_keycode_map) {
2610 printk(TPACPI_ERR
2611 "failed to allocate memory for key map\n");
2612 res = -ENOMEM;
2613 goto err_exit;
2614 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002615
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002616 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002617 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002618 "using Lenovo default hot key map\n");
2619 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2620 TPACPI_HOTKEY_MAP_SIZE);
2621 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002622 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002623 "using IBM default hot key map\n");
2624 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2625 TPACPI_HOTKEY_MAP_SIZE);
2626 }
2627
2628 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2629 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2630 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2631 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2632 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2633 tpacpi_inputdev->keycode = hotkey_keycode_map;
2634 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2635 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2636 set_bit(hotkey_keycode_map[i],
2637 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002638 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002639 if (i < sizeof(hotkey_reserved_mask)*8)
2640 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002641 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002642 }
2643
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002644 if (tp_features.hotkey_wlsw) {
2645 set_bit(EV_SW, tpacpi_inputdev->evbit);
2646 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2647 }
2648 if (tp_features.hotkey_tablet) {
2649 set_bit(EV_SW, tpacpi_inputdev->evbit);
2650 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002651 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002652
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002653 /* Do not issue duplicate brightness change events to
2654 * userspace */
2655 if (!tp_features.bright_acpimode)
2656 /* update bright_acpimode... */
2657 tpacpi_check_std_acpi_brightness_support();
2658
Henrique de Moraes Holschuh8bf3d4c2009-05-30 13:25:09 -03002659 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002660 printk(TPACPI_INFO
2661 "This ThinkPad has standard ACPI backlight "
2662 "brightness control, supported by the ACPI "
2663 "video driver\n");
2664 printk(TPACPI_NOTICE
2665 "Disabling thinkpad-acpi brightness events "
2666 "by default...\n");
2667
2668 /* The hotkey_reserved_mask change below is not
2669 * necessary while the keys are at KEY_RESERVED in the
2670 * default map, but better safe than sorry, leave it
2671 * here as a marker of what we have to do, especially
2672 * when we finally become able to set this at runtime
2673 * on response to X.org requests */
2674 hotkey_reserved_mask |=
2675 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2676 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002678
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002679 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2680 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002681 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002682 if (res) {
2683 hotkey_exit();
2684 return res;
2685 }
2686 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2687 & ~hotkey_reserved_mask)
2688 | hotkey_orig_mask);
2689 if (res < 0 && res != -ENXIO) {
2690 hotkey_exit();
2691 return res;
2692 }
2693
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2695 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002696 (hotkey_report_mode < 2) ?
2697 "enabled" : "disabled");
2698
2699 tpacpi_inputdev->open = &hotkey_inputdev_open;
2700 tpacpi_inputdev->close = &hotkey_inputdev_close;
2701
2702 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002703 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002704 tpacpi_input_send_tabletsw();
2705
2706 return 0;
2707
2708err_exit:
2709 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2710 hotkey_dev_attributes = NULL;
2711
2712 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002713}
2714
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002715static bool hotkey_notify_hotkey(const u32 hkey,
2716 bool *send_acpi_ev,
2717 bool *ignore_acpi_ev)
2718{
2719 /* 0x1000-0x1FFF: key presses */
2720 unsigned int scancode = hkey & 0xfff;
2721 *send_acpi_ev = true;
2722 *ignore_acpi_ev = false;
2723
2724 if (scancode > 0 && scancode < 0x21) {
2725 scancode--;
2726 if (!(hotkey_source_mask & (1 << scancode))) {
2727 tpacpi_input_send_key(scancode);
2728 *send_acpi_ev = false;
2729 } else {
2730 *ignore_acpi_ev = true;
2731 }
2732 return true;
2733 }
2734 return false;
2735}
2736
2737static bool hotkey_notify_wakeup(const u32 hkey,
2738 bool *send_acpi_ev,
2739 bool *ignore_acpi_ev)
2740{
2741 /* 0x2000-0x2FFF: Wakeup reason */
2742 *send_acpi_ev = true;
2743 *ignore_acpi_ev = false;
2744
2745 switch (hkey) {
2746 case 0x2304: /* suspend, undock */
2747 case 0x2404: /* hibernation, undock */
2748 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2749 *ignore_acpi_ev = true;
2750 break;
2751
2752 case 0x2305: /* suspend, bay eject */
2753 case 0x2405: /* hibernation, bay eject */
2754 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2755 *ignore_acpi_ev = true;
2756 break;
2757
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002758 case 0x2313: /* Battery on critical low level (S3) */
2759 case 0x2413: /* Battery on critical low level (S4) */
2760 printk(TPACPI_ALERT
2761 "EMERGENCY WAKEUP: battery almost empty\n");
2762 /* how to auto-heal: */
2763 /* 2313: woke up from S3, go to S4/S5 */
2764 /* 2413: woke up from S4, go to S5 */
2765 break;
2766
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002767 default:
2768 return false;
2769 }
2770
2771 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2772 printk(TPACPI_INFO
2773 "woke up due to a hot-unplug "
2774 "request...\n");
2775 hotkey_wakeup_reason_notify_change();
2776 }
2777 return true;
2778}
2779
2780static bool hotkey_notify_usrevent(const u32 hkey,
2781 bool *send_acpi_ev,
2782 bool *ignore_acpi_ev)
2783{
2784 /* 0x5000-0x5FFF: human interface helpers */
2785 *send_acpi_ev = true;
2786 *ignore_acpi_ev = false;
2787
2788 switch (hkey) {
2789 case 0x5010: /* Lenovo new BIOS: brightness changed */
2790 case 0x500b: /* X61t: tablet pen inserted into bay */
2791 case 0x500c: /* X61t: tablet pen removed from bay */
2792 return true;
2793
2794 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2795 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2796 tpacpi_input_send_tabletsw();
2797 hotkey_tablet_mode_notify_change();
2798 *send_acpi_ev = false;
2799 return true;
2800
2801 case 0x5001:
2802 case 0x5002:
2803 /* LID switch events. Do not propagate */
2804 *ignore_acpi_ev = true;
2805 return true;
2806
2807 default:
2808 return false;
2809 }
2810}
2811
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002812static bool hotkey_notify_thermal(const u32 hkey,
2813 bool *send_acpi_ev,
2814 bool *ignore_acpi_ev)
2815{
2816 /* 0x6000-0x6FFF: thermal alarms */
2817 *send_acpi_ev = true;
2818 *ignore_acpi_ev = false;
2819
2820 switch (hkey) {
2821 case 0x6011:
2822 printk(TPACPI_CRIT
2823 "THERMAL ALARM: battery is too hot!\n");
2824 /* recommended action: warn user through gui */
2825 return true;
2826 case 0x6012:
2827 printk(TPACPI_ALERT
2828 "THERMAL EMERGENCY: battery is extremely hot!\n");
2829 /* recommended action: immediate sleep/hibernate */
2830 return true;
2831 case 0x6021:
2832 printk(TPACPI_CRIT
2833 "THERMAL ALARM: "
2834 "a sensor reports something is too hot!\n");
2835 /* recommended action: warn user through gui, that */
2836 /* some internal component is too hot */
2837 return true;
2838 case 0x6022:
2839 printk(TPACPI_ALERT
2840 "THERMAL EMERGENCY: "
2841 "a sensor reports something is extremely hot!\n");
2842 /* recommended action: immediate sleep/hibernate */
2843 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002844 case 0x6030:
2845 printk(TPACPI_INFO
2846 "EC reports that Thermal Table has changed\n");
2847 /* recommended action: do nothing, we don't have
2848 * Lenovo ATM information */
2849 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002850 default:
2851 printk(TPACPI_ALERT
2852 "THERMAL ALERT: unknown thermal alarm received\n");
2853 return false;
2854 }
2855}
2856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002857static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2858{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002859 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002860 bool send_acpi_ev;
2861 bool ignore_acpi_ev;
2862 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002863
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002864 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002865 printk(TPACPI_ERR
2866 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002867 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002868 acpi_bus_generate_netlink_event(
2869 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002870 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002871 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002872 return;
2873 }
2874
2875 while (1) {
2876 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002877 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002878 return;
2879 }
2880
2881 if (hkey == 0) {
2882 /* queue empty */
2883 return;
2884 }
2885
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002886 send_acpi_ev = true;
2887 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002888
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002889 switch (hkey >> 12) {
2890 case 1:
2891 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002892 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2893 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002894 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002895 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002896 /* 0x2000-0x2FFF: Wakeup reason */
2897 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2898 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002899 break;
2900 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002901 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002902 if (hkey == 0x3003) {
2903 hotkey_autosleep_ack = 1;
2904 printk(TPACPI_INFO
2905 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002906 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002907 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002908 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002909 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002910 }
2911 break;
2912 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002913 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002914 if (hkey == 0x4003) {
2915 hotkey_autosleep_ack = 1;
2916 printk(TPACPI_INFO
2917 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002918 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002919 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002920 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002921 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002922 }
2923 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002924 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002925 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002926 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2927 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002928 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002929 case 6:
2930 /* 0x6000-0x6FFF: thermal alarms */
2931 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2932 &ignore_acpi_ev);
2933 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002934 case 7:
2935 /* 0x7000-0x7FFF: misc */
2936 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002937 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002938 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002939 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002940 break;
2941 }
2942 /* fallthrough to default */
2943 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002944 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002945 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002946 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002947 printk(TPACPI_NOTICE
2948 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002949 printk(TPACPI_NOTICE
2950 "please report the conditions when this "
2951 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002952 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002953
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002954 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002955 if (!ignore_acpi_ev &&
2956 (send_acpi_ev || hotkey_report_mode < 2)) {
2957 acpi_bus_generate_proc_event(ibm->acpi->device,
2958 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002959 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002960
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002961 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002962 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002963 acpi_bus_generate_netlink_event(
2964 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002965 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002966 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002967 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002968 }
2969}
2970
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002971static void hotkey_suspend(pm_message_t state)
2972{
2973 /* Do these on suspend, we get the events on early resume! */
2974 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2975 hotkey_autosleep_ack = 0;
2976}
2977
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002978static void hotkey_resume(void)
2979{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03002980 tpacpi_disable_brightness_delay();
2981
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002982 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002983 printk(TPACPI_ERR
2984 "error while trying to read hot key mask "
2985 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002986 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002987 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002988 hotkey_wakeup_reason_notify_change();
2989 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002990 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002991}
2992
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002993/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002994static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002996 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 int len = 0;
2998
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002999 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003000 len += sprintf(p + len, "status:\t\tnot supported\n");
3001 return len;
3002 }
3003
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003004 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003005 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003006 res = hotkey_status_get(&status);
3007 if (!res)
3008 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003009 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003010 if (res)
3011 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003014 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003015 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 len += sprintf(p + len,
3017 "commands:\tenable, disable, reset, <mask>\n");
3018 } else {
3019 len += sprintf(p + len, "mask:\t\tnot supported\n");
3020 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3021 }
3022
3023 return len;
3024}
3025
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003026static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003027{
3028 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003029 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3030 TPACPI_WARN
3031 "hotkey enable/disable functionality has been "
3032 "removed from the driver. Hotkeys are always "
3033 "enabled\n"))
3034 printk(TPACPI_ERR
3035 "Please remove the hotkey=enable module "
3036 "parameter, it is deprecated. Hotkeys are always "
3037 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003038}
3039
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003040static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003042 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003043 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003046 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return -ENODEV;
3048
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003049 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003050 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003051
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003052 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003053
3054 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 while ((cmd = next_cmd(&buf))) {
3056 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003057 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003059 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003060 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3064 /* mask set */
3065 } else if (sscanf(cmd, "%x", &mask) == 1) {
3066 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003067 } else {
3068 res = -EINVAL;
3069 goto errexit;
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003072
3073 if (!res)
3074 tpacpi_disclose_usertask("procfs hotkey",
3075 "set mask to 0x%08x\n", mask);
3076
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003077 if (!res && mask != hotkey_mask)
3078 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003080errexit:
3081 mutex_unlock(&hotkey_mutex);
3082 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003085static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003086 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003087 {"", 0},
3088};
3089
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003090static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003091 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003092 .notify = hotkey_notify,
3093 .handle = &hkey_handle,
3094 .type = ACPI_DEVICE_NOTIFY,
3095};
3096
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003097static struct ibm_struct hotkey_driver_data = {
3098 .name = "hotkey",
3099 .read = hotkey_read,
3100 .write = hotkey_write,
3101 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003102 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003103 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003104 .acpi = &ibm_hotkey_acpidriver,
3105};
3106
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003107/*************************************************************************
3108 * Bluetooth subdriver
3109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003111enum {
3112 /* ACPI GBDC/SBDC bits */
3113 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3114 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003115 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3116 off / last state */
3117};
3118
3119enum {
3120 /* ACPI \BLTH commands */
3121 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3122 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3123 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3124 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3125 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003126};
3127
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003128#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3129
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003130static struct rfkill *tpacpi_bluetooth_rfkill;
3131
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003132static void bluetooth_suspend(pm_message_t state)
3133{
3134 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003135 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3136 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3137 vdbg_printk(TPACPI_DBG_RFKILL,
3138 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003139}
3140
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003141static int bluetooth_get_radiosw(void)
3142{
3143 int status;
3144
3145 if (!tp_features.bluetooth)
3146 return -ENODEV;
3147
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003148 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3149 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003150 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003151
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003152#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3153 if (dbg_bluetoothemul)
3154 return (tpacpi_bluetooth_emulstate) ?
3155 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3156#endif
3157
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003158 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3159 return -EIO;
3160
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003161 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3162 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003163}
3164
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003165static void bluetooth_update_rfk(void)
3166{
3167 int status;
3168
3169 if (!tpacpi_bluetooth_rfkill)
3170 return;
3171
3172 status = bluetooth_get_radiosw();
3173 if (status < 0)
3174 return;
3175 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003176
3177 vdbg_printk(TPACPI_DBG_RFKILL,
3178 "forced rfkill state to %d\n",
3179 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003180}
3181
3182static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003183{
3184 int status;
3185
3186 if (!tp_features.bluetooth)
3187 return -ENODEV;
3188
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003189 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3190 * reason to risk weird behaviour. */
3191 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3192 && radio_on)
3193 return -EPERM;
3194
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003195 vdbg_printk(TPACPI_DBG_RFKILL,
3196 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3197
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003198#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3199 if (dbg_bluetoothemul) {
3200 tpacpi_bluetooth_emulstate = !!radio_on;
3201 if (update_rfk)
3202 bluetooth_update_rfk();
3203 return 0;
3204 }
3205#endif
3206
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003207 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003208 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003209 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003210 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003211 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003212 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3213 return -EIO;
3214
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003215 if (update_rfk)
3216 bluetooth_update_rfk();
3217
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003218 return 0;
3219}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003220
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003221/* sysfs bluetooth enable ---------------------------------------------- */
3222static ssize_t bluetooth_enable_show(struct device *dev,
3223 struct device_attribute *attr,
3224 char *buf)
3225{
3226 int status;
3227
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003228 printk_deprecated_rfkill_attribute("bluetooth_enable");
3229
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003230 status = bluetooth_get_radiosw();
3231 if (status < 0)
3232 return status;
3233
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003234 return snprintf(buf, PAGE_SIZE, "%d\n",
3235 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003236}
3237
3238static ssize_t bluetooth_enable_store(struct device *dev,
3239 struct device_attribute *attr,
3240 const char *buf, size_t count)
3241{
3242 unsigned long t;
3243 int res;
3244
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003245 printk_deprecated_rfkill_attribute("bluetooth_enable");
3246
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003247 if (parse_strtoul(buf, 1, &t))
3248 return -EINVAL;
3249
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003250 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3251
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003252 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003253
3254 return (res) ? res : count;
3255}
3256
3257static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003258 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003259 bluetooth_enable_show, bluetooth_enable_store);
3260
3261/* --------------------------------------------------------------------- */
3262
3263static struct attribute *bluetooth_attributes[] = {
3264 &dev_attr_bluetooth_enable.attr,
3265 NULL
3266};
3267
3268static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003269 .attrs = bluetooth_attributes,
3270};
3271
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003272static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3273{
3274 int bts = bluetooth_get_radiosw();
3275
3276 if (bts < 0)
3277 return bts;
3278
3279 *state = bts;
3280 return 0;
3281}
3282
3283static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3284{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003285 dbg_printk(TPACPI_DBG_RFKILL,
3286 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003287 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3288}
3289
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003290static void bluetooth_shutdown(void)
3291{
3292 /* Order firmware to save current state to NVRAM */
3293 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3294 TP_ACPI_BLTH_SAVE_STATE))
3295 printk(TPACPI_NOTICE
3296 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003297 else
3298 vdbg_printk(TPACPI_DBG_RFKILL,
3299 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003300}
3301
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003302static void bluetooth_exit(void)
3303{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003304 bluetooth_shutdown();
3305
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003306 if (tpacpi_bluetooth_rfkill)
3307 rfkill_unregister(tpacpi_bluetooth_rfkill);
3308
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003309 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3310 &bluetooth_attr_group);
3311}
3312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003313static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003315 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003316 int status = 0;
3317
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003318 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3319 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003320
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003321 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003322
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003323 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3324 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003325 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003326 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003328 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3329 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003330 str_supported(tp_features.bluetooth),
3331 status);
3332
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003333#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3334 if (dbg_bluetoothemul) {
3335 tp_features.bluetooth = 1;
3336 printk(TPACPI_INFO
3337 "bluetooth switch emulation enabled\n");
3338 } else
3339#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003340 if (tp_features.bluetooth &&
3341 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3342 /* no bluetooth hardware present in system */
3343 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003344 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003345 "bluetooth hardware not installed\n");
3346 }
3347
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003348 if (!tp_features.bluetooth)
3349 return 1;
3350
3351 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003352 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003353 if (res)
3354 return res;
3355
3356 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3357 &tpacpi_bluetooth_rfkill,
3358 RFKILL_TYPE_BLUETOOTH,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003359 TPACPI_RFK_BLUETOOTH_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003360 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003361 tpacpi_bluetooth_rfk_set,
3362 tpacpi_bluetooth_rfk_get);
3363 if (res) {
3364 bluetooth_exit();
3365 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003366 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003367
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003371/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003372static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
3374 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003375 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003377 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003380 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003381 (status == RFKILL_STATE_UNBLOCKED) ?
3382 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 len += sprintf(p + len, "commands:\tenable, disable\n");
3384 }
3385
3386 return len;
3387}
3388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003389static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 char *cmd;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003392 int state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003394 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003395 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 while ((cmd = next_cmd(&buf))) {
3398 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003399 state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003401 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } else
3403 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003406 if (state != -1) {
3407 tpacpi_disclose_usertask("procfs bluetooth",
3408 "attempt to %s\n",
3409 state ? "enable" : "disable");
3410 bluetooth_set_radiosw(state, 1);
3411 }
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414}
3415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003416static struct ibm_struct bluetooth_driver_data = {
3417 .name = "bluetooth",
3418 .read = bluetooth_read,
3419 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003420 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003421 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003422 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003423};
3424
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003425/*************************************************************************
3426 * Wan subdriver
3427 */
3428
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003429enum {
3430 /* ACPI GWAN/SWAN bits */
3431 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3432 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003433 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3434 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003435};
3436
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003437#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3438
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003439static struct rfkill *tpacpi_wan_rfkill;
3440
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003441static void wan_suspend(pm_message_t state)
3442{
3443 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003444 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3445 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3446 vdbg_printk(TPACPI_DBG_RFKILL,
3447 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003448}
3449
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003450static int wan_get_radiosw(void)
3451{
3452 int status;
3453
3454 if (!tp_features.wan)
3455 return -ENODEV;
3456
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003457 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3458 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003459 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003460
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003461#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3462 if (dbg_wwanemul)
3463 return (tpacpi_wwan_emulstate) ?
3464 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3465#endif
3466
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003467 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3468 return -EIO;
3469
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003470 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3471 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003472}
3473
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003474static void wan_update_rfk(void)
3475{
3476 int status;
3477
3478 if (!tpacpi_wan_rfkill)
3479 return;
3480
3481 status = wan_get_radiosw();
3482 if (status < 0)
3483 return;
3484 rfkill_force_state(tpacpi_wan_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003485
3486 vdbg_printk(TPACPI_DBG_RFKILL,
3487 "forced rfkill state to %d\n",
3488 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003489}
3490
3491static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003492{
3493 int status;
3494
3495 if (!tp_features.wan)
3496 return -ENODEV;
3497
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003498 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3499 * reason to risk weird behaviour. */
3500 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3501 && radio_on)
3502 return -EPERM;
3503
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003504 vdbg_printk(TPACPI_DBG_RFKILL,
3505 "will %s WWAN\n", radio_on ? "enable" : "disable");
3506
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003507#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3508 if (dbg_wwanemul) {
3509 tpacpi_wwan_emulstate = !!radio_on;
3510 if (update_rfk)
3511 wan_update_rfk();
3512 return 0;
3513 }
3514#endif
3515
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003516 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003517 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003518 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003519 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003520 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003521 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3522 return -EIO;
3523
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003524 if (update_rfk)
3525 wan_update_rfk();
3526
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003527 return 0;
3528}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003529
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003530/* sysfs wan enable ---------------------------------------------------- */
3531static ssize_t wan_enable_show(struct device *dev,
3532 struct device_attribute *attr,
3533 char *buf)
3534{
3535 int status;
3536
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003537 printk_deprecated_rfkill_attribute("wwan_enable");
3538
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003539 status = wan_get_radiosw();
3540 if (status < 0)
3541 return status;
3542
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003543 return snprintf(buf, PAGE_SIZE, "%d\n",
3544 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003545}
3546
3547static ssize_t wan_enable_store(struct device *dev,
3548 struct device_attribute *attr,
3549 const char *buf, size_t count)
3550{
3551 unsigned long t;
3552 int res;
3553
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003554 printk_deprecated_rfkill_attribute("wwan_enable");
3555
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003556 if (parse_strtoul(buf, 1, &t))
3557 return -EINVAL;
3558
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003559 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3560
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003561 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003562
3563 return (res) ? res : count;
3564}
3565
3566static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003567 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003568 wan_enable_show, wan_enable_store);
3569
3570/* --------------------------------------------------------------------- */
3571
3572static struct attribute *wan_attributes[] = {
3573 &dev_attr_wan_enable.attr,
3574 NULL
3575};
3576
3577static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003578 .attrs = wan_attributes,
3579};
3580
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003581static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3582{
3583 int wans = wan_get_radiosw();
3584
3585 if (wans < 0)
3586 return wans;
3587
3588 *state = wans;
3589 return 0;
3590}
3591
3592static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3593{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003594 dbg_printk(TPACPI_DBG_RFKILL,
3595 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003596 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3597}
3598
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003599static void wan_shutdown(void)
3600{
3601 /* Order firmware to save current state to NVRAM */
3602 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3603 TP_ACPI_WGSV_SAVE_STATE))
3604 printk(TPACPI_NOTICE
3605 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003606 else
3607 vdbg_printk(TPACPI_DBG_RFKILL,
3608 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003609}
3610
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003611static void wan_exit(void)
3612{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003613 wan_shutdown();
3614
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003615 if (tpacpi_wan_rfkill)
3616 rfkill_unregister(tpacpi_wan_rfkill);
3617
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003618 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3619 &wan_attr_group);
3620}
3621
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003622static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003623{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003624 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003625 int status = 0;
3626
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003627 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3628 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003629
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003630 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003631
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003632 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003633 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003634
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003635 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3636 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003637 str_supported(tp_features.wan),
3638 status);
3639
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003640#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3641 if (dbg_wwanemul) {
3642 tp_features.wan = 1;
3643 printk(TPACPI_INFO
3644 "wwan switch emulation enabled\n");
3645 } else
3646#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003647 if (tp_features.wan &&
3648 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3649 /* no wan hardware present in system */
3650 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003651 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003652 "wan hardware not installed\n");
3653 }
3654
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003655 if (!tp_features.wan)
3656 return 1;
3657
3658 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003659 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003660 if (res)
3661 return res;
3662
3663 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3664 &tpacpi_wan_rfkill,
3665 RFKILL_TYPE_WWAN,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003666 TPACPI_RFK_WWAN_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003667 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003668 tpacpi_wan_rfk_set,
3669 tpacpi_wan_rfk_get);
3670 if (res) {
3671 wan_exit();
3672 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003673 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003674
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003675 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003676}
3677
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003678/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003679static int wan_read(char *p)
3680{
3681 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003682 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003683
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003684 tpacpi_disclose_usertask("procfs wan", "read");
3685
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003686 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003687 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003688 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003689 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003690 (status == RFKILL_STATE_UNBLOCKED) ?
3691 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003692 len += sprintf(p + len, "commands:\tenable, disable\n");
3693 }
3694
3695 return len;
3696}
3697
3698static int wan_write(char *buf)
3699{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003700 char *cmd;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003701 int state = -1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003702
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003703 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003704 return -ENODEV;
3705
3706 while ((cmd = next_cmd(&buf))) {
3707 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003708 state = 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003709 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003710 state = 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003711 } else
3712 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003713 }
3714
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003715 if (state != -1) {
3716 tpacpi_disclose_usertask("procfs wan",
3717 "attempt to %s\n",
3718 state ? "enable" : "disable");
3719 wan_set_radiosw(state, 1);
3720 }
3721
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003722 return 0;
3723}
3724
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003725static struct ibm_struct wan_driver_data = {
3726 .name = "wan",
3727 .read = wan_read,
3728 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003729 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003730 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003731 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003732};
3733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003734/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003735 * UWB subdriver
3736 */
3737
3738enum {
3739 /* ACPI GUWB/SUWB bits */
3740 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3741 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3742};
3743
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003744#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3745
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003746static struct rfkill *tpacpi_uwb_rfkill;
3747
3748static int uwb_get_radiosw(void)
3749{
3750 int status;
3751
3752 if (!tp_features.uwb)
3753 return -ENODEV;
3754
3755 /* WLSW overrides UWB in firmware/hardware, reflect that */
3756 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3757 return RFKILL_STATE_HARD_BLOCKED;
3758
3759#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3760 if (dbg_uwbemul)
3761 return (tpacpi_uwb_emulstate) ?
3762 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3763#endif
3764
3765 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3766 return -EIO;
3767
3768 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3769 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3770}
3771
3772static void uwb_update_rfk(void)
3773{
3774 int status;
3775
3776 if (!tpacpi_uwb_rfkill)
3777 return;
3778
3779 status = uwb_get_radiosw();
3780 if (status < 0)
3781 return;
3782 rfkill_force_state(tpacpi_uwb_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003783
3784 vdbg_printk(TPACPI_DBG_RFKILL,
3785 "forced rfkill state to %d\n",
3786 status);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003787}
3788
3789static int uwb_set_radiosw(int radio_on, int update_rfk)
3790{
3791 int status;
3792
3793 if (!tp_features.uwb)
3794 return -ENODEV;
3795
3796 /* WLSW overrides UWB in firmware/hardware, but there is no
3797 * reason to risk weird behaviour. */
3798 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3799 && radio_on)
3800 return -EPERM;
3801
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003802 vdbg_printk(TPACPI_DBG_RFKILL,
3803 "will %s UWB\n", radio_on ? "enable" : "disable");
3804
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003805#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3806 if (dbg_uwbemul) {
3807 tpacpi_uwb_emulstate = !!radio_on;
3808 if (update_rfk)
3809 uwb_update_rfk();
3810 return 0;
3811 }
3812#endif
3813
3814 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3815 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3816 return -EIO;
3817
3818 if (update_rfk)
3819 uwb_update_rfk();
3820
3821 return 0;
3822}
3823
3824/* --------------------------------------------------------------------- */
3825
3826static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3827{
3828 int uwbs = uwb_get_radiosw();
3829
3830 if (uwbs < 0)
3831 return uwbs;
3832
3833 *state = uwbs;
3834 return 0;
3835}
3836
3837static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3838{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003839 dbg_printk(TPACPI_DBG_RFKILL,
3840 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003841 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3842}
3843
3844static void uwb_exit(void)
3845{
3846 if (tpacpi_uwb_rfkill)
3847 rfkill_unregister(tpacpi_uwb_rfkill);
3848}
3849
3850static int __init uwb_init(struct ibm_init_struct *iibm)
3851{
3852 int res;
3853 int status = 0;
3854
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003855 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3856 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003857
3858 TPACPI_ACPIHANDLE_INIT(hkey);
3859
3860 tp_features.uwb = hkey_handle &&
3861 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3862
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003863 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3864 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003865 str_supported(tp_features.uwb),
3866 status);
3867
3868#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3869 if (dbg_uwbemul) {
3870 tp_features.uwb = 1;
3871 printk(TPACPI_INFO
3872 "uwb switch emulation enabled\n");
3873 } else
3874#endif
3875 if (tp_features.uwb &&
3876 !(status & TP_ACPI_UWB_HWPRESENT)) {
3877 /* no uwb hardware present in system */
3878 tp_features.uwb = 0;
3879 dbg_printk(TPACPI_DBG_INIT,
3880 "uwb hardware not installed\n");
3881 }
3882
3883 if (!tp_features.uwb)
3884 return 1;
3885
3886 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3887 &tpacpi_uwb_rfkill,
3888 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003889 TPACPI_RFK_UWB_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003890 false,
3891 tpacpi_uwb_rfk_set,
3892 tpacpi_uwb_rfk_get);
3893
3894 return res;
3895}
3896
3897static struct ibm_struct uwb_driver_data = {
3898 .name = "uwb",
3899 .exit = uwb_exit,
3900 .flags.experimental = 1,
3901};
3902
3903/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003904 * Video subdriver
3905 */
3906
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003907#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3908
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003909enum video_access_mode {
3910 TPACPI_VIDEO_NONE = 0,
3911 TPACPI_VIDEO_570, /* 570 */
3912 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3913 TPACPI_VIDEO_NEW, /* all others */
3914};
3915
3916enum { /* video status flags, based on VIDEO_570 */
3917 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3918 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3919 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3920};
3921
3922enum { /* TPACPI_VIDEO_570 constants */
3923 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3924 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3925 * video_status_flags */
3926 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3927 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3928};
3929
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003930static enum video_access_mode video_supported;
3931static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003932
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003933static int video_autosw_get(void);
3934static int video_autosw_set(int enable);
3935
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003936TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003937
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003938static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003940 int ivga;
3941
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003942 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3943
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003944 TPACPI_ACPIHANDLE_INIT(vid);
3945 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003947 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3948 /* G41, assume IVGA doesn't change */
3949 vid_handle = vid2_handle;
3950
3951 if (!vid_handle)
3952 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003953 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003954 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3955 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003956 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003957 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3958 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003959 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960 else
3961 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003962 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003964 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3965 str_supported(video_supported != TPACPI_VIDEO_NONE),
3966 video_supported);
3967
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003968 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003971static void video_exit(void)
3972{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003973 dbg_printk(TPACPI_DBG_EXIT,
3974 "restoring original video autoswitch mode\n");
3975 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003976 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003977 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003978}
3979
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003980static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
3982 int status = 0;
3983 int i;
3984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003985 switch (video_supported) {
3986 case TPACPI_VIDEO_570:
3987 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3988 TP_ACPI_VIDEO_570_PHSCMD))
3989 return -EIO;
3990 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3991 break;
3992 case TPACPI_VIDEO_770:
3993 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3994 return -EIO;
3995 if (i)
3996 status |= TP_ACPI_VIDEO_S_LCD;
3997 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3998 return -EIO;
3999 if (i)
4000 status |= TP_ACPI_VIDEO_S_CRT;
4001 break;
4002 case TPACPI_VIDEO_NEW:
4003 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4004 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4005 return -EIO;
4006 if (i)
4007 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004009 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4010 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4011 return -EIO;
4012 if (i)
4013 status |= TP_ACPI_VIDEO_S_LCD;
4014 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4015 return -EIO;
4016 if (i)
4017 status |= TP_ACPI_VIDEO_S_DVI;
4018 break;
4019 default:
4020 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 return status;
4024}
4025
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004026static int video_outputsw_set(int status)
4027{
4028 int autosw;
4029 int res = 0;
4030
4031 switch (video_supported) {
4032 case TPACPI_VIDEO_570:
4033 res = acpi_evalf(NULL, NULL,
4034 "\\_SB.PHS2", "vdd",
4035 TP_ACPI_VIDEO_570_PHS2CMD,
4036 status | TP_ACPI_VIDEO_570_PHS2SET);
4037 break;
4038 case TPACPI_VIDEO_770:
4039 autosw = video_autosw_get();
4040 if (autosw < 0)
4041 return autosw;
4042
4043 res = video_autosw_set(1);
4044 if (res)
4045 return res;
4046 res = acpi_evalf(vid_handle, NULL,
4047 "ASWT", "vdd", status * 0x100, 0);
4048 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004049 printk(TPACPI_ERR
4050 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004051 return -EIO;
4052 }
4053 break;
4054 case TPACPI_VIDEO_NEW:
4055 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004056 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004057 break;
4058 default:
4059 return -ENOSYS;
4060 }
4061
4062 return (res)? 0 : -EIO;
4063}
4064
4065static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004067 int autosw = 0;
4068
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004069 switch (video_supported) {
4070 case TPACPI_VIDEO_570:
4071 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4072 return -EIO;
4073 break;
4074 case TPACPI_VIDEO_770:
4075 case TPACPI_VIDEO_NEW:
4076 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4077 return -EIO;
4078 break;
4079 default:
4080 return -ENOSYS;
4081 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004082
4083 return autosw & 1;
4084}
4085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004086static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004087{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004088 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004089 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004090 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004091}
4092
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004093static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004094{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004095 int autosw = video_autosw_get();
4096 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004098 if (autosw < 0)
4099 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004100
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004101 switch (video_supported) {
4102 case TPACPI_VIDEO_570:
4103 res = video_autosw_set(1);
4104 if (res)
4105 return res;
4106 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4107 break;
4108 case TPACPI_VIDEO_770:
4109 case TPACPI_VIDEO_NEW:
4110 res = video_autosw_set(1);
4111 if (res)
4112 return res;
4113 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4114 break;
4115 default:
4116 return -ENOSYS;
4117 }
4118 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004119 printk(TPACPI_ERR
4120 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004121 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004122 }
4123
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004124 return (res)? 0 : -EIO;
4125}
4126
4127static int video_expand_toggle(void)
4128{
4129 switch (video_supported) {
4130 case TPACPI_VIDEO_570:
4131 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4132 0 : -EIO;
4133 case TPACPI_VIDEO_770:
4134 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4135 0 : -EIO;
4136 case TPACPI_VIDEO_NEW:
4137 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4138 0 : -EIO;
4139 default:
4140 return -ENOSYS;
4141 }
4142 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004143}
4144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004145static int video_read(char *p)
4146{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004147 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004148 int len = 0;
4149
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004150 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004151 len += sprintf(p + len, "status:\t\tnot supported\n");
4152 return len;
4153 }
4154
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004155 status = video_outputsw_get();
4156 if (status < 0)
4157 return status;
4158
4159 autosw = video_autosw_get();
4160 if (autosw < 0)
4161 return autosw;
4162
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004163 len += sprintf(p + len, "status:\t\tsupported\n");
4164 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4165 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004166 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004167 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4168 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4169 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4170 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004171 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004172 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4173 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4174 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4175
4176 return len;
4177}
4178
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004179static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
4181 char *cmd;
4182 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004183 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004185 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004186 return -ENODEV;
4187
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004188 enable = 0;
4189 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 while ((cmd = next_cmd(&buf))) {
4192 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004193 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004195 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004197 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004199 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004200 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004201 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004202 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004203 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004204 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004205 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004207 res = video_autosw_set(1);
4208 if (res)
4209 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004211 res = video_autosw_set(0);
4212 if (res)
4213 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004215 res = video_outputsw_cycle();
4216 if (res)
4217 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004219 res = video_expand_toggle();
4220 if (res)
4221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 } else
4223 return -EINVAL;
4224 }
4225
4226 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004227 status = video_outputsw_get();
4228 if (status < 0)
4229 return status;
4230 res = video_outputsw_set((status & ~disable) | enable);
4231 if (res)
4232 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234
4235 return 0;
4236}
4237
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004238static struct ibm_struct video_driver_data = {
4239 .name = "video",
4240 .read = video_read,
4241 .write = video_write,
4242 .exit = video_exit,
4243};
4244
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004245#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004247/*************************************************************************
4248 * Light (thinklight) subdriver
4249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004251TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4252TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004253
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004254static int light_get_status(void)
4255{
4256 int status = 0;
4257
4258 if (tp_features.light_status) {
4259 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4260 return -EIO;
4261 return (!!status);
4262 }
4263
4264 return -ENXIO;
4265}
4266
4267static int light_set_status(int status)
4268{
4269 int rc;
4270
4271 if (tp_features.light) {
4272 if (cmos_handle) {
4273 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4274 (status)?
4275 TP_CMOS_THINKLIGHT_ON :
4276 TP_CMOS_THINKLIGHT_OFF);
4277 } else {
4278 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4279 (status)? 1 : 0);
4280 }
4281 return (rc)? 0 : -EIO;
4282 }
4283
4284 return -ENXIO;
4285}
4286
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004287static void light_set_status_worker(struct work_struct *work)
4288{
4289 struct tpacpi_led_classdev *data =
4290 container_of(work, struct tpacpi_led_classdev, work);
4291
4292 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004293 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004294}
4295
4296static void light_sysfs_set(struct led_classdev *led_cdev,
4297 enum led_brightness brightness)
4298{
4299 struct tpacpi_led_classdev *data =
4300 container_of(led_cdev,
4301 struct tpacpi_led_classdev,
4302 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004303 data->new_state = (brightness != LED_OFF) ?
4304 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004305 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004306}
4307
4308static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4309{
4310 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4311}
4312
4313static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4314 .led_classdev = {
4315 .name = "tpacpi::thinklight",
4316 .brightness_set = &light_sysfs_set,
4317 .brightness_get = &light_sysfs_get,
4318 }
4319};
4320
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004321static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004323 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004324
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004325 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4326
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004327 TPACPI_ACPIHANDLE_INIT(ledb);
4328 TPACPI_ACPIHANDLE_INIT(lght);
4329 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004330 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004331
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004332 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004333 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004334
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004335 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004336 /* light status not supported on
4337 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004338 tp_features.light_status =
4339 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004341 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4342 str_supported(tp_features.light),
4343 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004344
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004345 if (!tp_features.light)
4346 return 1;
4347
4348 rc = led_classdev_register(&tpacpi_pdev->dev,
4349 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004350
4351 if (rc < 0) {
4352 tp_features.light = 0;
4353 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004354 } else {
4355 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004356 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004357
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004358 return rc;
4359}
4360
4361static void light_exit(void)
4362{
4363 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4364 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004365 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004368static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004371 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004373 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004374 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004375 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004376 len += sprintf(p + len, "status:\t\tunknown\n");
4377 len += sprintf(p + len, "commands:\ton, off\n");
4378 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004379 status = light_get_status();
4380 if (status < 0)
4381 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004383 len += sprintf(p + len, "commands:\ton, off\n");
4384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386 return len;
4387}
4388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004389static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004392 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004393
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004394 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004395 return -ENODEV;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 while ((cmd = next_cmd(&buf))) {
4398 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004399 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004401 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 } else
4403 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
4405
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004406 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004409static struct ibm_struct light_driver_data = {
4410 .name = "light",
4411 .read = light_read,
4412 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004413 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004414};
4415
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416/*************************************************************************
4417 * Dock subdriver
4418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004420#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004421
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004422static void dock_notify(struct ibm_struct *ibm, u32 event);
4423static int dock_read(char *p);
4424static int dock_write(char *buf);
4425
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004426TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004427 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4428 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4429 "\\_SB.PCI.ISA.SLCE", /* 570 */
4430 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4431
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004432/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004433TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004434
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004435static const struct acpi_device_id ibm_pci_device_ids[] = {
4436 {PCI_ROOT_HID_STRING, 0},
4437 {"", 0},
4438};
4439
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004440static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4441 {
4442 .notify = dock_notify,
4443 .handle = &dock_handle,
4444 .type = ACPI_SYSTEM_NOTIFY,
4445 },
4446 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004447 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4448 * We just use it to get notifications of dock hotplug
4449 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004450 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004451 .notify = dock_notify,
4452 .handle = &pci_handle,
4453 .type = ACPI_SYSTEM_NOTIFY,
4454 },
4455};
4456
4457static struct ibm_struct dock_driver_data[2] = {
4458 {
4459 .name = "dock",
4460 .read = dock_read,
4461 .write = dock_write,
4462 .acpi = &ibm_dock_acpidriver[0],
4463 },
4464 {
4465 .name = "dock",
4466 .acpi = &ibm_dock_acpidriver[1],
4467 },
4468};
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470#define dock_docked() (_sta(dock_handle) & 1)
4471
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004472static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004473{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004474 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4475
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004476 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004477
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004478 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4479 str_supported(dock_handle != NULL));
4480
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004481 return (dock_handle)? 0 : 1;
4482}
4483
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004484static int __init dock_init2(struct ibm_init_struct *iibm)
4485{
4486 int dock2_needed;
4487
4488 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4489
4490 if (dock_driver_data[0].flags.acpi_driver_registered &&
4491 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004492 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004493 dock2_needed = (pci_handle != NULL);
4494 vdbg_printk(TPACPI_DBG_INIT,
4495 "dock PCI handler for the TP 570 is %s\n",
4496 str_supported(dock2_needed));
4497 } else {
4498 vdbg_printk(TPACPI_DBG_INIT,
4499 "dock subdriver part 2 not required\n");
4500 dock2_needed = 0;
4501 }
4502
4503 return (dock2_needed)? 0 : 1;
4504}
4505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004506static void dock_notify(struct ibm_struct *ibm, u32 event)
4507{
4508 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004509 int pci = ibm->acpi->hid && ibm->acpi->device &&
4510 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004511 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004512
4513 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004514 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004515 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004516 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004517 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004518 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004519 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004520 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004522 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004523 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004524 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004525 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004526 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004527 }
Len Brown14e04fb32007-08-23 15:20:26 -04004528 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004529 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004530 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004531 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004532}
4533
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004534static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 int len = 0;
4537 int docked = dock_docked();
4538
4539 if (!dock_handle)
4540 len += sprintf(p + len, "status:\t\tnot supported\n");
4541 else if (!docked)
4542 len += sprintf(p + len, "status:\t\tundocked\n");
4543 else {
4544 len += sprintf(p + len, "status:\t\tdocked\n");
4545 len += sprintf(p + len, "commands:\tdock, undock\n");
4546 }
4547
4548 return len;
4549}
4550
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
4553 char *cmd;
4554
4555 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 while ((cmd = next_cmd(&buf))) {
4559 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4561 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 return -EIO;
4563 } else if (strlencmp(cmd, "dock") == 0) {
4564 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4565 return -EIO;
4566 } else
4567 return -EINVAL;
4568 }
4569
4570 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004573#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004575/*************************************************************************
4576 * Bay subdriver
4577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004579#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004580
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004581TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004582 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4583 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4584 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4585 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004586TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004587 "_EJ0", /* all others */
4588 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004589TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004590 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4591 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004592TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004593 "_EJ0", /* 770x */
4594 ); /* all others */
4595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004596static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004598 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4599
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004600 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004601 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004602 TPACPI_ACPIHANDLE_INIT(bay_ej);
4603 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004604 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004605 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004606
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004607 tp_features.bay_status = bay_handle &&
4608 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4609 tp_features.bay_status2 = bay2_handle &&
4610 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004612 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4613 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4614 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4615 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004617 vdbg_printk(TPACPI_DBG_INIT,
4618 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004619 str_supported(tp_features.bay_status),
4620 str_supported(tp_features.bay_eject),
4621 str_supported(tp_features.bay_status2),
4622 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004623
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004624 return (tp_features.bay_status || tp_features.bay_eject ||
4625 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004628static void bay_notify(struct ibm_struct *ibm, u32 event)
4629{
Len Brown14e04fb32007-08-23 15:20:26 -04004630 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004631 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004632 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004633 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004634}
4635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004636#define bay_occupied(b) (_sta(b##_handle) & 1)
4637
4638static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004641 int occupied = bay_occupied(bay);
4642 int occupied2 = bay_occupied(bay2);
4643 int eject, eject2;
4644
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004645 len += sprintf(p + len, "status:\t\t%s\n",
4646 tp_features.bay_status ?
4647 (occupied ? "occupied" : "unoccupied") :
4648 "not supported");
4649 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004650 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4651 "occupied" : "unoccupied");
4652
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004653 eject = tp_features.bay_eject && occupied;
4654 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004655
4656 if (eject && eject2)
4657 len += sprintf(p + len, "commands:\teject, eject2\n");
4658 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004660 else if (eject2)
4661 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 return len;
4664}
4665
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004666static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
4668 char *cmd;
4669
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004670 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004671 return -ENODEV;
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004674 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004675 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4676 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004677 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004678 strlencmp(cmd, "eject2") == 0) {
4679 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return -EIO;
4681 } else
4682 return -EINVAL;
4683 }
4684
4685 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004686}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004687
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004688static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4689 .notify = bay_notify,
4690 .handle = &bay_handle,
4691 .type = ACPI_SYSTEM_NOTIFY,
4692};
4693
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004694static struct ibm_struct bay_driver_data = {
4695 .name = "bay",
4696 .read = bay_read,
4697 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004698 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004699};
4700
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004701#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004703/*************************************************************************
4704 * CMOS subdriver
4705 */
4706
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004707/* sysfs cmos_command -------------------------------------------------- */
4708static ssize_t cmos_command_store(struct device *dev,
4709 struct device_attribute *attr,
4710 const char *buf, size_t count)
4711{
4712 unsigned long cmos_cmd;
4713 int res;
4714
4715 if (parse_strtoul(buf, 21, &cmos_cmd))
4716 return -EINVAL;
4717
4718 res = issue_thinkpad_cmos_command(cmos_cmd);
4719 return (res)? res : count;
4720}
4721
4722static struct device_attribute dev_attr_cmos_command =
4723 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4724
4725/* --------------------------------------------------------------------- */
4726
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004727static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004728{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004729 int res;
4730
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004731 vdbg_printk(TPACPI_DBG_INIT,
4732 "initializing cmos commands subdriver\n");
4733
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004734 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004735
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004736 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4737 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004738
4739 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4740 if (res)
4741 return res;
4742
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004743 return (cmos_handle)? 0 : 1;
4744}
4745
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004746static void cmos_exit(void)
4747{
4748 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4749}
4750
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004751static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
4753 int len = 0;
4754
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004755 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4756 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 if (!cmos_handle)
4758 len += sprintf(p + len, "status:\t\tnot supported\n");
4759 else {
4760 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004761 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
4763
4764 return len;
4765}
4766
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004767static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
4769 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004770 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004773 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4774 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 /* cmos_cmd set */
4776 } else
4777 return -EINVAL;
4778
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004779 res = issue_thinkpad_cmos_command(cmos_cmd);
4780 if (res)
4781 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 }
4783
4784 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004785}
4786
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004787static struct ibm_struct cmos_driver_data = {
4788 .name = "cmos",
4789 .read = cmos_read,
4790 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004791 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004792};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004793
4794/*************************************************************************
4795 * LED subdriver
4796 */
4797
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004798enum led_access_mode {
4799 TPACPI_LED_NONE = 0,
4800 TPACPI_LED_570, /* 570 */
4801 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4802 TPACPI_LED_NEW, /* all others */
4803};
4804
4805enum { /* For TPACPI_LED_OLD */
4806 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4807 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4808 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4809};
4810
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004811static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004812
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004813TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004814 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4815 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004816 "LED", /* all others */
4817 ); /* R30, R31 */
4818
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004819#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004820static struct tpacpi_led_classdev *tpacpi_leds;
4821static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004822static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004823 /* there's a limit of 19 chars + NULL before 2.6.26 */
4824 "tpacpi::power",
4825 "tpacpi:orange:batt",
4826 "tpacpi:green:batt",
4827 "tpacpi::dock_active",
4828 "tpacpi::bay_active",
4829 "tpacpi::dock_batt",
4830 "tpacpi::unknown_led",
4831 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004832 "tpacpi::dock_status1",
4833 "tpacpi::dock_status2",
4834 "tpacpi::unknown_led2",
4835 "tpacpi::unknown_led3",
4836 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004837};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004838#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004839
4840static inline bool tpacpi_is_led_restricted(const unsigned int led)
4841{
4842#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4843 return false;
4844#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03004845 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004846#endif
4847}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004848
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004849static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004850{
4851 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004852 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853
4854 switch (led_supported) {
4855 case TPACPI_LED_570:
4856 if (!acpi_evalf(ec_handle,
4857 &status, "GLED", "dd", 1 << led))
4858 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004859 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004860 TPACPI_LED_OFF :
4861 ((status == 1)?
4862 TPACPI_LED_ON :
4863 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004864 tpacpi_led_state_cache[led] = led_s;
4865 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 default:
4867 return -ENXIO;
4868 }
4869
4870 /* not reached */
4871}
4872
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004873static int led_set_status(const unsigned int led,
4874 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004875{
4876 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004877 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4878 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004879
4880 int rc = 0;
4881
4882 switch (led_supported) {
4883 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004884 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004885 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004886 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004887 if (unlikely(tpacpi_is_led_restricted(led)))
4888 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004889 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4890 (1 << led), led_sled_arg1[ledstatus]))
4891 rc = -EIO;
4892 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004893 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004894 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004895 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004896 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004897 if (unlikely(tpacpi_is_led_restricted(led)))
4898 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004899 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4900 if (rc >= 0)
4901 rc = ec_write(TPACPI_LED_EC_HLBL,
4902 (ledstatus == TPACPI_LED_BLINK) << led);
4903 if (rc >= 0)
4904 rc = ec_write(TPACPI_LED_EC_HLCL,
4905 (ledstatus != TPACPI_LED_OFF) << led);
4906 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004907 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004908 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004909 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4910 return -EINVAL;
4911 if (unlikely(tpacpi_is_led_restricted(led)))
4912 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004913 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4914 led, led_led_arg1[ledstatus]))
4915 rc = -EIO;
4916 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004917 default:
4918 rc = -ENXIO;
4919 }
4920
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004921 if (!rc)
4922 tpacpi_led_state_cache[led] = ledstatus;
4923
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004924 return rc;
4925}
4926
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004927static void led_set_status_worker(struct work_struct *work)
4928{
4929 struct tpacpi_led_classdev *data =
4930 container_of(work, struct tpacpi_led_classdev, work);
4931
4932 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004933 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004934}
4935
4936static void led_sysfs_set(struct led_classdev *led_cdev,
4937 enum led_brightness brightness)
4938{
4939 struct tpacpi_led_classdev *data = container_of(led_cdev,
4940 struct tpacpi_led_classdev, led_classdev);
4941
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004942 if (brightness == LED_OFF)
4943 data->new_state = TPACPI_LED_OFF;
4944 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4945 data->new_state = TPACPI_LED_ON;
4946 else
4947 data->new_state = TPACPI_LED_BLINK;
4948
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004949 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004950}
4951
4952static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4953 unsigned long *delay_on, unsigned long *delay_off)
4954{
4955 struct tpacpi_led_classdev *data = container_of(led_cdev,
4956 struct tpacpi_led_classdev, led_classdev);
4957
4958 /* Can we choose the flash rate? */
4959 if (*delay_on == 0 && *delay_off == 0) {
4960 /* yes. set them to the hardware blink rate (1 Hz) */
4961 *delay_on = 500; /* ms */
4962 *delay_off = 500; /* ms */
4963 } else if ((*delay_on != 500) || (*delay_off != 500))
4964 return -EINVAL;
4965
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004966 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004967 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004968
4969 return 0;
4970}
4971
4972static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4973{
4974 int rc;
4975
4976 struct tpacpi_led_classdev *data = container_of(led_cdev,
4977 struct tpacpi_led_classdev, led_classdev);
4978
4979 rc = led_get_status(data->led);
4980
4981 if (rc == TPACPI_LED_OFF || rc < 0)
4982 rc = LED_OFF; /* no error handling in led class :( */
4983 else
4984 rc = LED_FULL;
4985
4986 return rc;
4987}
4988
4989static void led_exit(void)
4990{
4991 unsigned int i;
4992
4993 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4994 if (tpacpi_leds[i].led_classdev.name)
4995 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4996 }
4997
4998 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004999}
5000
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005001static int __init tpacpi_init_led(unsigned int led)
5002{
5003 int rc;
5004
5005 tpacpi_leds[led].led = led;
5006
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005007 /* LEDs with no name don't get registered */
5008 if (!tpacpi_led_names[led])
5009 return 0;
5010
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005011 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5012 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5013 if (led_supported == TPACPI_LED_570)
5014 tpacpi_leds[led].led_classdev.brightness_get =
5015 &led_sysfs_get;
5016
5017 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5018
5019 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5020
5021 rc = led_classdev_register(&tpacpi_pdev->dev,
5022 &tpacpi_leds[led].led_classdev);
5023 if (rc < 0)
5024 tpacpi_leds[led].led_classdev.name = NULL;
5025
5026 return rc;
5027}
5028
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005029static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5030 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5031 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5032 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5033
5034 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5035 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5036 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5037 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5038 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5039 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5040 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5041 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5042
5043 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5044 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5045 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5046 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5047 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5048
5049 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5050 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5051 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5052 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5053
5054 /* (1) - may have excess leds enabled on MSB */
5055
5056 /* Defaults (order matters, keep last, don't reorder!) */
5057 { /* Lenovo */
5058 .vendor = PCI_VENDOR_ID_LENOVO,
5059 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5060 .quirks = 0x1fffU,
5061 },
5062 { /* IBM ThinkPads with no EC version string */
5063 .vendor = PCI_VENDOR_ID_IBM,
5064 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5065 .quirks = 0x00ffU,
5066 },
5067 { /* IBM ThinkPads with EC version string */
5068 .vendor = PCI_VENDOR_ID_IBM,
5069 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5070 .quirks = 0x00bfU,
5071 },
5072};
5073
5074#undef TPACPI_LEDQ_IBM
5075#undef TPACPI_LEDQ_LNV
5076
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005077static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005078{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005079 unsigned int i;
5080 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005081 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005082
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005083 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5084
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005085 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005087 if (!led_handle)
5088 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005089 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005090 else if (strlencmp(led_path, "SLED") == 0)
5091 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005092 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005093 else if (strlencmp(led_path, "SYSL") == 0)
5094 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005095 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005096 else
5097 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005098 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005099
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005100 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5101 str_supported(led_supported), led_supported);
5102
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005103 if (led_supported == TPACPI_LED_NONE)
5104 return 1;
5105
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005106 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5107 GFP_KERNEL);
5108 if (!tpacpi_leds) {
5109 printk(TPACPI_ERR "Out of memory for LED data\n");
5110 return -ENOMEM;
5111 }
5112
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005113 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5114 ARRAY_SIZE(led_useful_qtable));
5115
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005116 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005117 if (!tpacpi_is_led_restricted(i) &&
5118 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005119 rc = tpacpi_init_led(i);
5120 if (rc < 0) {
5121 led_exit();
5122 return rc;
5123 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005124 }
5125 }
5126
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005127#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005128 printk(TPACPI_NOTICE
5129 "warning: userspace override of important "
5130 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005131#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005132 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005133}
5134
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005135#define str_led_status(s) \
5136 ((s) == TPACPI_LED_OFF ? "off" : \
5137 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005138
5139static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
5141 int len = 0;
5142
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005143 if (!led_supported) {
5144 len += sprintf(p + len, "status:\t\tnot supported\n");
5145 return len;
5146 }
5147 len += sprintf(p + len, "status:\t\tsupported\n");
5148
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005149 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005150 /* 570 */
5151 int i, status;
5152 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005153 status = led_get_status(i);
5154 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005155 return -EIO;
5156 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005157 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005158 }
5159 }
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 len += sprintf(p + len, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005162 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
5164 return len;
5165}
5166
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005167static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168{
5169 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005170 int led, rc;
5171 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005172
5173 if (!led_supported)
5174 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
5176 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005177 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return -EINVAL;
5179
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005180 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005181 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005183 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005184 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005185 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005186 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005187 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005188 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005189
5190 rc = led_set_status(led, s);
5191 if (rc < 0)
5192 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
5194
5195 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005196}
5197
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005198static struct ibm_struct led_driver_data = {
5199 .name = "led",
5200 .read = led_read,
5201 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005202 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005203};
5204
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005205/*************************************************************************
5206 * Beep subdriver
5207 */
5208
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005209TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005210
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005211#define TPACPI_BEEP_Q1 0x0001
5212
5213static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5214 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5215 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5216};
5217
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005218static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005219{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005220 unsigned long quirks;
5221
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005222 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005224 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005225
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005226 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5227 str_supported(beep_handle != NULL));
5228
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005229 quirks = tpacpi_check_quirks(beep_quirk_table,
5230 ARRAY_SIZE(beep_quirk_table));
5231
5232 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5233
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005234 return (beep_handle)? 0 : 1;
5235}
5236
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005237static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238{
5239 int len = 0;
5240
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005241 if (!beep_handle)
5242 len += sprintf(p + len, "status:\t\tnot supported\n");
5243 else {
5244 len += sprintf(p + len, "status:\t\tsupported\n");
5245 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 return len;
5249}
5250
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005251static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
5253 char *cmd;
5254 int beep_cmd;
5255
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005256 if (!beep_handle)
5257 return -ENODEV;
5258
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005260 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5261 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 /* beep_cmd set */
5263 } else
5264 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005265 if (tp_features.beep_needs_two_args) {
5266 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5267 beep_cmd, 0))
5268 return -EIO;
5269 } else {
5270 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5271 beep_cmd))
5272 return -EIO;
5273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
5275
5276 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005277}
5278
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005279static struct ibm_struct beep_driver_data = {
5280 .name = "beep",
5281 .read = beep_read,
5282 .write = beep_write,
5283};
5284
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005285/*************************************************************************
5286 * Thermal subdriver
5287 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005288
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005289enum thermal_access_mode {
5290 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5291 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5292 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5293 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5294 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5295};
5296
5297enum { /* TPACPI_THERMAL_TPEC_* */
5298 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5299 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5300 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5301};
5302
5303#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5304struct ibm_thermal_sensors_struct {
5305 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5306};
5307
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005308static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005309
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005310/* idx is zero-based */
5311static int thermal_get_sensor(int idx, s32 *value)
5312{
5313 int t;
5314 s8 tmp;
5315 char tmpi[5];
5316
5317 t = TP_EC_THERMAL_TMP0;
5318
5319 switch (thermal_read_mode) {
5320#if TPACPI_MAX_THERMAL_SENSORS >= 16
5321 case TPACPI_THERMAL_TPEC_16:
5322 if (idx >= 8 && idx <= 15) {
5323 t = TP_EC_THERMAL_TMP8;
5324 idx -= 8;
5325 }
5326 /* fallthrough */
5327#endif
5328 case TPACPI_THERMAL_TPEC_8:
5329 if (idx <= 7) {
5330 if (!acpi_ec_read(t + idx, &tmp))
5331 return -EIO;
5332 *value = tmp * 1000;
5333 return 0;
5334 }
5335 break;
5336
5337 case TPACPI_THERMAL_ACPI_UPDT:
5338 if (idx <= 7) {
5339 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5340 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5341 return -EIO;
5342 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5343 return -EIO;
5344 *value = (t - 2732) * 100;
5345 return 0;
5346 }
5347 break;
5348
5349 case TPACPI_THERMAL_ACPI_TMP07:
5350 if (idx <= 7) {
5351 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5352 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5353 return -EIO;
5354 if (t > 127 || t < -127)
5355 t = TP_EC_THERMAL_TMP_NA;
5356 *value = t * 1000;
5357 return 0;
5358 }
5359 break;
5360
5361 case TPACPI_THERMAL_NONE:
5362 default:
5363 return -ENOSYS;
5364 }
5365
5366 return -EINVAL;
5367}
5368
5369static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5370{
5371 int res, i;
5372 int n;
5373
5374 n = 8;
5375 i = 0;
5376
5377 if (!s)
5378 return -EINVAL;
5379
5380 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5381 n = 16;
5382
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005383 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005384 res = thermal_get_sensor(i, &s->temp[i]);
5385 if (res)
5386 return res;
5387 }
5388
5389 return n;
5390}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005391
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005392/* sysfs temp##_input -------------------------------------------------- */
5393
5394static ssize_t thermal_temp_input_show(struct device *dev,
5395 struct device_attribute *attr,
5396 char *buf)
5397{
5398 struct sensor_device_attribute *sensor_attr =
5399 to_sensor_dev_attr(attr);
5400 int idx = sensor_attr->index;
5401 s32 value;
5402 int res;
5403
5404 res = thermal_get_sensor(idx, &value);
5405 if (res)
5406 return res;
5407 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5408 return -ENXIO;
5409
5410 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5411}
5412
5413#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005414 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5415 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005416
5417static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5418 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5419 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5420 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5421 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5422 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5423 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5424 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5425 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5426 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5427 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5428 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5429 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5430 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5431 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5432 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5433 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5434};
5435
5436#define THERMAL_ATTRS(X) \
5437 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5438
5439static struct attribute *thermal_temp_input_attr[] = {
5440 THERMAL_ATTRS(8),
5441 THERMAL_ATTRS(9),
5442 THERMAL_ATTRS(10),
5443 THERMAL_ATTRS(11),
5444 THERMAL_ATTRS(12),
5445 THERMAL_ATTRS(13),
5446 THERMAL_ATTRS(14),
5447 THERMAL_ATTRS(15),
5448 THERMAL_ATTRS(0),
5449 THERMAL_ATTRS(1),
5450 THERMAL_ATTRS(2),
5451 THERMAL_ATTRS(3),
5452 THERMAL_ATTRS(4),
5453 THERMAL_ATTRS(5),
5454 THERMAL_ATTRS(6),
5455 THERMAL_ATTRS(7),
5456 NULL
5457};
5458
5459static const struct attribute_group thermal_temp_input16_group = {
5460 .attrs = thermal_temp_input_attr
5461};
5462
5463static const struct attribute_group thermal_temp_input8_group = {
5464 .attrs = &thermal_temp_input_attr[8]
5465};
5466
5467#undef THERMAL_SENSOR_ATTR_TEMP
5468#undef THERMAL_ATTRS
5469
5470/* --------------------------------------------------------------------- */
5471
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005472static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005473{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005474 u8 t, ta1, ta2;
5475 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005476 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005477 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005478
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005479 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5480
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005481 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005482
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005483 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005484 /*
5485 * Direct EC access mode: sensors at registers
5486 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5487 * non-implemented, thermal sensors return 0x80 when
5488 * not available
5489 */
5490
5491 ta1 = ta2 = 0;
5492 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005493 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005494 ta1 |= t;
5495 } else {
5496 ta1 = 0;
5497 break;
5498 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005499 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005500 ta2 |= t;
5501 } else {
5502 ta1 = 0;
5503 break;
5504 }
5505 }
5506 if (ta1 == 0) {
5507 /* This is sheer paranoia, but we handle it anyway */
5508 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005509 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005510 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005511 "falling back to ACPI TMPx access "
5512 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005513 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005514 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005515 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005516 "ThinkPad ACPI EC access misbehaving, "
5517 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005518 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005519 }
5520 } else {
5521 thermal_read_mode =
5522 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005523 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005524 }
5525 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005526 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5527 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005528 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005529 } else {
5530 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005531 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005532 }
5533 } else {
5534 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005535 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005536 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005537
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005538 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5539 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5540 thermal_read_mode);
5541
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005542 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005543 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005544 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005545 &thermal_temp_input16_group);
5546 if (res)
5547 return res;
5548 break;
5549 case TPACPI_THERMAL_TPEC_8:
5550 case TPACPI_THERMAL_ACPI_TMP07:
5551 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005552 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005553 &thermal_temp_input8_group);
5554 if (res)
5555 return res;
5556 break;
5557 case TPACPI_THERMAL_NONE:
5558 default:
5559 return 1;
5560 }
5561
5562 return 0;
5563}
5564
5565static void thermal_exit(void)
5566{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005567 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005568 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005569 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005570 &thermal_temp_input16_group);
5571 break;
5572 case TPACPI_THERMAL_TPEC_8:
5573 case TPACPI_THERMAL_ACPI_TMP07:
5574 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005575 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005576 &thermal_temp_input16_group);
5577 break;
5578 case TPACPI_THERMAL_NONE:
5579 default:
5580 break;
5581 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005582}
5583
5584static int thermal_read(char *p)
5585{
5586 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005587 int n, i;
5588 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005589
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005590 n = thermal_get_sensors(&t);
5591 if (unlikely(n < 0))
5592 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005593
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005594 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005595
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005596 if (n > 0) {
5597 for (i = 0; i < (n - 1); i++)
5598 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5599 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5600 } else
5601 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005602
5603 return len;
5604}
5605
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005606static struct ibm_struct thermal_driver_data = {
5607 .name = "thermal",
5608 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005609 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005610};
5611
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005612/*************************************************************************
5613 * EC Dump subdriver
5614 */
5615
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005616static u8 ecdump_regs[256];
5617
5618static int ecdump_read(char *p)
5619{
5620 int len = 0;
5621 int i, j;
5622 u8 v;
5623
5624 len += sprintf(p + len, "EC "
5625 " +00 +01 +02 +03 +04 +05 +06 +07"
5626 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5627 for (i = 0; i < 256; i += 16) {
5628 len += sprintf(p + len, "EC 0x%02x:", i);
5629 for (j = 0; j < 16; j++) {
5630 if (!acpi_ec_read(i + j, &v))
5631 break;
5632 if (v != ecdump_regs[i + j])
5633 len += sprintf(p + len, " *%02x", v);
5634 else
5635 len += sprintf(p + len, " %02x", v);
5636 ecdump_regs[i + j] = v;
5637 }
5638 len += sprintf(p + len, "\n");
5639 if (j != 16)
5640 break;
5641 }
5642
5643 /* These are way too dangerous to advertise openly... */
5644#if 0
5645 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5646 " (<offset> is 00-ff, <value> is 00-ff)\n");
5647 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5648 " (<offset> is 00-ff, <value> is 0-255)\n");
5649#endif
5650 return len;
5651}
5652
5653static int ecdump_write(char *buf)
5654{
5655 char *cmd;
5656 int i, v;
5657
5658 while ((cmd = next_cmd(&buf))) {
5659 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5660 /* i and v set */
5661 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5662 /* i and v set */
5663 } else
5664 return -EINVAL;
5665 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5666 if (!acpi_ec_write(i, v))
5667 return -EIO;
5668 } else
5669 return -EINVAL;
5670 }
5671
5672 return 0;
5673}
5674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005675static struct ibm_struct ecdump_driver_data = {
5676 .name = "ecdump",
5677 .read = ecdump_read,
5678 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005679 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005680};
5681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005682/*************************************************************************
5683 * Backlight/brightness subdriver
5684 */
Holger Macht8acb0252006-10-20 14:30:28 -07005685
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005686#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5687
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005688/*
5689 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5690 * CMOS NVRAM byte 0x5E, bits 0-3.
5691 *
5692 * EC HBRV (0x31) has the following layout
5693 * Bit 7: unknown function
5694 * Bit 6: unknown function
5695 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5696 * Bit 4: must be set to zero to avoid problems
5697 * Bit 3-0: backlight brightness level
5698 *
5699 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005700 *
5701 * WARNING: The X61 has been verified to use HBRV for something else, so
5702 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5703 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005704 */
5705
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005706enum {
5707 TP_EC_BACKLIGHT = 0x31,
5708
5709 /* TP_EC_BACKLIGHT bitmasks */
5710 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5711 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5712 TP_EC_BACKLIGHT_MAPSW = 0x20,
5713};
5714
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005715enum tpacpi_brightness_access_mode {
5716 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5717 TPACPI_BRGHT_MODE_EC, /* EC control */
5718 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5719 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5720 TPACPI_BRGHT_MODE_MAX
5721};
5722
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005723static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005724
5725static enum tpacpi_brightness_access_mode brightness_mode =
5726 TPACPI_BRGHT_MODE_MAX;
5727
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005728static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5729
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005730static struct mutex brightness_mutex;
5731
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005732/* NVRAM brightness access,
5733 * call with brightness_mutex held! */
5734static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005735{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005736 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005737
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005738 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5739 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5741 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005742
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005743 return lnvram;
5744}
5745
5746static void tpacpi_brightness_checkpoint_nvram(void)
5747{
5748 u8 lec = 0;
5749 u8 b_nvram;
5750
5751 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5752 return;
5753
5754 vdbg_printk(TPACPI_DBG_BRGHT,
5755 "trying to checkpoint backlight level to NVRAM...\n");
5756
5757 if (mutex_lock_killable(&brightness_mutex) < 0)
5758 return;
5759
5760 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5761 goto unlock;
5762 lec &= TP_EC_BACKLIGHT_LVLMSK;
5763 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5764
5765 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5766 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5767 /* NVRAM needs update */
5768 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5769 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5770 b_nvram |= lec;
5771 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5772 dbg_printk(TPACPI_DBG_BRGHT,
5773 "updated NVRAM backlight level to %u (0x%02x)\n",
5774 (unsigned int) lec, (unsigned int) b_nvram);
5775 } else
5776 vdbg_printk(TPACPI_DBG_BRGHT,
5777 "NVRAM backlight level already is %u (0x%02x)\n",
5778 (unsigned int) lec, (unsigned int) b_nvram);
5779
5780unlock:
5781 mutex_unlock(&brightness_mutex);
5782}
5783
5784
5785/* call with brightness_mutex held! */
5786static int tpacpi_brightness_get_raw(int *status)
5787{
5788 u8 lec = 0;
5789
5790 switch (brightness_mode) {
5791 case TPACPI_BRGHT_MODE_UCMS_STEP:
5792 *status = tpacpi_brightness_nvram_get();
5793 return 0;
5794 case TPACPI_BRGHT_MODE_EC:
5795 case TPACPI_BRGHT_MODE_ECNVRAM:
5796 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005797 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005798 *status = lec;
5799 return 0;
5800 default:
5801 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005802 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005803}
5804
5805/* call with brightness_mutex held! */
5806/* do NOT call with illegal backlight level value */
5807static int tpacpi_brightness_set_ec(unsigned int value)
5808{
5809 u8 lec = 0;
5810
5811 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5812 return -EIO;
5813
5814 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5815 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5816 (value & TP_EC_BACKLIGHT_LVLMSK))))
5817 return -EIO;
5818
5819 return 0;
5820}
5821
5822/* call with brightness_mutex held! */
5823static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5824{
5825 int cmos_cmd, inc;
5826 unsigned int current_value, i;
5827
5828 current_value = tpacpi_brightness_nvram_get();
5829
5830 if (value == current_value)
5831 return 0;
5832
5833 cmos_cmd = (value > current_value) ?
5834 TP_CMOS_BRIGHTNESS_UP :
5835 TP_CMOS_BRIGHTNESS_DOWN;
5836 inc = (value > current_value) ? 1 : -1;
5837
5838 for (i = current_value; i != value; i += inc)
5839 if (issue_thinkpad_cmos_command(cmos_cmd))
5840 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005841
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005842 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005843}
5844
5845/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005846static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005847{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005848 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005849
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005850 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5851 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005852 return -EINVAL;
5853
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005854 vdbg_printk(TPACPI_DBG_BRGHT,
5855 "set backlight level to %d\n", value);
5856
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005857 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005858 if (res < 0)
5859 return res;
5860
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005861 switch (brightness_mode) {
5862 case TPACPI_BRGHT_MODE_EC:
5863 case TPACPI_BRGHT_MODE_ECNVRAM:
5864 res = tpacpi_brightness_set_ec(value);
5865 break;
5866 case TPACPI_BRGHT_MODE_UCMS_STEP:
5867 res = tpacpi_brightness_set_ucmsstep(value);
5868 break;
5869 default:
5870 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005871 }
5872
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005873 mutex_unlock(&brightness_mutex);
5874 return res;
5875}
5876
5877/* sysfs backlight class ----------------------------------------------- */
5878
5879static int brightness_update_status(struct backlight_device *bd)
5880{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005881 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005882 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5883 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005884 bd->props.brightness : 0;
5885
5886 dbg_printk(TPACPI_DBG_BRGHT,
5887 "backlight: attempt to set level to %d\n",
5888 level);
5889
5890 /* it is the backlight class's job (caller) to handle
5891 * EINTR and other errors properly */
5892 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005893}
Holger Macht8acb0252006-10-20 14:30:28 -07005894
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005895static int brightness_get(struct backlight_device *bd)
5896{
5897 int status, res;
5898
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005899 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005900 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005901 return 0;
5902
5903 res = tpacpi_brightness_get_raw(&status);
5904
5905 mutex_unlock(&brightness_mutex);
5906
5907 if (res < 0)
5908 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005909
5910 return status & TP_EC_BACKLIGHT_LVLMSK;
5911}
5912
Richard Purdie599a52d2007-02-10 23:07:48 +00005913static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005914 .get_brightness = brightness_get,
5915 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005916};
5917
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005918/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005919
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005920static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005921{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005922 int b;
5923
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005924 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5925
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005926 mutex_init(&brightness_mutex);
5927
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005928 /*
5929 * We always attempt to detect acpi support, so as to switch
5930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5931 * going to publish a backlight interface
5932 */
5933 b = tpacpi_check_std_acpi_brightness_support();
5934 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005935
5936 if (acpi_video_backlight_support()) {
5937 if (brightness_enable > 1) {
5938 printk(TPACPI_NOTICE
5939 "Standard ACPI backlight interface "
5940 "available, not loading native one.\n");
5941 return 1;
5942 } else if (brightness_enable == 1) {
5943 printk(TPACPI_NOTICE
5944 "Backlight control force enabled, even if standard "
5945 "ACPI backlight interface is available\n");
5946 }
5947 } else {
5948 if (brightness_enable > 1) {
5949 printk(TPACPI_NOTICE
5950 "Standard ACPI backlight interface not "
5951 "available, thinkpad_acpi native "
5952 "brightness control enabled\n");
5953 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005954 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005955 }
5956
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005957 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005959 "brightness support disabled by "
5960 "module parameter\n");
5961 return 1;
5962 }
5963
5964 if (b > 16) {
5965 printk(TPACPI_ERR
5966 "Unsupported brightness interface, "
5967 "please contact %s\n", TPACPI_MAIL);
5968 return 1;
5969 }
5970 if (b == 16)
5971 tp_features.bright_16levels = 1;
5972
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005973 /*
5974 * Check for module parameter bogosity, note that we
5975 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5976 * able to detect "unspecified"
5977 */
5978 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005979 return -EINVAL;
5980
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005981 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5982 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5983 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5984 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5985 /*
5986 * IBM models that define HBRV probably have
5987 * EC-based backlight level control
5988 */
5989 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5990 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5991 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5992 else
5993 /* all other IBM ThinkPads */
5994 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5995 } else
5996 /* All Lenovo ThinkPads */
5997 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5998
5999 dbg_printk(TPACPI_DBG_BRGHT,
6000 "selected brightness_mode=%d\n",
6001 brightness_mode);
6002 }
6003
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006004 /* Safety */
6005 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6006 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6007 brightness_mode == TPACPI_BRGHT_MODE_EC))
6008 return -EINVAL;
6009
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006010 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006011 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006012
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006013 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006014 printk(TPACPI_INFO
6015 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006016
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03006017 ibm_backlight_device = backlight_device_register(
6018 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6019 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006020 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006021 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006022 return PTR_ERR(ibm_backlight_device);
6023 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006024 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6025 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006026
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006027 ibm_backlight_device->props.max_brightness =
6028 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006029 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006030 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006031
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006032 return 0;
6033}
6034
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006035static void brightness_suspend(pm_message_t state)
6036{
6037 tpacpi_brightness_checkpoint_nvram();
6038}
6039
6040static void brightness_shutdown(void)
6041{
6042 tpacpi_brightness_checkpoint_nvram();
6043}
6044
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006045static void brightness_exit(void)
6046{
6047 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006048 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006049 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006050 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006051 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006052
6053 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006054}
6055
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006056static int brightness_read(char *p)
6057{
6058 int len = 0;
6059 int level;
6060
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006061 level = brightness_get(NULL);
6062 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006063 len += sprintf(p + len, "level:\t\tunreadable\n");
6064 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006065 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006066 len += sprintf(p + len, "commands:\tup, down\n");
6067 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006068 " (<level> is 0-%d)\n",
6069 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006070 }
6071
6072 return len;
6073}
6074
6075static int brightness_write(char *buf)
6076{
6077 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006078 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006079 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02006080 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006081
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006082 level = brightness_get(NULL);
6083 if (level < 0)
6084 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006085
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006086 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006087 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006088 if (level < max_level)
6089 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006090 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006091 if (level > 0)
6092 level--;
6093 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6094 level >= 0 && level <= max_level) {
6095 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006096 } else
6097 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006098 }
6099
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006100 tpacpi_disclose_usertask("procfs brightness",
6101 "set level to %d\n", level);
6102
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006103 /*
6104 * Now we know what the final level should be, so we try to set it.
6105 * Doing it this way makes the syscall restartable in case of EINTR
6106 */
6107 rc = brightness_set(level);
6108 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006109}
6110
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006111static struct ibm_struct brightness_driver_data = {
6112 .name = "brightness",
6113 .read = brightness_read,
6114 .write = brightness_write,
6115 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006116 .suspend = brightness_suspend,
6117 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006118};
6119
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006120/*************************************************************************
6121 * Volume subdriver
6122 */
6123
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006124static int volume_offset = 0x30;
6125
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006126static int volume_read(char *p)
6127{
6128 int len = 0;
6129 u8 level;
6130
6131 if (!acpi_ec_read(volume_offset, &level)) {
6132 len += sprintf(p + len, "level:\t\tunreadable\n");
6133 } else {
6134 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6135 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6136 len += sprintf(p + len, "commands:\tup, down, mute\n");
6137 len += sprintf(p + len, "commands:\tlevel <level>"
6138 " (<level> is 0-15)\n");
6139 }
6140
6141 return len;
6142}
6143
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006144static int volume_write(char *buf)
6145{
6146 int cmos_cmd, inc, i;
6147 u8 level, mute;
6148 int new_level, new_mute;
6149 char *cmd;
6150
6151 while ((cmd = next_cmd(&buf))) {
6152 if (!acpi_ec_read(volume_offset, &level))
6153 return -EIO;
6154 new_mute = mute = level & 0x40;
6155 new_level = level = level & 0xf;
6156
6157 if (strlencmp(cmd, "up") == 0) {
6158 if (mute)
6159 new_mute = 0;
6160 else
6161 new_level = level == 15 ? 15 : level + 1;
6162 } else if (strlencmp(cmd, "down") == 0) {
6163 if (mute)
6164 new_mute = 0;
6165 else
6166 new_level = level == 0 ? 0 : level - 1;
6167 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6168 new_level >= 0 && new_level <= 15) {
6169 /* new_level set */
6170 } else if (strlencmp(cmd, "mute") == 0) {
6171 new_mute = 0x40;
6172 } else
6173 return -EINVAL;
6174
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006175 if (new_level != level) {
6176 /* mute doesn't change */
6177
6178 cmos_cmd = (new_level > level) ?
6179 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006180 inc = new_level > level ? 1 : -1;
6181
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006182 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006183 !acpi_ec_write(volume_offset, level)))
6184 return -EIO;
6185
6186 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006187 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006188 !acpi_ec_write(volume_offset, i + inc))
6189 return -EIO;
6190
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006191 if (mute &&
6192 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6193 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006194 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006195 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006196 }
6197
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006198 if (new_mute != mute) {
6199 /* level doesn't change */
6200
6201 cmos_cmd = (new_mute) ?
6202 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006203
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006204 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006205 !acpi_ec_write(volume_offset, level + new_mute))
6206 return -EIO;
6207 }
6208 }
6209
6210 return 0;
6211}
6212
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006213static struct ibm_struct volume_driver_data = {
6214 .name = "volume",
6215 .read = volume_read,
6216 .write = volume_write,
6217};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006218
6219/*************************************************************************
6220 * Fan subdriver
6221 */
6222
6223/*
6224 * FAN ACCESS MODES
6225 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006226 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006227 * ACPI GFAN method: returns fan level
6228 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006229 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006230 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006231 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006232 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006233 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6234 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006235 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6236 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006237 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006238 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006239 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6240 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006241 *
6242 * Fan speed changes of any sort (including those caused by the
6243 * disengaged mode) are usually done slowly by the firmware as the
6244 * maximum ammount of fan duty cycle change per second seems to be
6245 * limited.
6246 *
6247 * Reading is not available if GFAN exists.
6248 * Writing is not available if SFAN exists.
6249 *
6250 * Bits
6251 * 7 automatic mode engaged;
6252 * (default operation mode of the ThinkPad)
6253 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006254 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006255 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006256 * the tachometer while the fan controller ramps up
6257 * the speed (which can take up to a few *minutes*).
6258 * Speeds up fan to 100% duty-cycle, which is far above
6259 * the standard RPM levels. It is not impossible that
6260 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006261 * 5-3 unused in some models. Extra bits for fan level
6262 * in others, but still useless as all values above
6263 * 7 map to the same speed as level 7 in these models.
6264 * 2-0 fan level (0..7 usually)
6265 * 0x00 = stop
6266 * 0x07 = max (set when temperatures critical)
6267 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006268 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006269 *
6270 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6271 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6272 * does so, its initial value is meaningless (0x07).
6273 *
6274 * For firmware bugs, refer to:
6275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6276 *
6277 * ----
6278 *
6279 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6280 * Main fan tachometer reading (in RPM)
6281 *
6282 * This register is present on all ThinkPads with a new-style EC, and
6283 * it is known not to be present on the A21m/e, and T22, as there is
6284 * something else in offset 0x84 according to the ACPI DSDT. Other
6285 * ThinkPads from this same time period (and earlier) probably lack the
6286 * tachometer as well.
6287 *
Nick Andrew877d0312009-01-26 11:06:57 +01006288 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006289 * was never fixed by IBM to report the EC firmware version string
6290 * probably support the tachometer (like the early X models), so
6291 * detecting it is quite hard. We need more data to know for sure.
6292 *
6293 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6294 * might result.
6295 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006296 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6297 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006298 *
6299 * For firmware bugs, refer to:
6300 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6301 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006302 * ----
6303 *
6304 * ThinkPad EC register 0x31 bit 0 (only on select models)
6305 *
6306 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6307 * show the speed of the main fan. When bit 0 of EC register 0x31
6308 * is one, the tachometer registers show the speed of the auxiliary
6309 * fan.
6310 *
6311 * Fan control seems to affect both fans, regardless of the state
6312 * of this bit.
6313 *
6314 * So far, only the firmware for the X60/X61 non-tablet versions
6315 * seem to support this (firmware TP-7M).
6316 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006317 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006318 * ThinkPad X31, X40, X41. Not available in the X60.
6319 *
6320 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6321 * high speed. ACPI DSDT seems to map these three speeds to levels
6322 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6323 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6324 *
6325 * The speeds are stored on handles
6326 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6327 *
6328 * There are three default speed sets, acessible as handles:
6329 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6330 *
6331 * ACPI DSDT switches which set is in use depending on various
6332 * factors.
6333 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006334 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006335 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6336 * but the ACPI tables just mention level 7.
6337 */
6338
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006339enum { /* Fan control constants */
6340 fan_status_offset = 0x2f, /* EC register 0x2f */
6341 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6342 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006343 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6344 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006345
6346 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6347 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6348
6349 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6350};
6351
6352enum fan_status_access_mode {
6353 TPACPI_FAN_NONE = 0, /* No fan status or control */
6354 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6355 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6356};
6357
6358enum fan_control_access_mode {
6359 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6360 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6361 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6362 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6363};
6364
6365enum fan_control_commands {
6366 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6367 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6368 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6369 * and also watchdog cmd */
6370};
6371
6372static int fan_control_allowed;
6373
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006374static enum fan_status_access_mode fan_status_access_mode;
6375static enum fan_control_access_mode fan_control_access_mode;
6376static enum fan_control_commands fan_control_commands;
6377
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006378static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006379static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006380static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006381static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006382
6383static struct mutex fan_mutex;
6384
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006385static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006386static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006387
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006388TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6389TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006390 "\\FSPD", /* 600e/x, 770e, 770x */
6391 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006392TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006393 "JFNS", /* 770x-JL */
6394 ); /* all others */
6395
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006396/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006397 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6398 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6399 * be in auto mode (0x80).
6400 *
6401 * This is corrected by any write to HFSP either by the driver, or
6402 * by the firmware.
6403 *
6404 * We assume 0x07 really means auto mode while this quirk is active,
6405 * as this is far more likely than the ThinkPad being in level 7,
6406 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006407 *
6408 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6409 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006410 */
6411
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006412static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006413{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006414 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006415 printk(TPACPI_NOTICE
6416 "fan_init: initial fan status is unknown, "
6417 "assuming it is in auto mode\n");
6418 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006419 }
6420}
6421
6422static void fan_quirk1_handle(u8 *fan_status)
6423{
6424 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6425 if (*fan_status != fan_control_initial_status) {
6426 /* something changed the HFSP regisnter since
6427 * driver init time, so it is not undefined
6428 * anymore */
6429 tp_features.fan_ctrl_status_undef = 0;
6430 } else {
6431 /* Return most likely status. In fact, it
6432 * might be the only possible status */
6433 *fan_status = TP_EC_FAN_AUTO;
6434 }
6435 }
6436}
6437
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006438/* Select main fan on X60/X61, NOOP on others */
6439static bool fan_select_fan1(void)
6440{
6441 if (tp_features.second_fan) {
6442 u8 val;
6443
6444 if (ec_read(fan_select_offset, &val) < 0)
6445 return false;
6446 val &= 0xFEU;
6447 if (ec_write(fan_select_offset, val) < 0)
6448 return false;
6449 }
6450 return true;
6451}
6452
6453/* Select secondary fan on X60/X61 */
6454static bool fan_select_fan2(void)
6455{
6456 u8 val;
6457
6458 if (!tp_features.second_fan)
6459 return false;
6460
6461 if (ec_read(fan_select_offset, &val) < 0)
6462 return false;
6463 val |= 0x01U;
6464 if (ec_write(fan_select_offset, val) < 0)
6465 return false;
6466
6467 return true;
6468}
6469
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006470/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006471 * Call with fan_mutex held
6472 */
6473static void fan_update_desired_level(u8 status)
6474{
6475 if ((status &
6476 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6477 if (status > 7)
6478 fan_control_desired_level = 7;
6479 else
6480 fan_control_desired_level = status;
6481 }
6482}
6483
6484static int fan_get_status(u8 *status)
6485{
6486 u8 s;
6487
6488 /* TODO:
6489 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6490
6491 switch (fan_status_access_mode) {
6492 case TPACPI_FAN_RD_ACPI_GFAN:
6493 /* 570, 600e/x, 770e, 770x */
6494
6495 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6496 return -EIO;
6497
6498 if (likely(status))
6499 *status = s & 0x07;
6500
6501 break;
6502
6503 case TPACPI_FAN_RD_TPEC:
6504 /* all except 570, 600e/x, 770e, 770x */
6505 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6506 return -EIO;
6507
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006508 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006509 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006510 fan_quirk1_handle(status);
6511 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006512
6513 break;
6514
6515 default:
6516 return -ENXIO;
6517 }
6518
6519 return 0;
6520}
6521
6522static int fan_get_status_safe(u8 *status)
6523{
6524 int rc;
6525 u8 s;
6526
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006527 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006528 return -ERESTARTSYS;
6529 rc = fan_get_status(&s);
6530 if (!rc)
6531 fan_update_desired_level(s);
6532 mutex_unlock(&fan_mutex);
6533
6534 if (status)
6535 *status = s;
6536
6537 return rc;
6538}
6539
6540static int fan_get_speed(unsigned int *speed)
6541{
6542 u8 hi, lo;
6543
6544 switch (fan_status_access_mode) {
6545 case TPACPI_FAN_RD_TPEC:
6546 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006547 if (unlikely(!fan_select_fan1()))
6548 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006549 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6550 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6551 return -EIO;
6552
6553 if (likely(speed))
6554 *speed = (hi << 8) | lo;
6555
6556 break;
6557
6558 default:
6559 return -ENXIO;
6560 }
6561
6562 return 0;
6563}
6564
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006565static int fan2_get_speed(unsigned int *speed)
6566{
6567 u8 hi, lo;
6568 bool rc;
6569
6570 switch (fan_status_access_mode) {
6571 case TPACPI_FAN_RD_TPEC:
6572 /* all except 570, 600e/x, 770e, 770x */
6573 if (unlikely(!fan_select_fan2()))
6574 return -EIO;
6575 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6576 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6577 fan_select_fan1(); /* play it safe */
6578 if (rc)
6579 return -EIO;
6580
6581 if (likely(speed))
6582 *speed = (hi << 8) | lo;
6583
6584 break;
6585
6586 default:
6587 return -ENXIO;
6588 }
6589
6590 return 0;
6591}
6592
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006593static int fan_set_level(int level)
6594{
6595 if (!fan_control_allowed)
6596 return -EPERM;
6597
6598 switch (fan_control_access_mode) {
6599 case TPACPI_FAN_WR_ACPI_SFAN:
6600 if (level >= 0 && level <= 7) {
6601 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6602 return -EIO;
6603 } else
6604 return -EINVAL;
6605 break;
6606
6607 case TPACPI_FAN_WR_ACPI_FANS:
6608 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006609 if (!(level & TP_EC_FAN_AUTO) &&
6610 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006611 ((level < 0) || (level > 7)))
6612 return -EINVAL;
6613
6614 /* safety net should the EC not support AUTO
6615 * or FULLSPEED mode bits and just ignore them */
6616 if (level & TP_EC_FAN_FULLSPEED)
6617 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006618 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006619 level |= 4; /* safety min speed 4 */
6620
6621 if (!acpi_ec_write(fan_status_offset, level))
6622 return -EIO;
6623 else
6624 tp_features.fan_ctrl_status_undef = 0;
6625 break;
6626
6627 default:
6628 return -ENXIO;
6629 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006630
6631 vdbg_printk(TPACPI_DBG_FAN,
6632 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006633 return 0;
6634}
6635
6636static int fan_set_level_safe(int level)
6637{
6638 int rc;
6639
6640 if (!fan_control_allowed)
6641 return -EPERM;
6642
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006643 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006644 return -ERESTARTSYS;
6645
6646 if (level == TPACPI_FAN_LAST_LEVEL)
6647 level = fan_control_desired_level;
6648
6649 rc = fan_set_level(level);
6650 if (!rc)
6651 fan_update_desired_level(level);
6652
6653 mutex_unlock(&fan_mutex);
6654 return rc;
6655}
6656
6657static int fan_set_enable(void)
6658{
6659 u8 s;
6660 int rc;
6661
6662 if (!fan_control_allowed)
6663 return -EPERM;
6664
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006665 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006666 return -ERESTARTSYS;
6667
6668 switch (fan_control_access_mode) {
6669 case TPACPI_FAN_WR_ACPI_FANS:
6670 case TPACPI_FAN_WR_TPEC:
6671 rc = fan_get_status(&s);
6672 if (rc < 0)
6673 break;
6674
6675 /* Don't go out of emergency fan mode */
6676 if (s != 7) {
6677 s &= 0x07;
6678 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6679 }
6680
6681 if (!acpi_ec_write(fan_status_offset, s))
6682 rc = -EIO;
6683 else {
6684 tp_features.fan_ctrl_status_undef = 0;
6685 rc = 0;
6686 }
6687 break;
6688
6689 case TPACPI_FAN_WR_ACPI_SFAN:
6690 rc = fan_get_status(&s);
6691 if (rc < 0)
6692 break;
6693
6694 s &= 0x07;
6695
6696 /* Set fan to at least level 4 */
6697 s |= 4;
6698
6699 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006700 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006701 else
6702 rc = 0;
6703 break;
6704
6705 default:
6706 rc = -ENXIO;
6707 }
6708
6709 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006710
6711 if (!rc)
6712 vdbg_printk(TPACPI_DBG_FAN,
6713 "fan control: set fan control register to 0x%02x\n",
6714 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006715 return rc;
6716}
6717
6718static int fan_set_disable(void)
6719{
6720 int rc;
6721
6722 if (!fan_control_allowed)
6723 return -EPERM;
6724
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006725 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006726 return -ERESTARTSYS;
6727
6728 rc = 0;
6729 switch (fan_control_access_mode) {
6730 case TPACPI_FAN_WR_ACPI_FANS:
6731 case TPACPI_FAN_WR_TPEC:
6732 if (!acpi_ec_write(fan_status_offset, 0x00))
6733 rc = -EIO;
6734 else {
6735 fan_control_desired_level = 0;
6736 tp_features.fan_ctrl_status_undef = 0;
6737 }
6738 break;
6739
6740 case TPACPI_FAN_WR_ACPI_SFAN:
6741 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6742 rc = -EIO;
6743 else
6744 fan_control_desired_level = 0;
6745 break;
6746
6747 default:
6748 rc = -ENXIO;
6749 }
6750
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006751 if (!rc)
6752 vdbg_printk(TPACPI_DBG_FAN,
6753 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006754
6755 mutex_unlock(&fan_mutex);
6756 return rc;
6757}
6758
6759static int fan_set_speed(int speed)
6760{
6761 int rc;
6762
6763 if (!fan_control_allowed)
6764 return -EPERM;
6765
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006766 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006767 return -ERESTARTSYS;
6768
6769 rc = 0;
6770 switch (fan_control_access_mode) {
6771 case TPACPI_FAN_WR_ACPI_FANS:
6772 if (speed >= 0 && speed <= 65535) {
6773 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6774 speed, speed, speed))
6775 rc = -EIO;
6776 } else
6777 rc = -EINVAL;
6778 break;
6779
6780 default:
6781 rc = -ENXIO;
6782 }
6783
6784 mutex_unlock(&fan_mutex);
6785 return rc;
6786}
6787
6788static void fan_watchdog_reset(void)
6789{
6790 static int fan_watchdog_active;
6791
6792 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6793 return;
6794
6795 if (fan_watchdog_active)
6796 cancel_delayed_work(&fan_watchdog_task);
6797
6798 if (fan_watchdog_maxinterval > 0 &&
6799 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6800 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006801 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006802 msecs_to_jiffies(fan_watchdog_maxinterval
6803 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006804 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006805 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006806 "watchdog will not trigger\n");
6807 }
6808 } else
6809 fan_watchdog_active = 0;
6810}
6811
6812static void fan_watchdog_fire(struct work_struct *ignored)
6813{
6814 int rc;
6815
6816 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6817 return;
6818
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006819 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006820 rc = fan_set_enable();
6821 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006822 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006823 "will try again later...\n", -rc);
6824 /* reschedule for later */
6825 fan_watchdog_reset();
6826 }
6827}
6828
6829/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006830 * SYSFS fan layout: hwmon compatible (device)
6831 *
6832 * pwm*_enable:
6833 * 0: "disengaged" mode
6834 * 1: manual mode
6835 * 2: native EC "auto" mode (recommended, hardware default)
6836 *
6837 * pwm*: set speed in manual mode, ignored otherwise.
6838 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6839 * interpolation.
6840 *
6841 * fan*_input: tachometer reading, RPM
6842 *
6843 *
6844 * SYSFS fan layout: extensions
6845 *
6846 * fan_watchdog (driver):
6847 * fan watchdog interval in seconds, 0 disables (default), max 120
6848 */
6849
6850/* sysfs fan pwm1_enable ----------------------------------------------- */
6851static ssize_t fan_pwm1_enable_show(struct device *dev,
6852 struct device_attribute *attr,
6853 char *buf)
6854{
6855 int res, mode;
6856 u8 status;
6857
6858 res = fan_get_status_safe(&status);
6859 if (res)
6860 return res;
6861
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006862 if (status & TP_EC_FAN_FULLSPEED) {
6863 mode = 0;
6864 } else if (status & TP_EC_FAN_AUTO) {
6865 mode = 2;
6866 } else
6867 mode = 1;
6868
6869 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6870}
6871
6872static ssize_t fan_pwm1_enable_store(struct device *dev,
6873 struct device_attribute *attr,
6874 const char *buf, size_t count)
6875{
6876 unsigned long t;
6877 int res, level;
6878
6879 if (parse_strtoul(buf, 2, &t))
6880 return -EINVAL;
6881
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006882 tpacpi_disclose_usertask("hwmon pwm1_enable",
6883 "set fan mode to %lu\n", t);
6884
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006885 switch (t) {
6886 case 0:
6887 level = TP_EC_FAN_FULLSPEED;
6888 break;
6889 case 1:
6890 level = TPACPI_FAN_LAST_LEVEL;
6891 break;
6892 case 2:
6893 level = TP_EC_FAN_AUTO;
6894 break;
6895 case 3:
6896 /* reserved for software-controlled auto mode */
6897 return -ENOSYS;
6898 default:
6899 return -EINVAL;
6900 }
6901
6902 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006903 if (res == -ENXIO)
6904 return -EINVAL;
6905 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006906 return res;
6907
6908 fan_watchdog_reset();
6909
6910 return count;
6911}
6912
6913static struct device_attribute dev_attr_fan_pwm1_enable =
6914 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6915 fan_pwm1_enable_show, fan_pwm1_enable_store);
6916
6917/* sysfs fan pwm1 ------------------------------------------------------ */
6918static ssize_t fan_pwm1_show(struct device *dev,
6919 struct device_attribute *attr,
6920 char *buf)
6921{
6922 int res;
6923 u8 status;
6924
6925 res = fan_get_status_safe(&status);
6926 if (res)
6927 return res;
6928
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006929 if ((status &
6930 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6931 status = fan_control_desired_level;
6932
6933 if (status > 7)
6934 status = 7;
6935
6936 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6937}
6938
6939static ssize_t fan_pwm1_store(struct device *dev,
6940 struct device_attribute *attr,
6941 const char *buf, size_t count)
6942{
6943 unsigned long s;
6944 int rc;
6945 u8 status, newlevel;
6946
6947 if (parse_strtoul(buf, 255, &s))
6948 return -EINVAL;
6949
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006950 tpacpi_disclose_usertask("hwmon pwm1",
6951 "set fan speed to %lu\n", s);
6952
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006953 /* scale down from 0-255 to 0-7 */
6954 newlevel = (s >> 5) & 0x07;
6955
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006956 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006957 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006958
6959 rc = fan_get_status(&status);
6960 if (!rc && (status &
6961 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6962 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006963 if (rc == -ENXIO)
6964 rc = -EINVAL;
6965 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006966 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006967 fan_watchdog_reset();
6968 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006969 }
6970
6971 mutex_unlock(&fan_mutex);
6972 return (rc)? rc : count;
6973}
6974
6975static struct device_attribute dev_attr_fan_pwm1 =
6976 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6977 fan_pwm1_show, fan_pwm1_store);
6978
6979/* sysfs fan fan1_input ------------------------------------------------ */
6980static ssize_t fan_fan1_input_show(struct device *dev,
6981 struct device_attribute *attr,
6982 char *buf)
6983{
6984 int res;
6985 unsigned int speed;
6986
6987 res = fan_get_speed(&speed);
6988 if (res < 0)
6989 return res;
6990
6991 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6992}
6993
6994static struct device_attribute dev_attr_fan_fan1_input =
6995 __ATTR(fan1_input, S_IRUGO,
6996 fan_fan1_input_show, NULL);
6997
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03006998/* sysfs fan fan2_input ------------------------------------------------ */
6999static ssize_t fan_fan2_input_show(struct device *dev,
7000 struct device_attribute *attr,
7001 char *buf)
7002{
7003 int res;
7004 unsigned int speed;
7005
7006 res = fan2_get_speed(&speed);
7007 if (res < 0)
7008 return res;
7009
7010 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7011}
7012
7013static struct device_attribute dev_attr_fan_fan2_input =
7014 __ATTR(fan2_input, S_IRUGO,
7015 fan_fan2_input_show, NULL);
7016
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007017/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007018static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7019 char *buf)
7020{
7021 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7022}
7023
7024static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7025 const char *buf, size_t count)
7026{
7027 unsigned long t;
7028
7029 if (parse_strtoul(buf, 120, &t))
7030 return -EINVAL;
7031
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007032 if (!fan_control_allowed)
7033 return -EPERM;
7034
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007035 fan_watchdog_maxinterval = t;
7036 fan_watchdog_reset();
7037
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007038 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7039
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007040 return count;
7041}
7042
7043static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7044 fan_fan_watchdog_show, fan_fan_watchdog_store);
7045
7046/* --------------------------------------------------------------------- */
7047static struct attribute *fan_attributes[] = {
7048 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7049 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007050 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007051 NULL
7052};
7053
7054static const struct attribute_group fan_attr_group = {
7055 .attrs = fan_attributes,
7056};
7057
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007058#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7059#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007060
7061#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7062 { .vendor = PCI_VENDOR_ID_IBM, \
7063 .bios = TPACPI_MATCH_ANY, \
7064 .ec = TPID(__id1, __id2), \
7065 .quirks = __quirks }
7066
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007067#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7068 { .vendor = PCI_VENDOR_ID_LENOVO, \
7069 .bios = TPACPI_MATCH_ANY, \
7070 .ec = TPID(__id1, __id2), \
7071 .quirks = __quirks }
7072
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007073static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7074 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7075 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7076 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7077 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007078 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007079};
7080
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007081#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007082#undef TPACPI_FAN_QI
7083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007084static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007085{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007086 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007087 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007088
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007089 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7090 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007091
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007092 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007093 fan_status_access_mode = TPACPI_FAN_NONE;
7094 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007095 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007096 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007097 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007098 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007099 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007100
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007101 TPACPI_ACPIHANDLE_INIT(fans);
7102 TPACPI_ACPIHANDLE_INIT(gfan);
7103 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007104
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007105 quirks = tpacpi_check_quirks(fan_quirk_table,
7106 ARRAY_SIZE(fan_quirk_table));
7107
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007108 if (gfan_handle) {
7109 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007110 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007111 } else {
7112 /* all other ThinkPads: note that even old-style
7113 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007114 if (likely(acpi_ec_read(fan_status_offset,
7115 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007116 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007117 if (quirks & TPACPI_FAN_Q1)
7118 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007119 if (quirks & TPACPI_FAN_2FAN) {
7120 tp_features.second_fan = 1;
7121 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7122 "secondary fan support enabled\n");
7123 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007124 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007125 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007126 "ThinkPad ACPI EC access misbehaving, "
7127 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007128 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007129 }
7130 }
7131
7132 if (sfan_handle) {
7133 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007134 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007135 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007136 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007137 } else {
7138 if (!gfan_handle) {
7139 /* gfan without sfan means no fan control */
7140 /* all other models implement TP EC 0x2f control */
7141
7142 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007143 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007144 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007145 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007146 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007147 TPACPI_FAN_CMD_SPEED |
7148 TPACPI_FAN_CMD_LEVEL |
7149 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007150 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007151 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007152 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007153 TPACPI_FAN_CMD_LEVEL |
7154 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007155 }
7156 }
7157 }
7158
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007159 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7160 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007161 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7162 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7163 fan_status_access_mode, fan_control_access_mode);
7164
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007165 /* fan control master switch */
7166 if (!fan_control_allowed) {
7167 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7168 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007169 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007170 "fan control features disabled by parameter\n");
7171 }
7172
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007173 /* update fan_control_desired_level */
7174 if (fan_status_access_mode != TPACPI_FAN_NONE)
7175 fan_get_status_safe(NULL);
7176
7177 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7178 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007179 if (tp_features.second_fan) {
7180 /* attach second fan tachometer */
7181 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7182 &dev_attr_fan_fan2_input.attr;
7183 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007184 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007185 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007186 if (rc < 0)
7187 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03007188
7189 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7190 &driver_attr_fan_watchdog);
7191 if (rc < 0) {
7192 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7193 &fan_attr_group);
7194 return rc;
7195 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007196 return 0;
7197 } else
7198 return 1;
7199}
7200
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007201static void fan_exit(void)
7202{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007203 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007204 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007205
7206 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007207 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007208 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7209 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007210
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007211 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007212 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007213}
7214
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007215static void fan_suspend(pm_message_t state)
7216{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007217 int rc;
7218
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007219 if (!fan_control_allowed)
7220 return;
7221
7222 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007223 fan_control_resume_level = 0;
7224 rc = fan_get_status_safe(&fan_control_resume_level);
7225 if (rc < 0)
7226 printk(TPACPI_NOTICE
7227 "failed to read fan level for later "
7228 "restore during resume: %d\n", rc);
7229
7230 /* if it is undefined, don't attempt to restore it.
7231 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007232 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007233 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007234}
7235
7236static void fan_resume(void)
7237{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007238 u8 current_level = 7;
7239 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007240 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007241
7242 /* DSDT *always* updates status on resume */
7243 tp_features.fan_ctrl_status_undef = 0;
7244
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007245 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007246 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007247 (fan_get_status_safe(&current_level) < 0))
7248 return;
7249
7250 switch (fan_control_access_mode) {
7251 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007252 /* never decrease fan level */
7253 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007254 break;
7255 case TPACPI_FAN_WR_ACPI_FANS:
7256 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007257 /* never decrease fan level, scale is:
7258 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7259 *
7260 * We expect the firmware to set either 7 or AUTO, but we
7261 * handle FULLSPEED out of paranoia.
7262 *
7263 * So, we can safely only restore FULLSPEED or 7, anything
7264 * else could slow the fan. Restoring AUTO is useless, at
7265 * best that's exactly what the DSDT already set (it is the
7266 * slower it uses).
7267 *
7268 * Always keep in mind that the DSDT *will* have set the
7269 * fans to what the vendor supposes is the best level. We
7270 * muck with it only to speed the fan up.
7271 */
7272 if (fan_control_resume_level != 7 &&
7273 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7274 return;
7275 else
7276 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7277 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007278 break;
7279 default:
7280 return;
7281 }
7282 if (do_set) {
7283 printk(TPACPI_NOTICE
7284 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007285 fan_control_resume_level);
7286 rc = fan_set_level_safe(fan_control_resume_level);
7287 if (rc < 0)
7288 printk(TPACPI_NOTICE
7289 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007290 }
7291}
7292
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007293static int fan_read(char *p)
7294{
7295 int len = 0;
7296 int rc;
7297 u8 status;
7298 unsigned int speed = 0;
7299
7300 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007301 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007302 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007303 rc = fan_get_status_safe(&status);
7304 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007305 return rc;
7306
7307 len += sprintf(p + len, "status:\t\t%s\n"
7308 "level:\t\t%d\n",
7309 (status != 0) ? "enabled" : "disabled", status);
7310 break;
7311
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007312 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007313 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007314 rc = fan_get_status_safe(&status);
7315 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007316 return rc;
7317
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007318 len += sprintf(p + len, "status:\t\t%s\n",
7319 (status != 0) ? "enabled" : "disabled");
7320
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007321 rc = fan_get_speed(&speed);
7322 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007323 return rc;
7324
7325 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7326
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007327 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007328 /* Disengaged mode takes precedence */
7329 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007330 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007331 len += sprintf(p + len, "level:\t\tauto\n");
7332 else
7333 len += sprintf(p + len, "level:\t\t%d\n", status);
7334 break;
7335
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007336 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007337 default:
7338 len += sprintf(p + len, "status:\t\tnot supported\n");
7339 }
7340
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007341 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007342 len += sprintf(p + len, "commands:\tlevel <level>");
7343
7344 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007345 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007346 len += sprintf(p + len, " (<level> is 0-7)\n");
7347 break;
7348
7349 default:
7350 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007351 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007352 break;
7353 }
7354 }
7355
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007356 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007357 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007358 "commands:\twatchdog <timeout> (<timeout> "
7359 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007360
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007361 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007362 len += sprintf(p + len, "commands:\tspeed <speed>"
7363 " (<speed> is 0-65535)\n");
7364
7365 return len;
7366}
7367
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007368static int fan_write_cmd_level(const char *cmd, int *rc)
7369{
7370 int level;
7371
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007372 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007373 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007374 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007375 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007376 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007377 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007378 return 0;
7379
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007380 *rc = fan_set_level_safe(level);
7381 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007382 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007383 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007384 else if (!*rc)
7385 tpacpi_disclose_usertask("procfs fan",
7386 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007387
7388 return 1;
7389}
7390
7391static int fan_write_cmd_enable(const char *cmd, int *rc)
7392{
7393 if (strlencmp(cmd, "enable") != 0)
7394 return 0;
7395
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007396 *rc = fan_set_enable();
7397 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007398 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007399 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007400 else if (!*rc)
7401 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007402
7403 return 1;
7404}
7405
7406static int fan_write_cmd_disable(const char *cmd, int *rc)
7407{
7408 if (strlencmp(cmd, "disable") != 0)
7409 return 0;
7410
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007411 *rc = fan_set_disable();
7412 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007413 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007414 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007415 else if (!*rc)
7416 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007417
7418 return 1;
7419}
7420
7421static int fan_write_cmd_speed(const char *cmd, int *rc)
7422{
7423 int speed;
7424
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007425 /* TODO:
7426 * Support speed <low> <medium> <high> ? */
7427
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007428 if (sscanf(cmd, "speed %d", &speed) != 1)
7429 return 0;
7430
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007431 *rc = fan_set_speed(speed);
7432 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007433 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007434 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007435 else if (!*rc)
7436 tpacpi_disclose_usertask("procfs fan",
7437 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007438
7439 return 1;
7440}
7441
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007442static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7443{
7444 int interval;
7445
7446 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7447 return 0;
7448
7449 if (interval < 0 || interval > 120)
7450 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007451 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007452 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007453 tpacpi_disclose_usertask("procfs fan",
7454 "set watchdog timer to %d\n",
7455 interval);
7456 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007457
7458 return 1;
7459}
7460
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007461static int fan_write(char *buf)
7462{
7463 char *cmd;
7464 int rc = 0;
7465
7466 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007467 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007468 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007469 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007470 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007471 fan_write_cmd_disable(cmd, &rc) ||
7472 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007473 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007474 fan_write_cmd_speed(cmd, &rc))
7475 )
7476 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007477 else if (!rc)
7478 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007479 }
7480
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007481 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007482}
7483
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007484static struct ibm_struct fan_driver_data = {
7485 .name = "fan",
7486 .read = fan_read,
7487 .write = fan_write,
7488 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007489 .suspend = fan_suspend,
7490 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007491};
7492
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007493/****************************************************************************
7494 ****************************************************************************
7495 *
7496 * Infrastructure
7497 *
7498 ****************************************************************************
7499 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007501/* sysfs name ---------------------------------------------------------- */
7502static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7503 struct device_attribute *attr,
7504 char *buf)
7505{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007506 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007507}
7508
7509static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7510 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7511
7512/* --------------------------------------------------------------------- */
7513
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007514/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007515static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007516
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007517/*
7518 * Module and infrastructure proble, init and exit handling
7519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007521static int force_load;
7522
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007523#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007524static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007525{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007526 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007527
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007528 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007529}
7530#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7531
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007532static void ibm_exit(struct ibm_struct *ibm)
7533{
7534 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7535
7536 list_del_init(&ibm->all_drivers);
7537
7538 if (ibm->flags.acpi_notify_installed) {
7539 dbg_printk(TPACPI_DBG_EXIT,
7540 "%s: acpi_remove_notify_handler\n", ibm->name);
7541 BUG_ON(!ibm->acpi);
7542 acpi_remove_notify_handler(*ibm->acpi->handle,
7543 ibm->acpi->type,
7544 dispatch_acpi_notify);
7545 ibm->flags.acpi_notify_installed = 0;
7546 ibm->flags.acpi_notify_installed = 0;
7547 }
7548
7549 if (ibm->flags.proc_created) {
7550 dbg_printk(TPACPI_DBG_EXIT,
7551 "%s: remove_proc_entry\n", ibm->name);
7552 remove_proc_entry(ibm->name, proc_dir);
7553 ibm->flags.proc_created = 0;
7554 }
7555
7556 if (ibm->flags.acpi_driver_registered) {
7557 dbg_printk(TPACPI_DBG_EXIT,
7558 "%s: acpi_bus_unregister_driver\n", ibm->name);
7559 BUG_ON(!ibm->acpi);
7560 acpi_bus_unregister_driver(ibm->acpi->driver);
7561 kfree(ibm->acpi->driver);
7562 ibm->acpi->driver = NULL;
7563 ibm->flags.acpi_driver_registered = 0;
7564 }
7565
7566 if (ibm->flags.init_called && ibm->exit) {
7567 ibm->exit();
7568 ibm->flags.init_called = 0;
7569 }
7570
7571 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7572}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007573
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007574static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575{
7576 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007577 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 struct proc_dir_entry *entry;
7579
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007580 BUG_ON(ibm == NULL);
7581
7582 INIT_LIST_HEAD(&ibm->all_drivers);
7583
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007584 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 return 0;
7586
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007587 dbg_printk(TPACPI_DBG_INIT,
7588 "probing for %s\n", ibm->name);
7589
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007590 if (iibm->init) {
7591 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007592 if (ret > 0)
7593 return 0; /* probe failed */
7594 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007596
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007597 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598 }
7599
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007600 if (ibm->acpi) {
7601 if (ibm->acpi->hid) {
7602 ret = register_tpacpi_subdriver(ibm);
7603 if (ret)
7604 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007605 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007606
7607 if (ibm->acpi->notify) {
7608 ret = setup_acpi_notify(ibm);
7609 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007610 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007611 ibm->name);
7612 ret = 0;
7613 goto err_out;
7614 }
7615 if (ret < 0)
7616 goto err_out;
7617 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007618 }
7619
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007620 dbg_printk(TPACPI_DBG_INIT,
7621 "%s installed\n", ibm->name);
7622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007623 if (ibm->read) {
7624 entry = create_proc_entry(ibm->name,
7625 S_IFREG | S_IRUGO | S_IWUSR,
7626 proc_dir);
7627 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007628 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007629 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007630 ret = -ENODEV;
7631 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007632 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007633 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007634 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007635 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007636 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007637 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007640 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7641
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007643
7644err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007645 dbg_printk(TPACPI_DBG_INIT,
7646 "%s: at error exit path with result %d\n",
7647 ibm->name, ret);
7648
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007649 ibm_exit(ibm);
7650 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651}
7652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007653/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007655static bool __pure __init tpacpi_is_fw_digit(const char c)
7656{
7657 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7658}
7659
7660/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7661static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7662 const char t)
7663{
7664 return s && strlen(s) >= 8 &&
7665 tpacpi_is_fw_digit(s[0]) &&
7666 tpacpi_is_fw_digit(s[1]) &&
7667 s[2] == t && s[3] == 'T' &&
7668 tpacpi_is_fw_digit(s[4]) &&
7669 tpacpi_is_fw_digit(s[5]) &&
7670 s[6] == 'W' && s[7] == 'W';
7671}
7672
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007673/* returns 0 - probe ok, or < 0 - probe error.
7674 * Probe ok doesn't mean thinkpad found.
7675 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7676static int __must_check __init get_thinkpad_model_data(
7677 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007678{
Jeff Garzik18552562007-10-03 15:15:40 -04007679 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007680 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007681 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007682
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007683 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007684 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007685
7686 memset(tp, 0, sizeof(*tp));
7687
7688 if (dmi_name_in_vendors("IBM"))
7689 tp->vendor = PCI_VENDOR_ID_IBM;
7690 else if (dmi_name_in_vendors("LENOVO"))
7691 tp->vendor = PCI_VENDOR_ID_LENOVO;
7692 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007693 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007694
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007695 s = dmi_get_system_info(DMI_BIOS_VERSION);
7696 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7697 if (s && !tp->bios_version_str)
7698 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007699
7700 /* Really ancient ThinkPad 240X will fail this, which is fine */
7701 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007702 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007703
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007704 tp->bios_model = tp->bios_version_str[0]
7705 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007706 tp->bios_release = (tp->bios_version_str[4] << 8)
7707 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007708
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007709 /*
7710 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7711 * X32 or newer, all Z series; Some models must have an
7712 * up-to-date BIOS or they will not be detected.
7713 *
7714 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7715 */
7716 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007717 if (sscanf(dev->name,
7718 "IBM ThinkPad Embedded Controller -[%17c",
7719 ec_fw_string) == 1) {
7720 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7721 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007722
7723 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007724 if (!tp->ec_version_str)
7725 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007726
7727 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7728 tp->ec_model = ec_fw_string[0]
7729 | (ec_fw_string[1] << 8);
7730 tp->ec_release = (ec_fw_string[4] << 8)
7731 | ec_fw_string[5];
7732 } else {
7733 printk(TPACPI_NOTICE
7734 "ThinkPad firmware release %s "
7735 "doesn't match the known patterns\n",
7736 ec_fw_string);
7737 printk(TPACPI_NOTICE
7738 "please report this to %s\n",
7739 TPACPI_MAIL);
7740 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007741 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007742 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007743 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007744
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007745 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7746 if (s && !strnicmp(s, "ThinkPad", 8)) {
7747 tp->model_str = kstrdup(s, GFP_KERNEL);
7748 if (!tp->model_str)
7749 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007750 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007751
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007752 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7753 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7754 if (s && !tp->nummodel_str)
7755 return -ENOMEM;
7756
7757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758}
7759
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007760static int __init probe_for_thinkpad(void)
7761{
7762 int is_thinkpad;
7763
7764 if (acpi_disabled)
7765 return -ENODEV;
7766
7767 /*
7768 * Non-ancient models have better DMI tagging, but very old models
7769 * don't.
7770 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007771 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007772
7773 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007774 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007775 if (!ec_handle) {
7776 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007777 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007778 "Not yet supported ThinkPad detected!\n");
7779 return -ENODEV;
7780 }
7781
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007782 /*
7783 * Risks a regression on very old machines, but reduces potential
7784 * false positives a damn great deal
7785 */
7786 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007787 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007788
7789 if (!is_thinkpad && !force_load)
7790 return -ENODEV;
7791
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007792 return 0;
7793}
7794
7795
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007796/* Module init, exit, parameters */
7797
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007798static struct ibm_init_struct ibms_init[] __initdata = {
7799 {
7800 .init = thinkpad_acpi_driver_init,
7801 .data = &thinkpad_acpi_driver_data,
7802 },
7803 {
7804 .init = hotkey_init,
7805 .data = &hotkey_driver_data,
7806 },
7807 {
7808 .init = bluetooth_init,
7809 .data = &bluetooth_driver_data,
7810 },
7811 {
7812 .init = wan_init,
7813 .data = &wan_driver_data,
7814 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007815 {
7816 .init = uwb_init,
7817 .data = &uwb_driver_data,
7818 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007819#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007820 {
7821 .init = video_init,
7822 .data = &video_driver_data,
7823 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007824#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007825 {
7826 .init = light_init,
7827 .data = &light_driver_data,
7828 },
7829#ifdef CONFIG_THINKPAD_ACPI_DOCK
7830 {
7831 .init = dock_init,
7832 .data = &dock_driver_data[0],
7833 },
7834 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007835 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007836 .data = &dock_driver_data[1],
7837 },
7838#endif
7839#ifdef CONFIG_THINKPAD_ACPI_BAY
7840 {
7841 .init = bay_init,
7842 .data = &bay_driver_data,
7843 },
7844#endif
7845 {
7846 .init = cmos_init,
7847 .data = &cmos_driver_data,
7848 },
7849 {
7850 .init = led_init,
7851 .data = &led_driver_data,
7852 },
7853 {
7854 .init = beep_init,
7855 .data = &beep_driver_data,
7856 },
7857 {
7858 .init = thermal_init,
7859 .data = &thermal_driver_data,
7860 },
7861 {
7862 .data = &ecdump_driver_data,
7863 },
7864 {
7865 .init = brightness_init,
7866 .data = &brightness_driver_data,
7867 },
7868 {
7869 .data = &volume_driver_data,
7870 },
7871 {
7872 .init = fan_init,
7873 .data = &fan_driver_data,
7874 },
7875};
7876
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007877static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7878{
7879 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007880 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007881
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007882 if (!kp || !kp->name || !val)
7883 return -EINVAL;
7884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007885 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7886 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007887 WARN_ON(ibm == NULL);
7888
7889 if (!ibm || !ibm->name)
7890 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007891
7892 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7893 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007894 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007895 strcpy(ibms_init[i].param, val);
7896 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007897 return 0;
7898 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007899 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007900
7901 return -EINVAL;
7902}
7903
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007904module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007905MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007906 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007907
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007908module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007909MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007910
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007911module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007912MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007913 "Attempts to load the driver even on a "
7914 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007915
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007916module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007917MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007918 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007919
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007920module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007921MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007922 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007923 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007924
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007925module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007926MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007927 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007928
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007929module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007930MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007931 "used for backwards compatibility with userspace, "
7932 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007934#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007935 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007936 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007937 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007938
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007939TPACPI_PARAM(hotkey);
7940TPACPI_PARAM(bluetooth);
7941TPACPI_PARAM(video);
7942TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007943#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007944TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007945#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007946#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007947TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007948#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007949TPACPI_PARAM(cmos);
7950TPACPI_PARAM(led);
7951TPACPI_PARAM(beep);
7952TPACPI_PARAM(ecdump);
7953TPACPI_PARAM(brightness);
7954TPACPI_PARAM(volume);
7955TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007956
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007957#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7958module_param(dbg_wlswemul, uint, 0);
7959MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7960module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7961MODULE_PARM_DESC(wlsw_state,
7962 "Initial state of the emulated WLSW switch");
7963
7964module_param(dbg_bluetoothemul, uint, 0);
7965MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7966module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7967MODULE_PARM_DESC(bluetooth_state,
7968 "Initial state of the emulated bluetooth switch");
7969
7970module_param(dbg_wwanemul, uint, 0);
7971MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7972module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7973MODULE_PARM_DESC(wwan_state,
7974 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007975
7976module_param(dbg_uwbemul, uint, 0);
7977MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7978module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7979MODULE_PARM_DESC(uwb_state,
7980 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007981#endif
7982
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007983static void thinkpad_acpi_module_exit(void)
7984{
7985 struct ibm_struct *ibm, *itmp;
7986
7987 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7988
7989 list_for_each_entry_safe_reverse(ibm, itmp,
7990 &tpacpi_all_drivers,
7991 all_drivers) {
7992 ibm_exit(ibm);
7993 }
7994
7995 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7996
7997 if (tpacpi_inputdev) {
7998 if (tp_features.input_device_registered)
7999 input_unregister_device(tpacpi_inputdev);
8000 else
8001 input_free_device(tpacpi_inputdev);
8002 }
8003
8004 if (tpacpi_hwmon)
8005 hwmon_device_unregister(tpacpi_hwmon);
8006
8007 if (tp_features.sensors_pdev_attrs_registered)
8008 device_remove_file(&tpacpi_sensors_pdev->dev,
8009 &dev_attr_thinkpad_acpi_pdev_name);
8010 if (tpacpi_sensors_pdev)
8011 platform_device_unregister(tpacpi_sensors_pdev);
8012 if (tpacpi_pdev)
8013 platform_device_unregister(tpacpi_pdev);
8014
8015 if (tp_features.sensors_pdrv_attrs_registered)
8016 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8017 if (tp_features.platform_drv_attrs_registered)
8018 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8019
8020 if (tp_features.sensors_pdrv_registered)
8021 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8022
8023 if (tp_features.platform_drv_registered)
8024 platform_driver_unregister(&tpacpi_pdriver);
8025
8026 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008027 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008028
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008029 if (tpacpi_wq)
8030 destroy_workqueue(tpacpi_wq);
8031
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008032 kfree(thinkpad_id.bios_version_str);
8033 kfree(thinkpad_id.ec_version_str);
8034 kfree(thinkpad_id.model_str);
8035}
8036
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008037
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008038static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039{
8040 int ret, i;
8041
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008042 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8043
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008044 /* Parameter checking */
8045 if (hotkey_report_mode > 2)
8046 return -EINVAL;
8047
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008048 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008049
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008050 ret = get_thinkpad_model_data(&thinkpad_id);
8051 if (ret) {
8052 printk(TPACPI_ERR
8053 "unable to get DMI data: %d\n", ret);
8054 thinkpad_acpi_module_exit();
8055 return ret;
8056 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008057 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008058 if (ret) {
8059 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008060 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008063 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008064
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008065 TPACPI_ACPIHANDLE_INIT(ecrd);
8066 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008068 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8069 if (!tpacpi_wq) {
8070 thinkpad_acpi_module_exit();
8071 return -ENOMEM;
8072 }
8073
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008074 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008076 printk(TPACPI_ERR
8077 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008078 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 return -ENODEV;
8080 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008081
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008082 ret = platform_driver_register(&tpacpi_pdriver);
8083 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008084 printk(TPACPI_ERR
8085 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008086 thinkpad_acpi_module_exit();
8087 return ret;
8088 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008089 tp_features.platform_drv_registered = 1;
8090
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008091 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8092 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008093 printk(TPACPI_ERR
8094 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008095 thinkpad_acpi_module_exit();
8096 return ret;
8097 }
8098 tp_features.sensors_pdrv_registered = 1;
8099
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008100 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008101 if (!ret) {
8102 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008103 ret = tpacpi_create_driver_attributes(
8104 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008105 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008106 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008107 printk(TPACPI_ERR
8108 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008109 thinkpad_acpi_module_exit();
8110 return ret;
8111 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03008112 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008113
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008114
8115 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008116 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008117 NULL, 0);
8118 if (IS_ERR(tpacpi_pdev)) {
8119 ret = PTR_ERR(tpacpi_pdev);
8120 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008121 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008122 thinkpad_acpi_module_exit();
8123 return ret;
8124 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008125 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008126 TPACPI_HWMON_DRVR_NAME,
8127 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008128 if (IS_ERR(tpacpi_sensors_pdev)) {
8129 ret = PTR_ERR(tpacpi_sensors_pdev);
8130 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008131 printk(TPACPI_ERR
8132 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008133 thinkpad_acpi_module_exit();
8134 return ret;
8135 }
8136 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8137 &dev_attr_thinkpad_acpi_pdev_name);
8138 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008139 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008140 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008141 thinkpad_acpi_module_exit();
8142 return ret;
8143 }
8144 tp_features.sensors_pdev_attrs_registered = 1;
8145 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008146 if (IS_ERR(tpacpi_hwmon)) {
8147 ret = PTR_ERR(tpacpi_hwmon);
8148 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008149 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008150 thinkpad_acpi_module_exit();
8151 return ret;
8152 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03008153 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008154 tpacpi_inputdev = input_allocate_device();
8155 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008156 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008157 thinkpad_acpi_module_exit();
8158 return -ENOMEM;
8159 } else {
8160 /* Prepare input device, but don't register */
8161 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008162 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008163 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03008164 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8165 thinkpad_id.vendor :
8166 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008167 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8168 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8169 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008170 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8171 ret = ibm_init(&ibms_init[i]);
8172 if (ret >= 0 && *ibms_init[i].param)
8173 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008175 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 return ret;
8177 }
8178 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008179 ret = input_register_device(tpacpi_inputdev);
8180 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008181 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03008182 thinkpad_acpi_module_exit();
8183 return ret;
8184 } else {
8185 tp_features.input_device_registered = 1;
8186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008188 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 return 0;
8190}
8191
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03008192MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8193
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008194/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008195 * This will autoload the driver in almost every ThinkPad
8196 * in widespread use.
8197 *
8198 * Only _VERY_ old models, like the 240, 240x and 570 lack
8199 * the HKEY event interface.
8200 */
8201MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8202
8203/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008204 * DMI matching for module autoloading
8205 *
8206 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8207 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8208 *
8209 * Only models listed in thinkwiki will be supported, so add yours
8210 * if it is not there yet.
8211 */
8212#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01008213 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008214
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008215/* Ancient thinkpad BIOSes have to be identified by
8216 * BIOS type or model number, and there are far less
8217 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008218IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008219
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008220MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8221MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008222MODULE_DESCRIPTION(TPACPI_DESC);
8223MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008224MODULE_LICENSE("GPL");
8225
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008226module_init(thinkpad_acpi_module_init);
8227module_exit(thinkpad_acpi_module_exit);