blob: 38c34c79ff35f54dca2235af448f52f80587c688 [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 Holschuh0c780392008-01-08 13:02:43 -0200192
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200193#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
194#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
195#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200196
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197
198/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200199 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200 */
201
202struct ibm_struct;
203
204struct tp_acpi_drv_struct {
205 const struct acpi_device_id *hid;
206 struct acpi_driver *driver;
207
208 void (*notify) (struct ibm_struct *, u32);
209 acpi_handle *handle;
210 u32 type;
211 struct acpi_device *device;
212};
213
214struct ibm_struct {
215 char *name;
216
217 int (*read) (char *);
218 int (*write) (char *);
219 void (*exit) (void);
220 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200221 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200222 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200223
224 struct list_head all_drivers;
225
226 struct tp_acpi_drv_struct *acpi;
227
228 struct {
229 u8 acpi_driver_registered:1;
230 u8 acpi_notify_installed:1;
231 u8 proc_created:1;
232 u8 init_called:1;
233 u8 experimental:1;
234 } flags;
235};
236
237struct ibm_init_struct {
238 char param[32];
239
240 int (*init) (struct ibm_init_struct *);
241 struct ibm_struct *data;
242};
243
244static struct {
245#ifdef CONFIG_THINKPAD_ACPI_BAY
246 u32 bay_status:1;
247 u32 bay_eject:1;
248 u32 bay_status2:1;
249 u32 bay_eject2:1;
250#endif
251 u32 bluetooth:1;
252 u32 hotkey:1;
253 u32 hotkey_mask:1;
254 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200255 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256 u32 light:1;
257 u32 light_status:1;
258 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300259 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200261 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200262 u32 fan_ctrl_status_undef:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270} tp_features;
271
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300272static struct {
273 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300274 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300275} tp_warned;
276
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277struct thinkpad_id_data {
278 unsigned int vendor; /* ThinkPad vendor:
279 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
280
281 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
282 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
283
284 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
285 u16 ec_model;
286
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300287 char *model_str; /* ThinkPad T43 */
288 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200289};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290static struct thinkpad_id_data thinkpad_id;
291
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300292static enum {
293 TPACPI_LIFE_INIT = 0,
294 TPACPI_LIFE_RUNNING,
295 TPACPI_LIFE_EXITING,
296} tpacpi_lifecycle;
297
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200298static int experimental;
299static u32 dbg_level;
300
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300301static struct workqueue_struct *tpacpi_wq;
302
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300303/* Special LED class that can defer work */
304struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
307 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300308 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300309};
310
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200311#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312static int dbg_wlswemul;
313static int tpacpi_wlsw_emulstate;
314static int dbg_bluetoothemul;
315static int tpacpi_bluetooth_emulstate;
316static int dbg_wwanemul;
317static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200318static int dbg_uwbemul;
319static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200320#endif
321
322
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000323/*************************************************************************
324 * Debugging helpers
325 */
326
327#define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
331
332#ifdef CONFIG_THINKPAD_ACPI_DEBUG
333#define vdbg_printk dbg_printk
334static const char *str_supported(int is_supported);
335#else
336#define vdbg_printk(a_dbg_level, format, arg...) \
337 do { } while (0)
338#endif
339
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000340static void tpacpi_log_usertask(const char * const what)
341{
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
344}
345
346#define tpacpi_disclose_usertask(what, format, arg...) \
347 do { \
348 if (unlikely( \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
353 } \
354 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000355
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300356/****************************************************************************
357 ****************************************************************************
358 *
359 * ACPI Helpers and device model
360 *
361 ****************************************************************************
362 ****************************************************************************/
363
364/*************************************************************************
365 * ACPI basic handles
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300368static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200370#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 static acpi_handle object##_handle; \
372 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400373 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 static char *object##_paths[] = { paths }
375
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200376TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400377 "\\_SB.PCI.ISA.EC", /* 570 */
378 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
379 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
380 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
381 "\\_SB.PCI0.ICH3.EC0", /* R31 */
382 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300383 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200385TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
386TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200388TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
389 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400390 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
391 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300392 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400393
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200394TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 "^HKEY", /* R30, R31 */
396 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300397 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400398
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200399TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
400 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
401 "\\_SB.PCI0.VID0", /* 770e */
402 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
403 "\\_SB.PCI0.AGP.VID", /* all others */
404 ); /* R30, R31 */
405
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400406
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407/*************************************************************************
408 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200409 */
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static int acpi_evalf(acpi_handle handle,
412 void *res, char *method, char *fmt, ...)
413{
414 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400415 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200416 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400417 struct acpi_buffer result, *resultp;
418 union acpi_object out_obj;
419 acpi_status status;
420 va_list ap;
421 char res_type;
422 int success;
423 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200426 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428 }
429
430 if (*fmt == 'q') {
431 quiet = 1;
432 fmt++;
433 } else
434 quiet = 0;
435
436 res_type = *(fmt++);
437
438 params.count = 0;
439 params.pointer = &in_objs[0];
440
441 va_start(ap, fmt);
442 while (*fmt) {
443 char c = *(fmt++);
444 switch (c) {
445 case 'd': /* int */
446 in_objs[params.count].integer.value = va_arg(ap, int);
447 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
448 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400449 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200451 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 "with invalid format character '%c'\n", c);
453 return 0;
454 }
455 }
456 va_end(ap);
457
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400458 if (res_type != 'v') {
459 result.length = sizeof(out_obj);
460 result.pointer = &out_obj;
461 resultp = &result;
462 } else
463 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400468 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (res)
470 *(int *)res = out_obj.integer.value;
471 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
472 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 success = status == AE_OK;
475 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400476 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200478 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 "with invalid format character '%c'\n", res_type);
480 return 0;
481 }
482
483 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200484 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 method, fmt0, status);
486
487 return success;
488}
489
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200490static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300491{
492 int v;
493
494 if (ecrd_handle) {
495 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
496 return 0;
497 *p = v;
498 } else {
499 if (ec_read(i, p) < 0)
500 return 0;
501 }
502
503 return 1;
504}
505
506static int acpi_ec_write(int i, u8 v)
507{
508 if (ecwr_handle) {
509 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
510 return 0;
511 } else {
512 if (ec_write(i, v) < 0)
513 return 0;
514 }
515
516 return 1;
517}
518
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200519#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520static int _sta(acpi_handle handle)
521{
522 int status;
523
524 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
525 status = 0;
526
527 return status;
528}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200529#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300531static int issue_thinkpad_cmos_command(int cmos_cmd)
532{
533 if (!cmos_handle)
534 return -ENXIO;
535
536 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
537 return -EIO;
538
539 return 0;
540}
541
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542/*************************************************************************
543 * ACPI device model
544 */
545
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200546#define TPACPI_ACPIHANDLE_INIT(object) \
547 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200548 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
549
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300550static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300551 acpi_handle *handle, acpi_handle parent,
552 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300553{
554 int i;
555 acpi_status status;
556
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300557 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
558 name);
559
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300560 for (i = 0; i < num_paths; i++) {
561 status = acpi_get_handle(parent, paths[i], handle);
562 if (ACPI_SUCCESS(status)) {
563 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300564 dbg_printk(TPACPI_DBG_INIT,
565 "Found ACPI handle %s for %s\n",
566 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300567 return;
568 }
569 }
570
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300571 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
572 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300573 *handle = NULL;
574}
575
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300576static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577{
578 struct ibm_struct *ibm = data;
579
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300580 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
581 return;
582
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300583 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300584 return;
585
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300586 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587}
588
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300589static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300590{
591 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300592 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 BUG_ON(!ibm->acpi);
595
596 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597 return 0;
598
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300599 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300600 "setting up ACPI notify for %s\n", ibm->name);
601
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300602 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
603 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200604 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300605 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300606 return -ENODEV;
607 }
608
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700609 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300610 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200611 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300612 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300613
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300614 status = acpi_install_notify_handler(*ibm->acpi->handle,
615 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300616 if (ACPI_FAILURE(status)) {
617 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200618 printk(TPACPI_NOTICE
619 "another device driver is already "
620 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300621 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200622 printk(TPACPI_ERR
623 "acpi_install_notify_handler(%s) failed: %d\n",
624 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300625 }
626 return -ENODEV;
627 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300628 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300629 return 0;
630}
631
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300632static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300633{
634 return 0;
635}
636
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300637static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300638{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300639 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300640
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300641 dbg_printk(TPACPI_DBG_INIT,
642 "registering %s as an ACPI driver\n", ibm->name);
643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300644 BUG_ON(!ibm->acpi);
645
646 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
647 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300648 printk(TPACPI_ERR
649 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300650 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300651 }
652
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200653 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300654 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200655
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300656 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300657
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300658 rc = acpi_bus_register_driver(ibm->acpi->driver);
659 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200660 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200661 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300662 kfree(ibm->acpi->driver);
663 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300664 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300665 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300666
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300667 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668}
669
670
671/****************************************************************************
672 ****************************************************************************
673 *
674 * Procfs Helpers
675 *
676 ****************************************************************************
677 ****************************************************************************/
678
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300679static int dispatch_procfs_read(char *page, char **start, off_t off,
680 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300681{
682 struct ibm_struct *ibm = data;
683 int len;
684
685 if (!ibm || !ibm->read)
686 return -EINVAL;
687
688 len = ibm->read(page);
689 if (len < 0)
690 return len;
691
692 if (len <= off + count)
693 *eof = 1;
694 *start = page + off;
695 len -= off;
696 if (len > count)
697 len = count;
698 if (len < 0)
699 len = 0;
700
701 return len;
702}
703
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300704static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200705 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300706 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300707{
708 struct ibm_struct *ibm = data;
709 char *kernbuf;
710 int ret;
711
712 if (!ibm || !ibm->write)
713 return -EINVAL;
714
715 kernbuf = kmalloc(count + 2, GFP_KERNEL);
716 if (!kernbuf)
717 return -ENOMEM;
718
719 if (copy_from_user(kernbuf, userbuf, count)) {
720 kfree(kernbuf);
721 return -EFAULT;
722 }
723
724 kernbuf[count] = 0;
725 strcat(kernbuf, ",");
726 ret = ibm->write(kernbuf);
727 if (ret == 0)
728 ret = count;
729
730 kfree(kernbuf);
731
732 return ret;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static char *next_cmd(char **cmds)
736{
737 char *start = *cmds;
738 char *end;
739
740 while ((end = strchr(start, ',')) && end == start)
741 start = end + 1;
742
743 if (!end)
744 return NULL;
745
746 *end = 0;
747 *cmds = end + 1;
748 return start;
749}
750
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751
752/****************************************************************************
753 ****************************************************************************
754 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300755 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300756 *
757 ****************************************************************************
758 ****************************************************************************/
759
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300760static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300761static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700762static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300763static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300764static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200765static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300766
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200767static int tpacpi_suspend_handler(struct platform_device *pdev,
768 pm_message_t state)
769{
770 struct ibm_struct *ibm, *itmp;
771
772 list_for_each_entry_safe(ibm, itmp,
773 &tpacpi_all_drivers,
774 all_drivers) {
775 if (ibm->suspend)
776 (ibm->suspend)(state);
777 }
778
779 return 0;
780}
781
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300782static int tpacpi_resume_handler(struct platform_device *pdev)
783{
784 struct ibm_struct *ibm, *itmp;
785
786 list_for_each_entry_safe(ibm, itmp,
787 &tpacpi_all_drivers,
788 all_drivers) {
789 if (ibm->resume)
790 (ibm->resume)();
791 }
792
793 return 0;
794}
795
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200796static void tpacpi_shutdown_handler(struct platform_device *pdev)
797{
798 struct ibm_struct *ibm, *itmp;
799
800 list_for_each_entry_safe(ibm, itmp,
801 &tpacpi_all_drivers,
802 all_drivers) {
803 if (ibm->shutdown)
804 (ibm->shutdown)();
805 }
806}
807
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300808static struct platform_driver tpacpi_pdriver = {
809 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200810 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300811 .owner = THIS_MODULE,
812 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200813 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300814 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200815 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300816};
817
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300818static struct platform_driver tpacpi_hwmon_pdriver = {
819 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200820 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300821 .owner = THIS_MODULE,
822 },
823};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300824
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300825/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300826 * sysfs support helpers
827 */
828
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200829struct attribute_set {
830 unsigned int members, max_members;
831 struct attribute_group group;
832};
833
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300834struct attribute_set_obj {
835 struct attribute_set s;
836 struct attribute *a;
837} __attribute__((packed));
838
839static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200840 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300841{
842 struct attribute_set_obj *sobj;
843
844 if (max_members == 0)
845 return NULL;
846
847 /* Allocates space for implicit NULL at the end too */
848 sobj = kzalloc(sizeof(struct attribute_set_obj) +
849 max_members * sizeof(struct attribute *),
850 GFP_KERNEL);
851 if (!sobj)
852 return NULL;
853 sobj->s.max_members = max_members;
854 sobj->s.group.attrs = &sobj->a;
855 sobj->s.group.name = name;
856
857 return &sobj->s;
858}
859
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200860#define destroy_attr_set(_set) \
861 kfree(_set);
862
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300863/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200864static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300865{
866 if (!s || !attr)
867 return -EINVAL;
868
869 if (s->members >= s->max_members)
870 return -ENOMEM;
871
872 s->group.attrs[s->members] = attr;
873 s->members++;
874
875 return 0;
876}
877
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200878static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300879 struct attribute **attr,
880 unsigned int count)
881{
882 int i, res;
883
884 for (i = 0; i < count; i++) {
885 res = add_to_attr_set(s, attr[i]);
886 if (res)
887 return res;
888 }
889
890 return 0;
891}
892
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200893static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300894{
895 sysfs_remove_group(kobj, &s->group);
896 destroy_attr_set(s);
897}
898
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200899#define register_attr_set_with_sysfs(_attr_set, _kobj) \
900 sysfs_create_group(_kobj, &_attr_set->group)
901
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300902static int parse_strtoul(const char *buf,
903 unsigned long max, unsigned long *value)
904{
905 char *endp;
906
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300907 while (*buf && isspace(*buf))
908 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300909 *value = simple_strtoul(buf, &endp, 0);
910 while (*endp && isspace(*endp))
911 endp++;
912 if (*endp || *value > max)
913 return -EINVAL;
914
915 return 0;
916}
917
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300918static void tpacpi_disable_brightness_delay(void)
919{
920 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
921 printk(TPACPI_NOTICE
922 "ACPI backlight control delay disabled\n");
923}
924
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300925static int __init tpacpi_query_bcl_levels(acpi_handle handle)
926{
927 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
928 union acpi_object *obj;
929 int rc;
930
931 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
932 obj = (union acpi_object *)buffer.pointer;
933 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
934 printk(TPACPI_ERR "Unknown _BCL data, "
935 "please report this to %s\n", TPACPI_MAIL);
936 rc = 0;
937 } else {
938 rc = obj->package.count;
939 }
940 } else {
941 return 0;
942 }
943
944 kfree(buffer.pointer);
945 return rc;
946}
947
948static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
949 u32 lvl, void *context, void **rv)
950{
951 char name[ACPI_PATH_SEGMENT_LENGTH];
952 struct acpi_buffer buffer = { sizeof(name), &name };
953
954 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
955 !strncmp("_BCL", name, sizeof(name) - 1)) {
956 BUG_ON(!rv || !*rv);
957 **(int **)rv = tpacpi_query_bcl_levels(handle);
958 return AE_CTRL_TERMINATE;
959 } else {
960 return AE_OK;
961 }
962}
963
964/*
965 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
966 */
967static int __init tpacpi_check_std_acpi_brightness_support(void)
968{
969 int status;
970 int bcl_levels = 0;
971 void *bcl_ptr = &bcl_levels;
972
973 if (!vid_handle) {
974 TPACPI_ACPIHANDLE_INIT(vid);
975 }
976 if (!vid_handle)
977 return 0;
978
979 /*
980 * Search for a _BCL method, and execute it. This is safe on all
981 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
982 * BIOS in ACPI backlight control mode. We do NOT have to care
983 * about calling the _BCL method in an enabled video device, any
984 * will do for our purposes.
985 */
986
987 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
988 tpacpi_acpi_walk_find_bcl, NULL,
989 &bcl_ptr);
990
991 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
992 tp_features.bright_acpimode = 1;
993 return (bcl_levels - 2);
994 }
995
996 return 0;
997}
998
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300999static int __init tpacpi_new_rfkill(const unsigned int id,
1000 struct rfkill **rfk,
1001 const enum rfkill_type rfktype,
1002 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001003 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001004 int (*toggle_radio)(void *, enum rfkill_state),
1005 int (*get_state)(void *, enum rfkill_state *))
1006{
1007 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001008 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1009
1010 res = get_state(NULL, &initial_state);
1011 if (res < 0) {
1012 printk(TPACPI_ERR
1013 "failed to read initial state for %s, error %d; "
1014 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001015 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001016 /* try to set the initial state as the default for the rfkill
1017 * type, since we ask the firmware to preserve it across S5 in
1018 * NVRAM */
1019 rfkill_set_default(rfktype,
1020 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1021 RFKILL_STATE_UNBLOCKED :
1022 RFKILL_STATE_SOFT_BLOCKED);
1023 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001024
1025 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1026 if (!*rfk) {
1027 printk(TPACPI_ERR
1028 "failed to allocate memory for rfkill class\n");
1029 return -ENOMEM;
1030 }
1031
1032 (*rfk)->name = name;
1033 (*rfk)->get_state = get_state;
1034 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001035 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001036
1037 res = rfkill_register(*rfk);
1038 if (res < 0) {
1039 printk(TPACPI_ERR
1040 "failed to register %s rfkill switch: %d\n",
1041 name, res);
1042 rfkill_free(*rfk);
1043 *rfk = NULL;
1044 return res;
1045 }
1046
1047 return 0;
1048}
1049
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001050static void printk_deprecated_attribute(const char * const what,
1051 const char * const details)
1052{
1053 tpacpi_log_usertask("deprecated sysfs attribute");
1054 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1055 "will be removed. %s\n",
1056 what, details);
1057}
1058
1059static void printk_deprecated_rfkill_attribute(const char * const what)
1060{
1061 printk_deprecated_attribute(what,
1062 "Please switch to generic rfkill before year 2010");
1063}
1064
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001065/*************************************************************************
1066 * thinkpad-acpi driver attributes
1067 */
1068
1069/* interface_version --------------------------------------------------- */
1070static ssize_t tpacpi_driver_interface_version_show(
1071 struct device_driver *drv,
1072 char *buf)
1073{
1074 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1075}
1076
1077static DRIVER_ATTR(interface_version, S_IRUGO,
1078 tpacpi_driver_interface_version_show, NULL);
1079
1080/* debug_level --------------------------------------------------------- */
1081static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1082 char *buf)
1083{
1084 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1085}
1086
1087static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1088 const char *buf, size_t count)
1089{
1090 unsigned long t;
1091
1092 if (parse_strtoul(buf, 0xffff, &t))
1093 return -EINVAL;
1094
1095 dbg_level = t;
1096
1097 return count;
1098}
1099
1100static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1101 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1102
1103/* version ------------------------------------------------------------- */
1104static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1105 char *buf)
1106{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001107 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1108 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001109}
1110
1111static DRIVER_ATTR(version, S_IRUGO,
1112 tpacpi_driver_version_show, NULL);
1113
1114/* --------------------------------------------------------------------- */
1115
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001116#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1117
1118static void tpacpi_send_radiosw_update(void);
1119
1120/* wlsw_emulstate ------------------------------------------------------ */
1121static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1122 char *buf)
1123{
1124 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1125}
1126
1127static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1128 const char *buf, size_t count)
1129{
1130 unsigned long t;
1131
1132 if (parse_strtoul(buf, 1, &t))
1133 return -EINVAL;
1134
1135 if (tpacpi_wlsw_emulstate != t) {
1136 tpacpi_wlsw_emulstate = !!t;
1137 tpacpi_send_radiosw_update();
1138 } else
1139 tpacpi_wlsw_emulstate = !!t;
1140
1141 return count;
1142}
1143
1144static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1145 tpacpi_driver_wlsw_emulstate_show,
1146 tpacpi_driver_wlsw_emulstate_store);
1147
1148/* bluetooth_emulstate ------------------------------------------------- */
1149static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1150 struct device_driver *drv,
1151 char *buf)
1152{
1153 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1154}
1155
1156static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1157 struct device_driver *drv,
1158 const char *buf, size_t count)
1159{
1160 unsigned long t;
1161
1162 if (parse_strtoul(buf, 1, &t))
1163 return -EINVAL;
1164
1165 tpacpi_bluetooth_emulstate = !!t;
1166
1167 return count;
1168}
1169
1170static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1171 tpacpi_driver_bluetooth_emulstate_show,
1172 tpacpi_driver_bluetooth_emulstate_store);
1173
1174/* wwan_emulstate ------------------------------------------------- */
1175static ssize_t tpacpi_driver_wwan_emulstate_show(
1176 struct device_driver *drv,
1177 char *buf)
1178{
1179 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1180}
1181
1182static ssize_t tpacpi_driver_wwan_emulstate_store(
1183 struct device_driver *drv,
1184 const char *buf, size_t count)
1185{
1186 unsigned long t;
1187
1188 if (parse_strtoul(buf, 1, &t))
1189 return -EINVAL;
1190
1191 tpacpi_wwan_emulstate = !!t;
1192
1193 return count;
1194}
1195
1196static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1197 tpacpi_driver_wwan_emulstate_show,
1198 tpacpi_driver_wwan_emulstate_store);
1199
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001200/* uwb_emulstate ------------------------------------------------- */
1201static ssize_t tpacpi_driver_uwb_emulstate_show(
1202 struct device_driver *drv,
1203 char *buf)
1204{
1205 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1206}
1207
1208static ssize_t tpacpi_driver_uwb_emulstate_store(
1209 struct device_driver *drv,
1210 const char *buf, size_t count)
1211{
1212 unsigned long t;
1213
1214 if (parse_strtoul(buf, 1, &t))
1215 return -EINVAL;
1216
1217 tpacpi_uwb_emulstate = !!t;
1218
1219 return count;
1220}
1221
1222static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1223 tpacpi_driver_uwb_emulstate_show,
1224 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001225#endif
1226
1227/* --------------------------------------------------------------------- */
1228
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001229static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001230 &driver_attr_debug_level, &driver_attr_version,
1231 &driver_attr_interface_version,
1232};
1233
1234static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1235{
1236 int i, res;
1237
1238 i = 0;
1239 res = 0;
1240 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1241 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1242 i++;
1243 }
1244
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001245#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1246 if (!res && dbg_wlswemul)
1247 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1248 if (!res && dbg_bluetoothemul)
1249 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1250 if (!res && dbg_wwanemul)
1251 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001252 if (!res && dbg_uwbemul)
1253 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001254#endif
1255
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001256 return res;
1257}
1258
1259static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1260{
1261 int i;
1262
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001263 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001264 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001265
1266#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1267 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1268 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1269 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001270 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001271#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001272}
1273
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001274/****************************************************************************
1275 ****************************************************************************
1276 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001277 * Subdrivers
1278 *
1279 ****************************************************************************
1280 ****************************************************************************/
1281
1282/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001283 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001284 */
1285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001286static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001288 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1289 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001291 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001292 (thinkpad_id.bios_version_str) ?
1293 thinkpad_id.bios_version_str : "unknown",
1294 (thinkpad_id.ec_version_str) ?
1295 thinkpad_id.ec_version_str : "unknown");
1296
1297 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001298 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001299 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1300 "IBM" : ((thinkpad_id.vendor ==
1301 PCI_VENDOR_ID_LENOVO) ?
1302 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001303 thinkpad_id.model_str,
1304 (thinkpad_id.nummodel_str) ?
1305 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001310static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int len = 0;
1313
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001314 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1315 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 return len;
1318}
1319
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001320static struct ibm_struct thinkpad_acpi_driver_data = {
1321 .name = "driver",
1322 .read = thinkpad_acpi_driver_read,
1323};
1324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001325/*************************************************************************
1326 * Hotkey subdriver
1327 */
1328
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001329enum { /* hot key scan codes (derived from ACPI DSDT) */
1330 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1331 TP_ACPI_HOTKEYSCAN_FNF2,
1332 TP_ACPI_HOTKEYSCAN_FNF3,
1333 TP_ACPI_HOTKEYSCAN_FNF4,
1334 TP_ACPI_HOTKEYSCAN_FNF5,
1335 TP_ACPI_HOTKEYSCAN_FNF6,
1336 TP_ACPI_HOTKEYSCAN_FNF7,
1337 TP_ACPI_HOTKEYSCAN_FNF8,
1338 TP_ACPI_HOTKEYSCAN_FNF9,
1339 TP_ACPI_HOTKEYSCAN_FNF10,
1340 TP_ACPI_HOTKEYSCAN_FNF11,
1341 TP_ACPI_HOTKEYSCAN_FNF12,
1342 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1343 TP_ACPI_HOTKEYSCAN_FNINSERT,
1344 TP_ACPI_HOTKEYSCAN_FNDELETE,
1345 TP_ACPI_HOTKEYSCAN_FNHOME,
1346 TP_ACPI_HOTKEYSCAN_FNEND,
1347 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1348 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1349 TP_ACPI_HOTKEYSCAN_FNSPACE,
1350 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1351 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1352 TP_ACPI_HOTKEYSCAN_MUTE,
1353 TP_ACPI_HOTKEYSCAN_THINKPAD,
1354};
1355
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001356enum { /* Keys available through NVRAM polling */
1357 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1358 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1359};
1360
1361enum { /* Positions of some of the keys in hotkey masks */
1362 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1363 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1364 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1365 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1366 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1367 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1368 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1369 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1370 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1371 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1372 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1373};
1374
1375enum { /* NVRAM to ACPI HKEY group map */
1376 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1377 TP_ACPI_HKEY_ZOOM_MASK |
1378 TP_ACPI_HKEY_DISPSWTCH_MASK |
1379 TP_ACPI_HKEY_HIBERNATE_MASK,
1380 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1381 TP_ACPI_HKEY_BRGHTDWN_MASK,
1382 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1383 TP_ACPI_HKEY_VOLDWN_MASK |
1384 TP_ACPI_HKEY_MUTE_MASK,
1385};
1386
1387#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1388struct tp_nvram_state {
1389 u16 thinkpad_toggle:1;
1390 u16 zoom_toggle:1;
1391 u16 display_toggle:1;
1392 u16 thinklight_toggle:1;
1393 u16 hibernate_toggle:1;
1394 u16 displayexp_toggle:1;
1395 u16 display_state:1;
1396 u16 brightness_toggle:1;
1397 u16 volume_toggle:1;
1398 u16 mute:1;
1399
1400 u8 brightness_level;
1401 u8 volume_level;
1402};
1403
1404static struct task_struct *tpacpi_hotkey_task;
1405static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1406static int hotkey_poll_freq = 10; /* Hz */
1407static struct mutex hotkey_thread_mutex;
1408static struct mutex hotkey_thread_data_mutex;
1409static unsigned int hotkey_config_change;
1410
1411#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1412
1413#define hotkey_source_mask 0U
1414
1415#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1416
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001417static struct mutex hotkey_mutex;
1418
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001419static enum { /* Reasons for waking up */
1420 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1421 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1422 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1423} hotkey_wakeup_reason;
1424
1425static int hotkey_autosleep_ack;
1426
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001427static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001428static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001429static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001430static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001431
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001432static unsigned int hotkey_report_mode;
1433
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001434static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001435
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001436static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001437
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001438#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1439#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001440 do { \
1441 mutex_lock(&hotkey_thread_data_mutex); \
1442 hotkey_config_change++; \
1443 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001444#define HOTKEY_CONFIG_CRITICAL_END \
1445 mutex_unlock(&hotkey_thread_data_mutex);
1446#else
1447#define HOTKEY_CONFIG_CRITICAL_START
1448#define HOTKEY_CONFIG_CRITICAL_END
1449#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1450
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001451/* HKEY.MHKG() return bits */
1452#define TP_HOTKEY_TABLET_MASK (1 << 3)
1453
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001454static int hotkey_get_wlsw(int *status)
1455{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001456#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1457 if (dbg_wlswemul) {
1458 *status = !!tpacpi_wlsw_emulstate;
1459 return 0;
1460 }
1461#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001462 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1463 return -EIO;
1464 return 0;
1465}
1466
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001467static int hotkey_get_tablet_mode(int *status)
1468{
1469 int s;
1470
1471 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1472 return -EIO;
1473
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001474 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1475 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001476}
1477
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001478/*
1479 * Call with hotkey_mutex held
1480 */
1481static int hotkey_mask_get(void)
1482{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001483 u32 m = 0;
1484
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001485 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001486 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001487 return -EIO;
1488 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001489 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001490
1491 return 0;
1492}
1493
1494/*
1495 * Call with hotkey_mutex held
1496 */
1497static int hotkey_mask_set(u32 mask)
1498{
1499 int i;
1500 int rc = 0;
1501
1502 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001503 if (!tp_warned.hotkey_mask_ff &&
1504 (mask == 0xffff || mask == 0xffffff ||
1505 mask == 0xffffffff)) {
1506 tp_warned.hotkey_mask_ff = 1;
1507 printk(TPACPI_NOTICE
1508 "setting the hotkey mask to 0x%08x is likely "
1509 "not the best way to go about it\n", mask);
1510 printk(TPACPI_NOTICE
1511 "please consider using the driver defaults, "
1512 "and refer to up-to-date thinkpad-acpi "
1513 "documentation\n");
1514 }
1515
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001516 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 for (i = 0; i < 32; i++) {
1518 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001519 /* enable in firmware mask only keys not in NVRAM
1520 * mode, but enable the key in the cached hotkey_mask
1521 * regardless of mode, or the key will end up
1522 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001523 if (!acpi_evalf(hkey_handle,
1524 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001525 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001526 rc = -EIO;
1527 break;
1528 } else {
1529 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1530 }
1531 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001532 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001533
1534 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001535 if (!hotkey_mask_get() && !rc &&
1536 (hotkey_mask & ~hotkey_source_mask) !=
1537 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001538 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001539 "requested hot key mask 0x%08x, but "
1540 "firmware forced it to 0x%08x\n",
1541 mask, hotkey_mask);
1542 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001543 } else {
1544#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1545 HOTKEY_CONFIG_CRITICAL_START
1546 hotkey_mask = mask & hotkey_source_mask;
1547 HOTKEY_CONFIG_CRITICAL_END
1548 hotkey_mask_get();
1549 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001550 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001551 "requested hot key mask 0x%08x, "
1552 "forced to 0x%08x (NVRAM poll mask is "
1553 "0x%08x): no firmware mask support\n",
1554 mask, hotkey_mask, hotkey_source_mask);
1555 }
1556#else
1557 hotkey_mask_get();
1558 rc = -ENXIO;
1559#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001560 }
1561
1562 return rc;
1563}
1564
1565static int hotkey_status_get(int *status)
1566{
1567 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1568 return -EIO;
1569
1570 return 0;
1571}
1572
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001573static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001574{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001575 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001576 return -EIO;
1577
1578 return 0;
1579}
1580
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001581static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001582{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001583 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001584
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001585 if (tp_features.hotkey_tablet &&
1586 !hotkey_get_tablet_mode(&state)) {
1587 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001588
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001589 input_report_switch(tpacpi_inputdev,
1590 SW_TABLET_MODE, !!state);
1591 input_sync(tpacpi_inputdev);
1592
1593 mutex_unlock(&tpacpi_inputdev_send_mutex);
1594 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001595}
1596
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001597static void tpacpi_input_send_key(unsigned int scancode)
1598{
1599 unsigned int keycode;
1600
1601 keycode = hotkey_keycode_map[scancode];
1602
1603 if (keycode != KEY_RESERVED) {
1604 mutex_lock(&tpacpi_inputdev_send_mutex);
1605
1606 input_report_key(tpacpi_inputdev, keycode, 1);
1607 if (keycode == KEY_UNKNOWN)
1608 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1609 scancode);
1610 input_sync(tpacpi_inputdev);
1611
1612 input_report_key(tpacpi_inputdev, keycode, 0);
1613 if (keycode == KEY_UNKNOWN)
1614 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1615 scancode);
1616 input_sync(tpacpi_inputdev);
1617
1618 mutex_unlock(&tpacpi_inputdev_send_mutex);
1619 }
1620}
1621
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001622#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1623static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1624
1625static void tpacpi_hotkey_send_key(unsigned int scancode)
1626{
1627 tpacpi_input_send_key(scancode);
1628 if (hotkey_report_mode < 2) {
1629 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1630 0x80, 0x1001 + scancode);
1631 }
1632}
1633
1634static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1635{
1636 u8 d;
1637
1638 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1639 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1640 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1641 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1642 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1643 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1644 }
1645 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1646 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1647 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1648 }
1649 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1650 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1651 n->displayexp_toggle =
1652 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1653 }
1654 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1655 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1656 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1657 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1658 n->brightness_toggle =
1659 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1660 }
1661 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1662 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1663 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1664 >> TP_NVRAM_POS_LEVEL_VOLUME;
1665 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1666 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1667 }
1668}
1669
1670#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001671 do { \
1672 if ((mask & (1 << __scancode)) && \
1673 oldn->__member != newn->__member) \
1674 tpacpi_hotkey_send_key(__scancode); \
1675 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001676
1677#define TPACPI_MAY_SEND_KEY(__scancode) \
1678 do { if (mask & (1 << __scancode)) \
1679 tpacpi_hotkey_send_key(__scancode); } while (0)
1680
1681static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001682 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001683 u32 mask)
1684{
1685 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1686 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1687 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1688 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1689
1690 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1691
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1693
1694 /* handle volume */
1695 if (oldn->volume_toggle != newn->volume_toggle) {
1696 if (oldn->mute != newn->mute) {
1697 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1698 }
1699 if (oldn->volume_level > newn->volume_level) {
1700 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1701 } else if (oldn->volume_level < newn->volume_level) {
1702 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1703 } else if (oldn->mute == newn->mute) {
1704 /* repeated key presses that didn't change state */
1705 if (newn->mute) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1707 } else if (newn->volume_level != 0) {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1709 } else {
1710 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1711 }
1712 }
1713 }
1714
1715 /* handle brightness */
1716 if (oldn->brightness_toggle != newn->brightness_toggle) {
1717 if (oldn->brightness_level < newn->brightness_level) {
1718 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1719 } else if (oldn->brightness_level > newn->brightness_level) {
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1721 } else {
1722 /* repeated key presses that didn't change state */
1723 if (newn->brightness_level != 0) {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1725 } else {
1726 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1727 }
1728 }
1729 }
1730}
1731
1732#undef TPACPI_COMPARE_KEY
1733#undef TPACPI_MAY_SEND_KEY
1734
1735static int hotkey_kthread(void *data)
1736{
1737 struct tp_nvram_state s[2];
1738 u32 mask;
1739 unsigned int si, so;
1740 unsigned long t;
1741 unsigned int change_detector, must_reset;
1742
1743 mutex_lock(&hotkey_thread_mutex);
1744
1745 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1746 goto exit;
1747
1748 set_freezable();
1749
1750 so = 0;
1751 si = 1;
1752 t = 0;
1753
1754 /* Initial state for compares */
1755 mutex_lock(&hotkey_thread_data_mutex);
1756 change_detector = hotkey_config_change;
1757 mask = hotkey_source_mask & hotkey_mask;
1758 mutex_unlock(&hotkey_thread_data_mutex);
1759 hotkey_read_nvram(&s[so], mask);
1760
1761 while (!kthread_should_stop() && hotkey_poll_freq) {
1762 if (t == 0)
1763 t = 1000/hotkey_poll_freq;
1764 t = msleep_interruptible(t);
1765 if (unlikely(kthread_should_stop()))
1766 break;
1767 must_reset = try_to_freeze();
1768 if (t > 0 && !must_reset)
1769 continue;
1770
1771 mutex_lock(&hotkey_thread_data_mutex);
1772 if (must_reset || hotkey_config_change != change_detector) {
1773 /* forget old state on thaw or config change */
1774 si = so;
1775 t = 0;
1776 change_detector = hotkey_config_change;
1777 }
1778 mask = hotkey_source_mask & hotkey_mask;
1779 mutex_unlock(&hotkey_thread_data_mutex);
1780
1781 if (likely(mask)) {
1782 hotkey_read_nvram(&s[si], mask);
1783 if (likely(si != so)) {
1784 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001785 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001786 }
1787 }
1788
1789 so = si;
1790 si ^= 1;
1791 }
1792
1793exit:
1794 mutex_unlock(&hotkey_thread_mutex);
1795 return 0;
1796}
1797
1798static void hotkey_poll_stop_sync(void)
1799{
1800 if (tpacpi_hotkey_task) {
1801 if (frozen(tpacpi_hotkey_task) ||
1802 freezing(tpacpi_hotkey_task))
1803 thaw_process(tpacpi_hotkey_task);
1804
1805 kthread_stop(tpacpi_hotkey_task);
1806 tpacpi_hotkey_task = NULL;
1807 mutex_lock(&hotkey_thread_mutex);
1808 /* at this point, the thread did exit */
1809 mutex_unlock(&hotkey_thread_mutex);
1810 }
1811}
1812
1813/* call with hotkey_mutex held */
1814static void hotkey_poll_setup(int may_warn)
1815{
1816 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1817 hotkey_poll_freq > 0 &&
1818 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1819 if (!tpacpi_hotkey_task) {
1820 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001821 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001822 if (IS_ERR(tpacpi_hotkey_task)) {
1823 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001824 printk(TPACPI_ERR
1825 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001826 "for hotkey polling\n");
1827 }
1828 }
1829 } else {
1830 hotkey_poll_stop_sync();
1831 if (may_warn &&
1832 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001833 printk(TPACPI_NOTICE
1834 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001835 "which is currently disabled\n",
1836 hotkey_source_mask);
1837 }
1838 }
1839}
1840
1841static void hotkey_poll_setup_safe(int may_warn)
1842{
1843 mutex_lock(&hotkey_mutex);
1844 hotkey_poll_setup(may_warn);
1845 mutex_unlock(&hotkey_mutex);
1846}
1847
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001848#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1849
1850static void hotkey_poll_setup_safe(int __unused)
1851{
1852}
1853
1854#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1855
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001856static int hotkey_inputdev_open(struct input_dev *dev)
1857{
1858 switch (tpacpi_lifecycle) {
1859 case TPACPI_LIFE_INIT:
1860 /*
1861 * hotkey_init will call hotkey_poll_setup_safe
1862 * at the appropriate moment
1863 */
1864 return 0;
1865 case TPACPI_LIFE_EXITING:
1866 return -EBUSY;
1867 case TPACPI_LIFE_RUNNING:
1868 hotkey_poll_setup_safe(0);
1869 return 0;
1870 }
1871
1872 /* Should only happen if tpacpi_lifecycle is corrupt */
1873 BUG();
1874 return -EBUSY;
1875}
1876
1877static void hotkey_inputdev_close(struct input_dev *dev)
1878{
1879 /* disable hotkey polling when possible */
1880 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1881 hotkey_poll_setup_safe(0);
1882}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001883
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001884/* sysfs hotkey enable ------------------------------------------------- */
1885static ssize_t hotkey_enable_show(struct device *dev,
1886 struct device_attribute *attr,
1887 char *buf)
1888{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001889 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001890
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001891 printk_deprecated_attribute("hotkey_enable",
1892 "Hotkey reporting is always enabled");
1893
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001894 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001895 if (res)
1896 return res;
1897
1898 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1899}
1900
1901static ssize_t hotkey_enable_store(struct device *dev,
1902 struct device_attribute *attr,
1903 const char *buf, size_t count)
1904{
1905 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001906
1907 printk_deprecated_attribute("hotkey_enable",
1908 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001909
1910 if (parse_strtoul(buf, 1, &t))
1911 return -EINVAL;
1912
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001913 if (t == 0)
1914 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001915
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001916 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001917}
1918
1919static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001920 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001921 hotkey_enable_show, hotkey_enable_store);
1922
1923/* sysfs hotkey mask --------------------------------------------------- */
1924static ssize_t hotkey_mask_show(struct device *dev,
1925 struct device_attribute *attr,
1926 char *buf)
1927{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001928 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001929
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001930 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001931 return -ERESTARTSYS;
1932 res = hotkey_mask_get();
1933 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001934
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001935 return (res)?
1936 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001937}
1938
1939static ssize_t hotkey_mask_store(struct device *dev,
1940 struct device_attribute *attr,
1941 const char *buf, size_t count)
1942{
1943 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001944 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001945
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001946 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001947 return -EINVAL;
1948
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001949 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001950 return -ERESTARTSYS;
1951
1952 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001953
1954#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1955 hotkey_poll_setup(1);
1956#endif
1957
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001958 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001959
1960 return (res) ? res : count;
1961}
1962
1963static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001964 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001965 hotkey_mask_show, hotkey_mask_store);
1966
1967/* sysfs hotkey bios_enabled ------------------------------------------- */
1968static ssize_t hotkey_bios_enabled_show(struct device *dev,
1969 struct device_attribute *attr,
1970 char *buf)
1971{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001972 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001973}
1974
1975static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001976 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001977
1978/* sysfs hotkey bios_mask ---------------------------------------------- */
1979static ssize_t hotkey_bios_mask_show(struct device *dev,
1980 struct device_attribute *attr,
1981 char *buf)
1982{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001983 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001984}
1985
1986static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001987 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001988
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001989/* sysfs hotkey all_mask ----------------------------------------------- */
1990static ssize_t hotkey_all_mask_show(struct device *dev,
1991 struct device_attribute *attr,
1992 char *buf)
1993{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001994 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1995 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001996}
1997
1998static struct device_attribute dev_attr_hotkey_all_mask =
1999 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2000
2001/* sysfs hotkey recommended_mask --------------------------------------- */
2002static ssize_t hotkey_recommended_mask_show(struct device *dev,
2003 struct device_attribute *attr,
2004 char *buf)
2005{
2006 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002007 (hotkey_all_mask | hotkey_source_mask)
2008 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002009}
2010
2011static struct device_attribute dev_attr_hotkey_recommended_mask =
2012 __ATTR(hotkey_recommended_mask, S_IRUGO,
2013 hotkey_recommended_mask_show, NULL);
2014
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2016
2017/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2018static ssize_t hotkey_source_mask_show(struct device *dev,
2019 struct device_attribute *attr,
2020 char *buf)
2021{
2022 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2023}
2024
2025static ssize_t hotkey_source_mask_store(struct device *dev,
2026 struct device_attribute *attr,
2027 const char *buf, size_t count)
2028{
2029 unsigned long t;
2030
2031 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2032 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2033 return -EINVAL;
2034
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002035 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002036 return -ERESTARTSYS;
2037
2038 HOTKEY_CONFIG_CRITICAL_START
2039 hotkey_source_mask = t;
2040 HOTKEY_CONFIG_CRITICAL_END
2041
2042 hotkey_poll_setup(1);
2043
2044 mutex_unlock(&hotkey_mutex);
2045
2046 return count;
2047}
2048
2049static struct device_attribute dev_attr_hotkey_source_mask =
2050 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2051 hotkey_source_mask_show, hotkey_source_mask_store);
2052
2053/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2054static ssize_t hotkey_poll_freq_show(struct device *dev,
2055 struct device_attribute *attr,
2056 char *buf)
2057{
2058 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2059}
2060
2061static ssize_t hotkey_poll_freq_store(struct device *dev,
2062 struct device_attribute *attr,
2063 const char *buf, size_t count)
2064{
2065 unsigned long t;
2066
2067 if (parse_strtoul(buf, 25, &t))
2068 return -EINVAL;
2069
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002070 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002071 return -ERESTARTSYS;
2072
2073 hotkey_poll_freq = t;
2074
2075 hotkey_poll_setup(1);
2076 mutex_unlock(&hotkey_mutex);
2077
2078 return count;
2079}
2080
2081static struct device_attribute dev_attr_hotkey_poll_freq =
2082 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2083 hotkey_poll_freq_show, hotkey_poll_freq_store);
2084
2085#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2086
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002087/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002088static ssize_t hotkey_radio_sw_show(struct device *dev,
2089 struct device_attribute *attr,
2090 char *buf)
2091{
2092 int res, s;
2093 res = hotkey_get_wlsw(&s);
2094 if (res < 0)
2095 return res;
2096
2097 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2098}
2099
2100static struct device_attribute dev_attr_hotkey_radio_sw =
2101 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2102
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002103static void hotkey_radio_sw_notify_change(void)
2104{
2105 if (tp_features.hotkey_wlsw)
2106 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2107 "hotkey_radio_sw");
2108}
2109
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002110/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2111static ssize_t hotkey_tablet_mode_show(struct device *dev,
2112 struct device_attribute *attr,
2113 char *buf)
2114{
2115 int res, s;
2116 res = hotkey_get_tablet_mode(&s);
2117 if (res < 0)
2118 return res;
2119
2120 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2121}
2122
2123static struct device_attribute dev_attr_hotkey_tablet_mode =
2124 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2125
2126static void hotkey_tablet_mode_notify_change(void)
2127{
2128 if (tp_features.hotkey_tablet)
2129 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2130 "hotkey_tablet_mode");
2131}
2132
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002133/* sysfs hotkey report_mode -------------------------------------------- */
2134static ssize_t hotkey_report_mode_show(struct device *dev,
2135 struct device_attribute *attr,
2136 char *buf)
2137{
2138 return snprintf(buf, PAGE_SIZE, "%d\n",
2139 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2140}
2141
2142static struct device_attribute dev_attr_hotkey_report_mode =
2143 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2144
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002145/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002146static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2147 struct device_attribute *attr,
2148 char *buf)
2149{
2150 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2151}
2152
2153static struct device_attribute dev_attr_hotkey_wakeup_reason =
2154 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2155
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002156static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002157{
2158 if (tp_features.hotkey_mask)
2159 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2160 "wakeup_reason");
2161}
2162
2163/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002164static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2165 struct device_attribute *attr,
2166 char *buf)
2167{
2168 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2169}
2170
2171static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2172 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2173 hotkey_wakeup_hotunplug_complete_show, NULL);
2174
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002175static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002176{
2177 if (tp_features.hotkey_mask)
2178 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2179 "wakeup_hotunplug_complete");
2180}
2181
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002182/* --------------------------------------------------------------------- */
2183
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002184static struct attribute *hotkey_attributes[] __initdata = {
2185 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002186 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002187 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002188#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2189 &dev_attr_hotkey_mask.attr,
2190 &dev_attr_hotkey_all_mask.attr,
2191 &dev_attr_hotkey_recommended_mask.attr,
2192 &dev_attr_hotkey_source_mask.attr,
2193 &dev_attr_hotkey_poll_freq.attr,
2194#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002195};
2196
2197static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002198 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002199#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2200 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002201 &dev_attr_hotkey_all_mask.attr,
2202 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002203#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002204 &dev_attr_hotkey_wakeup_reason.attr,
2205 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002206};
2207
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002208static void bluetooth_update_rfk(void);
2209static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002210static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002211static void tpacpi_send_radiosw_update(void)
2212{
2213 int wlsw;
2214
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002215 /* Sync these BEFORE sending any rfkill events */
2216 if (tp_features.bluetooth)
2217 bluetooth_update_rfk();
2218 if (tp_features.wan)
2219 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002220 if (tp_features.uwb)
2221 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002222
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002223 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2224 mutex_lock(&tpacpi_inputdev_send_mutex);
2225
2226 input_report_switch(tpacpi_inputdev,
2227 SW_RFKILL_ALL, !!wlsw);
2228 input_sync(tpacpi_inputdev);
2229
2230 mutex_unlock(&tpacpi_inputdev_send_mutex);
2231 }
2232 hotkey_radio_sw_notify_change();
2233}
2234
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002235static void hotkey_exit(void)
2236{
2237#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2238 hotkey_poll_stop_sync();
2239#endif
2240
2241 if (hotkey_dev_attributes)
2242 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2243
2244 kfree(hotkey_keycode_map);
2245
2246 if (tp_features.hotkey) {
2247 dbg_printk(TPACPI_DBG_EXIT,
2248 "restoring original hot key mask\n");
2249 /* no short-circuit boolean operator below! */
2250 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002251 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002252 printk(TPACPI_ERR
2253 "failed to restore hot key mask "
2254 "to BIOS defaults\n");
2255 }
2256}
2257
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002258static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002259{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002260 /* Requirements for changing the default keymaps:
2261 *
2262 * 1. Many of the keys are mapped to KEY_RESERVED for very
2263 * good reasons. Do not change them unless you have deep
2264 * knowledge on the IBM and Lenovo ThinkPad firmware for
2265 * the various ThinkPad models. The driver behaves
2266 * differently for KEY_RESERVED: such keys have their
2267 * hot key mask *unset* in mask_recommended, and also
2268 * in the initial hot key mask programmed into the
2269 * firmware at driver load time, which means the firm-
2270 * ware may react very differently if you change them to
2271 * something else;
2272 *
2273 * 2. You must be subscribed to the linux-thinkpad and
2274 * ibm-acpi-devel mailing lists, and you should read the
2275 * list archives since 2007 if you want to change the
2276 * keymaps. This requirement exists so that you will
2277 * know the past history of problems with the thinkpad-
2278 * acpi driver keymaps, and also that you will be
2279 * listening to any bug reports;
2280 *
2281 * 3. Do not send thinkpad-acpi specific patches directly to
2282 * for merging, *ever*. Send them to the linux-acpi
2283 * mailinglist for comments. Merging is to be done only
2284 * through acpi-test and the ACPI maintainer.
2285 *
2286 * If the above is too much to ask, don't change the keymap.
2287 * Ask the thinkpad-acpi maintainer to do it, instead.
2288 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002289 static u16 ibm_keycode_map[] __initdata = {
2290 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2291 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2292 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2293 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002294
2295 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002296 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2297 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2298 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002299
2300 /* brightness: firmware always reacts to them, unless
2301 * X.org did some tricks in the radeon BIOS scratch
2302 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002303 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002304 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002305
2306 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002307 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002308
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002309 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2310 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002311
2312 /* Volume: firmware always react to it and reprograms
2313 * the built-in *extra* mixer. Never map it to control
2314 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002315 KEY_RESERVED, /* 0x14: VOLUME UP */
2316 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2317 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002318
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002319 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002320
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002321 /* (assignments unknown, please report if found) */
2322 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2323 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2324 };
2325 static u16 lenovo_keycode_map[] __initdata = {
2326 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2327 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2328 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2329 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002330
2331 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002332 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2333 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2334 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002335
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002336 /* These either have to go through ACPI video, or
2337 * act like in the IBM ThinkPads, so don't ever
2338 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002339 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002340 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002341
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002342 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002343
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002344 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2345 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002346
2347 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2348 * react to it and reprograms the built-in *extra* mixer.
2349 * Never map it to control another mixer by default.
2350 *
2351 * T60?, T61, R60?, R61: firmware and EC tries to send
2352 * these over the regular keyboard, so these are no-ops,
2353 * but there are still weird bugs re. MUTE, so do not
2354 * change unless you get test reports from all Lenovo
2355 * models. May cause the BIOS to interfere with the
2356 * HDA mixer.
2357 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002358 KEY_RESERVED, /* 0x14: VOLUME UP */
2359 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2360 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002361
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002362 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002363
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002364 /* (assignments unknown, please report if found) */
2365 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2366 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2367 };
2368
2369#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2370#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2371#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2372
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002373 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002374 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002375 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002376
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002377 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2378
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002379 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002380 BUG_ON(tpacpi_inputdev->open != NULL ||
2381 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002382
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002383 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002384 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002385
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002386#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2387 mutex_init(&hotkey_thread_mutex);
2388 mutex_init(&hotkey_thread_data_mutex);
2389#endif
2390
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002391 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002392 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002394 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002395 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002396
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002397 if (!tp_features.hotkey)
2398 return 1;
2399
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002400 tpacpi_disable_brightness_delay();
2401
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002402 hotkey_dev_attributes = create_attr_set(13, NULL);
2403 if (!hotkey_dev_attributes)
2404 return -ENOMEM;
2405 res = add_many_to_attr_set(hotkey_dev_attributes,
2406 hotkey_attributes,
2407 ARRAY_SIZE(hotkey_attributes));
2408 if (res)
2409 goto err_exit;
2410
2411 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2412 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2413 for HKEY interface version 0x100 */
2414 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2415 if ((hkeyv >> 8) != 1) {
2416 printk(TPACPI_ERR "unknown version of the "
2417 "HKEY interface: 0x%x\n", hkeyv);
2418 printk(TPACPI_ERR "please report this to %s\n",
2419 TPACPI_MAIL);
2420 } else {
2421 /*
2422 * MHKV 0x100 in A31, R40, R40e,
2423 * T4x, X31, and later
2424 */
2425 tp_features.hotkey_mask = 1;
2426 }
2427 }
2428
2429 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2430 str_supported(tp_features.hotkey_mask));
2431
2432 if (tp_features.hotkey_mask) {
2433 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2434 "MHKA", "qd")) {
2435 printk(TPACPI_ERR
2436 "missing MHKA handler, "
2437 "please report this to %s\n",
2438 TPACPI_MAIL);
2439 /* FN+F12, FN+F4, FN+F3 */
2440 hotkey_all_mask = 0x080cU;
2441 }
2442 }
2443
2444 /* hotkey_source_mask *must* be zero for
2445 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002446 if (tp_features.hotkey_mask) {
2447 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002448 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002449 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002450
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002451 hotkey_orig_mask = hotkey_mask;
2452 res = add_many_to_attr_set(
2453 hotkey_dev_attributes,
2454 hotkey_mask_attributes,
2455 ARRAY_SIZE(hotkey_mask_attributes));
2456 if (res)
2457 goto err_exit;
2458 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002459
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002460#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002461 if (tp_features.hotkey_mask) {
2462 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2463 & ~hotkey_all_mask;
2464 } else {
2465 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2466 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002467
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002468 vdbg_printk(TPACPI_DBG_INIT,
2469 "hotkey source mask 0x%08x, polling freq %d\n",
2470 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471#endif
2472
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002473#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2474 if (dbg_wlswemul) {
2475 tp_features.hotkey_wlsw = 1;
2476 printk(TPACPI_INFO
2477 "radio switch emulation enabled\n");
2478 } else
2479#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002480 /* Not all thinkpads have a hardware radio switch */
2481 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2482 tp_features.hotkey_wlsw = 1;
2483 printk(TPACPI_INFO
2484 "radio switch found; radios are %s\n",
2485 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002486 }
2487 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002488 res = add_to_attr_set(hotkey_dev_attributes,
2489 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002490
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002491 /* For X41t, X60t, X61t Tablets... */
2492 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2493 tp_features.hotkey_tablet = 1;
2494 printk(TPACPI_INFO
2495 "possible tablet mode switch found; "
2496 "ThinkPad in %s mode\n",
2497 (status & TP_HOTKEY_TABLET_MASK)?
2498 "tablet" : "laptop");
2499 res = add_to_attr_set(hotkey_dev_attributes,
2500 &dev_attr_hotkey_tablet_mode.attr);
2501 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002502
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002503 if (!res)
2504 res = register_attr_set_with_sysfs(
2505 hotkey_dev_attributes,
2506 &tpacpi_pdev->dev.kobj);
2507 if (res)
2508 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002509
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002510 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002511
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002512 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2513 GFP_KERNEL);
2514 if (!hotkey_keycode_map) {
2515 printk(TPACPI_ERR
2516 "failed to allocate memory for key map\n");
2517 res = -ENOMEM;
2518 goto err_exit;
2519 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002520
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002521 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2522 dbg_printk(TPACPI_DBG_INIT,
2523 "using Lenovo default hot key map\n");
2524 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2525 TPACPI_HOTKEY_MAP_SIZE);
2526 } else {
2527 dbg_printk(TPACPI_DBG_INIT,
2528 "using IBM default hot key map\n");
2529 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2530 TPACPI_HOTKEY_MAP_SIZE);
2531 }
2532
2533 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2534 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2535 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2536 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2537 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2538 tpacpi_inputdev->keycode = hotkey_keycode_map;
2539 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2540 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2541 set_bit(hotkey_keycode_map[i],
2542 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002543 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002544 if (i < sizeof(hotkey_reserved_mask)*8)
2545 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002546 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002547 }
2548
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002549 if (tp_features.hotkey_wlsw) {
2550 set_bit(EV_SW, tpacpi_inputdev->evbit);
2551 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2552 }
2553 if (tp_features.hotkey_tablet) {
2554 set_bit(EV_SW, tpacpi_inputdev->evbit);
2555 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002556 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002557
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002558 /* Do not issue duplicate brightness change events to
2559 * userspace */
2560 if (!tp_features.bright_acpimode)
2561 /* update bright_acpimode... */
2562 tpacpi_check_std_acpi_brightness_support();
2563
2564 if (tp_features.bright_acpimode) {
2565 printk(TPACPI_INFO
2566 "This ThinkPad has standard ACPI backlight "
2567 "brightness control, supported by the ACPI "
2568 "video driver\n");
2569 printk(TPACPI_NOTICE
2570 "Disabling thinkpad-acpi brightness events "
2571 "by default...\n");
2572
2573 /* The hotkey_reserved_mask change below is not
2574 * necessary while the keys are at KEY_RESERVED in the
2575 * default map, but better safe than sorry, leave it
2576 * here as a marker of what we have to do, especially
2577 * when we finally become able to set this at runtime
2578 * on response to X.org requests */
2579 hotkey_reserved_mask |=
2580 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2581 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002582 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002583
2584 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002585 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002586 if (res) {
2587 hotkey_exit();
2588 return res;
2589 }
2590 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2591 & ~hotkey_reserved_mask)
2592 | hotkey_orig_mask);
2593 if (res < 0 && res != -ENXIO) {
2594 hotkey_exit();
2595 return res;
2596 }
2597
2598 dbg_printk(TPACPI_DBG_INIT,
2599 "legacy hot key reporting over procfs %s\n",
2600 (hotkey_report_mode < 2) ?
2601 "enabled" : "disabled");
2602
2603 tpacpi_inputdev->open = &hotkey_inputdev_open;
2604 tpacpi_inputdev->close = &hotkey_inputdev_close;
2605
2606 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002607 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002608 tpacpi_input_send_tabletsw();
2609
2610 return 0;
2611
2612err_exit:
2613 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2614 hotkey_dev_attributes = NULL;
2615
2616 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002617}
2618
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002619static bool hotkey_notify_hotkey(const u32 hkey,
2620 bool *send_acpi_ev,
2621 bool *ignore_acpi_ev)
2622{
2623 /* 0x1000-0x1FFF: key presses */
2624 unsigned int scancode = hkey & 0xfff;
2625 *send_acpi_ev = true;
2626 *ignore_acpi_ev = false;
2627
2628 if (scancode > 0 && scancode < 0x21) {
2629 scancode--;
2630 if (!(hotkey_source_mask & (1 << scancode))) {
2631 tpacpi_input_send_key(scancode);
2632 *send_acpi_ev = false;
2633 } else {
2634 *ignore_acpi_ev = true;
2635 }
2636 return true;
2637 }
2638 return false;
2639}
2640
2641static bool hotkey_notify_wakeup(const u32 hkey,
2642 bool *send_acpi_ev,
2643 bool *ignore_acpi_ev)
2644{
2645 /* 0x2000-0x2FFF: Wakeup reason */
2646 *send_acpi_ev = true;
2647 *ignore_acpi_ev = false;
2648
2649 switch (hkey) {
2650 case 0x2304: /* suspend, undock */
2651 case 0x2404: /* hibernation, undock */
2652 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2653 *ignore_acpi_ev = true;
2654 break;
2655
2656 case 0x2305: /* suspend, bay eject */
2657 case 0x2405: /* hibernation, bay eject */
2658 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2659 *ignore_acpi_ev = true;
2660 break;
2661
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002662 case 0x2313: /* Battery on critical low level (S3) */
2663 case 0x2413: /* Battery on critical low level (S4) */
2664 printk(TPACPI_ALERT
2665 "EMERGENCY WAKEUP: battery almost empty\n");
2666 /* how to auto-heal: */
2667 /* 2313: woke up from S3, go to S4/S5 */
2668 /* 2413: woke up from S4, go to S5 */
2669 break;
2670
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002671 default:
2672 return false;
2673 }
2674
2675 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2676 printk(TPACPI_INFO
2677 "woke up due to a hot-unplug "
2678 "request...\n");
2679 hotkey_wakeup_reason_notify_change();
2680 }
2681 return true;
2682}
2683
2684static bool hotkey_notify_usrevent(const u32 hkey,
2685 bool *send_acpi_ev,
2686 bool *ignore_acpi_ev)
2687{
2688 /* 0x5000-0x5FFF: human interface helpers */
2689 *send_acpi_ev = true;
2690 *ignore_acpi_ev = false;
2691
2692 switch (hkey) {
2693 case 0x5010: /* Lenovo new BIOS: brightness changed */
2694 case 0x500b: /* X61t: tablet pen inserted into bay */
2695 case 0x500c: /* X61t: tablet pen removed from bay */
2696 return true;
2697
2698 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2699 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2700 tpacpi_input_send_tabletsw();
2701 hotkey_tablet_mode_notify_change();
2702 *send_acpi_ev = false;
2703 return true;
2704
2705 case 0x5001:
2706 case 0x5002:
2707 /* LID switch events. Do not propagate */
2708 *ignore_acpi_ev = true;
2709 return true;
2710
2711 default:
2712 return false;
2713 }
2714}
2715
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002716static bool hotkey_notify_thermal(const u32 hkey,
2717 bool *send_acpi_ev,
2718 bool *ignore_acpi_ev)
2719{
2720 /* 0x6000-0x6FFF: thermal alarms */
2721 *send_acpi_ev = true;
2722 *ignore_acpi_ev = false;
2723
2724 switch (hkey) {
2725 case 0x6011:
2726 printk(TPACPI_CRIT
2727 "THERMAL ALARM: battery is too hot!\n");
2728 /* recommended action: warn user through gui */
2729 return true;
2730 case 0x6012:
2731 printk(TPACPI_ALERT
2732 "THERMAL EMERGENCY: battery is extremely hot!\n");
2733 /* recommended action: immediate sleep/hibernate */
2734 return true;
2735 case 0x6021:
2736 printk(TPACPI_CRIT
2737 "THERMAL ALARM: "
2738 "a sensor reports something is too hot!\n");
2739 /* recommended action: warn user through gui, that */
2740 /* some internal component is too hot */
2741 return true;
2742 case 0x6022:
2743 printk(TPACPI_ALERT
2744 "THERMAL EMERGENCY: "
2745 "a sensor reports something is extremely hot!\n");
2746 /* recommended action: immediate sleep/hibernate */
2747 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002748 case 0x6030:
2749 printk(TPACPI_INFO
2750 "EC reports that Thermal Table has changed\n");
2751 /* recommended action: do nothing, we don't have
2752 * Lenovo ATM information */
2753 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002754 default:
2755 printk(TPACPI_ALERT
2756 "THERMAL ALERT: unknown thermal alarm received\n");
2757 return false;
2758 }
2759}
2760
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002761static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2762{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002763 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002764 bool send_acpi_ev;
2765 bool ignore_acpi_ev;
2766 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002767
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002768 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002769 printk(TPACPI_ERR
2770 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002771 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002772 acpi_bus_generate_netlink_event(
2773 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002774 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002775 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002776 return;
2777 }
2778
2779 while (1) {
2780 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002781 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002782 return;
2783 }
2784
2785 if (hkey == 0) {
2786 /* queue empty */
2787 return;
2788 }
2789
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002790 send_acpi_ev = true;
2791 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002792
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002793 switch (hkey >> 12) {
2794 case 1:
2795 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002796 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2797 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002798 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002799 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002800 /* 0x2000-0x2FFF: Wakeup reason */
2801 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2802 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002803 break;
2804 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002805 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002806 if (hkey == 0x3003) {
2807 hotkey_autosleep_ack = 1;
2808 printk(TPACPI_INFO
2809 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002810 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002811 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002812 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002813 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002814 }
2815 break;
2816 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002817 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002818 if (hkey == 0x4003) {
2819 hotkey_autosleep_ack = 1;
2820 printk(TPACPI_INFO
2821 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002822 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002823 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002824 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002825 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002826 }
2827 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002828 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002829 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002830 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2831 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002832 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002833 case 6:
2834 /* 0x6000-0x6FFF: thermal alarms */
2835 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2836 &ignore_acpi_ev);
2837 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002838 case 7:
2839 /* 0x7000-0x7FFF: misc */
2840 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002841 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002842 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002843 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002844 break;
2845 }
2846 /* fallthrough to default */
2847 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002848 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002849 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002850 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002851 printk(TPACPI_NOTICE
2852 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002853 printk(TPACPI_NOTICE
2854 "please report the conditions when this "
2855 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002856 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002857
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002858 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002859 if (!ignore_acpi_ev &&
2860 (send_acpi_ev || hotkey_report_mode < 2)) {
2861 acpi_bus_generate_proc_event(ibm->acpi->device,
2862 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002863 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002864
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002865 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002866 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002867 acpi_bus_generate_netlink_event(
2868 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002869 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002870 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002871 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002872 }
2873}
2874
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002875static void hotkey_suspend(pm_message_t state)
2876{
2877 /* Do these on suspend, we get the events on early resume! */
2878 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2879 hotkey_autosleep_ack = 0;
2880}
2881
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002882static void hotkey_resume(void)
2883{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002884 tpacpi_disable_brightness_delay();
2885
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002886 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002887 printk(TPACPI_ERR
2888 "error while trying to read hot key mask "
2889 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002890 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002891 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002892 hotkey_wakeup_reason_notify_change();
2893 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002894 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002895}
2896
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002897/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002898static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002900 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 int len = 0;
2902
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002903 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002904 len += sprintf(p + len, "status:\t\tnot supported\n");
2905 return len;
2906 }
2907
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002908 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002909 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002910 res = hotkey_status_get(&status);
2911 if (!res)
2912 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002913 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002914 if (res)
2915 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002918 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002919 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 len += sprintf(p + len,
2921 "commands:\tenable, disable, reset, <mask>\n");
2922 } else {
2923 len += sprintf(p + len, "mask:\t\tnot supported\n");
2924 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2925 }
2926
2927 return len;
2928}
2929
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002930static void hotkey_enabledisable_warn(void)
2931{
2932 tpacpi_log_usertask("procfs hotkey enable/disable");
2933 WARN(1, TPACPI_WARN
2934 "hotkey enable/disable functionality has been "
2935 "removed from the driver. Hotkeys are always enabled.\n");
2936}
2937
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002938static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002940 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002941 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002944 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return -ENODEV;
2946
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002947 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002948 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002949
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002950 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002951
2952 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 while ((cmd = next_cmd(&buf))) {
2954 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002955 hotkey_enabledisable_warn();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002957 hotkey_enabledisable_warn();
2958 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002960 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2962 /* mask set */
2963 } else if (sscanf(cmd, "%x", &mask) == 1) {
2964 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002965 } else {
2966 res = -EINVAL;
2967 goto errexit;
2968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002970 if (!res && mask != hotkey_mask)
2971 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002973errexit:
2974 mutex_unlock(&hotkey_mutex);
2975 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002976}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002978static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002979 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002980 {"", 0},
2981};
2982
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002983static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002984 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002985 .notify = hotkey_notify,
2986 .handle = &hkey_handle,
2987 .type = ACPI_DEVICE_NOTIFY,
2988};
2989
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002990static struct ibm_struct hotkey_driver_data = {
2991 .name = "hotkey",
2992 .read = hotkey_read,
2993 .write = hotkey_write,
2994 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002995 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002996 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002997 .acpi = &ibm_hotkey_acpidriver,
2998};
2999
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003000/*************************************************************************
3001 * Bluetooth subdriver
3002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003004enum {
3005 /* ACPI GBDC/SBDC bits */
3006 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3007 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003008 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3009 off / last state */
3010};
3011
3012enum {
3013 /* ACPI \BLTH commands */
3014 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3015 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3016 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3017 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3018 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003019};
3020
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003021static struct rfkill *tpacpi_bluetooth_rfkill;
3022
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003023static void bluetooth_suspend(pm_message_t state)
3024{
3025 /* Try to make sure radio will resume powered off */
3026 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3027 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
3028}
3029
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003030static int bluetooth_get_radiosw(void)
3031{
3032 int status;
3033
3034 if (!tp_features.bluetooth)
3035 return -ENODEV;
3036
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003037 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3038 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003039 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003040
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003041#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3042 if (dbg_bluetoothemul)
3043 return (tpacpi_bluetooth_emulstate) ?
3044 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3045#endif
3046
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003047 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3048 return -EIO;
3049
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003050 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3051 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003052}
3053
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003054static void bluetooth_update_rfk(void)
3055{
3056 int status;
3057
3058 if (!tpacpi_bluetooth_rfkill)
3059 return;
3060
3061 status = bluetooth_get_radiosw();
3062 if (status < 0)
3063 return;
3064 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3065}
3066
3067static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003068{
3069 int status;
3070
3071 if (!tp_features.bluetooth)
3072 return -ENODEV;
3073
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003074 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3075 * reason to risk weird behaviour. */
3076 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3077 && radio_on)
3078 return -EPERM;
3079
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003080#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3081 if (dbg_bluetoothemul) {
3082 tpacpi_bluetooth_emulstate = !!radio_on;
3083 if (update_rfk)
3084 bluetooth_update_rfk();
3085 return 0;
3086 }
3087#endif
3088
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003089 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003090 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003091 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003092 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003093 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003094 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3095 return -EIO;
3096
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003097 if (update_rfk)
3098 bluetooth_update_rfk();
3099
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003100 return 0;
3101}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003102
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003103/* sysfs bluetooth enable ---------------------------------------------- */
3104static ssize_t bluetooth_enable_show(struct device *dev,
3105 struct device_attribute *attr,
3106 char *buf)
3107{
3108 int status;
3109
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003110 printk_deprecated_rfkill_attribute("bluetooth_enable");
3111
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003112 status = bluetooth_get_radiosw();
3113 if (status < 0)
3114 return status;
3115
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003116 return snprintf(buf, PAGE_SIZE, "%d\n",
3117 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003118}
3119
3120static ssize_t bluetooth_enable_store(struct device *dev,
3121 struct device_attribute *attr,
3122 const char *buf, size_t count)
3123{
3124 unsigned long t;
3125 int res;
3126
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003127 printk_deprecated_rfkill_attribute("bluetooth_enable");
3128
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003129 if (parse_strtoul(buf, 1, &t))
3130 return -EINVAL;
3131
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003132 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003133
3134 return (res) ? res : count;
3135}
3136
3137static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003138 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003139 bluetooth_enable_show, bluetooth_enable_store);
3140
3141/* --------------------------------------------------------------------- */
3142
3143static struct attribute *bluetooth_attributes[] = {
3144 &dev_attr_bluetooth_enable.attr,
3145 NULL
3146};
3147
3148static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003149 .attrs = bluetooth_attributes,
3150};
3151
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003152static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3153{
3154 int bts = bluetooth_get_radiosw();
3155
3156 if (bts < 0)
3157 return bts;
3158
3159 *state = bts;
3160 return 0;
3161}
3162
3163static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3164{
3165 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3166}
3167
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003168static void bluetooth_shutdown(void)
3169{
3170 /* Order firmware to save current state to NVRAM */
3171 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3172 TP_ACPI_BLTH_SAVE_STATE))
3173 printk(TPACPI_NOTICE
3174 "failed to save bluetooth state to NVRAM\n");
3175}
3176
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003177static void bluetooth_exit(void)
3178{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003179 bluetooth_shutdown();
3180
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003181 if (tpacpi_bluetooth_rfkill)
3182 rfkill_unregister(tpacpi_bluetooth_rfkill);
3183
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003184 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3185 &bluetooth_attr_group);
3186}
3187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003188static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003190 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003191 int status = 0;
3192
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003193 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3194
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003195 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003196
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003197 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3198 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003199 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003200 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003202 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3203 str_supported(tp_features.bluetooth),
3204 status);
3205
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003206#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3207 if (dbg_bluetoothemul) {
3208 tp_features.bluetooth = 1;
3209 printk(TPACPI_INFO
3210 "bluetooth switch emulation enabled\n");
3211 } else
3212#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003213 if (tp_features.bluetooth &&
3214 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3215 /* no bluetooth hardware present in system */
3216 tp_features.bluetooth = 0;
3217 dbg_printk(TPACPI_DBG_INIT,
3218 "bluetooth hardware not installed\n");
3219 }
3220
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003221 if (!tp_features.bluetooth)
3222 return 1;
3223
3224 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003225 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003226 if (res)
3227 return res;
3228
3229 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3230 &tpacpi_bluetooth_rfkill,
3231 RFKILL_TYPE_BLUETOOTH,
3232 "tpacpi_bluetooth_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003233 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003234 tpacpi_bluetooth_rfk_set,
3235 tpacpi_bluetooth_rfk_get);
3236 if (res) {
3237 bluetooth_exit();
3238 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003239 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003240
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003244/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003245static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003248 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003250 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003253 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003254 (status == RFKILL_STATE_UNBLOCKED) ?
3255 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 len += sprintf(p + len, "commands:\tenable, disable\n");
3257 }
3258
3259 return len;
3260}
3261
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003262static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003266 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003267 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 while ((cmd = next_cmd(&buf))) {
3270 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003271 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003273 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 } else
3275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279}
3280
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003281static struct ibm_struct bluetooth_driver_data = {
3282 .name = "bluetooth",
3283 .read = bluetooth_read,
3284 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003285 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003286 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003287 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003288};
3289
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003290/*************************************************************************
3291 * Wan subdriver
3292 */
3293
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003294enum {
3295 /* ACPI GWAN/SWAN bits */
3296 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3297 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003298 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3299 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003300};
3301
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003302static struct rfkill *tpacpi_wan_rfkill;
3303
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003304static void wan_suspend(pm_message_t state)
3305{
3306 /* Try to make sure radio will resume powered off */
3307 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3308 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3309}
3310
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003311static int wan_get_radiosw(void)
3312{
3313 int status;
3314
3315 if (!tp_features.wan)
3316 return -ENODEV;
3317
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003318 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3319 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003320 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003321
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003322#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3323 if (dbg_wwanemul)
3324 return (tpacpi_wwan_emulstate) ?
3325 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3326#endif
3327
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003328 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3329 return -EIO;
3330
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003331 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3332 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003333}
3334
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003335static void wan_update_rfk(void)
3336{
3337 int status;
3338
3339 if (!tpacpi_wan_rfkill)
3340 return;
3341
3342 status = wan_get_radiosw();
3343 if (status < 0)
3344 return;
3345 rfkill_force_state(tpacpi_wan_rfkill, status);
3346}
3347
3348static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003349{
3350 int status;
3351
3352 if (!tp_features.wan)
3353 return -ENODEV;
3354
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003355 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3356 * reason to risk weird behaviour. */
3357 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3358 && radio_on)
3359 return -EPERM;
3360
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003361#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3362 if (dbg_wwanemul) {
3363 tpacpi_wwan_emulstate = !!radio_on;
3364 if (update_rfk)
3365 wan_update_rfk();
3366 return 0;
3367 }
3368#endif
3369
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003370 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003371 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003372 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003373 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003374 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003375 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3376 return -EIO;
3377
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003378 if (update_rfk)
3379 wan_update_rfk();
3380
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003381 return 0;
3382}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003383
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003384/* sysfs wan enable ---------------------------------------------------- */
3385static ssize_t wan_enable_show(struct device *dev,
3386 struct device_attribute *attr,
3387 char *buf)
3388{
3389 int status;
3390
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003391 printk_deprecated_rfkill_attribute("wwan_enable");
3392
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003393 status = wan_get_radiosw();
3394 if (status < 0)
3395 return status;
3396
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003397 return snprintf(buf, PAGE_SIZE, "%d\n",
3398 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003399}
3400
3401static ssize_t wan_enable_store(struct device *dev,
3402 struct device_attribute *attr,
3403 const char *buf, size_t count)
3404{
3405 unsigned long t;
3406 int res;
3407
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003408 printk_deprecated_rfkill_attribute("wwan_enable");
3409
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003410 if (parse_strtoul(buf, 1, &t))
3411 return -EINVAL;
3412
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003413 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003414
3415 return (res) ? res : count;
3416}
3417
3418static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003419 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003420 wan_enable_show, wan_enable_store);
3421
3422/* --------------------------------------------------------------------- */
3423
3424static struct attribute *wan_attributes[] = {
3425 &dev_attr_wan_enable.attr,
3426 NULL
3427};
3428
3429static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003430 .attrs = wan_attributes,
3431};
3432
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003433static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3434{
3435 int wans = wan_get_radiosw();
3436
3437 if (wans < 0)
3438 return wans;
3439
3440 *state = wans;
3441 return 0;
3442}
3443
3444static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3445{
3446 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3447}
3448
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003449static void wan_shutdown(void)
3450{
3451 /* Order firmware to save current state to NVRAM */
3452 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3453 TP_ACPI_WGSV_SAVE_STATE))
3454 printk(TPACPI_NOTICE
3455 "failed to save WWAN state to NVRAM\n");
3456}
3457
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003458static void wan_exit(void)
3459{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003460 wan_shutdown();
3461
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003462 if (tpacpi_wan_rfkill)
3463 rfkill_unregister(tpacpi_wan_rfkill);
3464
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003465 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3466 &wan_attr_group);
3467}
3468
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003469static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003470{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003471 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003472 int status = 0;
3473
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003474 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3475
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003476 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003477
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003478 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003479 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003480
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003481 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3482 str_supported(tp_features.wan),
3483 status);
3484
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003485#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3486 if (dbg_wwanemul) {
3487 tp_features.wan = 1;
3488 printk(TPACPI_INFO
3489 "wwan switch emulation enabled\n");
3490 } else
3491#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003492 if (tp_features.wan &&
3493 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3494 /* no wan hardware present in system */
3495 tp_features.wan = 0;
3496 dbg_printk(TPACPI_DBG_INIT,
3497 "wan hardware not installed\n");
3498 }
3499
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003500 if (!tp_features.wan)
3501 return 1;
3502
3503 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003504 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003505 if (res)
3506 return res;
3507
3508 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3509 &tpacpi_wan_rfkill,
3510 RFKILL_TYPE_WWAN,
3511 "tpacpi_wwan_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003512 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003513 tpacpi_wan_rfk_set,
3514 tpacpi_wan_rfk_get);
3515 if (res) {
3516 wan_exit();
3517 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003518 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003519
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003520 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003521}
3522
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003523/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003524static int wan_read(char *p)
3525{
3526 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003527 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003528
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003529 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003530 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003531 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003532 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003533 (status == RFKILL_STATE_UNBLOCKED) ?
3534 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003535 len += sprintf(p + len, "commands:\tenable, disable\n");
3536 }
3537
3538 return len;
3539}
3540
3541static int wan_write(char *buf)
3542{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003543 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003544
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003545 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003546 return -ENODEV;
3547
3548 while ((cmd = next_cmd(&buf))) {
3549 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003550 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003551 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003552 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003553 } else
3554 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003555 }
3556
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003557 return 0;
3558}
3559
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003560static struct ibm_struct wan_driver_data = {
3561 .name = "wan",
3562 .read = wan_read,
3563 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003564 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003565 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003566 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003567};
3568
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003569/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003570 * UWB subdriver
3571 */
3572
3573enum {
3574 /* ACPI GUWB/SUWB bits */
3575 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3576 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3577};
3578
3579static struct rfkill *tpacpi_uwb_rfkill;
3580
3581static int uwb_get_radiosw(void)
3582{
3583 int status;
3584
3585 if (!tp_features.uwb)
3586 return -ENODEV;
3587
3588 /* WLSW overrides UWB in firmware/hardware, reflect that */
3589 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3590 return RFKILL_STATE_HARD_BLOCKED;
3591
3592#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3593 if (dbg_uwbemul)
3594 return (tpacpi_uwb_emulstate) ?
3595 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3596#endif
3597
3598 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3599 return -EIO;
3600
3601 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3602 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3603}
3604
3605static void uwb_update_rfk(void)
3606{
3607 int status;
3608
3609 if (!tpacpi_uwb_rfkill)
3610 return;
3611
3612 status = uwb_get_radiosw();
3613 if (status < 0)
3614 return;
3615 rfkill_force_state(tpacpi_uwb_rfkill, status);
3616}
3617
3618static int uwb_set_radiosw(int radio_on, int update_rfk)
3619{
3620 int status;
3621
3622 if (!tp_features.uwb)
3623 return -ENODEV;
3624
3625 /* WLSW overrides UWB in firmware/hardware, but there is no
3626 * reason to risk weird behaviour. */
3627 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3628 && radio_on)
3629 return -EPERM;
3630
3631#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3632 if (dbg_uwbemul) {
3633 tpacpi_uwb_emulstate = !!radio_on;
3634 if (update_rfk)
3635 uwb_update_rfk();
3636 return 0;
3637 }
3638#endif
3639
3640 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3641 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3642 return -EIO;
3643
3644 if (update_rfk)
3645 uwb_update_rfk();
3646
3647 return 0;
3648}
3649
3650/* --------------------------------------------------------------------- */
3651
3652static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3653{
3654 int uwbs = uwb_get_radiosw();
3655
3656 if (uwbs < 0)
3657 return uwbs;
3658
3659 *state = uwbs;
3660 return 0;
3661}
3662
3663static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3664{
3665 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3666}
3667
3668static void uwb_exit(void)
3669{
3670 if (tpacpi_uwb_rfkill)
3671 rfkill_unregister(tpacpi_uwb_rfkill);
3672}
3673
3674static int __init uwb_init(struct ibm_init_struct *iibm)
3675{
3676 int res;
3677 int status = 0;
3678
3679 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3680
3681 TPACPI_ACPIHANDLE_INIT(hkey);
3682
3683 tp_features.uwb = hkey_handle &&
3684 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3685
3686 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3687 str_supported(tp_features.uwb),
3688 status);
3689
3690#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3691 if (dbg_uwbemul) {
3692 tp_features.uwb = 1;
3693 printk(TPACPI_INFO
3694 "uwb switch emulation enabled\n");
3695 } else
3696#endif
3697 if (tp_features.uwb &&
3698 !(status & TP_ACPI_UWB_HWPRESENT)) {
3699 /* no uwb hardware present in system */
3700 tp_features.uwb = 0;
3701 dbg_printk(TPACPI_DBG_INIT,
3702 "uwb hardware not installed\n");
3703 }
3704
3705 if (!tp_features.uwb)
3706 return 1;
3707
3708 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3709 &tpacpi_uwb_rfkill,
3710 RFKILL_TYPE_UWB,
3711 "tpacpi_uwb_sw",
3712 false,
3713 tpacpi_uwb_rfk_set,
3714 tpacpi_uwb_rfk_get);
3715
3716 return res;
3717}
3718
3719static struct ibm_struct uwb_driver_data = {
3720 .name = "uwb",
3721 .exit = uwb_exit,
3722 .flags.experimental = 1,
3723};
3724
3725/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003726 * Video subdriver
3727 */
3728
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003729#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3730
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003731enum video_access_mode {
3732 TPACPI_VIDEO_NONE = 0,
3733 TPACPI_VIDEO_570, /* 570 */
3734 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3735 TPACPI_VIDEO_NEW, /* all others */
3736};
3737
3738enum { /* video status flags, based on VIDEO_570 */
3739 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3740 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3741 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3742};
3743
3744enum { /* TPACPI_VIDEO_570 constants */
3745 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3746 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3747 * video_status_flags */
3748 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3749 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3750};
3751
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003752static enum video_access_mode video_supported;
3753static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003754
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003755static int video_autosw_get(void);
3756static int video_autosw_set(int enable);
3757
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003758TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003759
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003760static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003762 int ivga;
3763
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003764 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3765
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003766 TPACPI_ACPIHANDLE_INIT(vid);
3767 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003768
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003769 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3770 /* G41, assume IVGA doesn't change */
3771 vid_handle = vid2_handle;
3772
3773 if (!vid_handle)
3774 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003775 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003776 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3777 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003778 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003779 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3780 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003781 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003782 else
3783 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003784 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003786 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3787 str_supported(video_supported != TPACPI_VIDEO_NONE),
3788 video_supported);
3789
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003790 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791}
3792
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003793static void video_exit(void)
3794{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003795 dbg_printk(TPACPI_DBG_EXIT,
3796 "restoring original video autoswitch mode\n");
3797 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003798 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003799 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003800}
3801
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003802static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
3804 int status = 0;
3805 int i;
3806
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003807 switch (video_supported) {
3808 case TPACPI_VIDEO_570:
3809 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3810 TP_ACPI_VIDEO_570_PHSCMD))
3811 return -EIO;
3812 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3813 break;
3814 case TPACPI_VIDEO_770:
3815 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3816 return -EIO;
3817 if (i)
3818 status |= TP_ACPI_VIDEO_S_LCD;
3819 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3820 return -EIO;
3821 if (i)
3822 status |= TP_ACPI_VIDEO_S_CRT;
3823 break;
3824 case TPACPI_VIDEO_NEW:
3825 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3826 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3827 return -EIO;
3828 if (i)
3829 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003831 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3832 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3833 return -EIO;
3834 if (i)
3835 status |= TP_ACPI_VIDEO_S_LCD;
3836 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3837 return -EIO;
3838 if (i)
3839 status |= TP_ACPI_VIDEO_S_DVI;
3840 break;
3841 default:
3842 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 return status;
3846}
3847
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003848static int video_outputsw_set(int status)
3849{
3850 int autosw;
3851 int res = 0;
3852
3853 switch (video_supported) {
3854 case TPACPI_VIDEO_570:
3855 res = acpi_evalf(NULL, NULL,
3856 "\\_SB.PHS2", "vdd",
3857 TP_ACPI_VIDEO_570_PHS2CMD,
3858 status | TP_ACPI_VIDEO_570_PHS2SET);
3859 break;
3860 case TPACPI_VIDEO_770:
3861 autosw = video_autosw_get();
3862 if (autosw < 0)
3863 return autosw;
3864
3865 res = video_autosw_set(1);
3866 if (res)
3867 return res;
3868 res = acpi_evalf(vid_handle, NULL,
3869 "ASWT", "vdd", status * 0x100, 0);
3870 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003871 printk(TPACPI_ERR
3872 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003873 return -EIO;
3874 }
3875 break;
3876 case TPACPI_VIDEO_NEW:
3877 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003878 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003879 break;
3880 default:
3881 return -ENOSYS;
3882 }
3883
3884 return (res)? 0 : -EIO;
3885}
3886
3887static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003889 int autosw = 0;
3890
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003891 switch (video_supported) {
3892 case TPACPI_VIDEO_570:
3893 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3894 return -EIO;
3895 break;
3896 case TPACPI_VIDEO_770:
3897 case TPACPI_VIDEO_NEW:
3898 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3899 return -EIO;
3900 break;
3901 default:
3902 return -ENOSYS;
3903 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003904
3905 return autosw & 1;
3906}
3907
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003908static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003909{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003910 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003911 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003912 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003913}
3914
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003915static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003916{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003917 int autosw = video_autosw_get();
3918 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003919
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003920 if (autosw < 0)
3921 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003922
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003923 switch (video_supported) {
3924 case TPACPI_VIDEO_570:
3925 res = video_autosw_set(1);
3926 if (res)
3927 return res;
3928 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3929 break;
3930 case TPACPI_VIDEO_770:
3931 case TPACPI_VIDEO_NEW:
3932 res = video_autosw_set(1);
3933 if (res)
3934 return res;
3935 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3936 break;
3937 default:
3938 return -ENOSYS;
3939 }
3940 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003941 printk(TPACPI_ERR
3942 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003943 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003944 }
3945
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003946 return (res)? 0 : -EIO;
3947}
3948
3949static int video_expand_toggle(void)
3950{
3951 switch (video_supported) {
3952 case TPACPI_VIDEO_570:
3953 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3954 0 : -EIO;
3955 case TPACPI_VIDEO_770:
3956 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3957 0 : -EIO;
3958 case TPACPI_VIDEO_NEW:
3959 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3960 0 : -EIO;
3961 default:
3962 return -ENOSYS;
3963 }
3964 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003965}
3966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003967static int video_read(char *p)
3968{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003969 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003970 int len = 0;
3971
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003972 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003973 len += sprintf(p + len, "status:\t\tnot supported\n");
3974 return len;
3975 }
3976
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003977 status = video_outputsw_get();
3978 if (status < 0)
3979 return status;
3980
3981 autosw = video_autosw_get();
3982 if (autosw < 0)
3983 return autosw;
3984
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003985 len += sprintf(p + len, "status:\t\tsupported\n");
3986 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3987 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003988 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003989 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3990 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3991 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3992 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003993 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003994 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3995 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3996 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3997
3998 return len;
3999}
4000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004001static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
4003 char *cmd;
4004 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004005 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004007 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004008 return -ENODEV;
4009
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004010 enable = 0;
4011 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 while ((cmd = next_cmd(&buf))) {
4014 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004015 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004017 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004019 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004021 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004022 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004023 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004024 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004025 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004026 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004027 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004029 res = video_autosw_set(1);
4030 if (res)
4031 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004033 res = video_autosw_set(0);
4034 if (res)
4035 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004037 res = video_outputsw_cycle();
4038 if (res)
4039 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004041 res = video_expand_toggle();
4042 if (res)
4043 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 } else
4045 return -EINVAL;
4046 }
4047
4048 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004049 status = video_outputsw_get();
4050 if (status < 0)
4051 return status;
4052 res = video_outputsw_set((status & ~disable) | enable);
4053 if (res)
4054 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
4056
4057 return 0;
4058}
4059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004060static struct ibm_struct video_driver_data = {
4061 .name = "video",
4062 .read = video_read,
4063 .write = video_write,
4064 .exit = video_exit,
4065};
4066
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004067#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4068
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004069/*************************************************************************
4070 * Light (thinklight) subdriver
4071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004073TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4074TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004075
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004076static int light_get_status(void)
4077{
4078 int status = 0;
4079
4080 if (tp_features.light_status) {
4081 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4082 return -EIO;
4083 return (!!status);
4084 }
4085
4086 return -ENXIO;
4087}
4088
4089static int light_set_status(int status)
4090{
4091 int rc;
4092
4093 if (tp_features.light) {
4094 if (cmos_handle) {
4095 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4096 (status)?
4097 TP_CMOS_THINKLIGHT_ON :
4098 TP_CMOS_THINKLIGHT_OFF);
4099 } else {
4100 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4101 (status)? 1 : 0);
4102 }
4103 return (rc)? 0 : -EIO;
4104 }
4105
4106 return -ENXIO;
4107}
4108
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004109static void light_set_status_worker(struct work_struct *work)
4110{
4111 struct tpacpi_led_classdev *data =
4112 container_of(work, struct tpacpi_led_classdev, work);
4113
4114 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4115 light_set_status((data->new_brightness != LED_OFF));
4116}
4117
4118static void light_sysfs_set(struct led_classdev *led_cdev,
4119 enum led_brightness brightness)
4120{
4121 struct tpacpi_led_classdev *data =
4122 container_of(led_cdev,
4123 struct tpacpi_led_classdev,
4124 led_classdev);
4125 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004126 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004127}
4128
4129static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4130{
4131 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4132}
4133
4134static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4135 .led_classdev = {
4136 .name = "tpacpi::thinklight",
4137 .brightness_set = &light_sysfs_set,
4138 .brightness_get = &light_sysfs_get,
4139 }
4140};
4141
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004142static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004144 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004145
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004146 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4147
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004148 TPACPI_ACPIHANDLE_INIT(ledb);
4149 TPACPI_ACPIHANDLE_INIT(lght);
4150 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004151 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004152
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004153 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004154 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004155
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004156 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004157 /* light status not supported on
4158 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004159 tp_features.light_status =
4160 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004162 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4163 str_supported(tp_features.light),
4164 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004165
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004166 if (!tp_features.light)
4167 return 1;
4168
4169 rc = led_classdev_register(&tpacpi_pdev->dev,
4170 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004171
4172 if (rc < 0) {
4173 tp_features.light = 0;
4174 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004175 } else {
4176 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004177 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004178
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004179 return rc;
4180}
4181
4182static void light_exit(void)
4183{
4184 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4185 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004186 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004189static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
4191 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004192 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004194 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004195 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004196 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004197 len += sprintf(p + len, "status:\t\tunknown\n");
4198 len += sprintf(p + len, "commands:\ton, off\n");
4199 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004200 status = light_get_status();
4201 if (status < 0)
4202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004204 len += sprintf(p + len, "commands:\ton, off\n");
4205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 return len;
4208}
4209
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004210static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004213 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004214
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004215 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004216 return -ENODEV;
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 while ((cmd = next_cmd(&buf))) {
4219 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004220 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004222 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 } else
4224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
4226
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004227 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004230static struct ibm_struct light_driver_data = {
4231 .name = "light",
4232 .read = light_read,
4233 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004234 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004235};
4236
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004237/*************************************************************************
4238 * Dock subdriver
4239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004241#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004242
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004243static void dock_notify(struct ibm_struct *ibm, u32 event);
4244static int dock_read(char *p);
4245static int dock_write(char *buf);
4246
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004247TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004248 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4249 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4250 "\\_SB.PCI.ISA.SLCE", /* 570 */
4251 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4252
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004253/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004254TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004255
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004256static const struct acpi_device_id ibm_pci_device_ids[] = {
4257 {PCI_ROOT_HID_STRING, 0},
4258 {"", 0},
4259};
4260
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004261static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4262 {
4263 .notify = dock_notify,
4264 .handle = &dock_handle,
4265 .type = ACPI_SYSTEM_NOTIFY,
4266 },
4267 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004268 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4269 * We just use it to get notifications of dock hotplug
4270 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004271 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004272 .notify = dock_notify,
4273 .handle = &pci_handle,
4274 .type = ACPI_SYSTEM_NOTIFY,
4275 },
4276};
4277
4278static struct ibm_struct dock_driver_data[2] = {
4279 {
4280 .name = "dock",
4281 .read = dock_read,
4282 .write = dock_write,
4283 .acpi = &ibm_dock_acpidriver[0],
4284 },
4285 {
4286 .name = "dock",
4287 .acpi = &ibm_dock_acpidriver[1],
4288 },
4289};
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291#define dock_docked() (_sta(dock_handle) & 1)
4292
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004293static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004294{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004295 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4296
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004297 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004298
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004299 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4300 str_supported(dock_handle != NULL));
4301
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004302 return (dock_handle)? 0 : 1;
4303}
4304
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004305static int __init dock_init2(struct ibm_init_struct *iibm)
4306{
4307 int dock2_needed;
4308
4309 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4310
4311 if (dock_driver_data[0].flags.acpi_driver_registered &&
4312 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004313 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004314 dock2_needed = (pci_handle != NULL);
4315 vdbg_printk(TPACPI_DBG_INIT,
4316 "dock PCI handler for the TP 570 is %s\n",
4317 str_supported(dock2_needed));
4318 } else {
4319 vdbg_printk(TPACPI_DBG_INIT,
4320 "dock subdriver part 2 not required\n");
4321 dock2_needed = 0;
4322 }
4323
4324 return (dock2_needed)? 0 : 1;
4325}
4326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004327static void dock_notify(struct ibm_struct *ibm, u32 event)
4328{
4329 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004330 int pci = ibm->acpi->hid && ibm->acpi->device &&
4331 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004332 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004333
4334 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004335 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004336 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004337 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004338 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004339 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004340 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004341 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004342 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004343 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004344 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004345 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004346 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004347 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004348 }
Len Brown14e04fb32007-08-23 15:20:26 -04004349 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004350 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004351 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004352 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004353}
4354
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004355static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
4357 int len = 0;
4358 int docked = dock_docked();
4359
4360 if (!dock_handle)
4361 len += sprintf(p + len, "status:\t\tnot supported\n");
4362 else if (!docked)
4363 len += sprintf(p + len, "status:\t\tundocked\n");
4364 else {
4365 len += sprintf(p + len, "status:\t\tdocked\n");
4366 len += sprintf(p + len, "commands:\tdock, undock\n");
4367 }
4368
4369 return len;
4370}
4371
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004372static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
4374 char *cmd;
4375
4376 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004377 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
4379 while ((cmd = next_cmd(&buf))) {
4380 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004381 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4382 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return -EIO;
4384 } else if (strlencmp(cmd, "dock") == 0) {
4385 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4386 return -EIO;
4387 } else
4388 return -EINVAL;
4389 }
4390
4391 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004394#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004396/*************************************************************************
4397 * Bay subdriver
4398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004400#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004401
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004402TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004403 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4404 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4405 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4406 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004407TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408 "_EJ0", /* all others */
4409 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004410TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4412 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004413TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004414 "_EJ0", /* 770x */
4415 ); /* all others */
4416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004417static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004419 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4420
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004421 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004422 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004423 TPACPI_ACPIHANDLE_INIT(bay_ej);
4424 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004425 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004426 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004427
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004428 tp_features.bay_status = bay_handle &&
4429 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4430 tp_features.bay_status2 = bay2_handle &&
4431 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004432
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004433 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4434 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4435 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4436 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004438 vdbg_printk(TPACPI_DBG_INIT,
4439 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004440 str_supported(tp_features.bay_status),
4441 str_supported(tp_features.bay_eject),
4442 str_supported(tp_features.bay_status2),
4443 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004444
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004445 return (tp_features.bay_status || tp_features.bay_eject ||
4446 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004449static void bay_notify(struct ibm_struct *ibm, u32 event)
4450{
Len Brown14e04fb32007-08-23 15:20:26 -04004451 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004452 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004453 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004454 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004455}
4456
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004457#define bay_occupied(b) (_sta(b##_handle) & 1)
4458
4459static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
4461 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004462 int occupied = bay_occupied(bay);
4463 int occupied2 = bay_occupied(bay2);
4464 int eject, eject2;
4465
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004466 len += sprintf(p + len, "status:\t\t%s\n",
4467 tp_features.bay_status ?
4468 (occupied ? "occupied" : "unoccupied") :
4469 "not supported");
4470 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004471 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4472 "occupied" : "unoccupied");
4473
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004474 eject = tp_features.bay_eject && occupied;
4475 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476
4477 if (eject && eject2)
4478 len += sprintf(p + len, "commands:\teject, eject2\n");
4479 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004481 else if (eject2)
4482 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 return len;
4485}
4486
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
4489 char *cmd;
4490
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004491 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004492 return -ENODEV;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004495 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004496 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4497 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004498 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004499 strlencmp(cmd, "eject2") == 0) {
4500 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return -EIO;
4502 } else
4503 return -EINVAL;
4504 }
4505
4506 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004507}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004508
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004509static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4510 .notify = bay_notify,
4511 .handle = &bay_handle,
4512 .type = ACPI_SYSTEM_NOTIFY,
4513};
4514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004515static struct ibm_struct bay_driver_data = {
4516 .name = "bay",
4517 .read = bay_read,
4518 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004519 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004520};
4521
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004522#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004524/*************************************************************************
4525 * CMOS subdriver
4526 */
4527
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004528/* sysfs cmos_command -------------------------------------------------- */
4529static ssize_t cmos_command_store(struct device *dev,
4530 struct device_attribute *attr,
4531 const char *buf, size_t count)
4532{
4533 unsigned long cmos_cmd;
4534 int res;
4535
4536 if (parse_strtoul(buf, 21, &cmos_cmd))
4537 return -EINVAL;
4538
4539 res = issue_thinkpad_cmos_command(cmos_cmd);
4540 return (res)? res : count;
4541}
4542
4543static struct device_attribute dev_attr_cmos_command =
4544 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4545
4546/* --------------------------------------------------------------------- */
4547
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004548static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004549{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004550 int res;
4551
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004552 vdbg_printk(TPACPI_DBG_INIT,
4553 "initializing cmos commands subdriver\n");
4554
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004555 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004556
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004557 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4558 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004559
4560 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4561 if (res)
4562 return res;
4563
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004564 return (cmos_handle)? 0 : 1;
4565}
4566
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004567static void cmos_exit(void)
4568{
4569 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4570}
4571
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
4574 int len = 0;
4575
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004576 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4577 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (!cmos_handle)
4579 len += sprintf(p + len, "status:\t\tnot supported\n");
4580 else {
4581 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004582 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
4584
4585 return len;
4586}
4587
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004588static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
4590 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004591 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
4593 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004594 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4595 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 /* cmos_cmd set */
4597 } else
4598 return -EINVAL;
4599
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004600 res = issue_thinkpad_cmos_command(cmos_cmd);
4601 if (res)
4602 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
4604
4605 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004606}
4607
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004608static struct ibm_struct cmos_driver_data = {
4609 .name = "cmos",
4610 .read = cmos_read,
4611 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004612 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004613};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004614
4615/*************************************************************************
4616 * LED subdriver
4617 */
4618
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004619enum led_access_mode {
4620 TPACPI_LED_NONE = 0,
4621 TPACPI_LED_570, /* 570 */
4622 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4623 TPACPI_LED_NEW, /* all others */
4624};
4625
4626enum { /* For TPACPI_LED_OLD */
4627 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4628 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4629 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4630};
4631
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004632enum led_status_t {
4633 TPACPI_LED_OFF = 0,
4634 TPACPI_LED_ON,
4635 TPACPI_LED_BLINK,
4636};
4637
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004638static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004639
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004640TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004641 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4642 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004643 "LED", /* all others */
4644 ); /* R30, R31 */
4645
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004646#define TPACPI_LED_NUMLEDS 8
4647static struct tpacpi_led_classdev *tpacpi_leds;
4648static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004649static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004650 /* there's a limit of 19 chars + NULL before 2.6.26 */
4651 "tpacpi::power",
4652 "tpacpi:orange:batt",
4653 "tpacpi:green:batt",
4654 "tpacpi::dock_active",
4655 "tpacpi::bay_active",
4656 "tpacpi::dock_batt",
4657 "tpacpi::unknown_led",
4658 "tpacpi::standby",
4659};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004660#define TPACPI_SAFE_LEDS 0x0081U
4661
4662static inline bool tpacpi_is_led_restricted(const unsigned int led)
4663{
4664#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4665 return false;
4666#else
4667 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4668#endif
4669}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004670
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004671static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004672{
4673 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004674 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004675
4676 switch (led_supported) {
4677 case TPACPI_LED_570:
4678 if (!acpi_evalf(ec_handle,
4679 &status, "GLED", "dd", 1 << led))
4680 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004681 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004682 TPACPI_LED_OFF :
4683 ((status == 1)?
4684 TPACPI_LED_ON :
4685 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004686 tpacpi_led_state_cache[led] = led_s;
4687 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004688 default:
4689 return -ENXIO;
4690 }
4691
4692 /* not reached */
4693}
4694
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004695static int led_set_status(const unsigned int led,
4696 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004697{
4698 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004699 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4700 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004701
4702 int rc = 0;
4703
4704 switch (led_supported) {
4705 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004706 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004707 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004708 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004709 if (unlikely(tpacpi_is_led_restricted(led)))
4710 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004711 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4712 (1 << led), led_sled_arg1[ledstatus]))
4713 rc = -EIO;
4714 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004715 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004716 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004717 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004718 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004719 if (unlikely(tpacpi_is_led_restricted(led)))
4720 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004721 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4722 if (rc >= 0)
4723 rc = ec_write(TPACPI_LED_EC_HLBL,
4724 (ledstatus == TPACPI_LED_BLINK) << led);
4725 if (rc >= 0)
4726 rc = ec_write(TPACPI_LED_EC_HLCL,
4727 (ledstatus != TPACPI_LED_OFF) << led);
4728 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004729 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004730 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004731 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4732 return -EINVAL;
4733 if (unlikely(tpacpi_is_led_restricted(led)))
4734 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004735 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4736 led, led_led_arg1[ledstatus]))
4737 rc = -EIO;
4738 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004739 default:
4740 rc = -ENXIO;
4741 }
4742
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004743 if (!rc)
4744 tpacpi_led_state_cache[led] = ledstatus;
4745
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004746 return rc;
4747}
4748
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004749static void led_sysfs_set_status(unsigned int led,
4750 enum led_brightness brightness)
4751{
4752 led_set_status(led,
4753 (brightness == LED_OFF) ?
4754 TPACPI_LED_OFF :
4755 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4756 TPACPI_LED_BLINK : TPACPI_LED_ON);
4757}
4758
4759static void led_set_status_worker(struct work_struct *work)
4760{
4761 struct tpacpi_led_classdev *data =
4762 container_of(work, struct tpacpi_led_classdev, work);
4763
4764 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4765 led_sysfs_set_status(data->led, data->new_brightness);
4766}
4767
4768static void led_sysfs_set(struct led_classdev *led_cdev,
4769 enum led_brightness brightness)
4770{
4771 struct tpacpi_led_classdev *data = container_of(led_cdev,
4772 struct tpacpi_led_classdev, led_classdev);
4773
4774 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004775 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004776}
4777
4778static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4779 unsigned long *delay_on, unsigned long *delay_off)
4780{
4781 struct tpacpi_led_classdev *data = container_of(led_cdev,
4782 struct tpacpi_led_classdev, led_classdev);
4783
4784 /* Can we choose the flash rate? */
4785 if (*delay_on == 0 && *delay_off == 0) {
4786 /* yes. set them to the hardware blink rate (1 Hz) */
4787 *delay_on = 500; /* ms */
4788 *delay_off = 500; /* ms */
4789 } else if ((*delay_on != 500) || (*delay_off != 500))
4790 return -EINVAL;
4791
4792 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004793 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004794
4795 return 0;
4796}
4797
4798static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4799{
4800 int rc;
4801
4802 struct tpacpi_led_classdev *data = container_of(led_cdev,
4803 struct tpacpi_led_classdev, led_classdev);
4804
4805 rc = led_get_status(data->led);
4806
4807 if (rc == TPACPI_LED_OFF || rc < 0)
4808 rc = LED_OFF; /* no error handling in led class :( */
4809 else
4810 rc = LED_FULL;
4811
4812 return rc;
4813}
4814
4815static void led_exit(void)
4816{
4817 unsigned int i;
4818
4819 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4820 if (tpacpi_leds[i].led_classdev.name)
4821 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4822 }
4823
4824 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004825}
4826
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004827static int __init tpacpi_init_led(unsigned int led)
4828{
4829 int rc;
4830
4831 tpacpi_leds[led].led = led;
4832
4833 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4834 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4835 if (led_supported == TPACPI_LED_570)
4836 tpacpi_leds[led].led_classdev.brightness_get =
4837 &led_sysfs_get;
4838
4839 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4840
4841 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4842
4843 rc = led_classdev_register(&tpacpi_pdev->dev,
4844 &tpacpi_leds[led].led_classdev);
4845 if (rc < 0)
4846 tpacpi_leds[led].led_classdev.name = NULL;
4847
4848 return rc;
4849}
4850
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004851static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004852{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004853 unsigned int i;
4854 int rc;
4855
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004856 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4857
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004858 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004859
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004860 if (!led_handle)
4861 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004862 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863 else if (strlencmp(led_path, "SLED") == 0)
4864 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004865 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004866 else if (strlencmp(led_path, "SYSL") == 0)
4867 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004868 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869 else
4870 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004871 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004872
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004873 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4874 str_supported(led_supported), led_supported);
4875
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004876 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4877 GFP_KERNEL);
4878 if (!tpacpi_leds) {
4879 printk(TPACPI_ERR "Out of memory for LED data\n");
4880 return -ENOMEM;
4881 }
4882
4883 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004884 if (!tpacpi_is_led_restricted(i)) {
4885 rc = tpacpi_init_led(i);
4886 if (rc < 0) {
4887 led_exit();
4888 return rc;
4889 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004890 }
4891 }
4892
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004893#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4894 if (led_supported != TPACPI_LED_NONE)
4895 printk(TPACPI_NOTICE
4896 "warning: userspace override of important "
4897 "firmware LEDs is enabled\n");
4898#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004899 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004900}
4901
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004902#define str_led_status(s) \
4903 ((s) == TPACPI_LED_OFF ? "off" : \
4904 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004905
4906static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 int len = 0;
4909
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004910 if (!led_supported) {
4911 len += sprintf(p + len, "status:\t\tnot supported\n");
4912 return len;
4913 }
4914 len += sprintf(p + len, "status:\t\tsupported\n");
4915
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004916 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004917 /* 570 */
4918 int i, status;
4919 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004920 status = led_get_status(i);
4921 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922 return -EIO;
4923 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004924 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925 }
4926 }
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004929 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
4931 return len;
4932}
4933
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004934static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
4936 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004937 int led, rc;
4938 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004939
4940 if (!led_supported)
4941 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return -EINVAL;
4946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004947 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004948 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004950 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004951 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004952 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004954 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004955 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004956
4957 rc = led_set_status(led, s);
4958 if (rc < 0)
4959 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961
4962 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963}
4964
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004965static struct ibm_struct led_driver_data = {
4966 .name = "led",
4967 .read = led_read,
4968 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004969 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004970};
4971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004972/*************************************************************************
4973 * Beep subdriver
4974 */
4975
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004976TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004977
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004978static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004979{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004980 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4981
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004982 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004983
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004984 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4985 str_supported(beep_handle != NULL));
4986
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004987 return (beep_handle)? 0 : 1;
4988}
4989
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004990static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992 int len = 0;
4993
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004994 if (!beep_handle)
4995 len += sprintf(p + len, "status:\t\tnot supported\n");
4996 else {
4997 len += sprintf(p + len, "status:\t\tsupported\n");
4998 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
5001 return len;
5002}
5003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005004static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005{
5006 char *cmd;
5007 int beep_cmd;
5008
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005009 if (!beep_handle)
5010 return -ENODEV;
5011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005013 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5014 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 /* beep_cmd set */
5016 } else
5017 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005018 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return -EIO;
5020 }
5021
5022 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005023}
5024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005025static struct ibm_struct beep_driver_data = {
5026 .name = "beep",
5027 .read = beep_read,
5028 .write = beep_write,
5029};
5030
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005031/*************************************************************************
5032 * Thermal subdriver
5033 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005034
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005035enum thermal_access_mode {
5036 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5037 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5038 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5039 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5040 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5041};
5042
5043enum { /* TPACPI_THERMAL_TPEC_* */
5044 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5045 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5046 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5047};
5048
5049#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5050struct ibm_thermal_sensors_struct {
5051 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5052};
5053
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005054static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005055
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005056/* idx is zero-based */
5057static int thermal_get_sensor(int idx, s32 *value)
5058{
5059 int t;
5060 s8 tmp;
5061 char tmpi[5];
5062
5063 t = TP_EC_THERMAL_TMP0;
5064
5065 switch (thermal_read_mode) {
5066#if TPACPI_MAX_THERMAL_SENSORS >= 16
5067 case TPACPI_THERMAL_TPEC_16:
5068 if (idx >= 8 && idx <= 15) {
5069 t = TP_EC_THERMAL_TMP8;
5070 idx -= 8;
5071 }
5072 /* fallthrough */
5073#endif
5074 case TPACPI_THERMAL_TPEC_8:
5075 if (idx <= 7) {
5076 if (!acpi_ec_read(t + idx, &tmp))
5077 return -EIO;
5078 *value = tmp * 1000;
5079 return 0;
5080 }
5081 break;
5082
5083 case TPACPI_THERMAL_ACPI_UPDT:
5084 if (idx <= 7) {
5085 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5086 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5087 return -EIO;
5088 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5089 return -EIO;
5090 *value = (t - 2732) * 100;
5091 return 0;
5092 }
5093 break;
5094
5095 case TPACPI_THERMAL_ACPI_TMP07:
5096 if (idx <= 7) {
5097 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5098 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5099 return -EIO;
5100 if (t > 127 || t < -127)
5101 t = TP_EC_THERMAL_TMP_NA;
5102 *value = t * 1000;
5103 return 0;
5104 }
5105 break;
5106
5107 case TPACPI_THERMAL_NONE:
5108 default:
5109 return -ENOSYS;
5110 }
5111
5112 return -EINVAL;
5113}
5114
5115static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5116{
5117 int res, i;
5118 int n;
5119
5120 n = 8;
5121 i = 0;
5122
5123 if (!s)
5124 return -EINVAL;
5125
5126 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5127 n = 16;
5128
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005129 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005130 res = thermal_get_sensor(i, &s->temp[i]);
5131 if (res)
5132 return res;
5133 }
5134
5135 return n;
5136}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005137
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005138/* sysfs temp##_input -------------------------------------------------- */
5139
5140static ssize_t thermal_temp_input_show(struct device *dev,
5141 struct device_attribute *attr,
5142 char *buf)
5143{
5144 struct sensor_device_attribute *sensor_attr =
5145 to_sensor_dev_attr(attr);
5146 int idx = sensor_attr->index;
5147 s32 value;
5148 int res;
5149
5150 res = thermal_get_sensor(idx, &value);
5151 if (res)
5152 return res;
5153 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5154 return -ENXIO;
5155
5156 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5157}
5158
5159#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005160 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5161 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005162
5163static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5164 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5165 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5166 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5167 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5168 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5169 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5170 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5171 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5172 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5173 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5174 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5175 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5176 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5177 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5178 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5179 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5180};
5181
5182#define THERMAL_ATTRS(X) \
5183 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5184
5185static struct attribute *thermal_temp_input_attr[] = {
5186 THERMAL_ATTRS(8),
5187 THERMAL_ATTRS(9),
5188 THERMAL_ATTRS(10),
5189 THERMAL_ATTRS(11),
5190 THERMAL_ATTRS(12),
5191 THERMAL_ATTRS(13),
5192 THERMAL_ATTRS(14),
5193 THERMAL_ATTRS(15),
5194 THERMAL_ATTRS(0),
5195 THERMAL_ATTRS(1),
5196 THERMAL_ATTRS(2),
5197 THERMAL_ATTRS(3),
5198 THERMAL_ATTRS(4),
5199 THERMAL_ATTRS(5),
5200 THERMAL_ATTRS(6),
5201 THERMAL_ATTRS(7),
5202 NULL
5203};
5204
5205static const struct attribute_group thermal_temp_input16_group = {
5206 .attrs = thermal_temp_input_attr
5207};
5208
5209static const struct attribute_group thermal_temp_input8_group = {
5210 .attrs = &thermal_temp_input_attr[8]
5211};
5212
5213#undef THERMAL_SENSOR_ATTR_TEMP
5214#undef THERMAL_ATTRS
5215
5216/* --------------------------------------------------------------------- */
5217
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005218static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005219{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005220 u8 t, ta1, ta2;
5221 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005222 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005223 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005224
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005225 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5226
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005227 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005228
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005229 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005230 /*
5231 * Direct EC access mode: sensors at registers
5232 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5233 * non-implemented, thermal sensors return 0x80 when
5234 * not available
5235 */
5236
5237 ta1 = ta2 = 0;
5238 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005239 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005240 ta1 |= t;
5241 } else {
5242 ta1 = 0;
5243 break;
5244 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005245 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005246 ta2 |= t;
5247 } else {
5248 ta1 = 0;
5249 break;
5250 }
5251 }
5252 if (ta1 == 0) {
5253 /* This is sheer paranoia, but we handle it anyway */
5254 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005255 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005256 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005257 "falling back to ACPI TMPx access "
5258 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005259 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005260 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005261 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005262 "ThinkPad ACPI EC access misbehaving, "
5263 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005264 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005265 }
5266 } else {
5267 thermal_read_mode =
5268 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005269 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005270 }
5271 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005272 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5273 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005274 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005275 } else {
5276 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005277 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005278 }
5279 } else {
5280 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005281 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005282 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005283
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005284 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5285 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5286 thermal_read_mode);
5287
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005288 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005289 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005290 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005291 &thermal_temp_input16_group);
5292 if (res)
5293 return res;
5294 break;
5295 case TPACPI_THERMAL_TPEC_8:
5296 case TPACPI_THERMAL_ACPI_TMP07:
5297 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005298 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005299 &thermal_temp_input8_group);
5300 if (res)
5301 return res;
5302 break;
5303 case TPACPI_THERMAL_NONE:
5304 default:
5305 return 1;
5306 }
5307
5308 return 0;
5309}
5310
5311static void thermal_exit(void)
5312{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005313 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005314 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005315 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005316 &thermal_temp_input16_group);
5317 break;
5318 case TPACPI_THERMAL_TPEC_8:
5319 case TPACPI_THERMAL_ACPI_TMP07:
5320 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005321 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005322 &thermal_temp_input16_group);
5323 break;
5324 case TPACPI_THERMAL_NONE:
5325 default:
5326 break;
5327 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005328}
5329
5330static int thermal_read(char *p)
5331{
5332 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005333 int n, i;
5334 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005335
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005336 n = thermal_get_sensors(&t);
5337 if (unlikely(n < 0))
5338 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005340 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005342 if (n > 0) {
5343 for (i = 0; i < (n - 1); i++)
5344 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5345 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5346 } else
5347 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348
5349 return len;
5350}
5351
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005352static struct ibm_struct thermal_driver_data = {
5353 .name = "thermal",
5354 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005355 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005356};
5357
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005358/*************************************************************************
5359 * EC Dump subdriver
5360 */
5361
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362static u8 ecdump_regs[256];
5363
5364static int ecdump_read(char *p)
5365{
5366 int len = 0;
5367 int i, j;
5368 u8 v;
5369
5370 len += sprintf(p + len, "EC "
5371 " +00 +01 +02 +03 +04 +05 +06 +07"
5372 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5373 for (i = 0; i < 256; i += 16) {
5374 len += sprintf(p + len, "EC 0x%02x:", i);
5375 for (j = 0; j < 16; j++) {
5376 if (!acpi_ec_read(i + j, &v))
5377 break;
5378 if (v != ecdump_regs[i + j])
5379 len += sprintf(p + len, " *%02x", v);
5380 else
5381 len += sprintf(p + len, " %02x", v);
5382 ecdump_regs[i + j] = v;
5383 }
5384 len += sprintf(p + len, "\n");
5385 if (j != 16)
5386 break;
5387 }
5388
5389 /* These are way too dangerous to advertise openly... */
5390#if 0
5391 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5392 " (<offset> is 00-ff, <value> is 00-ff)\n");
5393 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5394 " (<offset> is 00-ff, <value> is 0-255)\n");
5395#endif
5396 return len;
5397}
5398
5399static int ecdump_write(char *buf)
5400{
5401 char *cmd;
5402 int i, v;
5403
5404 while ((cmd = next_cmd(&buf))) {
5405 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5406 /* i and v set */
5407 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5408 /* i and v set */
5409 } else
5410 return -EINVAL;
5411 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5412 if (!acpi_ec_write(i, v))
5413 return -EIO;
5414 } else
5415 return -EINVAL;
5416 }
5417
5418 return 0;
5419}
5420
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005421static struct ibm_struct ecdump_driver_data = {
5422 .name = "ecdump",
5423 .read = ecdump_read,
5424 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005425 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005426};
5427
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005428/*************************************************************************
5429 * Backlight/brightness subdriver
5430 */
Holger Macht8acb0252006-10-20 14:30:28 -07005431
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005432#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5433
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005434enum {
5435 TP_EC_BACKLIGHT = 0x31,
5436
5437 /* TP_EC_BACKLIGHT bitmasks */
5438 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5439 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5440 TP_EC_BACKLIGHT_MAPSW = 0x20,
5441};
5442
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005443static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005444static int brightness_mode;
5445static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5446
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005447static struct mutex brightness_mutex;
5448
5449/*
5450 * ThinkPads can read brightness from two places: EC 0x31, or
5451 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005452 *
5453 * EC 0x31 has the following layout
5454 * Bit 7: unknown function
5455 * Bit 6: unknown function
5456 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5457 * Bit 4: must be set to zero to avoid problems
5458 * Bit 3-0: backlight brightness level
5459 *
5460 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005461 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005462static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005463{
5464 u8 lec = 0, lcmos = 0, level = 0;
5465
5466 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005467 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005468 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005469 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005470 };
5471 if (brightness_mode & 2) {
5472 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5473 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5474 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5475 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5476 level = lcmos;
5477 }
5478
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005479 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005480 *status = lec; /* Prefer EC, CMOS is just a backing store */
5481 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005482 if (lec == lcmos)
5483 tp_warned.bright_cmos_ec_unsync = 0;
5484 else {
5485 if (!tp_warned.bright_cmos_ec_unsync) {
5486 printk(TPACPI_ERR
5487 "CMOS NVRAM (%u) and EC (%u) do not "
5488 "agree on display brightness level\n",
5489 (unsigned int) lcmos,
5490 (unsigned int) lec);
5491 tp_warned.bright_cmos_ec_unsync = 1;
5492 }
5493 return -EIO;
5494 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005495 } else {
5496 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005497 }
5498
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005499 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005500}
5501
5502/* May return EINTR which can always be mapped to ERESTARTSYS */
5503static int brightness_set(int value)
5504{
5505 int cmos_cmd, inc, i, res;
5506 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005507 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005508
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005509 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5510 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005511 return -EINVAL;
5512
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005513 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005514 if (res < 0)
5515 return res;
5516
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005517 res = brightness_get_raw(&current_value);
5518 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005519 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005520
5521 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5522 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005523
5524 cmos_cmd = value > current_value ?
5525 TP_CMOS_BRIGHTNESS_UP :
5526 TP_CMOS_BRIGHTNESS_DOWN;
5527 inc = (value > current_value)? 1 : -1;
5528
5529 res = 0;
5530 for (i = current_value; i != value; i += inc) {
5531 if ((brightness_mode & 2) &&
5532 issue_thinkpad_cmos_command(cmos_cmd)) {
5533 res = -EIO;
5534 goto errout;
5535 }
5536 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005537 !acpi_ec_write(TP_EC_BACKLIGHT,
5538 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005539 res = -EIO;
5540 goto errout;;
5541 }
5542 }
5543
5544errout:
5545 mutex_unlock(&brightness_mutex);
5546 return res;
5547}
5548
5549/* sysfs backlight class ----------------------------------------------- */
5550
5551static int brightness_update_status(struct backlight_device *bd)
5552{
5553 /* it is the backlight class's job (caller) to handle
5554 * EINTR and other errors properly */
5555 return brightness_set(
5556 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5557 bd->props.power == FB_BLANK_UNBLANK) ?
5558 bd->props.brightness : 0);
5559}
Holger Macht8acb0252006-10-20 14:30:28 -07005560
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005561static int brightness_get(struct backlight_device *bd)
5562{
5563 int status, res;
5564
5565 res = brightness_get_raw(&status);
5566 if (res < 0)
5567 return 0; /* FIXME: teach backlight about error handling */
5568
5569 return status & TP_EC_BACKLIGHT_LVLMSK;
5570}
5571
Richard Purdie599a52d2007-02-10 23:07:48 +00005572static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005573 .get_brightness = brightness_get,
5574 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005575};
5576
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005577/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005578
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005579static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005580{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005581 int b;
5582
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005583 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5584
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005585 mutex_init(&brightness_mutex);
5586
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005587 /*
5588 * We always attempt to detect acpi support, so as to switch
5589 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5590 * going to publish a backlight interface
5591 */
5592 b = tpacpi_check_std_acpi_brightness_support();
5593 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005594
5595 if (acpi_video_backlight_support()) {
5596 if (brightness_enable > 1) {
5597 printk(TPACPI_NOTICE
5598 "Standard ACPI backlight interface "
5599 "available, not loading native one.\n");
5600 return 1;
5601 } else if (brightness_enable == 1) {
5602 printk(TPACPI_NOTICE
5603 "Backlight control force enabled, even if standard "
5604 "ACPI backlight interface is available\n");
5605 }
5606 } else {
5607 if (brightness_enable > 1) {
5608 printk(TPACPI_NOTICE
5609 "Standard ACPI backlight interface not "
5610 "available, thinkpad_acpi native "
5611 "brightness control enabled\n");
5612 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005613 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005614 }
5615
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005616 if (!brightness_enable) {
5617 dbg_printk(TPACPI_DBG_INIT,
5618 "brightness support disabled by "
5619 "module parameter\n");
5620 return 1;
5621 }
5622
5623 if (b > 16) {
5624 printk(TPACPI_ERR
5625 "Unsupported brightness interface, "
5626 "please contact %s\n", TPACPI_MAIL);
5627 return 1;
5628 }
5629 if (b == 16)
5630 tp_features.bright_16levels = 1;
5631
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005632 if (!brightness_mode) {
5633 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5634 brightness_mode = 2;
5635 else
5636 brightness_mode = 3;
5637
5638 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5639 brightness_mode);
5640 }
5641
5642 if (brightness_mode > 3)
5643 return -EINVAL;
5644
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005645 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005646 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005647
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005648 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005649 printk(TPACPI_INFO
5650 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005651
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005652 ibm_backlight_device = backlight_device_register(
5653 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5654 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005655 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005656 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005657 return PTR_ERR(ibm_backlight_device);
5658 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005659 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005660
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005661 ibm_backlight_device->props.max_brightness =
5662 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005663 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005664 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005665
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005666 return 0;
5667}
5668
5669static void brightness_exit(void)
5670{
5671 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005672 vdbg_printk(TPACPI_DBG_EXIT,
5673 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005674 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005675 }
5676}
5677
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005678static int brightness_read(char *p)
5679{
5680 int len = 0;
5681 int level;
5682
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005683 level = brightness_get(NULL);
5684 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005685 len += sprintf(p + len, "level:\t\tunreadable\n");
5686 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005687 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005688 len += sprintf(p + len, "commands:\tup, down\n");
5689 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005690 " (<level> is 0-%d)\n",
5691 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005692 }
5693
5694 return len;
5695}
5696
5697static int brightness_write(char *buf)
5698{
5699 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005700 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005701 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005702 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005703
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005704 level = brightness_get(NULL);
5705 if (level < 0)
5706 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005707
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005708 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005709 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005710 if (level < max_level)
5711 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005712 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005713 if (level > 0)
5714 level--;
5715 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5716 level >= 0 && level <= max_level) {
5717 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005718 } else
5719 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005720 }
5721
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005722 /*
5723 * Now we know what the final level should be, so we try to set it.
5724 * Doing it this way makes the syscall restartable in case of EINTR
5725 */
5726 rc = brightness_set(level);
5727 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005728}
5729
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005730static struct ibm_struct brightness_driver_data = {
5731 .name = "brightness",
5732 .read = brightness_read,
5733 .write = brightness_write,
5734 .exit = brightness_exit,
5735};
5736
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005737/*************************************************************************
5738 * Volume subdriver
5739 */
5740
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005741static int volume_offset = 0x30;
5742
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005743static int volume_read(char *p)
5744{
5745 int len = 0;
5746 u8 level;
5747
5748 if (!acpi_ec_read(volume_offset, &level)) {
5749 len += sprintf(p + len, "level:\t\tunreadable\n");
5750 } else {
5751 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5752 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5753 len += sprintf(p + len, "commands:\tup, down, mute\n");
5754 len += sprintf(p + len, "commands:\tlevel <level>"
5755 " (<level> is 0-15)\n");
5756 }
5757
5758 return len;
5759}
5760
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005761static int volume_write(char *buf)
5762{
5763 int cmos_cmd, inc, i;
5764 u8 level, mute;
5765 int new_level, new_mute;
5766 char *cmd;
5767
5768 while ((cmd = next_cmd(&buf))) {
5769 if (!acpi_ec_read(volume_offset, &level))
5770 return -EIO;
5771 new_mute = mute = level & 0x40;
5772 new_level = level = level & 0xf;
5773
5774 if (strlencmp(cmd, "up") == 0) {
5775 if (mute)
5776 new_mute = 0;
5777 else
5778 new_level = level == 15 ? 15 : level + 1;
5779 } else if (strlencmp(cmd, "down") == 0) {
5780 if (mute)
5781 new_mute = 0;
5782 else
5783 new_level = level == 0 ? 0 : level - 1;
5784 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5785 new_level >= 0 && new_level <= 15) {
5786 /* new_level set */
5787 } else if (strlencmp(cmd, "mute") == 0) {
5788 new_mute = 0x40;
5789 } else
5790 return -EINVAL;
5791
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005792 if (new_level != level) {
5793 /* mute doesn't change */
5794
5795 cmos_cmd = (new_level > level) ?
5796 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005797 inc = new_level > level ? 1 : -1;
5798
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005799 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005800 !acpi_ec_write(volume_offset, level)))
5801 return -EIO;
5802
5803 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005804 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805 !acpi_ec_write(volume_offset, i + inc))
5806 return -EIO;
5807
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005808 if (mute &&
5809 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5810 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005811 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005812 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005813 }
5814
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005815 if (new_mute != mute) {
5816 /* level doesn't change */
5817
5818 cmos_cmd = (new_mute) ?
5819 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005820
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005821 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005822 !acpi_ec_write(volume_offset, level + new_mute))
5823 return -EIO;
5824 }
5825 }
5826
5827 return 0;
5828}
5829
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005830static struct ibm_struct volume_driver_data = {
5831 .name = "volume",
5832 .read = volume_read,
5833 .write = volume_write,
5834};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005835
5836/*************************************************************************
5837 * Fan subdriver
5838 */
5839
5840/*
5841 * FAN ACCESS MODES
5842 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005843 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005844 * ACPI GFAN method: returns fan level
5845 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005846 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005847 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005848 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005849 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005850 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5851 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005852 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5853 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005854 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005855 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005856 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5857 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005858 *
5859 * Fan speed changes of any sort (including those caused by the
5860 * disengaged mode) are usually done slowly by the firmware as the
5861 * maximum ammount of fan duty cycle change per second seems to be
5862 * limited.
5863 *
5864 * Reading is not available if GFAN exists.
5865 * Writing is not available if SFAN exists.
5866 *
5867 * Bits
5868 * 7 automatic mode engaged;
5869 * (default operation mode of the ThinkPad)
5870 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005871 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005872 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005873 * the tachometer while the fan controller ramps up
5874 * the speed (which can take up to a few *minutes*).
5875 * Speeds up fan to 100% duty-cycle, which is far above
5876 * the standard RPM levels. It is not impossible that
5877 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005878 * 5-3 unused in some models. Extra bits for fan level
5879 * in others, but still useless as all values above
5880 * 7 map to the same speed as level 7 in these models.
5881 * 2-0 fan level (0..7 usually)
5882 * 0x00 = stop
5883 * 0x07 = max (set when temperatures critical)
5884 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005885 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005886 *
5887 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5888 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5889 * does so, its initial value is meaningless (0x07).
5890 *
5891 * For firmware bugs, refer to:
5892 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5893 *
5894 * ----
5895 *
5896 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5897 * Main fan tachometer reading (in RPM)
5898 *
5899 * This register is present on all ThinkPads with a new-style EC, and
5900 * it is known not to be present on the A21m/e, and T22, as there is
5901 * something else in offset 0x84 according to the ACPI DSDT. Other
5902 * ThinkPads from this same time period (and earlier) probably lack the
5903 * tachometer as well.
5904 *
5905 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5906 * was never fixed by IBM to report the EC firmware version string
5907 * probably support the tachometer (like the early X models), so
5908 * detecting it is quite hard. We need more data to know for sure.
5909 *
5910 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5911 * might result.
5912 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005913 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5914 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005915 *
5916 * For firmware bugs, refer to:
5917 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5918 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005919 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005920 * ThinkPad X31, X40, X41. Not available in the X60.
5921 *
5922 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5923 * high speed. ACPI DSDT seems to map these three speeds to levels
5924 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5925 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5926 *
5927 * The speeds are stored on handles
5928 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5929 *
5930 * There are three default speed sets, acessible as handles:
5931 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5932 *
5933 * ACPI DSDT switches which set is in use depending on various
5934 * factors.
5935 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005936 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005937 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5938 * but the ACPI tables just mention level 7.
5939 */
5940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005941enum { /* Fan control constants */
5942 fan_status_offset = 0x2f, /* EC register 0x2f */
5943 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5944 * 0x84 must be read before 0x85 */
5945
5946 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5947 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5948
5949 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5950};
5951
5952enum fan_status_access_mode {
5953 TPACPI_FAN_NONE = 0, /* No fan status or control */
5954 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5955 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5956};
5957
5958enum fan_control_access_mode {
5959 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5960 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5961 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5962 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5963};
5964
5965enum fan_control_commands {
5966 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5967 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5968 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5969 * and also watchdog cmd */
5970};
5971
5972static int fan_control_allowed;
5973
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005974static enum fan_status_access_mode fan_status_access_mode;
5975static enum fan_control_access_mode fan_control_access_mode;
5976static enum fan_control_commands fan_control_commands;
5977
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005978static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005979static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005980static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005981static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005982
5983static struct mutex fan_mutex;
5984
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005985static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005986static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005987
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005988TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5989TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005990 "\\FSPD", /* 600e/x, 770e, 770x */
5991 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005992TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993 "JFNS", /* 770x-JL */
5994 ); /* all others */
5995
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005996/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02005997 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5998 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5999 * be in auto mode (0x80).
6000 *
6001 * This is corrected by any write to HFSP either by the driver, or
6002 * by the firmware.
6003 *
6004 * We assume 0x07 really means auto mode while this quirk is active,
6005 * as this is far more likely than the ThinkPad being in level 7,
6006 * which is only used by the firmware during thermal emergencies.
6007 */
6008
6009static void fan_quirk1_detect(void)
6010{
6011 /* In some ThinkPads, neither the EC nor the ACPI
6012 * DSDT initialize the HFSP register, and it ends up
6013 * being initially set to 0x07 when it *could* be
6014 * either 0x07 or 0x80.
6015 *
6016 * Enable for TP-1Y (T43), TP-78 (R51e),
6017 * TP-76 (R52), TP-70 (T43, R52), which are known
6018 * to be buggy. */
6019 if (fan_control_initial_status == 0x07) {
6020 switch (thinkpad_id.ec_model) {
6021 case 0x5931: /* TP-1Y */
6022 case 0x3837: /* TP-78 */
6023 case 0x3637: /* TP-76 */
6024 case 0x3037: /* TP-70 */
6025 printk(TPACPI_NOTICE
6026 "fan_init: initial fan status is unknown, "
6027 "assuming it is in auto mode\n");
6028 tp_features.fan_ctrl_status_undef = 1;
6029 ;;
6030 }
6031 }
6032}
6033
6034static void fan_quirk1_handle(u8 *fan_status)
6035{
6036 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6037 if (*fan_status != fan_control_initial_status) {
6038 /* something changed the HFSP regisnter since
6039 * driver init time, so it is not undefined
6040 * anymore */
6041 tp_features.fan_ctrl_status_undef = 0;
6042 } else {
6043 /* Return most likely status. In fact, it
6044 * might be the only possible status */
6045 *fan_status = TP_EC_FAN_AUTO;
6046 }
6047 }
6048}
6049
6050/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006051 * Call with fan_mutex held
6052 */
6053static void fan_update_desired_level(u8 status)
6054{
6055 if ((status &
6056 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6057 if (status > 7)
6058 fan_control_desired_level = 7;
6059 else
6060 fan_control_desired_level = status;
6061 }
6062}
6063
6064static int fan_get_status(u8 *status)
6065{
6066 u8 s;
6067
6068 /* TODO:
6069 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6070
6071 switch (fan_status_access_mode) {
6072 case TPACPI_FAN_RD_ACPI_GFAN:
6073 /* 570, 600e/x, 770e, 770x */
6074
6075 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6076 return -EIO;
6077
6078 if (likely(status))
6079 *status = s & 0x07;
6080
6081 break;
6082
6083 case TPACPI_FAN_RD_TPEC:
6084 /* all except 570, 600e/x, 770e, 770x */
6085 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6086 return -EIO;
6087
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006088 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006089 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006090 fan_quirk1_handle(status);
6091 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006092
6093 break;
6094
6095 default:
6096 return -ENXIO;
6097 }
6098
6099 return 0;
6100}
6101
6102static int fan_get_status_safe(u8 *status)
6103{
6104 int rc;
6105 u8 s;
6106
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006107 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006108 return -ERESTARTSYS;
6109 rc = fan_get_status(&s);
6110 if (!rc)
6111 fan_update_desired_level(s);
6112 mutex_unlock(&fan_mutex);
6113
6114 if (status)
6115 *status = s;
6116
6117 return rc;
6118}
6119
6120static int fan_get_speed(unsigned int *speed)
6121{
6122 u8 hi, lo;
6123
6124 switch (fan_status_access_mode) {
6125 case TPACPI_FAN_RD_TPEC:
6126 /* all except 570, 600e/x, 770e, 770x */
6127 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6128 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6129 return -EIO;
6130
6131 if (likely(speed))
6132 *speed = (hi << 8) | lo;
6133
6134 break;
6135
6136 default:
6137 return -ENXIO;
6138 }
6139
6140 return 0;
6141}
6142
6143static int fan_set_level(int level)
6144{
6145 if (!fan_control_allowed)
6146 return -EPERM;
6147
6148 switch (fan_control_access_mode) {
6149 case TPACPI_FAN_WR_ACPI_SFAN:
6150 if (level >= 0 && level <= 7) {
6151 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6152 return -EIO;
6153 } else
6154 return -EINVAL;
6155 break;
6156
6157 case TPACPI_FAN_WR_ACPI_FANS:
6158 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006159 if (!(level & TP_EC_FAN_AUTO) &&
6160 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006161 ((level < 0) || (level > 7)))
6162 return -EINVAL;
6163
6164 /* safety net should the EC not support AUTO
6165 * or FULLSPEED mode bits and just ignore them */
6166 if (level & TP_EC_FAN_FULLSPEED)
6167 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006168 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006169 level |= 4; /* safety min speed 4 */
6170
6171 if (!acpi_ec_write(fan_status_offset, level))
6172 return -EIO;
6173 else
6174 tp_features.fan_ctrl_status_undef = 0;
6175 break;
6176
6177 default:
6178 return -ENXIO;
6179 }
6180 return 0;
6181}
6182
6183static int fan_set_level_safe(int level)
6184{
6185 int rc;
6186
6187 if (!fan_control_allowed)
6188 return -EPERM;
6189
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006190 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006191 return -ERESTARTSYS;
6192
6193 if (level == TPACPI_FAN_LAST_LEVEL)
6194 level = fan_control_desired_level;
6195
6196 rc = fan_set_level(level);
6197 if (!rc)
6198 fan_update_desired_level(level);
6199
6200 mutex_unlock(&fan_mutex);
6201 return rc;
6202}
6203
6204static int fan_set_enable(void)
6205{
6206 u8 s;
6207 int rc;
6208
6209 if (!fan_control_allowed)
6210 return -EPERM;
6211
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006212 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006213 return -ERESTARTSYS;
6214
6215 switch (fan_control_access_mode) {
6216 case TPACPI_FAN_WR_ACPI_FANS:
6217 case TPACPI_FAN_WR_TPEC:
6218 rc = fan_get_status(&s);
6219 if (rc < 0)
6220 break;
6221
6222 /* Don't go out of emergency fan mode */
6223 if (s != 7) {
6224 s &= 0x07;
6225 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6226 }
6227
6228 if (!acpi_ec_write(fan_status_offset, s))
6229 rc = -EIO;
6230 else {
6231 tp_features.fan_ctrl_status_undef = 0;
6232 rc = 0;
6233 }
6234 break;
6235
6236 case TPACPI_FAN_WR_ACPI_SFAN:
6237 rc = fan_get_status(&s);
6238 if (rc < 0)
6239 break;
6240
6241 s &= 0x07;
6242
6243 /* Set fan to at least level 4 */
6244 s |= 4;
6245
6246 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006247 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006248 else
6249 rc = 0;
6250 break;
6251
6252 default:
6253 rc = -ENXIO;
6254 }
6255
6256 mutex_unlock(&fan_mutex);
6257 return rc;
6258}
6259
6260static int fan_set_disable(void)
6261{
6262 int rc;
6263
6264 if (!fan_control_allowed)
6265 return -EPERM;
6266
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006267 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006268 return -ERESTARTSYS;
6269
6270 rc = 0;
6271 switch (fan_control_access_mode) {
6272 case TPACPI_FAN_WR_ACPI_FANS:
6273 case TPACPI_FAN_WR_TPEC:
6274 if (!acpi_ec_write(fan_status_offset, 0x00))
6275 rc = -EIO;
6276 else {
6277 fan_control_desired_level = 0;
6278 tp_features.fan_ctrl_status_undef = 0;
6279 }
6280 break;
6281
6282 case TPACPI_FAN_WR_ACPI_SFAN:
6283 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6284 rc = -EIO;
6285 else
6286 fan_control_desired_level = 0;
6287 break;
6288
6289 default:
6290 rc = -ENXIO;
6291 }
6292
6293
6294 mutex_unlock(&fan_mutex);
6295 return rc;
6296}
6297
6298static int fan_set_speed(int speed)
6299{
6300 int rc;
6301
6302 if (!fan_control_allowed)
6303 return -EPERM;
6304
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006305 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006306 return -ERESTARTSYS;
6307
6308 rc = 0;
6309 switch (fan_control_access_mode) {
6310 case TPACPI_FAN_WR_ACPI_FANS:
6311 if (speed >= 0 && speed <= 65535) {
6312 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6313 speed, speed, speed))
6314 rc = -EIO;
6315 } else
6316 rc = -EINVAL;
6317 break;
6318
6319 default:
6320 rc = -ENXIO;
6321 }
6322
6323 mutex_unlock(&fan_mutex);
6324 return rc;
6325}
6326
6327static void fan_watchdog_reset(void)
6328{
6329 static int fan_watchdog_active;
6330
6331 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6332 return;
6333
6334 if (fan_watchdog_active)
6335 cancel_delayed_work(&fan_watchdog_task);
6336
6337 if (fan_watchdog_maxinterval > 0 &&
6338 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6339 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006340 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006341 msecs_to_jiffies(fan_watchdog_maxinterval
6342 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006343 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006344 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006345 "watchdog will not trigger\n");
6346 }
6347 } else
6348 fan_watchdog_active = 0;
6349}
6350
6351static void fan_watchdog_fire(struct work_struct *ignored)
6352{
6353 int rc;
6354
6355 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6356 return;
6357
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006358 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006359 rc = fan_set_enable();
6360 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006361 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006362 "will try again later...\n", -rc);
6363 /* reschedule for later */
6364 fan_watchdog_reset();
6365 }
6366}
6367
6368/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006369 * SYSFS fan layout: hwmon compatible (device)
6370 *
6371 * pwm*_enable:
6372 * 0: "disengaged" mode
6373 * 1: manual mode
6374 * 2: native EC "auto" mode (recommended, hardware default)
6375 *
6376 * pwm*: set speed in manual mode, ignored otherwise.
6377 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6378 * interpolation.
6379 *
6380 * fan*_input: tachometer reading, RPM
6381 *
6382 *
6383 * SYSFS fan layout: extensions
6384 *
6385 * fan_watchdog (driver):
6386 * fan watchdog interval in seconds, 0 disables (default), max 120
6387 */
6388
6389/* sysfs fan pwm1_enable ----------------------------------------------- */
6390static ssize_t fan_pwm1_enable_show(struct device *dev,
6391 struct device_attribute *attr,
6392 char *buf)
6393{
6394 int res, mode;
6395 u8 status;
6396
6397 res = fan_get_status_safe(&status);
6398 if (res)
6399 return res;
6400
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006401 if (status & TP_EC_FAN_FULLSPEED) {
6402 mode = 0;
6403 } else if (status & TP_EC_FAN_AUTO) {
6404 mode = 2;
6405 } else
6406 mode = 1;
6407
6408 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6409}
6410
6411static ssize_t fan_pwm1_enable_store(struct device *dev,
6412 struct device_attribute *attr,
6413 const char *buf, size_t count)
6414{
6415 unsigned long t;
6416 int res, level;
6417
6418 if (parse_strtoul(buf, 2, &t))
6419 return -EINVAL;
6420
6421 switch (t) {
6422 case 0:
6423 level = TP_EC_FAN_FULLSPEED;
6424 break;
6425 case 1:
6426 level = TPACPI_FAN_LAST_LEVEL;
6427 break;
6428 case 2:
6429 level = TP_EC_FAN_AUTO;
6430 break;
6431 case 3:
6432 /* reserved for software-controlled auto mode */
6433 return -ENOSYS;
6434 default:
6435 return -EINVAL;
6436 }
6437
6438 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006439 if (res == -ENXIO)
6440 return -EINVAL;
6441 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006442 return res;
6443
6444 fan_watchdog_reset();
6445
6446 return count;
6447}
6448
6449static struct device_attribute dev_attr_fan_pwm1_enable =
6450 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6451 fan_pwm1_enable_show, fan_pwm1_enable_store);
6452
6453/* sysfs fan pwm1 ------------------------------------------------------ */
6454static ssize_t fan_pwm1_show(struct device *dev,
6455 struct device_attribute *attr,
6456 char *buf)
6457{
6458 int res;
6459 u8 status;
6460
6461 res = fan_get_status_safe(&status);
6462 if (res)
6463 return res;
6464
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006465 if ((status &
6466 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6467 status = fan_control_desired_level;
6468
6469 if (status > 7)
6470 status = 7;
6471
6472 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6473}
6474
6475static ssize_t fan_pwm1_store(struct device *dev,
6476 struct device_attribute *attr,
6477 const char *buf, size_t count)
6478{
6479 unsigned long s;
6480 int rc;
6481 u8 status, newlevel;
6482
6483 if (parse_strtoul(buf, 255, &s))
6484 return -EINVAL;
6485
6486 /* scale down from 0-255 to 0-7 */
6487 newlevel = (s >> 5) & 0x07;
6488
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006489 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006490 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006491
6492 rc = fan_get_status(&status);
6493 if (!rc && (status &
6494 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6495 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006496 if (rc == -ENXIO)
6497 rc = -EINVAL;
6498 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006499 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006500 fan_watchdog_reset();
6501 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006502 }
6503
6504 mutex_unlock(&fan_mutex);
6505 return (rc)? rc : count;
6506}
6507
6508static struct device_attribute dev_attr_fan_pwm1 =
6509 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6510 fan_pwm1_show, fan_pwm1_store);
6511
6512/* sysfs fan fan1_input ------------------------------------------------ */
6513static ssize_t fan_fan1_input_show(struct device *dev,
6514 struct device_attribute *attr,
6515 char *buf)
6516{
6517 int res;
6518 unsigned int speed;
6519
6520 res = fan_get_speed(&speed);
6521 if (res < 0)
6522 return res;
6523
6524 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6525}
6526
6527static struct device_attribute dev_attr_fan_fan1_input =
6528 __ATTR(fan1_input, S_IRUGO,
6529 fan_fan1_input_show, NULL);
6530
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006531/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006532static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6533 char *buf)
6534{
6535 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6536}
6537
6538static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6539 const char *buf, size_t count)
6540{
6541 unsigned long t;
6542
6543 if (parse_strtoul(buf, 120, &t))
6544 return -EINVAL;
6545
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006546 if (!fan_control_allowed)
6547 return -EPERM;
6548
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006549 fan_watchdog_maxinterval = t;
6550 fan_watchdog_reset();
6551
6552 return count;
6553}
6554
6555static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6556 fan_fan_watchdog_show, fan_fan_watchdog_store);
6557
6558/* --------------------------------------------------------------------- */
6559static struct attribute *fan_attributes[] = {
6560 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6561 &dev_attr_fan_fan1_input.attr,
6562 NULL
6563};
6564
6565static const struct attribute_group fan_attr_group = {
6566 .attrs = fan_attributes,
6567};
6568
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006569static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006570{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006571 int rc;
6572
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006573 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6574
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006575 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006576 fan_status_access_mode = TPACPI_FAN_NONE;
6577 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006578 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006579 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006580 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006581 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006582
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006583 TPACPI_ACPIHANDLE_INIT(fans);
6584 TPACPI_ACPIHANDLE_INIT(gfan);
6585 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006586
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006587 if (gfan_handle) {
6588 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006589 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006590 } else {
6591 /* all other ThinkPads: note that even old-style
6592 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006593 if (likely(acpi_ec_read(fan_status_offset,
6594 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006595 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006596 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006597 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006598 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006599 "ThinkPad ACPI EC access misbehaving, "
6600 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006601 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006602 }
6603 }
6604
6605 if (sfan_handle) {
6606 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006607 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006608 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006609 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006610 } else {
6611 if (!gfan_handle) {
6612 /* gfan without sfan means no fan control */
6613 /* all other models implement TP EC 0x2f control */
6614
6615 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006616 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006617 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006618 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006619 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006620 TPACPI_FAN_CMD_SPEED |
6621 TPACPI_FAN_CMD_LEVEL |
6622 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006623 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006624 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006625 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006626 TPACPI_FAN_CMD_LEVEL |
6627 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006628 }
6629 }
6630 }
6631
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006632 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6633 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6634 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6635 fan_status_access_mode, fan_control_access_mode);
6636
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006637 /* fan control master switch */
6638 if (!fan_control_allowed) {
6639 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6640 fan_control_commands = 0;
6641 dbg_printk(TPACPI_DBG_INIT,
6642 "fan control features disabled by parameter\n");
6643 }
6644
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006645 /* update fan_control_desired_level */
6646 if (fan_status_access_mode != TPACPI_FAN_NONE)
6647 fan_get_status_safe(NULL);
6648
6649 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6650 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006651 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006652 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006653 if (rc < 0)
6654 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006655
6656 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6657 &driver_attr_fan_watchdog);
6658 if (rc < 0) {
6659 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6660 &fan_attr_group);
6661 return rc;
6662 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006663 return 0;
6664 } else
6665 return 1;
6666}
6667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006668static void fan_exit(void)
6669{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006670 vdbg_printk(TPACPI_DBG_EXIT,
6671 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006672
6673 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006674 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006675 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6676 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006677
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006678 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006679 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006680}
6681
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006682static void fan_suspend(pm_message_t state)
6683{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006684 int rc;
6685
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006686 if (!fan_control_allowed)
6687 return;
6688
6689 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006690 fan_control_resume_level = 0;
6691 rc = fan_get_status_safe(&fan_control_resume_level);
6692 if (rc < 0)
6693 printk(TPACPI_NOTICE
6694 "failed to read fan level for later "
6695 "restore during resume: %d\n", rc);
6696
6697 /* if it is undefined, don't attempt to restore it.
6698 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006699 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006700 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006701}
6702
6703static void fan_resume(void)
6704{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006705 u8 current_level = 7;
6706 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006707 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006708
6709 /* DSDT *always* updates status on resume */
6710 tp_features.fan_ctrl_status_undef = 0;
6711
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006712 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006713 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006714 (fan_get_status_safe(&current_level) < 0))
6715 return;
6716
6717 switch (fan_control_access_mode) {
6718 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006719 /* never decrease fan level */
6720 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006721 break;
6722 case TPACPI_FAN_WR_ACPI_FANS:
6723 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006724 /* never decrease fan level, scale is:
6725 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6726 *
6727 * We expect the firmware to set either 7 or AUTO, but we
6728 * handle FULLSPEED out of paranoia.
6729 *
6730 * So, we can safely only restore FULLSPEED or 7, anything
6731 * else could slow the fan. Restoring AUTO is useless, at
6732 * best that's exactly what the DSDT already set (it is the
6733 * slower it uses).
6734 *
6735 * Always keep in mind that the DSDT *will* have set the
6736 * fans to what the vendor supposes is the best level. We
6737 * muck with it only to speed the fan up.
6738 */
6739 if (fan_control_resume_level != 7 &&
6740 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6741 return;
6742 else
6743 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6744 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006745 break;
6746 default:
6747 return;
6748 }
6749 if (do_set) {
6750 printk(TPACPI_NOTICE
6751 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006752 fan_control_resume_level);
6753 rc = fan_set_level_safe(fan_control_resume_level);
6754 if (rc < 0)
6755 printk(TPACPI_NOTICE
6756 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006757 }
6758}
6759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006760static int fan_read(char *p)
6761{
6762 int len = 0;
6763 int rc;
6764 u8 status;
6765 unsigned int speed = 0;
6766
6767 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006768 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006769 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006770 rc = fan_get_status_safe(&status);
6771 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006772 return rc;
6773
6774 len += sprintf(p + len, "status:\t\t%s\n"
6775 "level:\t\t%d\n",
6776 (status != 0) ? "enabled" : "disabled", status);
6777 break;
6778
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006779 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006780 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006781 rc = fan_get_status_safe(&status);
6782 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006783 return rc;
6784
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006785 len += sprintf(p + len, "status:\t\t%s\n",
6786 (status != 0) ? "enabled" : "disabled");
6787
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006788 rc = fan_get_speed(&speed);
6789 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006790 return rc;
6791
6792 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6793
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006794 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006795 /* Disengaged mode takes precedence */
6796 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006797 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006798 len += sprintf(p + len, "level:\t\tauto\n");
6799 else
6800 len += sprintf(p + len, "level:\t\t%d\n", status);
6801 break;
6802
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006803 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006804 default:
6805 len += sprintf(p + len, "status:\t\tnot supported\n");
6806 }
6807
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006808 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006809 len += sprintf(p + len, "commands:\tlevel <level>");
6810
6811 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006812 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006813 len += sprintf(p + len, " (<level> is 0-7)\n");
6814 break;
6815
6816 default:
6817 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006818 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006819 break;
6820 }
6821 }
6822
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006823 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006824 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006825 "commands:\twatchdog <timeout> (<timeout> "
6826 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006827
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006828 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006829 len += sprintf(p + len, "commands:\tspeed <speed>"
6830 " (<speed> is 0-65535)\n");
6831
6832 return len;
6833}
6834
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006835static int fan_write_cmd_level(const char *cmd, int *rc)
6836{
6837 int level;
6838
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006839 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006840 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006841 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006842 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006843 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006844 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006845 return 0;
6846
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006847 *rc = fan_set_level_safe(level);
6848 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006849 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006850 "access mode %d", fan_control_access_mode);
6851
6852 return 1;
6853}
6854
6855static int fan_write_cmd_enable(const char *cmd, int *rc)
6856{
6857 if (strlencmp(cmd, "enable") != 0)
6858 return 0;
6859
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006860 *rc = fan_set_enable();
6861 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006862 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006863 "access mode %d", fan_control_access_mode);
6864
6865 return 1;
6866}
6867
6868static int fan_write_cmd_disable(const char *cmd, int *rc)
6869{
6870 if (strlencmp(cmd, "disable") != 0)
6871 return 0;
6872
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006873 *rc = fan_set_disable();
6874 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006875 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006876 "access mode %d", fan_control_access_mode);
6877
6878 return 1;
6879}
6880
6881static int fan_write_cmd_speed(const char *cmd, int *rc)
6882{
6883 int speed;
6884
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006885 /* TODO:
6886 * Support speed <low> <medium> <high> ? */
6887
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006888 if (sscanf(cmd, "speed %d", &speed) != 1)
6889 return 0;
6890
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006891 *rc = fan_set_speed(speed);
6892 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006893 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006894 "access mode %d", fan_control_access_mode);
6895
6896 return 1;
6897}
6898
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006899static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6900{
6901 int interval;
6902
6903 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6904 return 0;
6905
6906 if (interval < 0 || interval > 120)
6907 *rc = -EINVAL;
6908 else
6909 fan_watchdog_maxinterval = interval;
6910
6911 return 1;
6912}
6913
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006914static int fan_write(char *buf)
6915{
6916 char *cmd;
6917 int rc = 0;
6918
6919 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006920 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006921 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006922 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006923 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006924 fan_write_cmd_disable(cmd, &rc) ||
6925 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006926 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006927 fan_write_cmd_speed(cmd, &rc))
6928 )
6929 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006930 else if (!rc)
6931 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006932 }
6933
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006934 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006935}
6936
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006937static struct ibm_struct fan_driver_data = {
6938 .name = "fan",
6939 .read = fan_read,
6940 .write = fan_write,
6941 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006942 .suspend = fan_suspend,
6943 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006944};
6945
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006946/****************************************************************************
6947 ****************************************************************************
6948 *
6949 * Infrastructure
6950 *
6951 ****************************************************************************
6952 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006954/* sysfs name ---------------------------------------------------------- */
6955static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6956 struct device_attribute *attr,
6957 char *buf)
6958{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006959 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006960}
6961
6962static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6963 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6964
6965/* --------------------------------------------------------------------- */
6966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006967/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006968static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006969
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006970/*
6971 * Module and infrastructure proble, init and exit handling
6972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006974static int force_load;
6975
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006976#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006977static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006978{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006979 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006980
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006981 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006982}
6983#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6984
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006985static void ibm_exit(struct ibm_struct *ibm)
6986{
6987 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6988
6989 list_del_init(&ibm->all_drivers);
6990
6991 if (ibm->flags.acpi_notify_installed) {
6992 dbg_printk(TPACPI_DBG_EXIT,
6993 "%s: acpi_remove_notify_handler\n", ibm->name);
6994 BUG_ON(!ibm->acpi);
6995 acpi_remove_notify_handler(*ibm->acpi->handle,
6996 ibm->acpi->type,
6997 dispatch_acpi_notify);
6998 ibm->flags.acpi_notify_installed = 0;
6999 ibm->flags.acpi_notify_installed = 0;
7000 }
7001
7002 if (ibm->flags.proc_created) {
7003 dbg_printk(TPACPI_DBG_EXIT,
7004 "%s: remove_proc_entry\n", ibm->name);
7005 remove_proc_entry(ibm->name, proc_dir);
7006 ibm->flags.proc_created = 0;
7007 }
7008
7009 if (ibm->flags.acpi_driver_registered) {
7010 dbg_printk(TPACPI_DBG_EXIT,
7011 "%s: acpi_bus_unregister_driver\n", ibm->name);
7012 BUG_ON(!ibm->acpi);
7013 acpi_bus_unregister_driver(ibm->acpi->driver);
7014 kfree(ibm->acpi->driver);
7015 ibm->acpi->driver = NULL;
7016 ibm->flags.acpi_driver_registered = 0;
7017 }
7018
7019 if (ibm->flags.init_called && ibm->exit) {
7020 ibm->exit();
7021 ibm->flags.init_called = 0;
7022 }
7023
7024 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7025}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007026
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007027static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028{
7029 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007030 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 struct proc_dir_entry *entry;
7032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007033 BUG_ON(ibm == NULL);
7034
7035 INIT_LIST_HEAD(&ibm->all_drivers);
7036
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007037 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 return 0;
7039
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007040 dbg_printk(TPACPI_DBG_INIT,
7041 "probing for %s\n", ibm->name);
7042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007043 if (iibm->init) {
7044 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007045 if (ret > 0)
7046 return 0; /* probe failed */
7047 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007049
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007050 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 }
7052
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007053 if (ibm->acpi) {
7054 if (ibm->acpi->hid) {
7055 ret = register_tpacpi_subdriver(ibm);
7056 if (ret)
7057 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007058 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007059
7060 if (ibm->acpi->notify) {
7061 ret = setup_acpi_notify(ibm);
7062 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007063 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007064 ibm->name);
7065 ret = 0;
7066 goto err_out;
7067 }
7068 if (ret < 0)
7069 goto err_out;
7070 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007071 }
7072
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007073 dbg_printk(TPACPI_DBG_INIT,
7074 "%s installed\n", ibm->name);
7075
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007076 if (ibm->read) {
7077 entry = create_proc_entry(ibm->name,
7078 S_IFREG | S_IRUGO | S_IWUSR,
7079 proc_dir);
7080 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007081 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007082 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007083 ret = -ENODEV;
7084 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007085 }
7086 entry->owner = THIS_MODULE;
7087 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007088 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007089 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007090 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007091 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007094 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007097
7098err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007099 dbg_printk(TPACPI_DBG_INIT,
7100 "%s: at error exit path with result %d\n",
7101 ibm->name, ret);
7102
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007103 ibm_exit(ibm);
7104 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105}
7106
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007107/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007109/* returns 0 - probe ok, or < 0 - probe error.
7110 * Probe ok doesn't mean thinkpad found.
7111 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7112static int __must_check __init get_thinkpad_model_data(
7113 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007114{
Jeff Garzik18552562007-10-03 15:15:40 -04007115 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007116 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007117 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007118
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007119 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007120 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007121
7122 memset(tp, 0, sizeof(*tp));
7123
7124 if (dmi_name_in_vendors("IBM"))
7125 tp->vendor = PCI_VENDOR_ID_IBM;
7126 else if (dmi_name_in_vendors("LENOVO"))
7127 tp->vendor = PCI_VENDOR_ID_LENOVO;
7128 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007129 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007130
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007131 s = dmi_get_system_info(DMI_BIOS_VERSION);
7132 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7133 if (s && !tp->bios_version_str)
7134 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007135 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007136 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007137 tp->bios_model = tp->bios_version_str[0]
7138 | (tp->bios_version_str[1] << 8);
7139
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007140 /*
7141 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7142 * X32 or newer, all Z series; Some models must have an
7143 * up-to-date BIOS or they will not be detected.
7144 *
7145 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7146 */
7147 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007148 if (sscanf(dev->name,
7149 "IBM ThinkPad Embedded Controller -[%17c",
7150 ec_fw_string) == 1) {
7151 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7152 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007153
7154 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007155 if (!tp->ec_version_str)
7156 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007157 tp->ec_model = ec_fw_string[0]
7158 | (ec_fw_string[1] << 8);
7159 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007160 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007161 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007162
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007163 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7164 if (s && !strnicmp(s, "ThinkPad", 8)) {
7165 tp->model_str = kstrdup(s, GFP_KERNEL);
7166 if (!tp->model_str)
7167 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007168 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007169
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007170 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7171 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7172 if (s && !tp->nummodel_str)
7173 return -ENOMEM;
7174
7175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176}
7177
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007178static int __init probe_for_thinkpad(void)
7179{
7180 int is_thinkpad;
7181
7182 if (acpi_disabled)
7183 return -ENODEV;
7184
7185 /*
7186 * Non-ancient models have better DMI tagging, but very old models
7187 * don't.
7188 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007189 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007190
7191 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007192 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007193 if (!ec_handle) {
7194 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007195 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007196 "Not yet supported ThinkPad detected!\n");
7197 return -ENODEV;
7198 }
7199
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007200 /*
7201 * Risks a regression on very old machines, but reduces potential
7202 * false positives a damn great deal
7203 */
7204 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007205 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007206
7207 if (!is_thinkpad && !force_load)
7208 return -ENODEV;
7209
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007210 return 0;
7211}
7212
7213
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007214/* Module init, exit, parameters */
7215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007216static struct ibm_init_struct ibms_init[] __initdata = {
7217 {
7218 .init = thinkpad_acpi_driver_init,
7219 .data = &thinkpad_acpi_driver_data,
7220 },
7221 {
7222 .init = hotkey_init,
7223 .data = &hotkey_driver_data,
7224 },
7225 {
7226 .init = bluetooth_init,
7227 .data = &bluetooth_driver_data,
7228 },
7229 {
7230 .init = wan_init,
7231 .data = &wan_driver_data,
7232 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007233 {
7234 .init = uwb_init,
7235 .data = &uwb_driver_data,
7236 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007237#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007238 {
7239 .init = video_init,
7240 .data = &video_driver_data,
7241 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007242#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007243 {
7244 .init = light_init,
7245 .data = &light_driver_data,
7246 },
7247#ifdef CONFIG_THINKPAD_ACPI_DOCK
7248 {
7249 .init = dock_init,
7250 .data = &dock_driver_data[0],
7251 },
7252 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007253 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007254 .data = &dock_driver_data[1],
7255 },
7256#endif
7257#ifdef CONFIG_THINKPAD_ACPI_BAY
7258 {
7259 .init = bay_init,
7260 .data = &bay_driver_data,
7261 },
7262#endif
7263 {
7264 .init = cmos_init,
7265 .data = &cmos_driver_data,
7266 },
7267 {
7268 .init = led_init,
7269 .data = &led_driver_data,
7270 },
7271 {
7272 .init = beep_init,
7273 .data = &beep_driver_data,
7274 },
7275 {
7276 .init = thermal_init,
7277 .data = &thermal_driver_data,
7278 },
7279 {
7280 .data = &ecdump_driver_data,
7281 },
7282 {
7283 .init = brightness_init,
7284 .data = &brightness_driver_data,
7285 },
7286 {
7287 .data = &volume_driver_data,
7288 },
7289 {
7290 .init = fan_init,
7291 .data = &fan_driver_data,
7292 },
7293};
7294
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007295static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7296{
7297 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007298 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007299
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007300 if (!kp || !kp->name || !val)
7301 return -EINVAL;
7302
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007303 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7304 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007305 WARN_ON(ibm == NULL);
7306
7307 if (!ibm || !ibm->name)
7308 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007309
7310 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7311 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007312 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007313 strcpy(ibms_init[i].param, val);
7314 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007315 return 0;
7316 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007317 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007318
7319 return -EINVAL;
7320}
7321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007322module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007323MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007324 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007325
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007326module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007327MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007328
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007329module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007330MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007331 "Attempts to load the driver even on a "
7332 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007333
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007334module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007335MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007336 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007337
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007338module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007339MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007340 "Selects brightness control strategy: "
7341 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007342
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007343module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007344MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007345 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007346
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007347module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007348MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007349 "used for backwards compatibility with userspace, "
7350 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007351
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007352#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007353 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007354 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007355 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007356
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007357TPACPI_PARAM(hotkey);
7358TPACPI_PARAM(bluetooth);
7359TPACPI_PARAM(video);
7360TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007361#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007362TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007363#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007364#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007365TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007366#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007367TPACPI_PARAM(cmos);
7368TPACPI_PARAM(led);
7369TPACPI_PARAM(beep);
7370TPACPI_PARAM(ecdump);
7371TPACPI_PARAM(brightness);
7372TPACPI_PARAM(volume);
7373TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007374
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007375#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7376module_param(dbg_wlswemul, uint, 0);
7377MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7378module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7379MODULE_PARM_DESC(wlsw_state,
7380 "Initial state of the emulated WLSW switch");
7381
7382module_param(dbg_bluetoothemul, uint, 0);
7383MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7384module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7385MODULE_PARM_DESC(bluetooth_state,
7386 "Initial state of the emulated bluetooth switch");
7387
7388module_param(dbg_wwanemul, uint, 0);
7389MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7390module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7391MODULE_PARM_DESC(wwan_state,
7392 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007393
7394module_param(dbg_uwbemul, uint, 0);
7395MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7396module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7397MODULE_PARM_DESC(uwb_state,
7398 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007399#endif
7400
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007401static void thinkpad_acpi_module_exit(void)
7402{
7403 struct ibm_struct *ibm, *itmp;
7404
7405 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7406
7407 list_for_each_entry_safe_reverse(ibm, itmp,
7408 &tpacpi_all_drivers,
7409 all_drivers) {
7410 ibm_exit(ibm);
7411 }
7412
7413 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7414
7415 if (tpacpi_inputdev) {
7416 if (tp_features.input_device_registered)
7417 input_unregister_device(tpacpi_inputdev);
7418 else
7419 input_free_device(tpacpi_inputdev);
7420 }
7421
7422 if (tpacpi_hwmon)
7423 hwmon_device_unregister(tpacpi_hwmon);
7424
7425 if (tp_features.sensors_pdev_attrs_registered)
7426 device_remove_file(&tpacpi_sensors_pdev->dev,
7427 &dev_attr_thinkpad_acpi_pdev_name);
7428 if (tpacpi_sensors_pdev)
7429 platform_device_unregister(tpacpi_sensors_pdev);
7430 if (tpacpi_pdev)
7431 platform_device_unregister(tpacpi_pdev);
7432
7433 if (tp_features.sensors_pdrv_attrs_registered)
7434 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7435 if (tp_features.platform_drv_attrs_registered)
7436 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7437
7438 if (tp_features.sensors_pdrv_registered)
7439 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7440
7441 if (tp_features.platform_drv_registered)
7442 platform_driver_unregister(&tpacpi_pdriver);
7443
7444 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007445 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007446
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007447 if (tpacpi_wq)
7448 destroy_workqueue(tpacpi_wq);
7449
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007450 kfree(thinkpad_id.bios_version_str);
7451 kfree(thinkpad_id.ec_version_str);
7452 kfree(thinkpad_id.model_str);
7453}
7454
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007455
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007456static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457{
7458 int ret, i;
7459
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007460 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7461
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007462 /* Parameter checking */
7463 if (hotkey_report_mode > 2)
7464 return -EINVAL;
7465
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007466 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007467
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007468 ret = get_thinkpad_model_data(&thinkpad_id);
7469 if (ret) {
7470 printk(TPACPI_ERR
7471 "unable to get DMI data: %d\n", ret);
7472 thinkpad_acpi_module_exit();
7473 return ret;
7474 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007475 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007476 if (ret) {
7477 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007478 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007481 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007482
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007483 TPACPI_ACPIHANDLE_INIT(ecrd);
7484 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007486 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7487 if (!tpacpi_wq) {
7488 thinkpad_acpi_module_exit();
7489 return -ENOMEM;
7490 }
7491
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007492 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007494 printk(TPACPI_ERR
7495 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007496 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 return -ENODEV;
7498 }
7499 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007500
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007501 ret = platform_driver_register(&tpacpi_pdriver);
7502 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007503 printk(TPACPI_ERR
7504 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007505 thinkpad_acpi_module_exit();
7506 return ret;
7507 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007508 tp_features.platform_drv_registered = 1;
7509
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007510 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7511 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007512 printk(TPACPI_ERR
7513 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007514 thinkpad_acpi_module_exit();
7515 return ret;
7516 }
7517 tp_features.sensors_pdrv_registered = 1;
7518
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007519 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007520 if (!ret) {
7521 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007522 ret = tpacpi_create_driver_attributes(
7523 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007524 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007525 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007526 printk(TPACPI_ERR
7527 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007528 thinkpad_acpi_module_exit();
7529 return ret;
7530 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007531 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007532
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007533
7534 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007535 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007536 NULL, 0);
7537 if (IS_ERR(tpacpi_pdev)) {
7538 ret = PTR_ERR(tpacpi_pdev);
7539 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007540 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007541 thinkpad_acpi_module_exit();
7542 return ret;
7543 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007544 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007545 TPACPI_HWMON_DRVR_NAME,
7546 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007547 if (IS_ERR(tpacpi_sensors_pdev)) {
7548 ret = PTR_ERR(tpacpi_sensors_pdev);
7549 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007550 printk(TPACPI_ERR
7551 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007552 thinkpad_acpi_module_exit();
7553 return ret;
7554 }
7555 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7556 &dev_attr_thinkpad_acpi_pdev_name);
7557 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007558 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007559 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007560 thinkpad_acpi_module_exit();
7561 return ret;
7562 }
7563 tp_features.sensors_pdev_attrs_registered = 1;
7564 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007565 if (IS_ERR(tpacpi_hwmon)) {
7566 ret = PTR_ERR(tpacpi_hwmon);
7567 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007568 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007569 thinkpad_acpi_module_exit();
7570 return ret;
7571 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007572 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007573 tpacpi_inputdev = input_allocate_device();
7574 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007575 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007576 thinkpad_acpi_module_exit();
7577 return -ENOMEM;
7578 } else {
7579 /* Prepare input device, but don't register */
7580 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007581 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007582 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007583 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7584 thinkpad_id.vendor :
7585 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007586 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7587 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7588 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007589 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7590 ret = ibm_init(&ibms_init[i]);
7591 if (ret >= 0 && *ibms_init[i].param)
7592 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007594 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 return ret;
7596 }
7597 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007598 ret = input_register_device(tpacpi_inputdev);
7599 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007600 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007601 thinkpad_acpi_module_exit();
7602 return ret;
7603 } else {
7604 tp_features.input_device_registered = 1;
7605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007607 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 return 0;
7609}
7610
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007611MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7612
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007613/*
7614 * DMI matching for module autoloading
7615 *
7616 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7617 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7618 *
7619 * Only models listed in thinkwiki will be supported, so add yours
7620 * if it is not there yet.
7621 */
7622#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007623 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007624
7625/* Non-ancient thinkpads */
7626MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7627MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7628
7629/* Ancient thinkpad BIOSes have to be identified by
7630 * BIOS type or model number, and there are far less
7631 * BIOS types than model numbers... */
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007632IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7633IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7634IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007635
7636MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007637MODULE_DESCRIPTION(TPACPI_DESC);
7638MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007639MODULE_LICENSE("GPL");
7640
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007641module_init(thinkpad_acpi_module_init);
7642module_exit(thinkpad_acpi_module_exit);