blob: a186c5bbdcd9cee00f46af49846a8cac6c393c60 [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 Holschuhaa2fbce2009-01-11 03:01:10 -020024#define TPACPI_VERSION "0.22"
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +000025#define TPACPI_SYSFS_VERSION 0x020300
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300169/* rfkill switches */
170enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200173 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300174};
175
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000176/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200177#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000178#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181#define TPACPI_ERR KERN_ERR TPACPI_LOG
182#define TPACPI_WARN KERN_WARNING TPACPI_LOG
183#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184#define TPACPI_INFO KERN_INFO TPACPI_LOG
185#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200186
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000187/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200188#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000189#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200190#define TPACPI_DBG_INIT 0x0001
191#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000192#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000193#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000194#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000195#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200196
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200197#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200201
202/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200203 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200204 */
205
206struct ibm_struct;
207
208struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
211
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
216};
217
218struct ibm_struct {
219 char *name;
220
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200225 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200226 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200227
228 struct list_head all_drivers;
229
230 struct tp_acpi_drv_struct *acpi;
231
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
239};
240
241struct ibm_init_struct {
242 char param[32];
243
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
246};
247
248static struct {
249#ifdef CONFIG_THINKPAD_ACPI_BAY
250 u32 bay_status:1;
251 u32 bay_eject:1;
252 u32 bay_status2:1;
253 u32 bay_eject2:1;
254#endif
255 u32 bluetooth:1;
256 u32 hotkey:1;
257 u32 hotkey_mask:1;
258 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200259 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 u32 light:1;
261 u32 light_status:1;
262 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300263 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200265 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
274} tp_features;
275
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300276static struct {
277 u16 hotkey_mask_ff:1;
278} tp_warned;
279
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280struct thinkpad_id_data {
281 unsigned int vendor; /* ThinkPad vendor:
282 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283
284 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
285 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286
287 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
288 u16 ec_model;
289
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300290 char *model_str; /* ThinkPad T43 */
291 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200292};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293static struct thinkpad_id_data thinkpad_id;
294
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300295static enum {
296 TPACPI_LIFE_INIT = 0,
297 TPACPI_LIFE_RUNNING,
298 TPACPI_LIFE_EXITING,
299} tpacpi_lifecycle;
300
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200301static int experimental;
302static u32 dbg_level;
303
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300304static struct workqueue_struct *tpacpi_wq;
305
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300306/* Special LED class that can defer work */
307struct tpacpi_led_classdev {
308 struct led_classdev led_classdev;
309 struct work_struct work;
310 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300311 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300312};
313
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200314#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
315static int dbg_wlswemul;
316static int tpacpi_wlsw_emulstate;
317static int dbg_bluetoothemul;
318static int tpacpi_bluetooth_emulstate;
319static int dbg_wwanemul;
320static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200321static int dbg_uwbemul;
322static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200323#endif
324
325
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000326/*************************************************************************
327 * Debugging helpers
328 */
329
330#define dbg_printk(a_dbg_level, format, arg...) \
331 do { if (dbg_level & (a_dbg_level)) \
332 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
333 } while (0)
334
335#ifdef CONFIG_THINKPAD_ACPI_DEBUG
336#define vdbg_printk dbg_printk
337static const char *str_supported(int is_supported);
338#else
339#define vdbg_printk(a_dbg_level, format, arg...) \
340 do { } while (0)
341#endif
342
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000343static void tpacpi_log_usertask(const char * const what)
344{
345 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
346 what, task_tgid_vnr(current));
347}
348
349#define tpacpi_disclose_usertask(what, format, arg...) \
350 do { \
351 if (unlikely( \
352 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
353 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
354 printk(TPACPI_DEBUG "%s: PID %d: " format, \
355 what, task_tgid_vnr(current), ## arg); \
356 } \
357 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000358
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300359/****************************************************************************
360 ****************************************************************************
361 *
362 * ACPI Helpers and device model
363 *
364 ****************************************************************************
365 ****************************************************************************/
366
367/*************************************************************************
368 * ACPI basic handles
369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300371static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200373#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 static acpi_handle object##_handle; \
375 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400376 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 static char *object##_paths[] = { paths }
378
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200379TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400380 "\\_SB.PCI.ISA.EC", /* 570 */
381 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
382 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
383 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
384 "\\_SB.PCI0.ICH3.EC0", /* R31 */
385 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300386 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200388TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
389TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200391TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
392 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400393 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
394 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300395 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200397TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400398 "^HKEY", /* R30, R31 */
399 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300400 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400401
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200402TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
403 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
404 "\\_SB.PCI0.VID0", /* 770e */
405 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
406 "\\_SB.PCI0.AGP.VID", /* all others */
407 ); /* R30, R31 */
408
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400409
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300410/*************************************************************************
411 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200412 */
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static int acpi_evalf(acpi_handle handle,
415 void *res, char *method, char *fmt, ...)
416{
417 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400418 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200419 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400420 struct acpi_buffer result, *resultp;
421 union acpi_object out_obj;
422 acpi_status status;
423 va_list ap;
424 char res_type;
425 int success;
426 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200429 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431 }
432
433 if (*fmt == 'q') {
434 quiet = 1;
435 fmt++;
436 } else
437 quiet = 0;
438
439 res_type = *(fmt++);
440
441 params.count = 0;
442 params.pointer = &in_objs[0];
443
444 va_start(ap, fmt);
445 while (*fmt) {
446 char c = *(fmt++);
447 switch (c) {
448 case 'd': /* int */
449 in_objs[params.count].integer.value = va_arg(ap, int);
450 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
451 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400452 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200454 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 "with invalid format character '%c'\n", c);
456 return 0;
457 }
458 }
459 va_end(ap);
460
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400461 if (res_type != 'v') {
462 result.length = sizeof(out_obj);
463 result.pointer = &out_obj;
464 resultp = &result;
465 } else
466 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400468 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400471 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (res)
473 *(int *)res = out_obj.integer.value;
474 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
475 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400476 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 success = status == AE_OK;
478 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400479 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200481 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 "with invalid format character '%c'\n", res_type);
483 return 0;
484 }
485
486 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200487 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 method, fmt0, status);
489
490 return success;
491}
492
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200493static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300494{
495 int v;
496
497 if (ecrd_handle) {
498 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
499 return 0;
500 *p = v;
501 } else {
502 if (ec_read(i, p) < 0)
503 return 0;
504 }
505
506 return 1;
507}
508
509static int acpi_ec_write(int i, u8 v)
510{
511 if (ecwr_handle) {
512 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
513 return 0;
514 } else {
515 if (ec_write(i, v) < 0)
516 return 0;
517 }
518
519 return 1;
520}
521
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200522#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523static int _sta(acpi_handle handle)
524{
525 int status;
526
527 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
528 status = 0;
529
530 return status;
531}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200532#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300534static int issue_thinkpad_cmos_command(int cmos_cmd)
535{
536 if (!cmos_handle)
537 return -ENXIO;
538
539 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
540 return -EIO;
541
542 return 0;
543}
544
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300545/*************************************************************************
546 * ACPI device model
547 */
548
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200549#define TPACPI_ACPIHANDLE_INIT(object) \
550 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200551 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
552
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300553static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300554 acpi_handle *handle, acpi_handle parent,
555 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556{
557 int i;
558 acpi_status status;
559
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300560 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
561 name);
562
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563 for (i = 0; i < num_paths; i++) {
564 status = acpi_get_handle(parent, paths[i], handle);
565 if (ACPI_SUCCESS(status)) {
566 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300567 dbg_printk(TPACPI_DBG_INIT,
568 "Found ACPI handle %s for %s\n",
569 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570 return;
571 }
572 }
573
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300574 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
575 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576 *handle = NULL;
577}
578
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300579static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300580{
581 struct ibm_struct *ibm = data;
582
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300583 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
584 return;
585
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300586 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587 return;
588
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300589 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300590}
591
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300592static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593{
594 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300595 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300596
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300597 BUG_ON(!ibm->acpi);
598
599 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300600 return 0;
601
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300602 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300603 "setting up ACPI notify for %s\n", ibm->name);
604
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300605 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
606 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200607 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300608 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300609 return -ENODEV;
610 }
611
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700612 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300613 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200614 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300615 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300616
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300617 status = acpi_install_notify_handler(*ibm->acpi->handle,
618 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300619 if (ACPI_FAILURE(status)) {
620 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200621 printk(TPACPI_NOTICE
622 "another device driver is already "
623 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300624 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200625 printk(TPACPI_ERR
626 "acpi_install_notify_handler(%s) failed: %d\n",
627 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628 }
629 return -ENODEV;
630 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632 return 0;
633}
634
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300635static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300636{
637 return 0;
638}
639
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300640static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300641{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300642 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300643
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300644 dbg_printk(TPACPI_DBG_INIT,
645 "registering %s as an ACPI driver\n", ibm->name);
646
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300647 BUG_ON(!ibm->acpi);
648
649 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
650 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300651 printk(TPACPI_ERR
652 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300653 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300654 }
655
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200656 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300657 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200658
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300659 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300660
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300661 rc = acpi_bus_register_driver(ibm->acpi->driver);
662 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200663 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200664 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 kfree(ibm->acpi->driver);
666 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300667 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300668 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300669
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300670 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300671}
672
673
674/****************************************************************************
675 ****************************************************************************
676 *
677 * Procfs Helpers
678 *
679 ****************************************************************************
680 ****************************************************************************/
681
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300682static int dispatch_procfs_read(char *page, char **start, off_t off,
683 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684{
685 struct ibm_struct *ibm = data;
686 int len;
687
688 if (!ibm || !ibm->read)
689 return -EINVAL;
690
691 len = ibm->read(page);
692 if (len < 0)
693 return len;
694
695 if (len <= off + count)
696 *eof = 1;
697 *start = page + off;
698 len -= off;
699 if (len > count)
700 len = count;
701 if (len < 0)
702 len = 0;
703
704 return len;
705}
706
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300707static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200708 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300709 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300710{
711 struct ibm_struct *ibm = data;
712 char *kernbuf;
713 int ret;
714
715 if (!ibm || !ibm->write)
716 return -EINVAL;
717
718 kernbuf = kmalloc(count + 2, GFP_KERNEL);
719 if (!kernbuf)
720 return -ENOMEM;
721
722 if (copy_from_user(kernbuf, userbuf, count)) {
723 kfree(kernbuf);
724 return -EFAULT;
725 }
726
727 kernbuf[count] = 0;
728 strcat(kernbuf, ",");
729 ret = ibm->write(kernbuf);
730 if (ret == 0)
731 ret = count;
732
733 kfree(kernbuf);
734
735 return ret;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static char *next_cmd(char **cmds)
739{
740 char *start = *cmds;
741 char *end;
742
743 while ((end = strchr(start, ',')) && end == start)
744 start = end + 1;
745
746 if (!end)
747 return NULL;
748
749 *end = 0;
750 *cmds = end + 1;
751 return start;
752}
753
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754
755/****************************************************************************
756 ****************************************************************************
757 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300758 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300759 *
760 ****************************************************************************
761 ****************************************************************************/
762
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300763static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300764static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700765static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300766static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300767static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200768static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300769
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200770static int tpacpi_suspend_handler(struct platform_device *pdev,
771 pm_message_t state)
772{
773 struct ibm_struct *ibm, *itmp;
774
775 list_for_each_entry_safe(ibm, itmp,
776 &tpacpi_all_drivers,
777 all_drivers) {
778 if (ibm->suspend)
779 (ibm->suspend)(state);
780 }
781
782 return 0;
783}
784
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300785static int tpacpi_resume_handler(struct platform_device *pdev)
786{
787 struct ibm_struct *ibm, *itmp;
788
789 list_for_each_entry_safe(ibm, itmp,
790 &tpacpi_all_drivers,
791 all_drivers) {
792 if (ibm->resume)
793 (ibm->resume)();
794 }
795
796 return 0;
797}
798
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200799static void tpacpi_shutdown_handler(struct platform_device *pdev)
800{
801 struct ibm_struct *ibm, *itmp;
802
803 list_for_each_entry_safe(ibm, itmp,
804 &tpacpi_all_drivers,
805 all_drivers) {
806 if (ibm->shutdown)
807 (ibm->shutdown)();
808 }
809}
810
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300811static struct platform_driver tpacpi_pdriver = {
812 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200813 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300814 .owner = THIS_MODULE,
815 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200816 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300817 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200818 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300819};
820
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300821static struct platform_driver tpacpi_hwmon_pdriver = {
822 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200823 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300824 .owner = THIS_MODULE,
825 },
826};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300827
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300828/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300829 * sysfs support helpers
830 */
831
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200832struct attribute_set {
833 unsigned int members, max_members;
834 struct attribute_group group;
835};
836
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300837struct attribute_set_obj {
838 struct attribute_set s;
839 struct attribute *a;
840} __attribute__((packed));
841
842static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200843 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300844{
845 struct attribute_set_obj *sobj;
846
847 if (max_members == 0)
848 return NULL;
849
850 /* Allocates space for implicit NULL at the end too */
851 sobj = kzalloc(sizeof(struct attribute_set_obj) +
852 max_members * sizeof(struct attribute *),
853 GFP_KERNEL);
854 if (!sobj)
855 return NULL;
856 sobj->s.max_members = max_members;
857 sobj->s.group.attrs = &sobj->a;
858 sobj->s.group.name = name;
859
860 return &sobj->s;
861}
862
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200863#define destroy_attr_set(_set) \
864 kfree(_set);
865
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300866/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200867static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300868{
869 if (!s || !attr)
870 return -EINVAL;
871
872 if (s->members >= s->max_members)
873 return -ENOMEM;
874
875 s->group.attrs[s->members] = attr;
876 s->members++;
877
878 return 0;
879}
880
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200881static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300882 struct attribute **attr,
883 unsigned int count)
884{
885 int i, res;
886
887 for (i = 0; i < count; i++) {
888 res = add_to_attr_set(s, attr[i]);
889 if (res)
890 return res;
891 }
892
893 return 0;
894}
895
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200896static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300897{
898 sysfs_remove_group(kobj, &s->group);
899 destroy_attr_set(s);
900}
901
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200902#define register_attr_set_with_sysfs(_attr_set, _kobj) \
903 sysfs_create_group(_kobj, &_attr_set->group)
904
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300905static int parse_strtoul(const char *buf,
906 unsigned long max, unsigned long *value)
907{
908 char *endp;
909
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300910 while (*buf && isspace(*buf))
911 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300912 *value = simple_strtoul(buf, &endp, 0);
913 while (*endp && isspace(*endp))
914 endp++;
915 if (*endp || *value > max)
916 return -EINVAL;
917
918 return 0;
919}
920
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300921static void tpacpi_disable_brightness_delay(void)
922{
923 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
924 printk(TPACPI_NOTICE
925 "ACPI backlight control delay disabled\n");
926}
927
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300928static int __init tpacpi_query_bcl_levels(acpi_handle handle)
929{
930 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
931 union acpi_object *obj;
932 int rc;
933
934 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
935 obj = (union acpi_object *)buffer.pointer;
936 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
937 printk(TPACPI_ERR "Unknown _BCL data, "
938 "please report this to %s\n", TPACPI_MAIL);
939 rc = 0;
940 } else {
941 rc = obj->package.count;
942 }
943 } else {
944 return 0;
945 }
946
947 kfree(buffer.pointer);
948 return rc;
949}
950
951static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
952 u32 lvl, void *context, void **rv)
953{
954 char name[ACPI_PATH_SEGMENT_LENGTH];
955 struct acpi_buffer buffer = { sizeof(name), &name };
956
957 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
958 !strncmp("_BCL", name, sizeof(name) - 1)) {
959 BUG_ON(!rv || !*rv);
960 **(int **)rv = tpacpi_query_bcl_levels(handle);
961 return AE_CTRL_TERMINATE;
962 } else {
963 return AE_OK;
964 }
965}
966
967/*
968 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
969 */
970static int __init tpacpi_check_std_acpi_brightness_support(void)
971{
972 int status;
973 int bcl_levels = 0;
974 void *bcl_ptr = &bcl_levels;
975
976 if (!vid_handle) {
977 TPACPI_ACPIHANDLE_INIT(vid);
978 }
979 if (!vid_handle)
980 return 0;
981
982 /*
983 * Search for a _BCL method, and execute it. This is safe on all
984 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
985 * BIOS in ACPI backlight control mode. We do NOT have to care
986 * about calling the _BCL method in an enabled video device, any
987 * will do for our purposes.
988 */
989
990 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
991 tpacpi_acpi_walk_find_bcl, NULL,
992 &bcl_ptr);
993
994 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
995 tp_features.bright_acpimode = 1;
996 return (bcl_levels - 2);
997 }
998
999 return 0;
1000}
1001
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001002static int __init tpacpi_new_rfkill(const unsigned int id,
1003 struct rfkill **rfk,
1004 const enum rfkill_type rfktype,
1005 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001006 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001007 int (*toggle_radio)(void *, enum rfkill_state),
1008 int (*get_state)(void *, enum rfkill_state *))
1009{
1010 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001011 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1012
1013 res = get_state(NULL, &initial_state);
1014 if (res < 0) {
1015 printk(TPACPI_ERR
1016 "failed to read initial state for %s, error %d; "
1017 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001018 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001019 /* try to set the initial state as the default for the rfkill
1020 * type, since we ask the firmware to preserve it across S5 in
1021 * NVRAM */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001022 if (rfkill_set_default(rfktype,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001023 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1024 RFKILL_STATE_UNBLOCKED :
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001025 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1026 vdbg_printk(TPACPI_DBG_RFKILL,
1027 "Default state for %s cannot be changed\n",
1028 name);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001029 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001030
1031 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1032 if (!*rfk) {
1033 printk(TPACPI_ERR
1034 "failed to allocate memory for rfkill class\n");
1035 return -ENOMEM;
1036 }
1037
1038 (*rfk)->name = name;
1039 (*rfk)->get_state = get_state;
1040 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001041 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001042
1043 res = rfkill_register(*rfk);
1044 if (res < 0) {
1045 printk(TPACPI_ERR
1046 "failed to register %s rfkill switch: %d\n",
1047 name, res);
1048 rfkill_free(*rfk);
1049 *rfk = NULL;
1050 return res;
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001056static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1058{
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1063}
1064
1065static void printk_deprecated_rfkill_attribute(const char * const what)
1066{
1067 printk_deprecated_attribute(what,
1068 "Please switch to generic rfkill before year 2010");
1069}
1070
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001071/*************************************************************************
1072 * thinkpad-acpi driver attributes
1073 */
1074
1075/* interface_version --------------------------------------------------- */
1076static ssize_t tpacpi_driver_interface_version_show(
1077 struct device_driver *drv,
1078 char *buf)
1079{
1080 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1081}
1082
1083static DRIVER_ATTR(interface_version, S_IRUGO,
1084 tpacpi_driver_interface_version_show, NULL);
1085
1086/* debug_level --------------------------------------------------------- */
1087static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1088 char *buf)
1089{
1090 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1091}
1092
1093static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1094 const char *buf, size_t count)
1095{
1096 unsigned long t;
1097
1098 if (parse_strtoul(buf, 0xffff, &t))
1099 return -EINVAL;
1100
1101 dbg_level = t;
1102
1103 return count;
1104}
1105
1106static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1107 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1108
1109/* version ------------------------------------------------------------- */
1110static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1111 char *buf)
1112{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001113 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1114 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001115}
1116
1117static DRIVER_ATTR(version, S_IRUGO,
1118 tpacpi_driver_version_show, NULL);
1119
1120/* --------------------------------------------------------------------- */
1121
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001122#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1123
1124static void tpacpi_send_radiosw_update(void);
1125
1126/* wlsw_emulstate ------------------------------------------------------ */
1127static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1128 char *buf)
1129{
1130 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1131}
1132
1133static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1134 const char *buf, size_t count)
1135{
1136 unsigned long t;
1137
1138 if (parse_strtoul(buf, 1, &t))
1139 return -EINVAL;
1140
1141 if (tpacpi_wlsw_emulstate != t) {
1142 tpacpi_wlsw_emulstate = !!t;
1143 tpacpi_send_radiosw_update();
1144 } else
1145 tpacpi_wlsw_emulstate = !!t;
1146
1147 return count;
1148}
1149
1150static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1151 tpacpi_driver_wlsw_emulstate_show,
1152 tpacpi_driver_wlsw_emulstate_store);
1153
1154/* bluetooth_emulstate ------------------------------------------------- */
1155static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1156 struct device_driver *drv,
1157 char *buf)
1158{
1159 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1160}
1161
1162static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1163 struct device_driver *drv,
1164 const char *buf, size_t count)
1165{
1166 unsigned long t;
1167
1168 if (parse_strtoul(buf, 1, &t))
1169 return -EINVAL;
1170
1171 tpacpi_bluetooth_emulstate = !!t;
1172
1173 return count;
1174}
1175
1176static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1177 tpacpi_driver_bluetooth_emulstate_show,
1178 tpacpi_driver_bluetooth_emulstate_store);
1179
1180/* wwan_emulstate ------------------------------------------------- */
1181static ssize_t tpacpi_driver_wwan_emulstate_show(
1182 struct device_driver *drv,
1183 char *buf)
1184{
1185 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1186}
1187
1188static ssize_t tpacpi_driver_wwan_emulstate_store(
1189 struct device_driver *drv,
1190 const char *buf, size_t count)
1191{
1192 unsigned long t;
1193
1194 if (parse_strtoul(buf, 1, &t))
1195 return -EINVAL;
1196
1197 tpacpi_wwan_emulstate = !!t;
1198
1199 return count;
1200}
1201
1202static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1203 tpacpi_driver_wwan_emulstate_show,
1204 tpacpi_driver_wwan_emulstate_store);
1205
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001206/* uwb_emulstate ------------------------------------------------- */
1207static ssize_t tpacpi_driver_uwb_emulstate_show(
1208 struct device_driver *drv,
1209 char *buf)
1210{
1211 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1212}
1213
1214static ssize_t tpacpi_driver_uwb_emulstate_store(
1215 struct device_driver *drv,
1216 const char *buf, size_t count)
1217{
1218 unsigned long t;
1219
1220 if (parse_strtoul(buf, 1, &t))
1221 return -EINVAL;
1222
1223 tpacpi_uwb_emulstate = !!t;
1224
1225 return count;
1226}
1227
1228static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1229 tpacpi_driver_uwb_emulstate_show,
1230 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001231#endif
1232
1233/* --------------------------------------------------------------------- */
1234
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001235static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001236 &driver_attr_debug_level, &driver_attr_version,
1237 &driver_attr_interface_version,
1238};
1239
1240static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1241{
1242 int i, res;
1243
1244 i = 0;
1245 res = 0;
1246 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1247 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1248 i++;
1249 }
1250
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001251#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1252 if (!res && dbg_wlswemul)
1253 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1254 if (!res && dbg_bluetoothemul)
1255 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1256 if (!res && dbg_wwanemul)
1257 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001258 if (!res && dbg_uwbemul)
1259 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001260#endif
1261
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001262 return res;
1263}
1264
1265static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1266{
1267 int i;
1268
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001269 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001270 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001271
1272#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1273 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1274 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1275 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001276 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001277#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001278}
1279
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001280/****************************************************************************
1281 ****************************************************************************
1282 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001283 * Subdrivers
1284 *
1285 ****************************************************************************
1286 ****************************************************************************/
1287
1288/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001289 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001290 */
1291
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001292static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001294 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1295 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001297 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001298 (thinkpad_id.bios_version_str) ?
1299 thinkpad_id.bios_version_str : "unknown",
1300 (thinkpad_id.ec_version_str) ?
1301 thinkpad_id.ec_version_str : "unknown");
1302
1303 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001304 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001305 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1306 "IBM" : ((thinkpad_id.vendor ==
1307 PCI_VENDOR_ID_LENOVO) ?
1308 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001309 thinkpad_id.model_str,
1310 (thinkpad_id.nummodel_str) ?
1311 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
1314}
1315
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001316static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 int len = 0;
1319
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001320 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1321 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 return len;
1324}
1325
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001326static struct ibm_struct thinkpad_acpi_driver_data = {
1327 .name = "driver",
1328 .read = thinkpad_acpi_driver_read,
1329};
1330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001331/*************************************************************************
1332 * Hotkey subdriver
1333 */
1334
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001335enum { /* hot key scan codes (derived from ACPI DSDT) */
1336 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1337 TP_ACPI_HOTKEYSCAN_FNF2,
1338 TP_ACPI_HOTKEYSCAN_FNF3,
1339 TP_ACPI_HOTKEYSCAN_FNF4,
1340 TP_ACPI_HOTKEYSCAN_FNF5,
1341 TP_ACPI_HOTKEYSCAN_FNF6,
1342 TP_ACPI_HOTKEYSCAN_FNF7,
1343 TP_ACPI_HOTKEYSCAN_FNF8,
1344 TP_ACPI_HOTKEYSCAN_FNF9,
1345 TP_ACPI_HOTKEYSCAN_FNF10,
1346 TP_ACPI_HOTKEYSCAN_FNF11,
1347 TP_ACPI_HOTKEYSCAN_FNF12,
1348 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1349 TP_ACPI_HOTKEYSCAN_FNINSERT,
1350 TP_ACPI_HOTKEYSCAN_FNDELETE,
1351 TP_ACPI_HOTKEYSCAN_FNHOME,
1352 TP_ACPI_HOTKEYSCAN_FNEND,
1353 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1354 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1355 TP_ACPI_HOTKEYSCAN_FNSPACE,
1356 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1357 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1358 TP_ACPI_HOTKEYSCAN_MUTE,
1359 TP_ACPI_HOTKEYSCAN_THINKPAD,
1360};
1361
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001362enum { /* Keys available through NVRAM polling */
1363 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1364 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1365};
1366
1367enum { /* Positions of some of the keys in hotkey masks */
1368 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1369 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1370 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1371 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1372 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1373 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1374 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1375 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1376 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1377 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1378 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1379};
1380
1381enum { /* NVRAM to ACPI HKEY group map */
1382 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1383 TP_ACPI_HKEY_ZOOM_MASK |
1384 TP_ACPI_HKEY_DISPSWTCH_MASK |
1385 TP_ACPI_HKEY_HIBERNATE_MASK,
1386 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1387 TP_ACPI_HKEY_BRGHTDWN_MASK,
1388 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1389 TP_ACPI_HKEY_VOLDWN_MASK |
1390 TP_ACPI_HKEY_MUTE_MASK,
1391};
1392
1393#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1394struct tp_nvram_state {
1395 u16 thinkpad_toggle:1;
1396 u16 zoom_toggle:1;
1397 u16 display_toggle:1;
1398 u16 thinklight_toggle:1;
1399 u16 hibernate_toggle:1;
1400 u16 displayexp_toggle:1;
1401 u16 display_state:1;
1402 u16 brightness_toggle:1;
1403 u16 volume_toggle:1;
1404 u16 mute:1;
1405
1406 u8 brightness_level;
1407 u8 volume_level;
1408};
1409
1410static struct task_struct *tpacpi_hotkey_task;
1411static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1412static int hotkey_poll_freq = 10; /* Hz */
1413static struct mutex hotkey_thread_mutex;
1414static struct mutex hotkey_thread_data_mutex;
1415static unsigned int hotkey_config_change;
1416
1417#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1418
1419#define hotkey_source_mask 0U
1420
1421#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001423static struct mutex hotkey_mutex;
1424
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001425static enum { /* Reasons for waking up */
1426 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1427 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1428 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1429} hotkey_wakeup_reason;
1430
1431static int hotkey_autosleep_ack;
1432
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001433static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001434static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001435static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001436static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001437
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001438static unsigned int hotkey_report_mode;
1439
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001440static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001441
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001442static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001443
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001444#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1445#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001446 do { \
1447 mutex_lock(&hotkey_thread_data_mutex); \
1448 hotkey_config_change++; \
1449 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001450#define HOTKEY_CONFIG_CRITICAL_END \
1451 mutex_unlock(&hotkey_thread_data_mutex);
1452#else
1453#define HOTKEY_CONFIG_CRITICAL_START
1454#define HOTKEY_CONFIG_CRITICAL_END
1455#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1456
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001457/* HKEY.MHKG() return bits */
1458#define TP_HOTKEY_TABLET_MASK (1 << 3)
1459
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001460static int hotkey_get_wlsw(int *status)
1461{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001462#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1463 if (dbg_wlswemul) {
1464 *status = !!tpacpi_wlsw_emulstate;
1465 return 0;
1466 }
1467#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001468 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1469 return -EIO;
1470 return 0;
1471}
1472
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001473static int hotkey_get_tablet_mode(int *status)
1474{
1475 int s;
1476
1477 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1478 return -EIO;
1479
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001480 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1481 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001482}
1483
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001484/*
1485 * Call with hotkey_mutex held
1486 */
1487static int hotkey_mask_get(void)
1488{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001489 u32 m = 0;
1490
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001491 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001492 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001493 return -EIO;
1494 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001495 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001496
1497 return 0;
1498}
1499
1500/*
1501 * Call with hotkey_mutex held
1502 */
1503static int hotkey_mask_set(u32 mask)
1504{
1505 int i;
1506 int rc = 0;
1507
1508 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001509 if (!tp_warned.hotkey_mask_ff &&
1510 (mask == 0xffff || mask == 0xffffff ||
1511 mask == 0xffffffff)) {
1512 tp_warned.hotkey_mask_ff = 1;
1513 printk(TPACPI_NOTICE
1514 "setting the hotkey mask to 0x%08x is likely "
1515 "not the best way to go about it\n", mask);
1516 printk(TPACPI_NOTICE
1517 "please consider using the driver defaults, "
1518 "and refer to up-to-date thinkpad-acpi "
1519 "documentation\n");
1520 }
1521
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001522 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001523 for (i = 0; i < 32; i++) {
1524 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001525 /* enable in firmware mask only keys not in NVRAM
1526 * mode, but enable the key in the cached hotkey_mask
1527 * regardless of mode, or the key will end up
1528 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001529 if (!acpi_evalf(hkey_handle,
1530 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001531 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001532 rc = -EIO;
1533 break;
1534 } else {
1535 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1536 }
1537 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001538 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001539
1540 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001541 if (!hotkey_mask_get() && !rc &&
1542 (hotkey_mask & ~hotkey_source_mask) !=
1543 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001544 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001545 "requested hot key mask 0x%08x, but "
1546 "firmware forced it to 0x%08x\n",
1547 mask, hotkey_mask);
1548 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001549 } else {
1550#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1551 HOTKEY_CONFIG_CRITICAL_START
1552 hotkey_mask = mask & hotkey_source_mask;
1553 HOTKEY_CONFIG_CRITICAL_END
1554 hotkey_mask_get();
1555 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001556 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001557 "requested hot key mask 0x%08x, "
1558 "forced to 0x%08x (NVRAM poll mask is "
1559 "0x%08x): no firmware mask support\n",
1560 mask, hotkey_mask, hotkey_source_mask);
1561 }
1562#else
1563 hotkey_mask_get();
1564 rc = -ENXIO;
1565#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001566 }
1567
1568 return rc;
1569}
1570
1571static int hotkey_status_get(int *status)
1572{
1573 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1574 return -EIO;
1575
1576 return 0;
1577}
1578
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001579static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001580{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001581 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001582 return -EIO;
1583
1584 return 0;
1585}
1586
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001587static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001588{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001589 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001590
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001591 if (tp_features.hotkey_tablet &&
1592 !hotkey_get_tablet_mode(&state)) {
1593 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001594
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001595 input_report_switch(tpacpi_inputdev,
1596 SW_TABLET_MODE, !!state);
1597 input_sync(tpacpi_inputdev);
1598
1599 mutex_unlock(&tpacpi_inputdev_send_mutex);
1600 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001601}
1602
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001603static void tpacpi_input_send_key(unsigned int scancode)
1604{
1605 unsigned int keycode;
1606
1607 keycode = hotkey_keycode_map[scancode];
1608
1609 if (keycode != KEY_RESERVED) {
1610 mutex_lock(&tpacpi_inputdev_send_mutex);
1611
1612 input_report_key(tpacpi_inputdev, keycode, 1);
1613 if (keycode == KEY_UNKNOWN)
1614 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1615 scancode);
1616 input_sync(tpacpi_inputdev);
1617
1618 input_report_key(tpacpi_inputdev, keycode, 0);
1619 if (keycode == KEY_UNKNOWN)
1620 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1621 scancode);
1622 input_sync(tpacpi_inputdev);
1623
1624 mutex_unlock(&tpacpi_inputdev_send_mutex);
1625 }
1626}
1627
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001628#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1629static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1630
1631static void tpacpi_hotkey_send_key(unsigned int scancode)
1632{
1633 tpacpi_input_send_key(scancode);
1634 if (hotkey_report_mode < 2) {
1635 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1636 0x80, 0x1001 + scancode);
1637 }
1638}
1639
1640static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1641{
1642 u8 d;
1643
1644 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1645 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1646 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1647 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1648 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1649 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1650 }
1651 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1652 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1653 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1654 }
1655 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1656 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1657 n->displayexp_toggle =
1658 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1659 }
1660 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1661 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1662 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1663 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1664 n->brightness_toggle =
1665 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1666 }
1667 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1668 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1669 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1670 >> TP_NVRAM_POS_LEVEL_VOLUME;
1671 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1672 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1673 }
1674}
1675
1676#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001677 do { \
1678 if ((mask & (1 << __scancode)) && \
1679 oldn->__member != newn->__member) \
1680 tpacpi_hotkey_send_key(__scancode); \
1681 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001682
1683#define TPACPI_MAY_SEND_KEY(__scancode) \
1684 do { if (mask & (1 << __scancode)) \
1685 tpacpi_hotkey_send_key(__scancode); } while (0)
1686
1687static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001688 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001689 u32 mask)
1690{
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1694 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1695
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1697
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1699
1700 /* handle volume */
1701 if (oldn->volume_toggle != newn->volume_toggle) {
1702 if (oldn->mute != newn->mute) {
1703 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1704 }
1705 if (oldn->volume_level > newn->volume_level) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1707 } else if (oldn->volume_level < newn->volume_level) {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1709 } else if (oldn->mute == newn->mute) {
1710 /* repeated key presses that didn't change state */
1711 if (newn->mute) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1713 } else if (newn->volume_level != 0) {
1714 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1715 } else {
1716 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1717 }
1718 }
1719 }
1720
1721 /* handle brightness */
1722 if (oldn->brightness_toggle != newn->brightness_toggle) {
1723 if (oldn->brightness_level < newn->brightness_level) {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1725 } else if (oldn->brightness_level > newn->brightness_level) {
1726 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1727 } else {
1728 /* repeated key presses that didn't change state */
1729 if (newn->brightness_level != 0) {
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1731 } else {
1732 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1733 }
1734 }
1735 }
1736}
1737
1738#undef TPACPI_COMPARE_KEY
1739#undef TPACPI_MAY_SEND_KEY
1740
1741static int hotkey_kthread(void *data)
1742{
1743 struct tp_nvram_state s[2];
1744 u32 mask;
1745 unsigned int si, so;
1746 unsigned long t;
1747 unsigned int change_detector, must_reset;
1748
1749 mutex_lock(&hotkey_thread_mutex);
1750
1751 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1752 goto exit;
1753
1754 set_freezable();
1755
1756 so = 0;
1757 si = 1;
1758 t = 0;
1759
1760 /* Initial state for compares */
1761 mutex_lock(&hotkey_thread_data_mutex);
1762 change_detector = hotkey_config_change;
1763 mask = hotkey_source_mask & hotkey_mask;
1764 mutex_unlock(&hotkey_thread_data_mutex);
1765 hotkey_read_nvram(&s[so], mask);
1766
1767 while (!kthread_should_stop() && hotkey_poll_freq) {
1768 if (t == 0)
1769 t = 1000/hotkey_poll_freq;
1770 t = msleep_interruptible(t);
1771 if (unlikely(kthread_should_stop()))
1772 break;
1773 must_reset = try_to_freeze();
1774 if (t > 0 && !must_reset)
1775 continue;
1776
1777 mutex_lock(&hotkey_thread_data_mutex);
1778 if (must_reset || hotkey_config_change != change_detector) {
1779 /* forget old state on thaw or config change */
1780 si = so;
1781 t = 0;
1782 change_detector = hotkey_config_change;
1783 }
1784 mask = hotkey_source_mask & hotkey_mask;
1785 mutex_unlock(&hotkey_thread_data_mutex);
1786
1787 if (likely(mask)) {
1788 hotkey_read_nvram(&s[si], mask);
1789 if (likely(si != so)) {
1790 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001791 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001792 }
1793 }
1794
1795 so = si;
1796 si ^= 1;
1797 }
1798
1799exit:
1800 mutex_unlock(&hotkey_thread_mutex);
1801 return 0;
1802}
1803
1804static void hotkey_poll_stop_sync(void)
1805{
1806 if (tpacpi_hotkey_task) {
1807 if (frozen(tpacpi_hotkey_task) ||
1808 freezing(tpacpi_hotkey_task))
1809 thaw_process(tpacpi_hotkey_task);
1810
1811 kthread_stop(tpacpi_hotkey_task);
1812 tpacpi_hotkey_task = NULL;
1813 mutex_lock(&hotkey_thread_mutex);
1814 /* at this point, the thread did exit */
1815 mutex_unlock(&hotkey_thread_mutex);
1816 }
1817}
1818
1819/* call with hotkey_mutex held */
1820static void hotkey_poll_setup(int may_warn)
1821{
1822 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1823 hotkey_poll_freq > 0 &&
1824 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1825 if (!tpacpi_hotkey_task) {
1826 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001827 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001828 if (IS_ERR(tpacpi_hotkey_task)) {
1829 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001830 printk(TPACPI_ERR
1831 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001832 "for hotkey polling\n");
1833 }
1834 }
1835 } else {
1836 hotkey_poll_stop_sync();
1837 if (may_warn &&
1838 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001839 printk(TPACPI_NOTICE
1840 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001841 "which is currently disabled\n",
1842 hotkey_source_mask);
1843 }
1844 }
1845}
1846
1847static void hotkey_poll_setup_safe(int may_warn)
1848{
1849 mutex_lock(&hotkey_mutex);
1850 hotkey_poll_setup(may_warn);
1851 mutex_unlock(&hotkey_mutex);
1852}
1853
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001854#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1855
1856static void hotkey_poll_setup_safe(int __unused)
1857{
1858}
1859
1860#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1861
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001862static int hotkey_inputdev_open(struct input_dev *dev)
1863{
1864 switch (tpacpi_lifecycle) {
1865 case TPACPI_LIFE_INIT:
1866 /*
1867 * hotkey_init will call hotkey_poll_setup_safe
1868 * at the appropriate moment
1869 */
1870 return 0;
1871 case TPACPI_LIFE_EXITING:
1872 return -EBUSY;
1873 case TPACPI_LIFE_RUNNING:
1874 hotkey_poll_setup_safe(0);
1875 return 0;
1876 }
1877
1878 /* Should only happen if tpacpi_lifecycle is corrupt */
1879 BUG();
1880 return -EBUSY;
1881}
1882
1883static void hotkey_inputdev_close(struct input_dev *dev)
1884{
1885 /* disable hotkey polling when possible */
1886 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1887 hotkey_poll_setup_safe(0);
1888}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001889
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001890/* sysfs hotkey enable ------------------------------------------------- */
1891static ssize_t hotkey_enable_show(struct device *dev,
1892 struct device_attribute *attr,
1893 char *buf)
1894{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001895 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001896
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001897 printk_deprecated_attribute("hotkey_enable",
1898 "Hotkey reporting is always enabled");
1899
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001900 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001901 if (res)
1902 return res;
1903
1904 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1905}
1906
1907static ssize_t hotkey_enable_store(struct device *dev,
1908 struct device_attribute *attr,
1909 const char *buf, size_t count)
1910{
1911 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001912
1913 printk_deprecated_attribute("hotkey_enable",
1914 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001915
1916 if (parse_strtoul(buf, 1, &t))
1917 return -EINVAL;
1918
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001919 if (t == 0)
1920 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001921
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001922 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001923}
1924
1925static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001926 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001927 hotkey_enable_show, hotkey_enable_store);
1928
1929/* sysfs hotkey mask --------------------------------------------------- */
1930static ssize_t hotkey_mask_show(struct device *dev,
1931 struct device_attribute *attr,
1932 char *buf)
1933{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001934 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001935
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001936 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001937 return -ERESTARTSYS;
1938 res = hotkey_mask_get();
1939 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001940
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001941 return (res)?
1942 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001943}
1944
1945static ssize_t hotkey_mask_store(struct device *dev,
1946 struct device_attribute *attr,
1947 const char *buf, size_t count)
1948{
1949 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001950 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001951
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001952 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001953 return -EINVAL;
1954
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001955 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001956 return -ERESTARTSYS;
1957
1958 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001959
1960#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1961 hotkey_poll_setup(1);
1962#endif
1963
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001964 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001965
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00001966 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
1967
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001968 return (res) ? res : count;
1969}
1970
1971static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001972 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001973 hotkey_mask_show, hotkey_mask_store);
1974
1975/* sysfs hotkey bios_enabled ------------------------------------------- */
1976static ssize_t hotkey_bios_enabled_show(struct device *dev,
1977 struct device_attribute *attr,
1978 char *buf)
1979{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001980 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001981}
1982
1983static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001984 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001985
1986/* sysfs hotkey bios_mask ---------------------------------------------- */
1987static ssize_t hotkey_bios_mask_show(struct device *dev,
1988 struct device_attribute *attr,
1989 char *buf)
1990{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001991 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001992}
1993
1994static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001995 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001996
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001997/* sysfs hotkey all_mask ----------------------------------------------- */
1998static ssize_t hotkey_all_mask_show(struct device *dev,
1999 struct device_attribute *attr,
2000 char *buf)
2001{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2003 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002004}
2005
2006static struct device_attribute dev_attr_hotkey_all_mask =
2007 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2008
2009/* sysfs hotkey recommended_mask --------------------------------------- */
2010static ssize_t hotkey_recommended_mask_show(struct device *dev,
2011 struct device_attribute *attr,
2012 char *buf)
2013{
2014 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015 (hotkey_all_mask | hotkey_source_mask)
2016 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002017}
2018
2019static struct device_attribute dev_attr_hotkey_recommended_mask =
2020 __ATTR(hotkey_recommended_mask, S_IRUGO,
2021 hotkey_recommended_mask_show, NULL);
2022
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002023#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2024
2025/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2026static ssize_t hotkey_source_mask_show(struct device *dev,
2027 struct device_attribute *attr,
2028 char *buf)
2029{
2030 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2031}
2032
2033static ssize_t hotkey_source_mask_store(struct device *dev,
2034 struct device_attribute *attr,
2035 const char *buf, size_t count)
2036{
2037 unsigned long t;
2038
2039 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2040 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2041 return -EINVAL;
2042
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002043 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002044 return -ERESTARTSYS;
2045
2046 HOTKEY_CONFIG_CRITICAL_START
2047 hotkey_source_mask = t;
2048 HOTKEY_CONFIG_CRITICAL_END
2049
2050 hotkey_poll_setup(1);
2051
2052 mutex_unlock(&hotkey_mutex);
2053
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002054 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2055
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002056 return count;
2057}
2058
2059static struct device_attribute dev_attr_hotkey_source_mask =
2060 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2061 hotkey_source_mask_show, hotkey_source_mask_store);
2062
2063/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2064static ssize_t hotkey_poll_freq_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2067{
2068 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2069}
2070
2071static ssize_t hotkey_poll_freq_store(struct device *dev,
2072 struct device_attribute *attr,
2073 const char *buf, size_t count)
2074{
2075 unsigned long t;
2076
2077 if (parse_strtoul(buf, 25, &t))
2078 return -EINVAL;
2079
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002080 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002081 return -ERESTARTSYS;
2082
2083 hotkey_poll_freq = t;
2084
2085 hotkey_poll_setup(1);
2086 mutex_unlock(&hotkey_mutex);
2087
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002088 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2089
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002090 return count;
2091}
2092
2093static struct device_attribute dev_attr_hotkey_poll_freq =
2094 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2095 hotkey_poll_freq_show, hotkey_poll_freq_store);
2096
2097#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2098
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002099/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002100static ssize_t hotkey_radio_sw_show(struct device *dev,
2101 struct device_attribute *attr,
2102 char *buf)
2103{
2104 int res, s;
2105 res = hotkey_get_wlsw(&s);
2106 if (res < 0)
2107 return res;
2108
2109 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2110}
2111
2112static struct device_attribute dev_attr_hotkey_radio_sw =
2113 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2114
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002115static void hotkey_radio_sw_notify_change(void)
2116{
2117 if (tp_features.hotkey_wlsw)
2118 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2119 "hotkey_radio_sw");
2120}
2121
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002122/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2123static ssize_t hotkey_tablet_mode_show(struct device *dev,
2124 struct device_attribute *attr,
2125 char *buf)
2126{
2127 int res, s;
2128 res = hotkey_get_tablet_mode(&s);
2129 if (res < 0)
2130 return res;
2131
2132 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2133}
2134
2135static struct device_attribute dev_attr_hotkey_tablet_mode =
2136 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2137
2138static void hotkey_tablet_mode_notify_change(void)
2139{
2140 if (tp_features.hotkey_tablet)
2141 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2142 "hotkey_tablet_mode");
2143}
2144
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002145/* sysfs hotkey report_mode -------------------------------------------- */
2146static ssize_t hotkey_report_mode_show(struct device *dev,
2147 struct device_attribute *attr,
2148 char *buf)
2149{
2150 return snprintf(buf, PAGE_SIZE, "%d\n",
2151 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2152}
2153
2154static struct device_attribute dev_attr_hotkey_report_mode =
2155 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2156
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002157/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002158static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2159 struct device_attribute *attr,
2160 char *buf)
2161{
2162 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2163}
2164
2165static struct device_attribute dev_attr_hotkey_wakeup_reason =
2166 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2167
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002168static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002169{
2170 if (tp_features.hotkey_mask)
2171 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2172 "wakeup_reason");
2173}
2174
2175/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002176static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2177 struct device_attribute *attr,
2178 char *buf)
2179{
2180 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2181}
2182
2183static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2184 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2185 hotkey_wakeup_hotunplug_complete_show, NULL);
2186
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002187static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002188{
2189 if (tp_features.hotkey_mask)
2190 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2191 "wakeup_hotunplug_complete");
2192}
2193
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002194/* --------------------------------------------------------------------- */
2195
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002196static struct attribute *hotkey_attributes[] __initdata = {
2197 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002198 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002199 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002200#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2201 &dev_attr_hotkey_mask.attr,
2202 &dev_attr_hotkey_all_mask.attr,
2203 &dev_attr_hotkey_recommended_mask.attr,
2204 &dev_attr_hotkey_source_mask.attr,
2205 &dev_attr_hotkey_poll_freq.attr,
2206#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002207};
2208
2209static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002210 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002211#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2212 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002213 &dev_attr_hotkey_all_mask.attr,
2214 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002215#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002216 &dev_attr_hotkey_wakeup_reason.attr,
2217 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002218};
2219
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002220static void bluetooth_update_rfk(void);
2221static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002222static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002223static void tpacpi_send_radiosw_update(void)
2224{
2225 int wlsw;
2226
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002227 /* Sync these BEFORE sending any rfkill events */
2228 if (tp_features.bluetooth)
2229 bluetooth_update_rfk();
2230 if (tp_features.wan)
2231 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002232 if (tp_features.uwb)
2233 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002234
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002235 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2236 mutex_lock(&tpacpi_inputdev_send_mutex);
2237
2238 input_report_switch(tpacpi_inputdev,
2239 SW_RFKILL_ALL, !!wlsw);
2240 input_sync(tpacpi_inputdev);
2241
2242 mutex_unlock(&tpacpi_inputdev_send_mutex);
2243 }
2244 hotkey_radio_sw_notify_change();
2245}
2246
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002247static void hotkey_exit(void)
2248{
2249#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2250 hotkey_poll_stop_sync();
2251#endif
2252
2253 if (hotkey_dev_attributes)
2254 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2255
2256 kfree(hotkey_keycode_map);
2257
2258 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002259 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002260 "restoring original hot key mask\n");
2261 /* no short-circuit boolean operator below! */
2262 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002263 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002264 printk(TPACPI_ERR
2265 "failed to restore hot key mask "
2266 "to BIOS defaults\n");
2267 }
2268}
2269
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002270static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002271{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002272 /* Requirements for changing the default keymaps:
2273 *
2274 * 1. Many of the keys are mapped to KEY_RESERVED for very
2275 * good reasons. Do not change them unless you have deep
2276 * knowledge on the IBM and Lenovo ThinkPad firmware for
2277 * the various ThinkPad models. The driver behaves
2278 * differently for KEY_RESERVED: such keys have their
2279 * hot key mask *unset* in mask_recommended, and also
2280 * in the initial hot key mask programmed into the
2281 * firmware at driver load time, which means the firm-
2282 * ware may react very differently if you change them to
2283 * something else;
2284 *
2285 * 2. You must be subscribed to the linux-thinkpad and
2286 * ibm-acpi-devel mailing lists, and you should read the
2287 * list archives since 2007 if you want to change the
2288 * keymaps. This requirement exists so that you will
2289 * know the past history of problems with the thinkpad-
2290 * acpi driver keymaps, and also that you will be
2291 * listening to any bug reports;
2292 *
2293 * 3. Do not send thinkpad-acpi specific patches directly to
2294 * for merging, *ever*. Send them to the linux-acpi
2295 * mailinglist for comments. Merging is to be done only
2296 * through acpi-test and the ACPI maintainer.
2297 *
2298 * If the above is too much to ask, don't change the keymap.
2299 * Ask the thinkpad-acpi maintainer to do it, instead.
2300 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002301 static u16 ibm_keycode_map[] __initdata = {
2302 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2303 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2304 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2305 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002306
2307 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002308 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2309 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2310 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002311
2312 /* brightness: firmware always reacts to them, unless
2313 * X.org did some tricks in the radeon BIOS scratch
2314 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002315 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002316 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002317
2318 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002319 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002320
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002321 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2322 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002323
2324 /* Volume: firmware always react to it and reprograms
2325 * the built-in *extra* mixer. Never map it to control
2326 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002327 KEY_RESERVED, /* 0x14: VOLUME UP */
2328 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2329 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002330
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002331 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002332
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002333 /* (assignments unknown, please report if found) */
2334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2335 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2336 };
2337 static u16 lenovo_keycode_map[] __initdata = {
2338 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2339 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2340 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2341 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002342
2343 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002344 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2345 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2346 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002347
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002348 /* These either have to go through ACPI video, or
2349 * act like in the IBM ThinkPads, so don't ever
2350 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002351 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002352 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002353
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002354 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002355
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002356 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2357 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002358
2359 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2360 * react to it and reprograms the built-in *extra* mixer.
2361 * Never map it to control another mixer by default.
2362 *
2363 * T60?, T61, R60?, R61: firmware and EC tries to send
2364 * these over the regular keyboard, so these are no-ops,
2365 * but there are still weird bugs re. MUTE, so do not
2366 * change unless you get test reports from all Lenovo
2367 * models. May cause the BIOS to interfere with the
2368 * HDA mixer.
2369 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002370 KEY_RESERVED, /* 0x14: VOLUME UP */
2371 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2372 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002373
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002374 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002375
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002376 /* (assignments unknown, please report if found) */
2377 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2378 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2379 };
2380
2381#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2382#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2383#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2384
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002385 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002386 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002387 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002388
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002389 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2390 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002391
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002392 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002393 BUG_ON(tpacpi_inputdev->open != NULL ||
2394 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002395
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002396 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002397 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002398
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002399#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2400 mutex_init(&hotkey_thread_mutex);
2401 mutex_init(&hotkey_thread_data_mutex);
2402#endif
2403
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002404 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002405 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002406
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002407 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2408 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002409 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002410
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002411 if (!tp_features.hotkey)
2412 return 1;
2413
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002414 tpacpi_disable_brightness_delay();
2415
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002416 hotkey_dev_attributes = create_attr_set(13, NULL);
2417 if (!hotkey_dev_attributes)
2418 return -ENOMEM;
2419 res = add_many_to_attr_set(hotkey_dev_attributes,
2420 hotkey_attributes,
2421 ARRAY_SIZE(hotkey_attributes));
2422 if (res)
2423 goto err_exit;
2424
2425 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2426 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2427 for HKEY interface version 0x100 */
2428 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2429 if ((hkeyv >> 8) != 1) {
2430 printk(TPACPI_ERR "unknown version of the "
2431 "HKEY interface: 0x%x\n", hkeyv);
2432 printk(TPACPI_ERR "please report this to %s\n",
2433 TPACPI_MAIL);
2434 } else {
2435 /*
2436 * MHKV 0x100 in A31, R40, R40e,
2437 * T4x, X31, and later
2438 */
2439 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2441 "firmware HKEY interface version: 0x%x\n",
2442 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002443 }
2444 }
2445
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002446 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2447 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002448 str_supported(tp_features.hotkey_mask));
2449
2450 if (tp_features.hotkey_mask) {
2451 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2452 "MHKA", "qd")) {
2453 printk(TPACPI_ERR
2454 "missing MHKA handler, "
2455 "please report this to %s\n",
2456 TPACPI_MAIL);
2457 /* FN+F12, FN+F4, FN+F3 */
2458 hotkey_all_mask = 0x080cU;
2459 }
2460 }
2461
2462 /* hotkey_source_mask *must* be zero for
2463 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002464 if (tp_features.hotkey_mask) {
2465 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002466 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002467 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002468
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002469 hotkey_orig_mask = hotkey_mask;
2470 res = add_many_to_attr_set(
2471 hotkey_dev_attributes,
2472 hotkey_mask_attributes,
2473 ARRAY_SIZE(hotkey_mask_attributes));
2474 if (res)
2475 goto err_exit;
2476 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002477
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002478#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002479 if (tp_features.hotkey_mask) {
2480 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2481 & ~hotkey_all_mask;
2482 } else {
2483 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2484 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002486 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002487 "hotkey source mask 0x%08x, polling freq %d\n",
2488 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002489#endif
2490
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002491#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2492 if (dbg_wlswemul) {
2493 tp_features.hotkey_wlsw = 1;
2494 printk(TPACPI_INFO
2495 "radio switch emulation enabled\n");
2496 } else
2497#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002498 /* Not all thinkpads have a hardware radio switch */
2499 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2500 tp_features.hotkey_wlsw = 1;
2501 printk(TPACPI_INFO
2502 "radio switch found; radios are %s\n",
2503 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002504 }
2505 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002506 res = add_to_attr_set(hotkey_dev_attributes,
2507 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002508
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002509 /* For X41t, X60t, X61t Tablets... */
2510 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2511 tp_features.hotkey_tablet = 1;
2512 printk(TPACPI_INFO
2513 "possible tablet mode switch found; "
2514 "ThinkPad in %s mode\n",
2515 (status & TP_HOTKEY_TABLET_MASK)?
2516 "tablet" : "laptop");
2517 res = add_to_attr_set(hotkey_dev_attributes,
2518 &dev_attr_hotkey_tablet_mode.attr);
2519 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002520
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002521 if (!res)
2522 res = register_attr_set_with_sysfs(
2523 hotkey_dev_attributes,
2524 &tpacpi_pdev->dev.kobj);
2525 if (res)
2526 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002527
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002528 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002529
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002530 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2531 GFP_KERNEL);
2532 if (!hotkey_keycode_map) {
2533 printk(TPACPI_ERR
2534 "failed to allocate memory for key map\n");
2535 res = -ENOMEM;
2536 goto err_exit;
2537 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002538
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002539 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002540 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002541 "using Lenovo default hot key map\n");
2542 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2543 TPACPI_HOTKEY_MAP_SIZE);
2544 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002545 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002546 "using IBM default hot key map\n");
2547 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2548 TPACPI_HOTKEY_MAP_SIZE);
2549 }
2550
2551 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2552 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2553 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2554 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2555 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2556 tpacpi_inputdev->keycode = hotkey_keycode_map;
2557 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2558 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2559 set_bit(hotkey_keycode_map[i],
2560 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002561 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002562 if (i < sizeof(hotkey_reserved_mask)*8)
2563 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002564 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002565 }
2566
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002567 if (tp_features.hotkey_wlsw) {
2568 set_bit(EV_SW, tpacpi_inputdev->evbit);
2569 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2570 }
2571 if (tp_features.hotkey_tablet) {
2572 set_bit(EV_SW, tpacpi_inputdev->evbit);
2573 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002574 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002575
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002576 /* Do not issue duplicate brightness change events to
2577 * userspace */
2578 if (!tp_features.bright_acpimode)
2579 /* update bright_acpimode... */
2580 tpacpi_check_std_acpi_brightness_support();
2581
2582 if (tp_features.bright_acpimode) {
2583 printk(TPACPI_INFO
2584 "This ThinkPad has standard ACPI backlight "
2585 "brightness control, supported by the ACPI "
2586 "video driver\n");
2587 printk(TPACPI_NOTICE
2588 "Disabling thinkpad-acpi brightness events "
2589 "by default...\n");
2590
2591 /* The hotkey_reserved_mask change below is not
2592 * necessary while the keys are at KEY_RESERVED in the
2593 * default map, but better safe than sorry, leave it
2594 * here as a marker of what we have to do, especially
2595 * when we finally become able to set this at runtime
2596 * on response to X.org requests */
2597 hotkey_reserved_mask |=
2598 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2599 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002600 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002601
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002602 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2603 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002604 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002605 if (res) {
2606 hotkey_exit();
2607 return res;
2608 }
2609 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2610 & ~hotkey_reserved_mask)
2611 | hotkey_orig_mask);
2612 if (res < 0 && res != -ENXIO) {
2613 hotkey_exit();
2614 return res;
2615 }
2616
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002617 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2618 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002619 (hotkey_report_mode < 2) ?
2620 "enabled" : "disabled");
2621
2622 tpacpi_inputdev->open = &hotkey_inputdev_open;
2623 tpacpi_inputdev->close = &hotkey_inputdev_close;
2624
2625 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002626 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002627 tpacpi_input_send_tabletsw();
2628
2629 return 0;
2630
2631err_exit:
2632 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2633 hotkey_dev_attributes = NULL;
2634
2635 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002636}
2637
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002638static bool hotkey_notify_hotkey(const u32 hkey,
2639 bool *send_acpi_ev,
2640 bool *ignore_acpi_ev)
2641{
2642 /* 0x1000-0x1FFF: key presses */
2643 unsigned int scancode = hkey & 0xfff;
2644 *send_acpi_ev = true;
2645 *ignore_acpi_ev = false;
2646
2647 if (scancode > 0 && scancode < 0x21) {
2648 scancode--;
2649 if (!(hotkey_source_mask & (1 << scancode))) {
2650 tpacpi_input_send_key(scancode);
2651 *send_acpi_ev = false;
2652 } else {
2653 *ignore_acpi_ev = true;
2654 }
2655 return true;
2656 }
2657 return false;
2658}
2659
2660static bool hotkey_notify_wakeup(const u32 hkey,
2661 bool *send_acpi_ev,
2662 bool *ignore_acpi_ev)
2663{
2664 /* 0x2000-0x2FFF: Wakeup reason */
2665 *send_acpi_ev = true;
2666 *ignore_acpi_ev = false;
2667
2668 switch (hkey) {
2669 case 0x2304: /* suspend, undock */
2670 case 0x2404: /* hibernation, undock */
2671 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2672 *ignore_acpi_ev = true;
2673 break;
2674
2675 case 0x2305: /* suspend, bay eject */
2676 case 0x2405: /* hibernation, bay eject */
2677 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2678 *ignore_acpi_ev = true;
2679 break;
2680
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002681 case 0x2313: /* Battery on critical low level (S3) */
2682 case 0x2413: /* Battery on critical low level (S4) */
2683 printk(TPACPI_ALERT
2684 "EMERGENCY WAKEUP: battery almost empty\n");
2685 /* how to auto-heal: */
2686 /* 2313: woke up from S3, go to S4/S5 */
2687 /* 2413: woke up from S4, go to S5 */
2688 break;
2689
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002690 default:
2691 return false;
2692 }
2693
2694 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2695 printk(TPACPI_INFO
2696 "woke up due to a hot-unplug "
2697 "request...\n");
2698 hotkey_wakeup_reason_notify_change();
2699 }
2700 return true;
2701}
2702
2703static bool hotkey_notify_usrevent(const u32 hkey,
2704 bool *send_acpi_ev,
2705 bool *ignore_acpi_ev)
2706{
2707 /* 0x5000-0x5FFF: human interface helpers */
2708 *send_acpi_ev = true;
2709 *ignore_acpi_ev = false;
2710
2711 switch (hkey) {
2712 case 0x5010: /* Lenovo new BIOS: brightness changed */
2713 case 0x500b: /* X61t: tablet pen inserted into bay */
2714 case 0x500c: /* X61t: tablet pen removed from bay */
2715 return true;
2716
2717 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2718 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2719 tpacpi_input_send_tabletsw();
2720 hotkey_tablet_mode_notify_change();
2721 *send_acpi_ev = false;
2722 return true;
2723
2724 case 0x5001:
2725 case 0x5002:
2726 /* LID switch events. Do not propagate */
2727 *ignore_acpi_ev = true;
2728 return true;
2729
2730 default:
2731 return false;
2732 }
2733}
2734
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002735static bool hotkey_notify_thermal(const u32 hkey,
2736 bool *send_acpi_ev,
2737 bool *ignore_acpi_ev)
2738{
2739 /* 0x6000-0x6FFF: thermal alarms */
2740 *send_acpi_ev = true;
2741 *ignore_acpi_ev = false;
2742
2743 switch (hkey) {
2744 case 0x6011:
2745 printk(TPACPI_CRIT
2746 "THERMAL ALARM: battery is too hot!\n");
2747 /* recommended action: warn user through gui */
2748 return true;
2749 case 0x6012:
2750 printk(TPACPI_ALERT
2751 "THERMAL EMERGENCY: battery is extremely hot!\n");
2752 /* recommended action: immediate sleep/hibernate */
2753 return true;
2754 case 0x6021:
2755 printk(TPACPI_CRIT
2756 "THERMAL ALARM: "
2757 "a sensor reports something is too hot!\n");
2758 /* recommended action: warn user through gui, that */
2759 /* some internal component is too hot */
2760 return true;
2761 case 0x6022:
2762 printk(TPACPI_ALERT
2763 "THERMAL EMERGENCY: "
2764 "a sensor reports something is extremely hot!\n");
2765 /* recommended action: immediate sleep/hibernate */
2766 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002767 case 0x6030:
2768 printk(TPACPI_INFO
2769 "EC reports that Thermal Table has changed\n");
2770 /* recommended action: do nothing, we don't have
2771 * Lenovo ATM information */
2772 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002773 default:
2774 printk(TPACPI_ALERT
2775 "THERMAL ALERT: unknown thermal alarm received\n");
2776 return false;
2777 }
2778}
2779
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002780static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2781{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002782 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002783 bool send_acpi_ev;
2784 bool ignore_acpi_ev;
2785 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002786
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002787 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002788 printk(TPACPI_ERR
2789 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002790 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002791 acpi_bus_generate_netlink_event(
2792 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002793 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002794 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002795 return;
2796 }
2797
2798 while (1) {
2799 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002800 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002801 return;
2802 }
2803
2804 if (hkey == 0) {
2805 /* queue empty */
2806 return;
2807 }
2808
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002809 send_acpi_ev = true;
2810 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002811
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002812 switch (hkey >> 12) {
2813 case 1:
2814 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002815 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2816 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002817 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002818 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002819 /* 0x2000-0x2FFF: Wakeup reason */
2820 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2821 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002822 break;
2823 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002824 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002825 if (hkey == 0x3003) {
2826 hotkey_autosleep_ack = 1;
2827 printk(TPACPI_INFO
2828 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002829 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002830 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002831 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002832 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002833 }
2834 break;
2835 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002836 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002837 if (hkey == 0x4003) {
2838 hotkey_autosleep_ack = 1;
2839 printk(TPACPI_INFO
2840 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002841 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002842 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002843 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002844 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002845 }
2846 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002847 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002848 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002849 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2850 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002851 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002852 case 6:
2853 /* 0x6000-0x6FFF: thermal alarms */
2854 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2855 &ignore_acpi_ev);
2856 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002857 case 7:
2858 /* 0x7000-0x7FFF: misc */
2859 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002860 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002861 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002862 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002863 break;
2864 }
2865 /* fallthrough to default */
2866 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002867 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002868 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002869 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002870 printk(TPACPI_NOTICE
2871 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002872 printk(TPACPI_NOTICE
2873 "please report the conditions when this "
2874 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002875 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002876
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002877 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002878 if (!ignore_acpi_ev &&
2879 (send_acpi_ev || hotkey_report_mode < 2)) {
2880 acpi_bus_generate_proc_event(ibm->acpi->device,
2881 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002882 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002883
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002884 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002885 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002886 acpi_bus_generate_netlink_event(
2887 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002888 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002889 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002890 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002891 }
2892}
2893
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002894static void hotkey_suspend(pm_message_t state)
2895{
2896 /* Do these on suspend, we get the events on early resume! */
2897 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2898 hotkey_autosleep_ack = 0;
2899}
2900
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002901static void hotkey_resume(void)
2902{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002903 tpacpi_disable_brightness_delay();
2904
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002905 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002906 printk(TPACPI_ERR
2907 "error while trying to read hot key mask "
2908 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002909 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002910 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002911 hotkey_wakeup_reason_notify_change();
2912 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002913 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002914}
2915
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002916/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002917static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002919 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 int len = 0;
2921
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002922 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923 len += sprintf(p + len, "status:\t\tnot supported\n");
2924 return len;
2925 }
2926
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002927 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002928 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002929 res = hotkey_status_get(&status);
2930 if (!res)
2931 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002932 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002933 if (res)
2934 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002937 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002938 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 len += sprintf(p + len,
2940 "commands:\tenable, disable, reset, <mask>\n");
2941 } else {
2942 len += sprintf(p + len, "mask:\t\tnot supported\n");
2943 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2944 }
2945
2946 return len;
2947}
2948
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002949static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002950{
2951 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002952 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
2953 TPACPI_WARN
2954 "hotkey enable/disable functionality has been "
2955 "removed from the driver. Hotkeys are always "
2956 "enabled\n"))
2957 printk(TPACPI_ERR
2958 "Please remove the hotkey=enable module "
2959 "parameter, it is deprecated. Hotkeys are always "
2960 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002961}
2962
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002963static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002965 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002966 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002969 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return -ENODEV;
2971
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002972 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002973 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002974
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002975 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002976
2977 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 while ((cmd = next_cmd(&buf))) {
2979 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002980 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00002982 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002983 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002985 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2987 /* mask set */
2988 } else if (sscanf(cmd, "%x", &mask) == 1) {
2989 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002990 } else {
2991 res = -EINVAL;
2992 goto errexit;
2993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002995
2996 if (!res)
2997 tpacpi_disclose_usertask("procfs hotkey",
2998 "set mask to 0x%08x\n", mask);
2999
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003000 if (!res && mask != hotkey_mask)
3001 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003003errexit:
3004 mutex_unlock(&hotkey_mutex);
3005 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003008static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003009 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003010 {"", 0},
3011};
3012
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003013static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003014 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003015 .notify = hotkey_notify,
3016 .handle = &hkey_handle,
3017 .type = ACPI_DEVICE_NOTIFY,
3018};
3019
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003020static struct ibm_struct hotkey_driver_data = {
3021 .name = "hotkey",
3022 .read = hotkey_read,
3023 .write = hotkey_write,
3024 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003025 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003026 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003027 .acpi = &ibm_hotkey_acpidriver,
3028};
3029
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003030/*************************************************************************
3031 * Bluetooth subdriver
3032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003034enum {
3035 /* ACPI GBDC/SBDC bits */
3036 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3037 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003038 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3039 off / last state */
3040};
3041
3042enum {
3043 /* ACPI \BLTH commands */
3044 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3045 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3046 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3047 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3048 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003049};
3050
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003051#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3052
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003053static struct rfkill *tpacpi_bluetooth_rfkill;
3054
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003055static void bluetooth_suspend(pm_message_t state)
3056{
3057 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003058 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3059 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3060 vdbg_printk(TPACPI_DBG_RFKILL,
3061 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003062}
3063
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003064static int bluetooth_get_radiosw(void)
3065{
3066 int status;
3067
3068 if (!tp_features.bluetooth)
3069 return -ENODEV;
3070
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003071 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3072 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003073 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003074
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003075#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3076 if (dbg_bluetoothemul)
3077 return (tpacpi_bluetooth_emulstate) ?
3078 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3079#endif
3080
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003081 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3082 return -EIO;
3083
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003084 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3085 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003086}
3087
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003088static void bluetooth_update_rfk(void)
3089{
3090 int status;
3091
3092 if (!tpacpi_bluetooth_rfkill)
3093 return;
3094
3095 status = bluetooth_get_radiosw();
3096 if (status < 0)
3097 return;
3098 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003099
3100 vdbg_printk(TPACPI_DBG_RFKILL,
3101 "forced rfkill state to %d\n",
3102 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003103}
3104
3105static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003106{
3107 int status;
3108
3109 if (!tp_features.bluetooth)
3110 return -ENODEV;
3111
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003112 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3113 * reason to risk weird behaviour. */
3114 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3115 && radio_on)
3116 return -EPERM;
3117
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003118 vdbg_printk(TPACPI_DBG_RFKILL,
3119 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3120
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003121#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3122 if (dbg_bluetoothemul) {
3123 tpacpi_bluetooth_emulstate = !!radio_on;
3124 if (update_rfk)
3125 bluetooth_update_rfk();
3126 return 0;
3127 }
3128#endif
3129
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003130 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003131 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003132 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003133 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003134 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003135 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3136 return -EIO;
3137
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003138 if (update_rfk)
3139 bluetooth_update_rfk();
3140
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003141 return 0;
3142}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003143
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003144/* sysfs bluetooth enable ---------------------------------------------- */
3145static ssize_t bluetooth_enable_show(struct device *dev,
3146 struct device_attribute *attr,
3147 char *buf)
3148{
3149 int status;
3150
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003151 printk_deprecated_rfkill_attribute("bluetooth_enable");
3152
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003153 status = bluetooth_get_radiosw();
3154 if (status < 0)
3155 return status;
3156
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003157 return snprintf(buf, PAGE_SIZE, "%d\n",
3158 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003159}
3160
3161static ssize_t bluetooth_enable_store(struct device *dev,
3162 struct device_attribute *attr,
3163 const char *buf, size_t count)
3164{
3165 unsigned long t;
3166 int res;
3167
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003168 printk_deprecated_rfkill_attribute("bluetooth_enable");
3169
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003170 if (parse_strtoul(buf, 1, &t))
3171 return -EINVAL;
3172
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003173 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3174
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003175 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003176
3177 return (res) ? res : count;
3178}
3179
3180static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003181 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003182 bluetooth_enable_show, bluetooth_enable_store);
3183
3184/* --------------------------------------------------------------------- */
3185
3186static struct attribute *bluetooth_attributes[] = {
3187 &dev_attr_bluetooth_enable.attr,
3188 NULL
3189};
3190
3191static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003192 .attrs = bluetooth_attributes,
3193};
3194
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003195static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3196{
3197 int bts = bluetooth_get_radiosw();
3198
3199 if (bts < 0)
3200 return bts;
3201
3202 *state = bts;
3203 return 0;
3204}
3205
3206static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3207{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003208 dbg_printk(TPACPI_DBG_RFKILL,
3209 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003210 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3211}
3212
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003213static void bluetooth_shutdown(void)
3214{
3215 /* Order firmware to save current state to NVRAM */
3216 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3217 TP_ACPI_BLTH_SAVE_STATE))
3218 printk(TPACPI_NOTICE
3219 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003220 else
3221 vdbg_printk(TPACPI_DBG_RFKILL,
3222 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003223}
3224
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003225static void bluetooth_exit(void)
3226{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003227 bluetooth_shutdown();
3228
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003229 if (tpacpi_bluetooth_rfkill)
3230 rfkill_unregister(tpacpi_bluetooth_rfkill);
3231
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003232 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3233 &bluetooth_attr_group);
3234}
3235
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003236static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003238 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003239 int status = 0;
3240
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003241 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3242 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003243
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003244 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003245
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003246 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3247 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003248 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003249 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003251 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3252 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003253 str_supported(tp_features.bluetooth),
3254 status);
3255
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003256#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3257 if (dbg_bluetoothemul) {
3258 tp_features.bluetooth = 1;
3259 printk(TPACPI_INFO
3260 "bluetooth switch emulation enabled\n");
3261 } else
3262#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003263 if (tp_features.bluetooth &&
3264 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3265 /* no bluetooth hardware present in system */
3266 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003267 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003268 "bluetooth hardware not installed\n");
3269 }
3270
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003271 if (!tp_features.bluetooth)
3272 return 1;
3273
3274 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003275 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003276 if (res)
3277 return res;
3278
3279 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3280 &tpacpi_bluetooth_rfkill,
3281 RFKILL_TYPE_BLUETOOTH,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003282 TPACPI_RFK_BLUETOOTH_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003283 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003284 tpacpi_bluetooth_rfk_set,
3285 tpacpi_bluetooth_rfk_get);
3286 if (res) {
3287 bluetooth_exit();
3288 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003289 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003290
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003294/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003295static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003298 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003300 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003303 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003304 (status == RFKILL_STATE_UNBLOCKED) ?
3305 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 len += sprintf(p + len, "commands:\tenable, disable\n");
3307 }
3308
3309 return len;
3310}
3311
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003312static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003315 int state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003317 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 while ((cmd = next_cmd(&buf))) {
3321 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003322 state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003324 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 } else
3326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003329 if (state != -1) {
3330 tpacpi_disclose_usertask("procfs bluetooth",
3331 "attempt to %s\n",
3332 state ? "enable" : "disable");
3333 bluetooth_set_radiosw(state, 1);
3334 }
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return 0;
3337}
3338
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003339static struct ibm_struct bluetooth_driver_data = {
3340 .name = "bluetooth",
3341 .read = bluetooth_read,
3342 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003343 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003344 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003345 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003346};
3347
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003348/*************************************************************************
3349 * Wan subdriver
3350 */
3351
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003352enum {
3353 /* ACPI GWAN/SWAN bits */
3354 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3355 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003356 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3357 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003358};
3359
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003360#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3361
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003362static struct rfkill *tpacpi_wan_rfkill;
3363
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003364static void wan_suspend(pm_message_t state)
3365{
3366 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003367 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3368 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3369 vdbg_printk(TPACPI_DBG_RFKILL,
3370 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003371}
3372
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003373static int wan_get_radiosw(void)
3374{
3375 int status;
3376
3377 if (!tp_features.wan)
3378 return -ENODEV;
3379
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003380 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3381 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003382 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003383
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003384#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3385 if (dbg_wwanemul)
3386 return (tpacpi_wwan_emulstate) ?
3387 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3388#endif
3389
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003390 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3391 return -EIO;
3392
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003393 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3394 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003395}
3396
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003397static void wan_update_rfk(void)
3398{
3399 int status;
3400
3401 if (!tpacpi_wan_rfkill)
3402 return;
3403
3404 status = wan_get_radiosw();
3405 if (status < 0)
3406 return;
3407 rfkill_force_state(tpacpi_wan_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003408
3409 vdbg_printk(TPACPI_DBG_RFKILL,
3410 "forced rfkill state to %d\n",
3411 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003412}
3413
3414static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003415{
3416 int status;
3417
3418 if (!tp_features.wan)
3419 return -ENODEV;
3420
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003421 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3422 * reason to risk weird behaviour. */
3423 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3424 && radio_on)
3425 return -EPERM;
3426
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003427 vdbg_printk(TPACPI_DBG_RFKILL,
3428 "will %s WWAN\n", radio_on ? "enable" : "disable");
3429
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003430#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3431 if (dbg_wwanemul) {
3432 tpacpi_wwan_emulstate = !!radio_on;
3433 if (update_rfk)
3434 wan_update_rfk();
3435 return 0;
3436 }
3437#endif
3438
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003439 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003440 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003441 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003442 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003443 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003444 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3445 return -EIO;
3446
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003447 if (update_rfk)
3448 wan_update_rfk();
3449
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003450 return 0;
3451}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003452
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003453/* sysfs wan enable ---------------------------------------------------- */
3454static ssize_t wan_enable_show(struct device *dev,
3455 struct device_attribute *attr,
3456 char *buf)
3457{
3458 int status;
3459
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003460 printk_deprecated_rfkill_attribute("wwan_enable");
3461
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003462 status = wan_get_radiosw();
3463 if (status < 0)
3464 return status;
3465
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003466 return snprintf(buf, PAGE_SIZE, "%d\n",
3467 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003468}
3469
3470static ssize_t wan_enable_store(struct device *dev,
3471 struct device_attribute *attr,
3472 const char *buf, size_t count)
3473{
3474 unsigned long t;
3475 int res;
3476
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003477 printk_deprecated_rfkill_attribute("wwan_enable");
3478
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003479 if (parse_strtoul(buf, 1, &t))
3480 return -EINVAL;
3481
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003482 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3483
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003484 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003485
3486 return (res) ? res : count;
3487}
3488
3489static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003490 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003491 wan_enable_show, wan_enable_store);
3492
3493/* --------------------------------------------------------------------- */
3494
3495static struct attribute *wan_attributes[] = {
3496 &dev_attr_wan_enable.attr,
3497 NULL
3498};
3499
3500static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003501 .attrs = wan_attributes,
3502};
3503
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003504static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3505{
3506 int wans = wan_get_radiosw();
3507
3508 if (wans < 0)
3509 return wans;
3510
3511 *state = wans;
3512 return 0;
3513}
3514
3515static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3516{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003517 dbg_printk(TPACPI_DBG_RFKILL,
3518 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003519 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3520}
3521
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003522static void wan_shutdown(void)
3523{
3524 /* Order firmware to save current state to NVRAM */
3525 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3526 TP_ACPI_WGSV_SAVE_STATE))
3527 printk(TPACPI_NOTICE
3528 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003529 else
3530 vdbg_printk(TPACPI_DBG_RFKILL,
3531 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003532}
3533
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003534static void wan_exit(void)
3535{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003536 wan_shutdown();
3537
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003538 if (tpacpi_wan_rfkill)
3539 rfkill_unregister(tpacpi_wan_rfkill);
3540
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003541 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3542 &wan_attr_group);
3543}
3544
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003545static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003546{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003547 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003548 int status = 0;
3549
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003550 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3551 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003552
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003553 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003554
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003555 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003556 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003557
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003558 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3559 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003560 str_supported(tp_features.wan),
3561 status);
3562
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003563#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3564 if (dbg_wwanemul) {
3565 tp_features.wan = 1;
3566 printk(TPACPI_INFO
3567 "wwan switch emulation enabled\n");
3568 } else
3569#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003570 if (tp_features.wan &&
3571 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3572 /* no wan hardware present in system */
3573 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003574 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003575 "wan hardware not installed\n");
3576 }
3577
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003578 if (!tp_features.wan)
3579 return 1;
3580
3581 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003582 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003583 if (res)
3584 return res;
3585
3586 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3587 &tpacpi_wan_rfkill,
3588 RFKILL_TYPE_WWAN,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003589 TPACPI_RFK_WWAN_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003590 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003591 tpacpi_wan_rfk_set,
3592 tpacpi_wan_rfk_get);
3593 if (res) {
3594 wan_exit();
3595 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003596 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003597
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003598 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003599}
3600
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003601/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003602static int wan_read(char *p)
3603{
3604 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003605 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003606
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003607 tpacpi_disclose_usertask("procfs wan", "read");
3608
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003609 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003610 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003611 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003612 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003613 (status == RFKILL_STATE_UNBLOCKED) ?
3614 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003615 len += sprintf(p + len, "commands:\tenable, disable\n");
3616 }
3617
3618 return len;
3619}
3620
3621static int wan_write(char *buf)
3622{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003623 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003624 int state = -1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003625
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003626 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003627 return -ENODEV;
3628
3629 while ((cmd = next_cmd(&buf))) {
3630 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003631 state = 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003632 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003633 state = 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003634 } else
3635 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003636 }
3637
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003638 if (state != -1) {
3639 tpacpi_disclose_usertask("procfs wan",
3640 "attempt to %s\n",
3641 state ? "enable" : "disable");
3642 wan_set_radiosw(state, 1);
3643 }
3644
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003645 return 0;
3646}
3647
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003648static struct ibm_struct wan_driver_data = {
3649 .name = "wan",
3650 .read = wan_read,
3651 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003652 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003653 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003654 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003655};
3656
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003657/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003658 * UWB subdriver
3659 */
3660
3661enum {
3662 /* ACPI GUWB/SUWB bits */
3663 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3664 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3665};
3666
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003667#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3668
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003669static struct rfkill *tpacpi_uwb_rfkill;
3670
3671static int uwb_get_radiosw(void)
3672{
3673 int status;
3674
3675 if (!tp_features.uwb)
3676 return -ENODEV;
3677
3678 /* WLSW overrides UWB in firmware/hardware, reflect that */
3679 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3680 return RFKILL_STATE_HARD_BLOCKED;
3681
3682#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3683 if (dbg_uwbemul)
3684 return (tpacpi_uwb_emulstate) ?
3685 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3686#endif
3687
3688 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3689 return -EIO;
3690
3691 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3692 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3693}
3694
3695static void uwb_update_rfk(void)
3696{
3697 int status;
3698
3699 if (!tpacpi_uwb_rfkill)
3700 return;
3701
3702 status = uwb_get_radiosw();
3703 if (status < 0)
3704 return;
3705 rfkill_force_state(tpacpi_uwb_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003706
3707 vdbg_printk(TPACPI_DBG_RFKILL,
3708 "forced rfkill state to %d\n",
3709 status);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003710}
3711
3712static int uwb_set_radiosw(int radio_on, int update_rfk)
3713{
3714 int status;
3715
3716 if (!tp_features.uwb)
3717 return -ENODEV;
3718
3719 /* WLSW overrides UWB in firmware/hardware, but there is no
3720 * reason to risk weird behaviour. */
3721 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3722 && radio_on)
3723 return -EPERM;
3724
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003725 vdbg_printk(TPACPI_DBG_RFKILL,
3726 "will %s UWB\n", radio_on ? "enable" : "disable");
3727
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003728#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3729 if (dbg_uwbemul) {
3730 tpacpi_uwb_emulstate = !!radio_on;
3731 if (update_rfk)
3732 uwb_update_rfk();
3733 return 0;
3734 }
3735#endif
3736
3737 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3738 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3739 return -EIO;
3740
3741 if (update_rfk)
3742 uwb_update_rfk();
3743
3744 return 0;
3745}
3746
3747/* --------------------------------------------------------------------- */
3748
3749static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3750{
3751 int uwbs = uwb_get_radiosw();
3752
3753 if (uwbs < 0)
3754 return uwbs;
3755
3756 *state = uwbs;
3757 return 0;
3758}
3759
3760static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3761{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003762 dbg_printk(TPACPI_DBG_RFKILL,
3763 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003764 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3765}
3766
3767static void uwb_exit(void)
3768{
3769 if (tpacpi_uwb_rfkill)
3770 rfkill_unregister(tpacpi_uwb_rfkill);
3771}
3772
3773static int __init uwb_init(struct ibm_init_struct *iibm)
3774{
3775 int res;
3776 int status = 0;
3777
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003778 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3779 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003780
3781 TPACPI_ACPIHANDLE_INIT(hkey);
3782
3783 tp_features.uwb = hkey_handle &&
3784 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3785
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003786 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3787 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003788 str_supported(tp_features.uwb),
3789 status);
3790
3791#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3792 if (dbg_uwbemul) {
3793 tp_features.uwb = 1;
3794 printk(TPACPI_INFO
3795 "uwb switch emulation enabled\n");
3796 } else
3797#endif
3798 if (tp_features.uwb &&
3799 !(status & TP_ACPI_UWB_HWPRESENT)) {
3800 /* no uwb hardware present in system */
3801 tp_features.uwb = 0;
3802 dbg_printk(TPACPI_DBG_INIT,
3803 "uwb hardware not installed\n");
3804 }
3805
3806 if (!tp_features.uwb)
3807 return 1;
3808
3809 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3810 &tpacpi_uwb_rfkill,
3811 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003812 TPACPI_RFK_UWB_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003813 false,
3814 tpacpi_uwb_rfk_set,
3815 tpacpi_uwb_rfk_get);
3816
3817 return res;
3818}
3819
3820static struct ibm_struct uwb_driver_data = {
3821 .name = "uwb",
3822 .exit = uwb_exit,
3823 .flags.experimental = 1,
3824};
3825
3826/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003827 * Video subdriver
3828 */
3829
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003830#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3831
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003832enum video_access_mode {
3833 TPACPI_VIDEO_NONE = 0,
3834 TPACPI_VIDEO_570, /* 570 */
3835 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3836 TPACPI_VIDEO_NEW, /* all others */
3837};
3838
3839enum { /* video status flags, based on VIDEO_570 */
3840 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3841 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3842 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3843};
3844
3845enum { /* TPACPI_VIDEO_570 constants */
3846 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3847 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3848 * video_status_flags */
3849 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3850 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3851};
3852
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003853static enum video_access_mode video_supported;
3854static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003855
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003856static int video_autosw_get(void);
3857static int video_autosw_set(int enable);
3858
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003859TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003861static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863 int ivga;
3864
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003865 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3866
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003867 TPACPI_ACPIHANDLE_INIT(vid);
3868 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003869
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003870 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3871 /* G41, assume IVGA doesn't change */
3872 vid_handle = vid2_handle;
3873
3874 if (!vid_handle)
3875 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003876 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003877 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3878 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003879 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003880 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3881 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003882 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003883 else
3884 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003885 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003887 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3888 str_supported(video_supported != TPACPI_VIDEO_NONE),
3889 video_supported);
3890
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003891 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892}
3893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894static void video_exit(void)
3895{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003896 dbg_printk(TPACPI_DBG_EXIT,
3897 "restoring original video autoswitch mode\n");
3898 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003899 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003900 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003901}
3902
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003903static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
3905 int status = 0;
3906 int i;
3907
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003908 switch (video_supported) {
3909 case TPACPI_VIDEO_570:
3910 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3911 TP_ACPI_VIDEO_570_PHSCMD))
3912 return -EIO;
3913 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3914 break;
3915 case TPACPI_VIDEO_770:
3916 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3917 return -EIO;
3918 if (i)
3919 status |= TP_ACPI_VIDEO_S_LCD;
3920 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3921 return -EIO;
3922 if (i)
3923 status |= TP_ACPI_VIDEO_S_CRT;
3924 break;
3925 case TPACPI_VIDEO_NEW:
3926 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3927 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3928 return -EIO;
3929 if (i)
3930 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003932 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3933 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3934 return -EIO;
3935 if (i)
3936 status |= TP_ACPI_VIDEO_S_LCD;
3937 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3938 return -EIO;
3939 if (i)
3940 status |= TP_ACPI_VIDEO_S_DVI;
3941 break;
3942 default:
3943 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946 return status;
3947}
3948
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003949static int video_outputsw_set(int status)
3950{
3951 int autosw;
3952 int res = 0;
3953
3954 switch (video_supported) {
3955 case TPACPI_VIDEO_570:
3956 res = acpi_evalf(NULL, NULL,
3957 "\\_SB.PHS2", "vdd",
3958 TP_ACPI_VIDEO_570_PHS2CMD,
3959 status | TP_ACPI_VIDEO_570_PHS2SET);
3960 break;
3961 case TPACPI_VIDEO_770:
3962 autosw = video_autosw_get();
3963 if (autosw < 0)
3964 return autosw;
3965
3966 res = video_autosw_set(1);
3967 if (res)
3968 return res;
3969 res = acpi_evalf(vid_handle, NULL,
3970 "ASWT", "vdd", status * 0x100, 0);
3971 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003972 printk(TPACPI_ERR
3973 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003974 return -EIO;
3975 }
3976 break;
3977 case TPACPI_VIDEO_NEW:
3978 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003979 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003980 break;
3981 default:
3982 return -ENOSYS;
3983 }
3984
3985 return (res)? 0 : -EIO;
3986}
3987
3988static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003990 int autosw = 0;
3991
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003992 switch (video_supported) {
3993 case TPACPI_VIDEO_570:
3994 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3995 return -EIO;
3996 break;
3997 case TPACPI_VIDEO_770:
3998 case TPACPI_VIDEO_NEW:
3999 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4000 return -EIO;
4001 break;
4002 default:
4003 return -ENOSYS;
4004 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005
4006 return autosw & 1;
4007}
4008
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004009static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004010{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004011 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004012 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004013 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004014}
4015
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004016static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004017{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004018 int autosw = video_autosw_get();
4019 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004020
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004021 if (autosw < 0)
4022 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004024 switch (video_supported) {
4025 case TPACPI_VIDEO_570:
4026 res = video_autosw_set(1);
4027 if (res)
4028 return res;
4029 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4030 break;
4031 case TPACPI_VIDEO_770:
4032 case TPACPI_VIDEO_NEW:
4033 res = video_autosw_set(1);
4034 if (res)
4035 return res;
4036 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4037 break;
4038 default:
4039 return -ENOSYS;
4040 }
4041 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004042 printk(TPACPI_ERR
4043 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004044 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004045 }
4046
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004047 return (res)? 0 : -EIO;
4048}
4049
4050static int video_expand_toggle(void)
4051{
4052 switch (video_supported) {
4053 case TPACPI_VIDEO_570:
4054 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4055 0 : -EIO;
4056 case TPACPI_VIDEO_770:
4057 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4058 0 : -EIO;
4059 case TPACPI_VIDEO_NEW:
4060 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4061 0 : -EIO;
4062 default:
4063 return -ENOSYS;
4064 }
4065 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004066}
4067
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004068static int video_read(char *p)
4069{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004070 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004071 int len = 0;
4072
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004073 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004074 len += sprintf(p + len, "status:\t\tnot supported\n");
4075 return len;
4076 }
4077
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004078 status = video_outputsw_get();
4079 if (status < 0)
4080 return status;
4081
4082 autosw = video_autosw_get();
4083 if (autosw < 0)
4084 return autosw;
4085
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004086 len += sprintf(p + len, "status:\t\tsupported\n");
4087 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4088 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004089 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004090 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4091 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4092 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4093 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004094 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004095 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4096 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4097 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4098
4099 return len;
4100}
4101
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004102static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 char *cmd;
4105 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004106 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004108 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004109 return -ENODEV;
4110
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004111 enable = 0;
4112 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 while ((cmd = next_cmd(&buf))) {
4115 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004116 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004118 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004120 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004122 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004123 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004124 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004125 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004126 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004127 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004128 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004130 res = video_autosw_set(1);
4131 if (res)
4132 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004134 res = video_autosw_set(0);
4135 if (res)
4136 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004138 res = video_outputsw_cycle();
4139 if (res)
4140 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004142 res = video_expand_toggle();
4143 if (res)
4144 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 } else
4146 return -EINVAL;
4147 }
4148
4149 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004150 status = video_outputsw_get();
4151 if (status < 0)
4152 return status;
4153 res = video_outputsw_set((status & ~disable) | enable);
4154 if (res)
4155 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
4157
4158 return 0;
4159}
4160
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004161static struct ibm_struct video_driver_data = {
4162 .name = "video",
4163 .read = video_read,
4164 .write = video_write,
4165 .exit = video_exit,
4166};
4167
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004168#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4169
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004170/*************************************************************************
4171 * Light (thinklight) subdriver
4172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004174TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4175TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004176
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004177static int light_get_status(void)
4178{
4179 int status = 0;
4180
4181 if (tp_features.light_status) {
4182 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4183 return -EIO;
4184 return (!!status);
4185 }
4186
4187 return -ENXIO;
4188}
4189
4190static int light_set_status(int status)
4191{
4192 int rc;
4193
4194 if (tp_features.light) {
4195 if (cmos_handle) {
4196 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4197 (status)?
4198 TP_CMOS_THINKLIGHT_ON :
4199 TP_CMOS_THINKLIGHT_OFF);
4200 } else {
4201 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4202 (status)? 1 : 0);
4203 }
4204 return (rc)? 0 : -EIO;
4205 }
4206
4207 return -ENXIO;
4208}
4209
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004210static void light_set_status_worker(struct work_struct *work)
4211{
4212 struct tpacpi_led_classdev *data =
4213 container_of(work, struct tpacpi_led_classdev, work);
4214
4215 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4216 light_set_status((data->new_brightness != LED_OFF));
4217}
4218
4219static void light_sysfs_set(struct led_classdev *led_cdev,
4220 enum led_brightness brightness)
4221{
4222 struct tpacpi_led_classdev *data =
4223 container_of(led_cdev,
4224 struct tpacpi_led_classdev,
4225 led_classdev);
4226 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004227 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004228}
4229
4230static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4231{
4232 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4233}
4234
4235static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4236 .led_classdev = {
4237 .name = "tpacpi::thinklight",
4238 .brightness_set = &light_sysfs_set,
4239 .brightness_get = &light_sysfs_get,
4240 }
4241};
4242
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004243static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004245 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004246
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004247 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4248
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004249 TPACPI_ACPIHANDLE_INIT(ledb);
4250 TPACPI_ACPIHANDLE_INIT(lght);
4251 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004252 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004253
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004254 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004255 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004256
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004257 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004258 /* light status not supported on
4259 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004260 tp_features.light_status =
4261 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004263 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4264 str_supported(tp_features.light),
4265 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004266
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004267 if (!tp_features.light)
4268 return 1;
4269
4270 rc = led_classdev_register(&tpacpi_pdev->dev,
4271 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004272
4273 if (rc < 0) {
4274 tp_features.light = 0;
4275 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004276 } else {
4277 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004278 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004279
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004280 return rc;
4281}
4282
4283static void light_exit(void)
4284{
4285 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4286 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004287 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004290static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004293 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004295 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004296 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004297 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004298 len += sprintf(p + len, "status:\t\tunknown\n");
4299 len += sprintf(p + len, "commands:\ton, off\n");
4300 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004301 status = light_get_status();
4302 if (status < 0)
4303 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004305 len += sprintf(p + len, "commands:\ton, off\n");
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 return len;
4309}
4310
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004311static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004314 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004315
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004316 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004317 return -ENODEV;
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 while ((cmd = next_cmd(&buf))) {
4320 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004321 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004323 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 } else
4325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 }
4327
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004328 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004331static struct ibm_struct light_driver_data = {
4332 .name = "light",
4333 .read = light_read,
4334 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004335 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004336};
4337
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004338/*************************************************************************
4339 * Dock subdriver
4340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004342#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004343
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004344static void dock_notify(struct ibm_struct *ibm, u32 event);
4345static int dock_read(char *p);
4346static int dock_write(char *buf);
4347
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004348TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004349 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4350 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4351 "\\_SB.PCI.ISA.SLCE", /* 570 */
4352 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4353
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004354/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004355TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004356
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004357static const struct acpi_device_id ibm_pci_device_ids[] = {
4358 {PCI_ROOT_HID_STRING, 0},
4359 {"", 0},
4360};
4361
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004362static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4363 {
4364 .notify = dock_notify,
4365 .handle = &dock_handle,
4366 .type = ACPI_SYSTEM_NOTIFY,
4367 },
4368 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004369 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4370 * We just use it to get notifications of dock hotplug
4371 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004372 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004373 .notify = dock_notify,
4374 .handle = &pci_handle,
4375 .type = ACPI_SYSTEM_NOTIFY,
4376 },
4377};
4378
4379static struct ibm_struct dock_driver_data[2] = {
4380 {
4381 .name = "dock",
4382 .read = dock_read,
4383 .write = dock_write,
4384 .acpi = &ibm_dock_acpidriver[0],
4385 },
4386 {
4387 .name = "dock",
4388 .acpi = &ibm_dock_acpidriver[1],
4389 },
4390};
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392#define dock_docked() (_sta(dock_handle) & 1)
4393
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004394static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004395{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004396 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4397
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004398 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004399
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004400 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4401 str_supported(dock_handle != NULL));
4402
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004403 return (dock_handle)? 0 : 1;
4404}
4405
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004406static int __init dock_init2(struct ibm_init_struct *iibm)
4407{
4408 int dock2_needed;
4409
4410 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4411
4412 if (dock_driver_data[0].flags.acpi_driver_registered &&
4413 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004414 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004415 dock2_needed = (pci_handle != NULL);
4416 vdbg_printk(TPACPI_DBG_INIT,
4417 "dock PCI handler for the TP 570 is %s\n",
4418 str_supported(dock2_needed));
4419 } else {
4420 vdbg_printk(TPACPI_DBG_INIT,
4421 "dock subdriver part 2 not required\n");
4422 dock2_needed = 0;
4423 }
4424
4425 return (dock2_needed)? 0 : 1;
4426}
4427
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004428static void dock_notify(struct ibm_struct *ibm, u32 event)
4429{
4430 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004431 int pci = ibm->acpi->hid && ibm->acpi->device &&
4432 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004433 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004434
4435 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004436 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004437 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004438 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004439 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004440 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004441 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004442 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004443 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004444 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004446 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004448 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004449 }
Len Brown14e04fb32007-08-23 15:20:26 -04004450 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004451 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004452 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004453 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004454}
4455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004456static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
4458 int len = 0;
4459 int docked = dock_docked();
4460
4461 if (!dock_handle)
4462 len += sprintf(p + len, "status:\t\tnot supported\n");
4463 else if (!docked)
4464 len += sprintf(p + len, "status:\t\tundocked\n");
4465 else {
4466 len += sprintf(p + len, "status:\t\tdocked\n");
4467 len += sprintf(p + len, "commands:\tdock, undock\n");
4468 }
4469
4470 return len;
4471}
4472
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004473static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
4475 char *cmd;
4476
4477 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004478 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 while ((cmd = next_cmd(&buf))) {
4481 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4483 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return -EIO;
4485 } else if (strlencmp(cmd, "dock") == 0) {
4486 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4487 return -EIO;
4488 } else
4489 return -EINVAL;
4490 }
4491
4492 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004495#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497/*************************************************************************
4498 * Bay subdriver
4499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004501#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004502
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004503TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004504 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4505 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4506 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4507 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004508TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004509 "_EJ0", /* all others */
4510 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004511TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004512 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4513 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004514TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004515 "_EJ0", /* 770x */
4516 ); /* all others */
4517
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004518static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004520 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4521
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004522 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004523 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004524 TPACPI_ACPIHANDLE_INIT(bay_ej);
4525 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004526 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004527 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004528
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004529 tp_features.bay_status = bay_handle &&
4530 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4531 tp_features.bay_status2 = bay2_handle &&
4532 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004533
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004534 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4535 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4536 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4537 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004539 vdbg_printk(TPACPI_DBG_INIT,
4540 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004541 str_supported(tp_features.bay_status),
4542 str_supported(tp_features.bay_eject),
4543 str_supported(tp_features.bay_status2),
4544 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004545
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004546 return (tp_features.bay_status || tp_features.bay_eject ||
4547 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004550static void bay_notify(struct ibm_struct *ibm, u32 event)
4551{
Len Brown14e04fb32007-08-23 15:20:26 -04004552 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004553 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004554 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004555 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004556}
4557
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558#define bay_occupied(b) (_sta(b##_handle) & 1)
4559
4560static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
4562 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563 int occupied = bay_occupied(bay);
4564 int occupied2 = bay_occupied(bay2);
4565 int eject, eject2;
4566
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004567 len += sprintf(p + len, "status:\t\t%s\n",
4568 tp_features.bay_status ?
4569 (occupied ? "occupied" : "unoccupied") :
4570 "not supported");
4571 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4573 "occupied" : "unoccupied");
4574
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004575 eject = tp_features.bay_eject && occupied;
4576 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004577
4578 if (eject && eject2)
4579 len += sprintf(p + len, "commands:\teject, eject2\n");
4580 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004582 else if (eject2)
4583 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 return len;
4586}
4587
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004588static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
4590 char *cmd;
4591
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004592 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004593 return -ENODEV;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004596 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004597 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4598 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004599 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004600 strlencmp(cmd, "eject2") == 0) {
4601 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 return -EIO;
4603 } else
4604 return -EINVAL;
4605 }
4606
4607 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004608}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004609
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004610static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4611 .notify = bay_notify,
4612 .handle = &bay_handle,
4613 .type = ACPI_SYSTEM_NOTIFY,
4614};
4615
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004616static struct ibm_struct bay_driver_data = {
4617 .name = "bay",
4618 .read = bay_read,
4619 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004620 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004621};
4622
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004623#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004625/*************************************************************************
4626 * CMOS subdriver
4627 */
4628
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004629/* sysfs cmos_command -------------------------------------------------- */
4630static ssize_t cmos_command_store(struct device *dev,
4631 struct device_attribute *attr,
4632 const char *buf, size_t count)
4633{
4634 unsigned long cmos_cmd;
4635 int res;
4636
4637 if (parse_strtoul(buf, 21, &cmos_cmd))
4638 return -EINVAL;
4639
4640 res = issue_thinkpad_cmos_command(cmos_cmd);
4641 return (res)? res : count;
4642}
4643
4644static struct device_attribute dev_attr_cmos_command =
4645 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4646
4647/* --------------------------------------------------------------------- */
4648
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004649static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004650{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004651 int res;
4652
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004653 vdbg_printk(TPACPI_DBG_INIT,
4654 "initializing cmos commands subdriver\n");
4655
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004656 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004657
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004658 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4659 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004660
4661 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4662 if (res)
4663 return res;
4664
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004665 return (cmos_handle)? 0 : 1;
4666}
4667
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004668static void cmos_exit(void)
4669{
4670 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4671}
4672
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004673static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
4675 int len = 0;
4676
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004677 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4678 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (!cmos_handle)
4680 len += sprintf(p + len, "status:\t\tnot supported\n");
4681 else {
4682 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004683 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
4685
4686 return len;
4687}
4688
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004689static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
4691 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004692 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004695 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4696 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 /* cmos_cmd set */
4698 } else
4699 return -EINVAL;
4700
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004701 res = issue_thinkpad_cmos_command(cmos_cmd);
4702 if (res)
4703 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 }
4705
4706 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004707}
4708
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004709static struct ibm_struct cmos_driver_data = {
4710 .name = "cmos",
4711 .read = cmos_read,
4712 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004713 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004714};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004715
4716/*************************************************************************
4717 * LED subdriver
4718 */
4719
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004720enum led_access_mode {
4721 TPACPI_LED_NONE = 0,
4722 TPACPI_LED_570, /* 570 */
4723 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4724 TPACPI_LED_NEW, /* all others */
4725};
4726
4727enum { /* For TPACPI_LED_OLD */
4728 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4729 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4730 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4731};
4732
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004733enum led_status_t {
4734 TPACPI_LED_OFF = 0,
4735 TPACPI_LED_ON,
4736 TPACPI_LED_BLINK,
4737};
4738
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004739static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004740
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004741TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004742 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4743 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004744 "LED", /* all others */
4745 ); /* R30, R31 */
4746
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004747#define TPACPI_LED_NUMLEDS 8
4748static struct tpacpi_led_classdev *tpacpi_leds;
4749static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004750static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004751 /* there's a limit of 19 chars + NULL before 2.6.26 */
4752 "tpacpi::power",
4753 "tpacpi:orange:batt",
4754 "tpacpi:green:batt",
4755 "tpacpi::dock_active",
4756 "tpacpi::bay_active",
4757 "tpacpi::dock_batt",
4758 "tpacpi::unknown_led",
4759 "tpacpi::standby",
4760};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004761#define TPACPI_SAFE_LEDS 0x0081U
4762
4763static inline bool tpacpi_is_led_restricted(const unsigned int led)
4764{
4765#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4766 return false;
4767#else
4768 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4769#endif
4770}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004771
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004772static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004773{
4774 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004775 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004776
4777 switch (led_supported) {
4778 case TPACPI_LED_570:
4779 if (!acpi_evalf(ec_handle,
4780 &status, "GLED", "dd", 1 << led))
4781 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004782 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004783 TPACPI_LED_OFF :
4784 ((status == 1)?
4785 TPACPI_LED_ON :
4786 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004787 tpacpi_led_state_cache[led] = led_s;
4788 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004789 default:
4790 return -ENXIO;
4791 }
4792
4793 /* not reached */
4794}
4795
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004796static int led_set_status(const unsigned int led,
4797 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004798{
4799 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004800 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4801 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004802
4803 int rc = 0;
4804
4805 switch (led_supported) {
4806 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004807 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004808 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004809 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004810 if (unlikely(tpacpi_is_led_restricted(led)))
4811 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004812 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4813 (1 << led), led_sled_arg1[ledstatus]))
4814 rc = -EIO;
4815 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004816 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004817 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004818 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004819 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004820 if (unlikely(tpacpi_is_led_restricted(led)))
4821 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004822 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4823 if (rc >= 0)
4824 rc = ec_write(TPACPI_LED_EC_HLBL,
4825 (ledstatus == TPACPI_LED_BLINK) << led);
4826 if (rc >= 0)
4827 rc = ec_write(TPACPI_LED_EC_HLCL,
4828 (ledstatus != TPACPI_LED_OFF) << led);
4829 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004830 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004831 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004832 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4833 return -EINVAL;
4834 if (unlikely(tpacpi_is_led_restricted(led)))
4835 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004836 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4837 led, led_led_arg1[ledstatus]))
4838 rc = -EIO;
4839 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 default:
4841 rc = -ENXIO;
4842 }
4843
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004844 if (!rc)
4845 tpacpi_led_state_cache[led] = ledstatus;
4846
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004847 return rc;
4848}
4849
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004850static void led_sysfs_set_status(unsigned int led,
4851 enum led_brightness brightness)
4852{
4853 led_set_status(led,
4854 (brightness == LED_OFF) ?
4855 TPACPI_LED_OFF :
4856 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4857 TPACPI_LED_BLINK : TPACPI_LED_ON);
4858}
4859
4860static void led_set_status_worker(struct work_struct *work)
4861{
4862 struct tpacpi_led_classdev *data =
4863 container_of(work, struct tpacpi_led_classdev, work);
4864
4865 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4866 led_sysfs_set_status(data->led, data->new_brightness);
4867}
4868
4869static void led_sysfs_set(struct led_classdev *led_cdev,
4870 enum led_brightness brightness)
4871{
4872 struct tpacpi_led_classdev *data = container_of(led_cdev,
4873 struct tpacpi_led_classdev, led_classdev);
4874
4875 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004876 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004877}
4878
4879static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4880 unsigned long *delay_on, unsigned long *delay_off)
4881{
4882 struct tpacpi_led_classdev *data = container_of(led_cdev,
4883 struct tpacpi_led_classdev, led_classdev);
4884
4885 /* Can we choose the flash rate? */
4886 if (*delay_on == 0 && *delay_off == 0) {
4887 /* yes. set them to the hardware blink rate (1 Hz) */
4888 *delay_on = 500; /* ms */
4889 *delay_off = 500; /* ms */
4890 } else if ((*delay_on != 500) || (*delay_off != 500))
4891 return -EINVAL;
4892
4893 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004894 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004895
4896 return 0;
4897}
4898
4899static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4900{
4901 int rc;
4902
4903 struct tpacpi_led_classdev *data = container_of(led_cdev,
4904 struct tpacpi_led_classdev, led_classdev);
4905
4906 rc = led_get_status(data->led);
4907
4908 if (rc == TPACPI_LED_OFF || rc < 0)
4909 rc = LED_OFF; /* no error handling in led class :( */
4910 else
4911 rc = LED_FULL;
4912
4913 return rc;
4914}
4915
4916static void led_exit(void)
4917{
4918 unsigned int i;
4919
4920 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4921 if (tpacpi_leds[i].led_classdev.name)
4922 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4923 }
4924
4925 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004926}
4927
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004928static int __init tpacpi_init_led(unsigned int led)
4929{
4930 int rc;
4931
4932 tpacpi_leds[led].led = led;
4933
4934 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4935 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4936 if (led_supported == TPACPI_LED_570)
4937 tpacpi_leds[led].led_classdev.brightness_get =
4938 &led_sysfs_get;
4939
4940 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4941
4942 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4943
4944 rc = led_classdev_register(&tpacpi_pdev->dev,
4945 &tpacpi_leds[led].led_classdev);
4946 if (rc < 0)
4947 tpacpi_leds[led].led_classdev.name = NULL;
4948
4949 return rc;
4950}
4951
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004952static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004954 unsigned int i;
4955 int rc;
4956
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004957 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4958
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004959 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004960
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004961 if (!led_handle)
4962 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004963 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004964 else if (strlencmp(led_path, "SLED") == 0)
4965 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004966 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004967 else if (strlencmp(led_path, "SYSL") == 0)
4968 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004969 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004970 else
4971 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004972 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004973
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004974 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4975 str_supported(led_supported), led_supported);
4976
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004977 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4978 GFP_KERNEL);
4979 if (!tpacpi_leds) {
4980 printk(TPACPI_ERR "Out of memory for LED data\n");
4981 return -ENOMEM;
4982 }
4983
4984 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004985 if (!tpacpi_is_led_restricted(i)) {
4986 rc = tpacpi_init_led(i);
4987 if (rc < 0) {
4988 led_exit();
4989 return rc;
4990 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004991 }
4992 }
4993
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004994#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4995 if (led_supported != TPACPI_LED_NONE)
4996 printk(TPACPI_NOTICE
4997 "warning: userspace override of important "
4998 "firmware LEDs is enabled\n");
4999#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005000 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005001}
5002
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005003#define str_led_status(s) \
5004 ((s) == TPACPI_LED_OFF ? "off" : \
5005 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005006
5007static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008{
5009 int len = 0;
5010
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005011 if (!led_supported) {
5012 len += sprintf(p + len, "status:\t\tnot supported\n");
5013 return len;
5014 }
5015 len += sprintf(p + len, "status:\t\tsupported\n");
5016
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005017 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005018 /* 570 */
5019 int i, status;
5020 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005021 status = led_get_status(i);
5022 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005023 return -EIO;
5024 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005025 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005026 }
5027 }
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005030 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
5032 return len;
5033}
5034
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005035static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
5037 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005038 int led, rc;
5039 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005040
5041 if (!led_supported)
5042 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
5044 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005045 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return -EINVAL;
5047
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005048 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005049 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005051 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005052 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005053 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005054 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005055 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005056 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005057
5058 rc = led_set_status(led, s);
5059 if (rc < 0)
5060 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
5062
5063 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005064}
5065
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005066static struct ibm_struct led_driver_data = {
5067 .name = "led",
5068 .read = led_read,
5069 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005070 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005071};
5072
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005073/*************************************************************************
5074 * Beep subdriver
5075 */
5076
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005077TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005078
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005079static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005080{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005081 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5082
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005083 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005084
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005085 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5086 str_supported(beep_handle != NULL));
5087
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005088 return (beep_handle)? 0 : 1;
5089}
5090
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005091static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093 int len = 0;
5094
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005095 if (!beep_handle)
5096 len += sprintf(p + len, "status:\t\tnot supported\n");
5097 else {
5098 len += sprintf(p + len, "status:\t\tsupported\n");
5099 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 return len;
5103}
5104
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005105static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
5107 char *cmd;
5108 int beep_cmd;
5109
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005110 if (!beep_handle)
5111 return -ENODEV;
5112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005114 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5115 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* beep_cmd set */
5117 } else
5118 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005119 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 return -EIO;
5121 }
5122
5123 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005124}
5125
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005126static struct ibm_struct beep_driver_data = {
5127 .name = "beep",
5128 .read = beep_read,
5129 .write = beep_write,
5130};
5131
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005132/*************************************************************************
5133 * Thermal subdriver
5134 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005135
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005136enum thermal_access_mode {
5137 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5138 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5139 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5140 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5141 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5142};
5143
5144enum { /* TPACPI_THERMAL_TPEC_* */
5145 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5146 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5147 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5148};
5149
5150#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5151struct ibm_thermal_sensors_struct {
5152 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5153};
5154
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005155static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005156
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005157/* idx is zero-based */
5158static int thermal_get_sensor(int idx, s32 *value)
5159{
5160 int t;
5161 s8 tmp;
5162 char tmpi[5];
5163
5164 t = TP_EC_THERMAL_TMP0;
5165
5166 switch (thermal_read_mode) {
5167#if TPACPI_MAX_THERMAL_SENSORS >= 16
5168 case TPACPI_THERMAL_TPEC_16:
5169 if (idx >= 8 && idx <= 15) {
5170 t = TP_EC_THERMAL_TMP8;
5171 idx -= 8;
5172 }
5173 /* fallthrough */
5174#endif
5175 case TPACPI_THERMAL_TPEC_8:
5176 if (idx <= 7) {
5177 if (!acpi_ec_read(t + idx, &tmp))
5178 return -EIO;
5179 *value = tmp * 1000;
5180 return 0;
5181 }
5182 break;
5183
5184 case TPACPI_THERMAL_ACPI_UPDT:
5185 if (idx <= 7) {
5186 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5187 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5188 return -EIO;
5189 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5190 return -EIO;
5191 *value = (t - 2732) * 100;
5192 return 0;
5193 }
5194 break;
5195
5196 case TPACPI_THERMAL_ACPI_TMP07:
5197 if (idx <= 7) {
5198 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5199 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5200 return -EIO;
5201 if (t > 127 || t < -127)
5202 t = TP_EC_THERMAL_TMP_NA;
5203 *value = t * 1000;
5204 return 0;
5205 }
5206 break;
5207
5208 case TPACPI_THERMAL_NONE:
5209 default:
5210 return -ENOSYS;
5211 }
5212
5213 return -EINVAL;
5214}
5215
5216static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5217{
5218 int res, i;
5219 int n;
5220
5221 n = 8;
5222 i = 0;
5223
5224 if (!s)
5225 return -EINVAL;
5226
5227 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5228 n = 16;
5229
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005230 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005231 res = thermal_get_sensor(i, &s->temp[i]);
5232 if (res)
5233 return res;
5234 }
5235
5236 return n;
5237}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005238
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005239/* sysfs temp##_input -------------------------------------------------- */
5240
5241static ssize_t thermal_temp_input_show(struct device *dev,
5242 struct device_attribute *attr,
5243 char *buf)
5244{
5245 struct sensor_device_attribute *sensor_attr =
5246 to_sensor_dev_attr(attr);
5247 int idx = sensor_attr->index;
5248 s32 value;
5249 int res;
5250
5251 res = thermal_get_sensor(idx, &value);
5252 if (res)
5253 return res;
5254 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5255 return -ENXIO;
5256
5257 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5258}
5259
5260#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005261 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5262 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005263
5264static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5265 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5266 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5267 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5268 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5269 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5270 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5271 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5272 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5273 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5274 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5275 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5276 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5277 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5278 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5279 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5280 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5281};
5282
5283#define THERMAL_ATTRS(X) \
5284 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5285
5286static struct attribute *thermal_temp_input_attr[] = {
5287 THERMAL_ATTRS(8),
5288 THERMAL_ATTRS(9),
5289 THERMAL_ATTRS(10),
5290 THERMAL_ATTRS(11),
5291 THERMAL_ATTRS(12),
5292 THERMAL_ATTRS(13),
5293 THERMAL_ATTRS(14),
5294 THERMAL_ATTRS(15),
5295 THERMAL_ATTRS(0),
5296 THERMAL_ATTRS(1),
5297 THERMAL_ATTRS(2),
5298 THERMAL_ATTRS(3),
5299 THERMAL_ATTRS(4),
5300 THERMAL_ATTRS(5),
5301 THERMAL_ATTRS(6),
5302 THERMAL_ATTRS(7),
5303 NULL
5304};
5305
5306static const struct attribute_group thermal_temp_input16_group = {
5307 .attrs = thermal_temp_input_attr
5308};
5309
5310static const struct attribute_group thermal_temp_input8_group = {
5311 .attrs = &thermal_temp_input_attr[8]
5312};
5313
5314#undef THERMAL_SENSOR_ATTR_TEMP
5315#undef THERMAL_ATTRS
5316
5317/* --------------------------------------------------------------------- */
5318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005319static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005320{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005321 u8 t, ta1, ta2;
5322 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005323 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005324 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005325
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005326 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5327
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005328 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005329
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005330 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005331 /*
5332 * Direct EC access mode: sensors at registers
5333 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5334 * non-implemented, thermal sensors return 0x80 when
5335 * not available
5336 */
5337
5338 ta1 = ta2 = 0;
5339 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005340 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005341 ta1 |= t;
5342 } else {
5343 ta1 = 0;
5344 break;
5345 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005346 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005347 ta2 |= t;
5348 } else {
5349 ta1 = 0;
5350 break;
5351 }
5352 }
5353 if (ta1 == 0) {
5354 /* This is sheer paranoia, but we handle it anyway */
5355 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005356 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005357 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005358 "falling back to ACPI TMPx access "
5359 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005360 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005361 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005362 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005363 "ThinkPad ACPI EC access misbehaving, "
5364 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005365 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005366 }
5367 } else {
5368 thermal_read_mode =
5369 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005370 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005371 }
5372 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005373 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5374 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005375 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005376 } else {
5377 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005378 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005379 }
5380 } else {
5381 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005382 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005383 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005384
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005385 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5386 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5387 thermal_read_mode);
5388
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005389 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005390 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005391 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005392 &thermal_temp_input16_group);
5393 if (res)
5394 return res;
5395 break;
5396 case TPACPI_THERMAL_TPEC_8:
5397 case TPACPI_THERMAL_ACPI_TMP07:
5398 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005399 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005400 &thermal_temp_input8_group);
5401 if (res)
5402 return res;
5403 break;
5404 case TPACPI_THERMAL_NONE:
5405 default:
5406 return 1;
5407 }
5408
5409 return 0;
5410}
5411
5412static void thermal_exit(void)
5413{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005414 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005415 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005416 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005417 &thermal_temp_input16_group);
5418 break;
5419 case TPACPI_THERMAL_TPEC_8:
5420 case TPACPI_THERMAL_ACPI_TMP07:
5421 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005422 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005423 &thermal_temp_input16_group);
5424 break;
5425 case TPACPI_THERMAL_NONE:
5426 default:
5427 break;
5428 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005429}
5430
5431static int thermal_read(char *p)
5432{
5433 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005434 int n, i;
5435 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005436
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005437 n = thermal_get_sensors(&t);
5438 if (unlikely(n < 0))
5439 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005440
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005441 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005442
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005443 if (n > 0) {
5444 for (i = 0; i < (n - 1); i++)
5445 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5446 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5447 } else
5448 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005449
5450 return len;
5451}
5452
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005453static struct ibm_struct thermal_driver_data = {
5454 .name = "thermal",
5455 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005456 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005457};
5458
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005459/*************************************************************************
5460 * EC Dump subdriver
5461 */
5462
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005463static u8 ecdump_regs[256];
5464
5465static int ecdump_read(char *p)
5466{
5467 int len = 0;
5468 int i, j;
5469 u8 v;
5470
5471 len += sprintf(p + len, "EC "
5472 " +00 +01 +02 +03 +04 +05 +06 +07"
5473 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5474 for (i = 0; i < 256; i += 16) {
5475 len += sprintf(p + len, "EC 0x%02x:", i);
5476 for (j = 0; j < 16; j++) {
5477 if (!acpi_ec_read(i + j, &v))
5478 break;
5479 if (v != ecdump_regs[i + j])
5480 len += sprintf(p + len, " *%02x", v);
5481 else
5482 len += sprintf(p + len, " %02x", v);
5483 ecdump_regs[i + j] = v;
5484 }
5485 len += sprintf(p + len, "\n");
5486 if (j != 16)
5487 break;
5488 }
5489
5490 /* These are way too dangerous to advertise openly... */
5491#if 0
5492 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5493 " (<offset> is 00-ff, <value> is 00-ff)\n");
5494 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5495 " (<offset> is 00-ff, <value> is 0-255)\n");
5496#endif
5497 return len;
5498}
5499
5500static int ecdump_write(char *buf)
5501{
5502 char *cmd;
5503 int i, v;
5504
5505 while ((cmd = next_cmd(&buf))) {
5506 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5507 /* i and v set */
5508 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5509 /* i and v set */
5510 } else
5511 return -EINVAL;
5512 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5513 if (!acpi_ec_write(i, v))
5514 return -EIO;
5515 } else
5516 return -EINVAL;
5517 }
5518
5519 return 0;
5520}
5521
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005522static struct ibm_struct ecdump_driver_data = {
5523 .name = "ecdump",
5524 .read = ecdump_read,
5525 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005526 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005527};
5528
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005529/*************************************************************************
5530 * Backlight/brightness subdriver
5531 */
Holger Macht8acb0252006-10-20 14:30:28 -07005532
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005533#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5534
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005535/*
5536 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5537 * CMOS NVRAM byte 0x5E, bits 0-3.
5538 *
5539 * EC HBRV (0x31) has the following layout
5540 * Bit 7: unknown function
5541 * Bit 6: unknown function
5542 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5543 * Bit 4: must be set to zero to avoid problems
5544 * Bit 3-0: backlight brightness level
5545 *
5546 * brightness_get_raw returns status data in the HBRV layout
5547 */
5548
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005549enum {
5550 TP_EC_BACKLIGHT = 0x31,
5551
5552 /* TP_EC_BACKLIGHT bitmasks */
5553 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5554 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5555 TP_EC_BACKLIGHT_MAPSW = 0x20,
5556};
5557
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005558enum tpacpi_brightness_access_mode {
5559 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5560 TPACPI_BRGHT_MODE_EC, /* EC control */
5561 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5562 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5563 TPACPI_BRGHT_MODE_MAX
5564};
5565
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005566static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005567
5568static enum tpacpi_brightness_access_mode brightness_mode =
5569 TPACPI_BRGHT_MODE_MAX;
5570
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005571static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5572
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005573static struct mutex brightness_mutex;
5574
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005575/* NVRAM brightness access,
5576 * call with brightness_mutex held! */
5577static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005578{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005579 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005580
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005581 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5582 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5583 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5584 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005585
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005586 return lnvram;
5587}
5588
5589static void tpacpi_brightness_checkpoint_nvram(void)
5590{
5591 u8 lec = 0;
5592 u8 b_nvram;
5593
5594 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5595 return;
5596
5597 vdbg_printk(TPACPI_DBG_BRGHT,
5598 "trying to checkpoint backlight level to NVRAM...\n");
5599
5600 if (mutex_lock_killable(&brightness_mutex) < 0)
5601 return;
5602
5603 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5604 goto unlock;
5605 lec &= TP_EC_BACKLIGHT_LVLMSK;
5606 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5607
5608 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5609 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5610 /* NVRAM needs update */
5611 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5612 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5613 b_nvram |= lec;
5614 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5615 dbg_printk(TPACPI_DBG_BRGHT,
5616 "updated NVRAM backlight level to %u (0x%02x)\n",
5617 (unsigned int) lec, (unsigned int) b_nvram);
5618 } else
5619 vdbg_printk(TPACPI_DBG_BRGHT,
5620 "NVRAM backlight level already is %u (0x%02x)\n",
5621 (unsigned int) lec, (unsigned int) b_nvram);
5622
5623unlock:
5624 mutex_unlock(&brightness_mutex);
5625}
5626
5627
5628/* call with brightness_mutex held! */
5629static int tpacpi_brightness_get_raw(int *status)
5630{
5631 u8 lec = 0;
5632
5633 switch (brightness_mode) {
5634 case TPACPI_BRGHT_MODE_UCMS_STEP:
5635 *status = tpacpi_brightness_nvram_get();
5636 return 0;
5637 case TPACPI_BRGHT_MODE_EC:
5638 case TPACPI_BRGHT_MODE_ECNVRAM:
5639 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005640 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005641 *status = lec;
5642 return 0;
5643 default:
5644 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005645 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005646}
5647
5648/* call with brightness_mutex held! */
5649/* do NOT call with illegal backlight level value */
5650static int tpacpi_brightness_set_ec(unsigned int value)
5651{
5652 u8 lec = 0;
5653
5654 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5655 return -EIO;
5656
5657 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5658 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5659 (value & TP_EC_BACKLIGHT_LVLMSK))))
5660 return -EIO;
5661
5662 return 0;
5663}
5664
5665/* call with brightness_mutex held! */
5666static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5667{
5668 int cmos_cmd, inc;
5669 unsigned int current_value, i;
5670
5671 current_value = tpacpi_brightness_nvram_get();
5672
5673 if (value == current_value)
5674 return 0;
5675
5676 cmos_cmd = (value > current_value) ?
5677 TP_CMOS_BRIGHTNESS_UP :
5678 TP_CMOS_BRIGHTNESS_DOWN;
5679 inc = (value > current_value) ? 1 : -1;
5680
5681 for (i = current_value; i != value; i += inc)
5682 if (issue_thinkpad_cmos_command(cmos_cmd))
5683 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005684
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005685 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005686}
5687
5688/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005689static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005690{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005691 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005692
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005693 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5694 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005695 return -EINVAL;
5696
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005697 vdbg_printk(TPACPI_DBG_BRGHT,
5698 "set backlight level to %d\n", value);
5699
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005700 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005701 if (res < 0)
5702 return res;
5703
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005704 switch (brightness_mode) {
5705 case TPACPI_BRGHT_MODE_EC:
5706 case TPACPI_BRGHT_MODE_ECNVRAM:
5707 res = tpacpi_brightness_set_ec(value);
5708 break;
5709 case TPACPI_BRGHT_MODE_UCMS_STEP:
5710 res = tpacpi_brightness_set_ucmsstep(value);
5711 break;
5712 default:
5713 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005714 }
5715
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005716 mutex_unlock(&brightness_mutex);
5717 return res;
5718}
5719
5720/* sysfs backlight class ----------------------------------------------- */
5721
5722static int brightness_update_status(struct backlight_device *bd)
5723{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005724 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005725 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5726 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005727 bd->props.brightness : 0;
5728
5729 dbg_printk(TPACPI_DBG_BRGHT,
5730 "backlight: attempt to set level to %d\n",
5731 level);
5732
5733 /* it is the backlight class's job (caller) to handle
5734 * EINTR and other errors properly */
5735 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005736}
Holger Macht8acb0252006-10-20 14:30:28 -07005737
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005738static int brightness_get(struct backlight_device *bd)
5739{
5740 int status, res;
5741
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005742 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005743 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005744 return 0;
5745
5746 res = tpacpi_brightness_get_raw(&status);
5747
5748 mutex_unlock(&brightness_mutex);
5749
5750 if (res < 0)
5751 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005752
5753 return status & TP_EC_BACKLIGHT_LVLMSK;
5754}
5755
Richard Purdie599a52d2007-02-10 23:07:48 +00005756static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005757 .get_brightness = brightness_get,
5758 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005759};
5760
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005761/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005762
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005763static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005764{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005765 int b;
5766
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005767 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5768
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005769 mutex_init(&brightness_mutex);
5770
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005771 /*
5772 * We always attempt to detect acpi support, so as to switch
5773 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5774 * going to publish a backlight interface
5775 */
5776 b = tpacpi_check_std_acpi_brightness_support();
5777 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005778
5779 if (acpi_video_backlight_support()) {
5780 if (brightness_enable > 1) {
5781 printk(TPACPI_NOTICE
5782 "Standard ACPI backlight interface "
5783 "available, not loading native one.\n");
5784 return 1;
5785 } else if (brightness_enable == 1) {
5786 printk(TPACPI_NOTICE
5787 "Backlight control force enabled, even if standard "
5788 "ACPI backlight interface is available\n");
5789 }
5790 } else {
5791 if (brightness_enable > 1) {
5792 printk(TPACPI_NOTICE
5793 "Standard ACPI backlight interface not "
5794 "available, thinkpad_acpi native "
5795 "brightness control enabled\n");
5796 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005797 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005798 }
5799
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005800 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005801 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005802 "brightness support disabled by "
5803 "module parameter\n");
5804 return 1;
5805 }
5806
5807 if (b > 16) {
5808 printk(TPACPI_ERR
5809 "Unsupported brightness interface, "
5810 "please contact %s\n", TPACPI_MAIL);
5811 return 1;
5812 }
5813 if (b == 16)
5814 tp_features.bright_16levels = 1;
5815
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005816 /*
5817 * Check for module parameter bogosity, note that we
5818 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5819 * able to detect "unspecified"
5820 */
5821 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005822 return -EINVAL;
5823
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005824 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5825 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5826 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5827 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5828 /*
5829 * IBM models that define HBRV probably have
5830 * EC-based backlight level control
5831 */
5832 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5833 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5834 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5835 else
5836 /* all other IBM ThinkPads */
5837 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5838 } else
5839 /* All Lenovo ThinkPads */
5840 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5841
5842 dbg_printk(TPACPI_DBG_BRGHT,
5843 "selected brightness_mode=%d\n",
5844 brightness_mode);
5845 }
5846
5847 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005848 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005849
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005850 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005851 printk(TPACPI_INFO
5852 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005853
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005854 ibm_backlight_device = backlight_device_register(
5855 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5856 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005857 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005858 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005859 return PTR_ERR(ibm_backlight_device);
5860 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5862 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005863
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005864 ibm_backlight_device->props.max_brightness =
5865 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005866 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005867 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005868
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005869 return 0;
5870}
5871
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005872static void brightness_suspend(pm_message_t state)
5873{
5874 tpacpi_brightness_checkpoint_nvram();
5875}
5876
5877static void brightness_shutdown(void)
5878{
5879 tpacpi_brightness_checkpoint_nvram();
5880}
5881
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005882static void brightness_exit(void)
5883{
5884 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005885 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005886 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005887 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005888 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005889
5890 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005891}
5892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005893static int brightness_read(char *p)
5894{
5895 int len = 0;
5896 int level;
5897
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005898 level = brightness_get(NULL);
5899 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005900 len += sprintf(p + len, "level:\t\tunreadable\n");
5901 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005902 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005903 len += sprintf(p + len, "commands:\tup, down\n");
5904 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005905 " (<level> is 0-%d)\n",
5906 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005907 }
5908
5909 return len;
5910}
5911
5912static int brightness_write(char *buf)
5913{
5914 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005915 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005916 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005917 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005918
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005919 level = brightness_get(NULL);
5920 if (level < 0)
5921 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005922
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005923 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005924 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005925 if (level < max_level)
5926 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005927 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005928 if (level > 0)
5929 level--;
5930 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5931 level >= 0 && level <= max_level) {
5932 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005933 } else
5934 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005935 }
5936
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005937 tpacpi_disclose_usertask("procfs brightness",
5938 "set level to %d\n", level);
5939
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005940 /*
5941 * Now we know what the final level should be, so we try to set it.
5942 * Doing it this way makes the syscall restartable in case of EINTR
5943 */
5944 rc = brightness_set(level);
5945 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005946}
5947
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005948static struct ibm_struct brightness_driver_data = {
5949 .name = "brightness",
5950 .read = brightness_read,
5951 .write = brightness_write,
5952 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005953 .suspend = brightness_suspend,
5954 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005955};
5956
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005957/*************************************************************************
5958 * Volume subdriver
5959 */
5960
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005961static int volume_offset = 0x30;
5962
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005963static int volume_read(char *p)
5964{
5965 int len = 0;
5966 u8 level;
5967
5968 if (!acpi_ec_read(volume_offset, &level)) {
5969 len += sprintf(p + len, "level:\t\tunreadable\n");
5970 } else {
5971 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5972 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5973 len += sprintf(p + len, "commands:\tup, down, mute\n");
5974 len += sprintf(p + len, "commands:\tlevel <level>"
5975 " (<level> is 0-15)\n");
5976 }
5977
5978 return len;
5979}
5980
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005981static int volume_write(char *buf)
5982{
5983 int cmos_cmd, inc, i;
5984 u8 level, mute;
5985 int new_level, new_mute;
5986 char *cmd;
5987
5988 while ((cmd = next_cmd(&buf))) {
5989 if (!acpi_ec_read(volume_offset, &level))
5990 return -EIO;
5991 new_mute = mute = level & 0x40;
5992 new_level = level = level & 0xf;
5993
5994 if (strlencmp(cmd, "up") == 0) {
5995 if (mute)
5996 new_mute = 0;
5997 else
5998 new_level = level == 15 ? 15 : level + 1;
5999 } else if (strlencmp(cmd, "down") == 0) {
6000 if (mute)
6001 new_mute = 0;
6002 else
6003 new_level = level == 0 ? 0 : level - 1;
6004 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6005 new_level >= 0 && new_level <= 15) {
6006 /* new_level set */
6007 } else if (strlencmp(cmd, "mute") == 0) {
6008 new_mute = 0x40;
6009 } else
6010 return -EINVAL;
6011
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006012 if (new_level != level) {
6013 /* mute doesn't change */
6014
6015 cmos_cmd = (new_level > level) ?
6016 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006017 inc = new_level > level ? 1 : -1;
6018
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006019 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006020 !acpi_ec_write(volume_offset, level)))
6021 return -EIO;
6022
6023 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006024 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006025 !acpi_ec_write(volume_offset, i + inc))
6026 return -EIO;
6027
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006028 if (mute &&
6029 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6030 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006031 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006032 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006033 }
6034
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006035 if (new_mute != mute) {
6036 /* level doesn't change */
6037
6038 cmos_cmd = (new_mute) ?
6039 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006040
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006041 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006042 !acpi_ec_write(volume_offset, level + new_mute))
6043 return -EIO;
6044 }
6045 }
6046
6047 return 0;
6048}
6049
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006050static struct ibm_struct volume_driver_data = {
6051 .name = "volume",
6052 .read = volume_read,
6053 .write = volume_write,
6054};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006055
6056/*************************************************************************
6057 * Fan subdriver
6058 */
6059
6060/*
6061 * FAN ACCESS MODES
6062 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006063 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006064 * ACPI GFAN method: returns fan level
6065 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006066 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006067 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006068 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006069 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006070 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6071 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006072 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6073 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006074 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006075 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006076 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6077 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006078 *
6079 * Fan speed changes of any sort (including those caused by the
6080 * disengaged mode) are usually done slowly by the firmware as the
6081 * maximum ammount of fan duty cycle change per second seems to be
6082 * limited.
6083 *
6084 * Reading is not available if GFAN exists.
6085 * Writing is not available if SFAN exists.
6086 *
6087 * Bits
6088 * 7 automatic mode engaged;
6089 * (default operation mode of the ThinkPad)
6090 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006091 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006092 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006093 * the tachometer while the fan controller ramps up
6094 * the speed (which can take up to a few *minutes*).
6095 * Speeds up fan to 100% duty-cycle, which is far above
6096 * the standard RPM levels. It is not impossible that
6097 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006098 * 5-3 unused in some models. Extra bits for fan level
6099 * in others, but still useless as all values above
6100 * 7 map to the same speed as level 7 in these models.
6101 * 2-0 fan level (0..7 usually)
6102 * 0x00 = stop
6103 * 0x07 = max (set when temperatures critical)
6104 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006105 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006106 *
6107 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6108 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6109 * does so, its initial value is meaningless (0x07).
6110 *
6111 * For firmware bugs, refer to:
6112 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6113 *
6114 * ----
6115 *
6116 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6117 * Main fan tachometer reading (in RPM)
6118 *
6119 * This register is present on all ThinkPads with a new-style EC, and
6120 * it is known not to be present on the A21m/e, and T22, as there is
6121 * something else in offset 0x84 according to the ACPI DSDT. Other
6122 * ThinkPads from this same time period (and earlier) probably lack the
6123 * tachometer as well.
6124 *
Nick Andrew877d0312009-01-26 11:06:57 +01006125 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006126 * was never fixed by IBM to report the EC firmware version string
6127 * probably support the tachometer (like the early X models), so
6128 * detecting it is quite hard. We need more data to know for sure.
6129 *
6130 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6131 * might result.
6132 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006133 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6134 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006135 *
6136 * For firmware bugs, refer to:
6137 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6138 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006139 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006140 * ThinkPad X31, X40, X41. Not available in the X60.
6141 *
6142 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6143 * high speed. ACPI DSDT seems to map these three speeds to levels
6144 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6145 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6146 *
6147 * The speeds are stored on handles
6148 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6149 *
6150 * There are three default speed sets, acessible as handles:
6151 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6152 *
6153 * ACPI DSDT switches which set is in use depending on various
6154 * factors.
6155 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006156 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006157 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6158 * but the ACPI tables just mention level 7.
6159 */
6160
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006161enum { /* Fan control constants */
6162 fan_status_offset = 0x2f, /* EC register 0x2f */
6163 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6164 * 0x84 must be read before 0x85 */
6165
6166 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6167 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6168
6169 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6170};
6171
6172enum fan_status_access_mode {
6173 TPACPI_FAN_NONE = 0, /* No fan status or control */
6174 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6175 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6176};
6177
6178enum fan_control_access_mode {
6179 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6180 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6181 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6182 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6183};
6184
6185enum fan_control_commands {
6186 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6187 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6188 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6189 * and also watchdog cmd */
6190};
6191
6192static int fan_control_allowed;
6193
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006194static enum fan_status_access_mode fan_status_access_mode;
6195static enum fan_control_access_mode fan_control_access_mode;
6196static enum fan_control_commands fan_control_commands;
6197
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006198static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006199static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006200static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006201static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006202
6203static struct mutex fan_mutex;
6204
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006205static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006206static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006207
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006208TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6209TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006210 "\\FSPD", /* 600e/x, 770e, 770x */
6211 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006212TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006213 "JFNS", /* 770x-JL */
6214 ); /* all others */
6215
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006216/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006217 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6218 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6219 * be in auto mode (0x80).
6220 *
6221 * This is corrected by any write to HFSP either by the driver, or
6222 * by the firmware.
6223 *
6224 * We assume 0x07 really means auto mode while this quirk is active,
6225 * as this is far more likely than the ThinkPad being in level 7,
6226 * which is only used by the firmware during thermal emergencies.
6227 */
6228
6229static void fan_quirk1_detect(void)
6230{
6231 /* In some ThinkPads, neither the EC nor the ACPI
6232 * DSDT initialize the HFSP register, and it ends up
6233 * being initially set to 0x07 when it *could* be
6234 * either 0x07 or 0x80.
6235 *
6236 * Enable for TP-1Y (T43), TP-78 (R51e),
6237 * TP-76 (R52), TP-70 (T43, R52), which are known
6238 * to be buggy. */
6239 if (fan_control_initial_status == 0x07) {
6240 switch (thinkpad_id.ec_model) {
6241 case 0x5931: /* TP-1Y */
6242 case 0x3837: /* TP-78 */
6243 case 0x3637: /* TP-76 */
6244 case 0x3037: /* TP-70 */
6245 printk(TPACPI_NOTICE
6246 "fan_init: initial fan status is unknown, "
6247 "assuming it is in auto mode\n");
6248 tp_features.fan_ctrl_status_undef = 1;
6249 ;;
6250 }
6251 }
6252}
6253
6254static void fan_quirk1_handle(u8 *fan_status)
6255{
6256 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6257 if (*fan_status != fan_control_initial_status) {
6258 /* something changed the HFSP regisnter since
6259 * driver init time, so it is not undefined
6260 * anymore */
6261 tp_features.fan_ctrl_status_undef = 0;
6262 } else {
6263 /* Return most likely status. In fact, it
6264 * might be the only possible status */
6265 *fan_status = TP_EC_FAN_AUTO;
6266 }
6267 }
6268}
6269
6270/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006271 * Call with fan_mutex held
6272 */
6273static void fan_update_desired_level(u8 status)
6274{
6275 if ((status &
6276 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6277 if (status > 7)
6278 fan_control_desired_level = 7;
6279 else
6280 fan_control_desired_level = status;
6281 }
6282}
6283
6284static int fan_get_status(u8 *status)
6285{
6286 u8 s;
6287
6288 /* TODO:
6289 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6290
6291 switch (fan_status_access_mode) {
6292 case TPACPI_FAN_RD_ACPI_GFAN:
6293 /* 570, 600e/x, 770e, 770x */
6294
6295 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6296 return -EIO;
6297
6298 if (likely(status))
6299 *status = s & 0x07;
6300
6301 break;
6302
6303 case TPACPI_FAN_RD_TPEC:
6304 /* all except 570, 600e/x, 770e, 770x */
6305 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6306 return -EIO;
6307
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006308 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006309 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006310 fan_quirk1_handle(status);
6311 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006312
6313 break;
6314
6315 default:
6316 return -ENXIO;
6317 }
6318
6319 return 0;
6320}
6321
6322static int fan_get_status_safe(u8 *status)
6323{
6324 int rc;
6325 u8 s;
6326
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006327 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006328 return -ERESTARTSYS;
6329 rc = fan_get_status(&s);
6330 if (!rc)
6331 fan_update_desired_level(s);
6332 mutex_unlock(&fan_mutex);
6333
6334 if (status)
6335 *status = s;
6336
6337 return rc;
6338}
6339
6340static int fan_get_speed(unsigned int *speed)
6341{
6342 u8 hi, lo;
6343
6344 switch (fan_status_access_mode) {
6345 case TPACPI_FAN_RD_TPEC:
6346 /* all except 570, 600e/x, 770e, 770x */
6347 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6348 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6349 return -EIO;
6350
6351 if (likely(speed))
6352 *speed = (hi << 8) | lo;
6353
6354 break;
6355
6356 default:
6357 return -ENXIO;
6358 }
6359
6360 return 0;
6361}
6362
6363static int fan_set_level(int level)
6364{
6365 if (!fan_control_allowed)
6366 return -EPERM;
6367
6368 switch (fan_control_access_mode) {
6369 case TPACPI_FAN_WR_ACPI_SFAN:
6370 if (level >= 0 && level <= 7) {
6371 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6372 return -EIO;
6373 } else
6374 return -EINVAL;
6375 break;
6376
6377 case TPACPI_FAN_WR_ACPI_FANS:
6378 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006379 if (!(level & TP_EC_FAN_AUTO) &&
6380 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006381 ((level < 0) || (level > 7)))
6382 return -EINVAL;
6383
6384 /* safety net should the EC not support AUTO
6385 * or FULLSPEED mode bits and just ignore them */
6386 if (level & TP_EC_FAN_FULLSPEED)
6387 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006388 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006389 level |= 4; /* safety min speed 4 */
6390
6391 if (!acpi_ec_write(fan_status_offset, level))
6392 return -EIO;
6393 else
6394 tp_features.fan_ctrl_status_undef = 0;
6395 break;
6396
6397 default:
6398 return -ENXIO;
6399 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006400
6401 vdbg_printk(TPACPI_DBG_FAN,
6402 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006403 return 0;
6404}
6405
6406static int fan_set_level_safe(int level)
6407{
6408 int rc;
6409
6410 if (!fan_control_allowed)
6411 return -EPERM;
6412
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006413 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006414 return -ERESTARTSYS;
6415
6416 if (level == TPACPI_FAN_LAST_LEVEL)
6417 level = fan_control_desired_level;
6418
6419 rc = fan_set_level(level);
6420 if (!rc)
6421 fan_update_desired_level(level);
6422
6423 mutex_unlock(&fan_mutex);
6424 return rc;
6425}
6426
6427static int fan_set_enable(void)
6428{
6429 u8 s;
6430 int rc;
6431
6432 if (!fan_control_allowed)
6433 return -EPERM;
6434
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006435 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006436 return -ERESTARTSYS;
6437
6438 switch (fan_control_access_mode) {
6439 case TPACPI_FAN_WR_ACPI_FANS:
6440 case TPACPI_FAN_WR_TPEC:
6441 rc = fan_get_status(&s);
6442 if (rc < 0)
6443 break;
6444
6445 /* Don't go out of emergency fan mode */
6446 if (s != 7) {
6447 s &= 0x07;
6448 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6449 }
6450
6451 if (!acpi_ec_write(fan_status_offset, s))
6452 rc = -EIO;
6453 else {
6454 tp_features.fan_ctrl_status_undef = 0;
6455 rc = 0;
6456 }
6457 break;
6458
6459 case TPACPI_FAN_WR_ACPI_SFAN:
6460 rc = fan_get_status(&s);
6461 if (rc < 0)
6462 break;
6463
6464 s &= 0x07;
6465
6466 /* Set fan to at least level 4 */
6467 s |= 4;
6468
6469 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006470 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006471 else
6472 rc = 0;
6473 break;
6474
6475 default:
6476 rc = -ENXIO;
6477 }
6478
6479 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006480
6481 if (!rc)
6482 vdbg_printk(TPACPI_DBG_FAN,
6483 "fan control: set fan control register to 0x%02x\n",
6484 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006485 return rc;
6486}
6487
6488static int fan_set_disable(void)
6489{
6490 int rc;
6491
6492 if (!fan_control_allowed)
6493 return -EPERM;
6494
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006495 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006496 return -ERESTARTSYS;
6497
6498 rc = 0;
6499 switch (fan_control_access_mode) {
6500 case TPACPI_FAN_WR_ACPI_FANS:
6501 case TPACPI_FAN_WR_TPEC:
6502 if (!acpi_ec_write(fan_status_offset, 0x00))
6503 rc = -EIO;
6504 else {
6505 fan_control_desired_level = 0;
6506 tp_features.fan_ctrl_status_undef = 0;
6507 }
6508 break;
6509
6510 case TPACPI_FAN_WR_ACPI_SFAN:
6511 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6512 rc = -EIO;
6513 else
6514 fan_control_desired_level = 0;
6515 break;
6516
6517 default:
6518 rc = -ENXIO;
6519 }
6520
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006521 if (!rc)
6522 vdbg_printk(TPACPI_DBG_FAN,
6523 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006524
6525 mutex_unlock(&fan_mutex);
6526 return rc;
6527}
6528
6529static int fan_set_speed(int speed)
6530{
6531 int rc;
6532
6533 if (!fan_control_allowed)
6534 return -EPERM;
6535
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006536 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006537 return -ERESTARTSYS;
6538
6539 rc = 0;
6540 switch (fan_control_access_mode) {
6541 case TPACPI_FAN_WR_ACPI_FANS:
6542 if (speed >= 0 && speed <= 65535) {
6543 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6544 speed, speed, speed))
6545 rc = -EIO;
6546 } else
6547 rc = -EINVAL;
6548 break;
6549
6550 default:
6551 rc = -ENXIO;
6552 }
6553
6554 mutex_unlock(&fan_mutex);
6555 return rc;
6556}
6557
6558static void fan_watchdog_reset(void)
6559{
6560 static int fan_watchdog_active;
6561
6562 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6563 return;
6564
6565 if (fan_watchdog_active)
6566 cancel_delayed_work(&fan_watchdog_task);
6567
6568 if (fan_watchdog_maxinterval > 0 &&
6569 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6570 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006571 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006572 msecs_to_jiffies(fan_watchdog_maxinterval
6573 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006574 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006575 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006576 "watchdog will not trigger\n");
6577 }
6578 } else
6579 fan_watchdog_active = 0;
6580}
6581
6582static void fan_watchdog_fire(struct work_struct *ignored)
6583{
6584 int rc;
6585
6586 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6587 return;
6588
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006589 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006590 rc = fan_set_enable();
6591 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006592 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006593 "will try again later...\n", -rc);
6594 /* reschedule for later */
6595 fan_watchdog_reset();
6596 }
6597}
6598
6599/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006600 * SYSFS fan layout: hwmon compatible (device)
6601 *
6602 * pwm*_enable:
6603 * 0: "disengaged" mode
6604 * 1: manual mode
6605 * 2: native EC "auto" mode (recommended, hardware default)
6606 *
6607 * pwm*: set speed in manual mode, ignored otherwise.
6608 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6609 * interpolation.
6610 *
6611 * fan*_input: tachometer reading, RPM
6612 *
6613 *
6614 * SYSFS fan layout: extensions
6615 *
6616 * fan_watchdog (driver):
6617 * fan watchdog interval in seconds, 0 disables (default), max 120
6618 */
6619
6620/* sysfs fan pwm1_enable ----------------------------------------------- */
6621static ssize_t fan_pwm1_enable_show(struct device *dev,
6622 struct device_attribute *attr,
6623 char *buf)
6624{
6625 int res, mode;
6626 u8 status;
6627
6628 res = fan_get_status_safe(&status);
6629 if (res)
6630 return res;
6631
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006632 if (status & TP_EC_FAN_FULLSPEED) {
6633 mode = 0;
6634 } else if (status & TP_EC_FAN_AUTO) {
6635 mode = 2;
6636 } else
6637 mode = 1;
6638
6639 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6640}
6641
6642static ssize_t fan_pwm1_enable_store(struct device *dev,
6643 struct device_attribute *attr,
6644 const char *buf, size_t count)
6645{
6646 unsigned long t;
6647 int res, level;
6648
6649 if (parse_strtoul(buf, 2, &t))
6650 return -EINVAL;
6651
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006652 tpacpi_disclose_usertask("hwmon pwm1_enable",
6653 "set fan mode to %lu\n", t);
6654
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006655 switch (t) {
6656 case 0:
6657 level = TP_EC_FAN_FULLSPEED;
6658 break;
6659 case 1:
6660 level = TPACPI_FAN_LAST_LEVEL;
6661 break;
6662 case 2:
6663 level = TP_EC_FAN_AUTO;
6664 break;
6665 case 3:
6666 /* reserved for software-controlled auto mode */
6667 return -ENOSYS;
6668 default:
6669 return -EINVAL;
6670 }
6671
6672 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006673 if (res == -ENXIO)
6674 return -EINVAL;
6675 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006676 return res;
6677
6678 fan_watchdog_reset();
6679
6680 return count;
6681}
6682
6683static struct device_attribute dev_attr_fan_pwm1_enable =
6684 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6685 fan_pwm1_enable_show, fan_pwm1_enable_store);
6686
6687/* sysfs fan pwm1 ------------------------------------------------------ */
6688static ssize_t fan_pwm1_show(struct device *dev,
6689 struct device_attribute *attr,
6690 char *buf)
6691{
6692 int res;
6693 u8 status;
6694
6695 res = fan_get_status_safe(&status);
6696 if (res)
6697 return res;
6698
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006699 if ((status &
6700 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6701 status = fan_control_desired_level;
6702
6703 if (status > 7)
6704 status = 7;
6705
6706 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6707}
6708
6709static ssize_t fan_pwm1_store(struct device *dev,
6710 struct device_attribute *attr,
6711 const char *buf, size_t count)
6712{
6713 unsigned long s;
6714 int rc;
6715 u8 status, newlevel;
6716
6717 if (parse_strtoul(buf, 255, &s))
6718 return -EINVAL;
6719
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006720 tpacpi_disclose_usertask("hwmon pwm1",
6721 "set fan speed to %lu\n", s);
6722
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006723 /* scale down from 0-255 to 0-7 */
6724 newlevel = (s >> 5) & 0x07;
6725
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006726 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006727 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006728
6729 rc = fan_get_status(&status);
6730 if (!rc && (status &
6731 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6732 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006733 if (rc == -ENXIO)
6734 rc = -EINVAL;
6735 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006736 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006737 fan_watchdog_reset();
6738 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006739 }
6740
6741 mutex_unlock(&fan_mutex);
6742 return (rc)? rc : count;
6743}
6744
6745static struct device_attribute dev_attr_fan_pwm1 =
6746 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6747 fan_pwm1_show, fan_pwm1_store);
6748
6749/* sysfs fan fan1_input ------------------------------------------------ */
6750static ssize_t fan_fan1_input_show(struct device *dev,
6751 struct device_attribute *attr,
6752 char *buf)
6753{
6754 int res;
6755 unsigned int speed;
6756
6757 res = fan_get_speed(&speed);
6758 if (res < 0)
6759 return res;
6760
6761 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6762}
6763
6764static struct device_attribute dev_attr_fan_fan1_input =
6765 __ATTR(fan1_input, S_IRUGO,
6766 fan_fan1_input_show, NULL);
6767
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006768/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006769static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6770 char *buf)
6771{
6772 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6773}
6774
6775static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6776 const char *buf, size_t count)
6777{
6778 unsigned long t;
6779
6780 if (parse_strtoul(buf, 120, &t))
6781 return -EINVAL;
6782
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006783 if (!fan_control_allowed)
6784 return -EPERM;
6785
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006786 fan_watchdog_maxinterval = t;
6787 fan_watchdog_reset();
6788
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006789 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6790
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006791 return count;
6792}
6793
6794static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6795 fan_fan_watchdog_show, fan_fan_watchdog_store);
6796
6797/* --------------------------------------------------------------------- */
6798static struct attribute *fan_attributes[] = {
6799 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6800 &dev_attr_fan_fan1_input.attr,
6801 NULL
6802};
6803
6804static const struct attribute_group fan_attr_group = {
6805 .attrs = fan_attributes,
6806};
6807
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006808static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006809{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006810 int rc;
6811
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006812 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6813 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006814
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006815 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006816 fan_status_access_mode = TPACPI_FAN_NONE;
6817 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006818 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006819 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006820 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006821 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006822
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006823 TPACPI_ACPIHANDLE_INIT(fans);
6824 TPACPI_ACPIHANDLE_INIT(gfan);
6825 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006826
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006827 if (gfan_handle) {
6828 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006829 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006830 } else {
6831 /* all other ThinkPads: note that even old-style
6832 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006833 if (likely(acpi_ec_read(fan_status_offset,
6834 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006835 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006836 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006837 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006838 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006839 "ThinkPad ACPI EC access misbehaving, "
6840 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006841 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006842 }
6843 }
6844
6845 if (sfan_handle) {
6846 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006847 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006848 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006849 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006850 } else {
6851 if (!gfan_handle) {
6852 /* gfan without sfan means no fan control */
6853 /* all other models implement TP EC 0x2f control */
6854
6855 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006856 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006857 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006858 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006859 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006860 TPACPI_FAN_CMD_SPEED |
6861 TPACPI_FAN_CMD_LEVEL |
6862 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006863 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006864 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006865 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006866 TPACPI_FAN_CMD_LEVEL |
6867 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006868 }
6869 }
6870 }
6871
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006872 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6873 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006874 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6875 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6876 fan_status_access_mode, fan_control_access_mode);
6877
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006878 /* fan control master switch */
6879 if (!fan_control_allowed) {
6880 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6881 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006882 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006883 "fan control features disabled by parameter\n");
6884 }
6885
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006886 /* update fan_control_desired_level */
6887 if (fan_status_access_mode != TPACPI_FAN_NONE)
6888 fan_get_status_safe(NULL);
6889
6890 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6891 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006892 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006893 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006894 if (rc < 0)
6895 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006896
6897 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6898 &driver_attr_fan_watchdog);
6899 if (rc < 0) {
6900 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6901 &fan_attr_group);
6902 return rc;
6903 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006904 return 0;
6905 } else
6906 return 1;
6907}
6908
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006909static void fan_exit(void)
6910{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006911 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006912 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006913
6914 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006915 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006916 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6917 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006918
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006919 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006920 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006921}
6922
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006923static void fan_suspend(pm_message_t state)
6924{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006925 int rc;
6926
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006927 if (!fan_control_allowed)
6928 return;
6929
6930 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006931 fan_control_resume_level = 0;
6932 rc = fan_get_status_safe(&fan_control_resume_level);
6933 if (rc < 0)
6934 printk(TPACPI_NOTICE
6935 "failed to read fan level for later "
6936 "restore during resume: %d\n", rc);
6937
6938 /* if it is undefined, don't attempt to restore it.
6939 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006940 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006941 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006942}
6943
6944static void fan_resume(void)
6945{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006946 u8 current_level = 7;
6947 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006948 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006949
6950 /* DSDT *always* updates status on resume */
6951 tp_features.fan_ctrl_status_undef = 0;
6952
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006953 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006954 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006955 (fan_get_status_safe(&current_level) < 0))
6956 return;
6957
6958 switch (fan_control_access_mode) {
6959 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006960 /* never decrease fan level */
6961 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006962 break;
6963 case TPACPI_FAN_WR_ACPI_FANS:
6964 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006965 /* never decrease fan level, scale is:
6966 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6967 *
6968 * We expect the firmware to set either 7 or AUTO, but we
6969 * handle FULLSPEED out of paranoia.
6970 *
6971 * So, we can safely only restore FULLSPEED or 7, anything
6972 * else could slow the fan. Restoring AUTO is useless, at
6973 * best that's exactly what the DSDT already set (it is the
6974 * slower it uses).
6975 *
6976 * Always keep in mind that the DSDT *will* have set the
6977 * fans to what the vendor supposes is the best level. We
6978 * muck with it only to speed the fan up.
6979 */
6980 if (fan_control_resume_level != 7 &&
6981 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6982 return;
6983 else
6984 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6985 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006986 break;
6987 default:
6988 return;
6989 }
6990 if (do_set) {
6991 printk(TPACPI_NOTICE
6992 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006993 fan_control_resume_level);
6994 rc = fan_set_level_safe(fan_control_resume_level);
6995 if (rc < 0)
6996 printk(TPACPI_NOTICE
6997 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006998 }
6999}
7000
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007001static int fan_read(char *p)
7002{
7003 int len = 0;
7004 int rc;
7005 u8 status;
7006 unsigned int speed = 0;
7007
7008 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007009 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007010 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007011 rc = fan_get_status_safe(&status);
7012 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007013 return rc;
7014
7015 len += sprintf(p + len, "status:\t\t%s\n"
7016 "level:\t\t%d\n",
7017 (status != 0) ? "enabled" : "disabled", status);
7018 break;
7019
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007020 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007021 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007022 rc = fan_get_status_safe(&status);
7023 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007024 return rc;
7025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007026 len += sprintf(p + len, "status:\t\t%s\n",
7027 (status != 0) ? "enabled" : "disabled");
7028
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007029 rc = fan_get_speed(&speed);
7030 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007031 return rc;
7032
7033 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7034
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007035 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007036 /* Disengaged mode takes precedence */
7037 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007038 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007039 len += sprintf(p + len, "level:\t\tauto\n");
7040 else
7041 len += sprintf(p + len, "level:\t\t%d\n", status);
7042 break;
7043
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007044 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007045 default:
7046 len += sprintf(p + len, "status:\t\tnot supported\n");
7047 }
7048
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007049 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007050 len += sprintf(p + len, "commands:\tlevel <level>");
7051
7052 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007053 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007054 len += sprintf(p + len, " (<level> is 0-7)\n");
7055 break;
7056
7057 default:
7058 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007059 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007060 break;
7061 }
7062 }
7063
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007064 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007065 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007066 "commands:\twatchdog <timeout> (<timeout> "
7067 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007068
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007069 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007070 len += sprintf(p + len, "commands:\tspeed <speed>"
7071 " (<speed> is 0-65535)\n");
7072
7073 return len;
7074}
7075
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007076static int fan_write_cmd_level(const char *cmd, int *rc)
7077{
7078 int level;
7079
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007080 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007081 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007082 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007083 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007084 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007085 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007086 return 0;
7087
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007088 *rc = fan_set_level_safe(level);
7089 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007090 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007091 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007092 else if (!*rc)
7093 tpacpi_disclose_usertask("procfs fan",
7094 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007095
7096 return 1;
7097}
7098
7099static int fan_write_cmd_enable(const char *cmd, int *rc)
7100{
7101 if (strlencmp(cmd, "enable") != 0)
7102 return 0;
7103
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007104 *rc = fan_set_enable();
7105 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007106 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007107 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007108 else if (!*rc)
7109 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007110
7111 return 1;
7112}
7113
7114static int fan_write_cmd_disable(const char *cmd, int *rc)
7115{
7116 if (strlencmp(cmd, "disable") != 0)
7117 return 0;
7118
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007119 *rc = fan_set_disable();
7120 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007121 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007122 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007123 else if (!*rc)
7124 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007125
7126 return 1;
7127}
7128
7129static int fan_write_cmd_speed(const char *cmd, int *rc)
7130{
7131 int speed;
7132
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007133 /* TODO:
7134 * Support speed <low> <medium> <high> ? */
7135
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007136 if (sscanf(cmd, "speed %d", &speed) != 1)
7137 return 0;
7138
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007139 *rc = fan_set_speed(speed);
7140 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007141 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007142 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007143 else if (!*rc)
7144 tpacpi_disclose_usertask("procfs fan",
7145 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007146
7147 return 1;
7148}
7149
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007150static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7151{
7152 int interval;
7153
7154 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7155 return 0;
7156
7157 if (interval < 0 || interval > 120)
7158 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007159 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007160 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007161 tpacpi_disclose_usertask("procfs fan",
7162 "set watchdog timer to %d\n",
7163 interval);
7164 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007165
7166 return 1;
7167}
7168
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007169static int fan_write(char *buf)
7170{
7171 char *cmd;
7172 int rc = 0;
7173
7174 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007175 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007176 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007177 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007178 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007179 fan_write_cmd_disable(cmd, &rc) ||
7180 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007181 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007182 fan_write_cmd_speed(cmd, &rc))
7183 )
7184 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007185 else if (!rc)
7186 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007187 }
7188
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007189 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007190}
7191
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007192static struct ibm_struct fan_driver_data = {
7193 .name = "fan",
7194 .read = fan_read,
7195 .write = fan_write,
7196 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007197 .suspend = fan_suspend,
7198 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007199};
7200
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007201/****************************************************************************
7202 ****************************************************************************
7203 *
7204 * Infrastructure
7205 *
7206 ****************************************************************************
7207 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007209/* sysfs name ---------------------------------------------------------- */
7210static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7211 struct device_attribute *attr,
7212 char *buf)
7213{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007214 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007215}
7216
7217static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7218 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7219
7220/* --------------------------------------------------------------------- */
7221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007222/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007223static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007224
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007225/*
7226 * Module and infrastructure proble, init and exit handling
7227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007229static int force_load;
7230
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007231#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007232static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007233{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007234 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007235
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007236 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007237}
7238#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7239
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007240static void ibm_exit(struct ibm_struct *ibm)
7241{
7242 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7243
7244 list_del_init(&ibm->all_drivers);
7245
7246 if (ibm->flags.acpi_notify_installed) {
7247 dbg_printk(TPACPI_DBG_EXIT,
7248 "%s: acpi_remove_notify_handler\n", ibm->name);
7249 BUG_ON(!ibm->acpi);
7250 acpi_remove_notify_handler(*ibm->acpi->handle,
7251 ibm->acpi->type,
7252 dispatch_acpi_notify);
7253 ibm->flags.acpi_notify_installed = 0;
7254 ibm->flags.acpi_notify_installed = 0;
7255 }
7256
7257 if (ibm->flags.proc_created) {
7258 dbg_printk(TPACPI_DBG_EXIT,
7259 "%s: remove_proc_entry\n", ibm->name);
7260 remove_proc_entry(ibm->name, proc_dir);
7261 ibm->flags.proc_created = 0;
7262 }
7263
7264 if (ibm->flags.acpi_driver_registered) {
7265 dbg_printk(TPACPI_DBG_EXIT,
7266 "%s: acpi_bus_unregister_driver\n", ibm->name);
7267 BUG_ON(!ibm->acpi);
7268 acpi_bus_unregister_driver(ibm->acpi->driver);
7269 kfree(ibm->acpi->driver);
7270 ibm->acpi->driver = NULL;
7271 ibm->flags.acpi_driver_registered = 0;
7272 }
7273
7274 if (ibm->flags.init_called && ibm->exit) {
7275 ibm->exit();
7276 ibm->flags.init_called = 0;
7277 }
7278
7279 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7280}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007281
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007282static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283{
7284 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007285 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 struct proc_dir_entry *entry;
7287
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007288 BUG_ON(ibm == NULL);
7289
7290 INIT_LIST_HEAD(&ibm->all_drivers);
7291
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007292 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 return 0;
7294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007295 dbg_printk(TPACPI_DBG_INIT,
7296 "probing for %s\n", ibm->name);
7297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007298 if (iibm->init) {
7299 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007300 if (ret > 0)
7301 return 0; /* probe failed */
7302 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007304
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007305 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 }
7307
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007308 if (ibm->acpi) {
7309 if (ibm->acpi->hid) {
7310 ret = register_tpacpi_subdriver(ibm);
7311 if (ret)
7312 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007313 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007314
7315 if (ibm->acpi->notify) {
7316 ret = setup_acpi_notify(ibm);
7317 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007318 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007319 ibm->name);
7320 ret = 0;
7321 goto err_out;
7322 }
7323 if (ret < 0)
7324 goto err_out;
7325 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007326 }
7327
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007328 dbg_printk(TPACPI_DBG_INIT,
7329 "%s installed\n", ibm->name);
7330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007331 if (ibm->read) {
7332 entry = create_proc_entry(ibm->name,
7333 S_IFREG | S_IRUGO | S_IWUSR,
7334 proc_dir);
7335 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007336 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007337 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007338 ret = -ENODEV;
7339 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007340 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007341 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007342 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007343 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007344 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007345 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007348 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7349
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007351
7352err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007353 dbg_printk(TPACPI_DBG_INIT,
7354 "%s: at error exit path with result %d\n",
7355 ibm->name, ret);
7356
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007357 ibm_exit(ibm);
7358 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359}
7360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007361/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007363/* returns 0 - probe ok, or < 0 - probe error.
7364 * Probe ok doesn't mean thinkpad found.
7365 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7366static int __must_check __init get_thinkpad_model_data(
7367 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007368{
Jeff Garzik18552562007-10-03 15:15:40 -04007369 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007370 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007371 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007372
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007373 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007374 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007375
7376 memset(tp, 0, sizeof(*tp));
7377
7378 if (dmi_name_in_vendors("IBM"))
7379 tp->vendor = PCI_VENDOR_ID_IBM;
7380 else if (dmi_name_in_vendors("LENOVO"))
7381 tp->vendor = PCI_VENDOR_ID_LENOVO;
7382 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007383 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007384
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007385 s = dmi_get_system_info(DMI_BIOS_VERSION);
7386 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7387 if (s && !tp->bios_version_str)
7388 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007389 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007390 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007391 tp->bios_model = tp->bios_version_str[0]
7392 | (tp->bios_version_str[1] << 8);
7393
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007394 /*
7395 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7396 * X32 or newer, all Z series; Some models must have an
7397 * up-to-date BIOS or they will not be detected.
7398 *
7399 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7400 */
7401 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007402 if (sscanf(dev->name,
7403 "IBM ThinkPad Embedded Controller -[%17c",
7404 ec_fw_string) == 1) {
7405 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7406 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007407
7408 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007409 if (!tp->ec_version_str)
7410 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007411 tp->ec_model = ec_fw_string[0]
7412 | (ec_fw_string[1] << 8);
7413 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007414 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007415 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007416
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007417 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7418 if (s && !strnicmp(s, "ThinkPad", 8)) {
7419 tp->model_str = kstrdup(s, GFP_KERNEL);
7420 if (!tp->model_str)
7421 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007422 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007423
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007424 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7425 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7426 if (s && !tp->nummodel_str)
7427 return -ENOMEM;
7428
7429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430}
7431
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007432static int __init probe_for_thinkpad(void)
7433{
7434 int is_thinkpad;
7435
7436 if (acpi_disabled)
7437 return -ENODEV;
7438
7439 /*
7440 * Non-ancient models have better DMI tagging, but very old models
7441 * don't.
7442 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007443 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007444
7445 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007446 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007447 if (!ec_handle) {
7448 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007449 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007450 "Not yet supported ThinkPad detected!\n");
7451 return -ENODEV;
7452 }
7453
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007454 /*
7455 * Risks a regression on very old machines, but reduces potential
7456 * false positives a damn great deal
7457 */
7458 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007459 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007460
7461 if (!is_thinkpad && !force_load)
7462 return -ENODEV;
7463
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007464 return 0;
7465}
7466
7467
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007468/* Module init, exit, parameters */
7469
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007470static struct ibm_init_struct ibms_init[] __initdata = {
7471 {
7472 .init = thinkpad_acpi_driver_init,
7473 .data = &thinkpad_acpi_driver_data,
7474 },
7475 {
7476 .init = hotkey_init,
7477 .data = &hotkey_driver_data,
7478 },
7479 {
7480 .init = bluetooth_init,
7481 .data = &bluetooth_driver_data,
7482 },
7483 {
7484 .init = wan_init,
7485 .data = &wan_driver_data,
7486 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007487 {
7488 .init = uwb_init,
7489 .data = &uwb_driver_data,
7490 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007491#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007492 {
7493 .init = video_init,
7494 .data = &video_driver_data,
7495 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007496#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007497 {
7498 .init = light_init,
7499 .data = &light_driver_data,
7500 },
7501#ifdef CONFIG_THINKPAD_ACPI_DOCK
7502 {
7503 .init = dock_init,
7504 .data = &dock_driver_data[0],
7505 },
7506 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007507 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007508 .data = &dock_driver_data[1],
7509 },
7510#endif
7511#ifdef CONFIG_THINKPAD_ACPI_BAY
7512 {
7513 .init = bay_init,
7514 .data = &bay_driver_data,
7515 },
7516#endif
7517 {
7518 .init = cmos_init,
7519 .data = &cmos_driver_data,
7520 },
7521 {
7522 .init = led_init,
7523 .data = &led_driver_data,
7524 },
7525 {
7526 .init = beep_init,
7527 .data = &beep_driver_data,
7528 },
7529 {
7530 .init = thermal_init,
7531 .data = &thermal_driver_data,
7532 },
7533 {
7534 .data = &ecdump_driver_data,
7535 },
7536 {
7537 .init = brightness_init,
7538 .data = &brightness_driver_data,
7539 },
7540 {
7541 .data = &volume_driver_data,
7542 },
7543 {
7544 .init = fan_init,
7545 .data = &fan_driver_data,
7546 },
7547};
7548
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007549static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7550{
7551 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007552 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007553
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007554 if (!kp || !kp->name || !val)
7555 return -EINVAL;
7556
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007557 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7558 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007559 WARN_ON(ibm == NULL);
7560
7561 if (!ibm || !ibm->name)
7562 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007563
7564 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7565 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007566 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007567 strcpy(ibms_init[i].param, val);
7568 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007569 return 0;
7570 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007571 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007572
7573 return -EINVAL;
7574}
7575
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007576module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007577MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007578 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007579
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007580module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007581MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007582
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007583module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007584MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007585 "Attempts to load the driver even on a "
7586 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007587
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007588module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007589MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007590 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007591
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007592module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007593MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007594 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007595 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007596
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007597module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007598MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007599 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007600
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007601module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007602MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007603 "used for backwards compatibility with userspace, "
7604 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007605
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007606#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007607 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007608 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007609 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007610
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007611TPACPI_PARAM(hotkey);
7612TPACPI_PARAM(bluetooth);
7613TPACPI_PARAM(video);
7614TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007615#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007616TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007617#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007618#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007619TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007620#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007621TPACPI_PARAM(cmos);
7622TPACPI_PARAM(led);
7623TPACPI_PARAM(beep);
7624TPACPI_PARAM(ecdump);
7625TPACPI_PARAM(brightness);
7626TPACPI_PARAM(volume);
7627TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007628
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007629#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7630module_param(dbg_wlswemul, uint, 0);
7631MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7632module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7633MODULE_PARM_DESC(wlsw_state,
7634 "Initial state of the emulated WLSW switch");
7635
7636module_param(dbg_bluetoothemul, uint, 0);
7637MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7638module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7639MODULE_PARM_DESC(bluetooth_state,
7640 "Initial state of the emulated bluetooth switch");
7641
7642module_param(dbg_wwanemul, uint, 0);
7643MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7644module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7645MODULE_PARM_DESC(wwan_state,
7646 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007647
7648module_param(dbg_uwbemul, uint, 0);
7649MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7650module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7651MODULE_PARM_DESC(uwb_state,
7652 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007653#endif
7654
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007655static void thinkpad_acpi_module_exit(void)
7656{
7657 struct ibm_struct *ibm, *itmp;
7658
7659 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7660
7661 list_for_each_entry_safe_reverse(ibm, itmp,
7662 &tpacpi_all_drivers,
7663 all_drivers) {
7664 ibm_exit(ibm);
7665 }
7666
7667 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7668
7669 if (tpacpi_inputdev) {
7670 if (tp_features.input_device_registered)
7671 input_unregister_device(tpacpi_inputdev);
7672 else
7673 input_free_device(tpacpi_inputdev);
7674 }
7675
7676 if (tpacpi_hwmon)
7677 hwmon_device_unregister(tpacpi_hwmon);
7678
7679 if (tp_features.sensors_pdev_attrs_registered)
7680 device_remove_file(&tpacpi_sensors_pdev->dev,
7681 &dev_attr_thinkpad_acpi_pdev_name);
7682 if (tpacpi_sensors_pdev)
7683 platform_device_unregister(tpacpi_sensors_pdev);
7684 if (tpacpi_pdev)
7685 platform_device_unregister(tpacpi_pdev);
7686
7687 if (tp_features.sensors_pdrv_attrs_registered)
7688 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7689 if (tp_features.platform_drv_attrs_registered)
7690 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7691
7692 if (tp_features.sensors_pdrv_registered)
7693 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7694
7695 if (tp_features.platform_drv_registered)
7696 platform_driver_unregister(&tpacpi_pdriver);
7697
7698 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007699 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007700
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007701 if (tpacpi_wq)
7702 destroy_workqueue(tpacpi_wq);
7703
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007704 kfree(thinkpad_id.bios_version_str);
7705 kfree(thinkpad_id.ec_version_str);
7706 kfree(thinkpad_id.model_str);
7707}
7708
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007709
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007710static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711{
7712 int ret, i;
7713
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007714 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7715
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007716 /* Parameter checking */
7717 if (hotkey_report_mode > 2)
7718 return -EINVAL;
7719
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007720 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007721
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007722 ret = get_thinkpad_model_data(&thinkpad_id);
7723 if (ret) {
7724 printk(TPACPI_ERR
7725 "unable to get DMI data: %d\n", ret);
7726 thinkpad_acpi_module_exit();
7727 return ret;
7728 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007729 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007730 if (ret) {
7731 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007732 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007735 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007736
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007737 TPACPI_ACPIHANDLE_INIT(ecrd);
7738 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007740 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7741 if (!tpacpi_wq) {
7742 thinkpad_acpi_module_exit();
7743 return -ENOMEM;
7744 }
7745
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007746 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007748 printk(TPACPI_ERR
7749 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007750 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 return -ENODEV;
7752 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007753
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007754 ret = platform_driver_register(&tpacpi_pdriver);
7755 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007756 printk(TPACPI_ERR
7757 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007758 thinkpad_acpi_module_exit();
7759 return ret;
7760 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007761 tp_features.platform_drv_registered = 1;
7762
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007763 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7764 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007765 printk(TPACPI_ERR
7766 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007767 thinkpad_acpi_module_exit();
7768 return ret;
7769 }
7770 tp_features.sensors_pdrv_registered = 1;
7771
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007772 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007773 if (!ret) {
7774 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007775 ret = tpacpi_create_driver_attributes(
7776 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007777 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007778 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007779 printk(TPACPI_ERR
7780 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007781 thinkpad_acpi_module_exit();
7782 return ret;
7783 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007784 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007785
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007786
7787 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007788 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007789 NULL, 0);
7790 if (IS_ERR(tpacpi_pdev)) {
7791 ret = PTR_ERR(tpacpi_pdev);
7792 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007793 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007794 thinkpad_acpi_module_exit();
7795 return ret;
7796 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007797 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007798 TPACPI_HWMON_DRVR_NAME,
7799 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007800 if (IS_ERR(tpacpi_sensors_pdev)) {
7801 ret = PTR_ERR(tpacpi_sensors_pdev);
7802 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007803 printk(TPACPI_ERR
7804 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007805 thinkpad_acpi_module_exit();
7806 return ret;
7807 }
7808 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7809 &dev_attr_thinkpad_acpi_pdev_name);
7810 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007811 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007812 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007813 thinkpad_acpi_module_exit();
7814 return ret;
7815 }
7816 tp_features.sensors_pdev_attrs_registered = 1;
7817 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007818 if (IS_ERR(tpacpi_hwmon)) {
7819 ret = PTR_ERR(tpacpi_hwmon);
7820 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007821 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007822 thinkpad_acpi_module_exit();
7823 return ret;
7824 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007825 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007826 tpacpi_inputdev = input_allocate_device();
7827 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007828 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007829 thinkpad_acpi_module_exit();
7830 return -ENOMEM;
7831 } else {
7832 /* Prepare input device, but don't register */
7833 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007834 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007835 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007836 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7837 thinkpad_id.vendor :
7838 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007839 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7840 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7841 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007842 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7843 ret = ibm_init(&ibms_init[i]);
7844 if (ret >= 0 && *ibms_init[i].param)
7845 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007847 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 return ret;
7849 }
7850 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007851 ret = input_register_device(tpacpi_inputdev);
7852 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007853 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007854 thinkpad_acpi_module_exit();
7855 return ret;
7856 } else {
7857 tp_features.input_device_registered = 1;
7858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007860 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 return 0;
7862}
7863
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007864MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7865
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007866/*
7867 * DMI matching for module autoloading
7868 *
7869 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7870 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7871 *
7872 * Only models listed in thinkwiki will be supported, so add yours
7873 * if it is not there yet.
7874 */
7875#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007876 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007877
7878/* Non-ancient thinkpads */
7879MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7880MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7881
7882/* Ancient thinkpad BIOSes have to be identified by
7883 * BIOS type or model number, and there are far less
7884 * BIOS types than model numbers... */
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007885IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7886IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7887IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007888
7889MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007890MODULE_DESCRIPTION(TPACPI_DESC);
7891MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007892MODULE_LICENSE("GPL");
7893
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007894module_init(thinkpad_acpi_module_init);
7895module_exit(thinkpad_acpi_module_exit);