blob: 852be7c1a1721ca9e08fad41d07fc5bc76b38137 [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 Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
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 Holschuh94954cc2007-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 Holschuh94954cc2007-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
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001428static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001429static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001430static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001431static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001432
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001433static unsigned int hotkey_report_mode;
1434
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001435static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001436
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001437static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001438
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001439#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1440#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001441 do { \
1442 mutex_lock(&hotkey_thread_data_mutex); \
1443 hotkey_config_change++; \
1444 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001445#define HOTKEY_CONFIG_CRITICAL_END \
1446 mutex_unlock(&hotkey_thread_data_mutex);
1447#else
1448#define HOTKEY_CONFIG_CRITICAL_START
1449#define HOTKEY_CONFIG_CRITICAL_END
1450#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1451
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001452/* HKEY.MHKG() return bits */
1453#define TP_HOTKEY_TABLET_MASK (1 << 3)
1454
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001455static int hotkey_get_wlsw(int *status)
1456{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001457#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1458 if (dbg_wlswemul) {
1459 *status = !!tpacpi_wlsw_emulstate;
1460 return 0;
1461 }
1462#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001463 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1464 return -EIO;
1465 return 0;
1466}
1467
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001468static int hotkey_get_tablet_mode(int *status)
1469{
1470 int s;
1471
1472 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1473 return -EIO;
1474
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001475 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1476 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001477}
1478
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001479/*
1480 * Call with hotkey_mutex held
1481 */
1482static int hotkey_mask_get(void)
1483{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001484 u32 m = 0;
1485
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001486 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001487 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001488 return -EIO;
1489 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001490 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001491
1492 return 0;
1493}
1494
1495/*
1496 * Call with hotkey_mutex held
1497 */
1498static int hotkey_mask_set(u32 mask)
1499{
1500 int i;
1501 int rc = 0;
1502
1503 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001504 if (!tp_warned.hotkey_mask_ff &&
1505 (mask == 0xffff || mask == 0xffffff ||
1506 mask == 0xffffffff)) {
1507 tp_warned.hotkey_mask_ff = 1;
1508 printk(TPACPI_NOTICE
1509 "setting the hotkey mask to 0x%08x is likely "
1510 "not the best way to go about it\n", mask);
1511 printk(TPACPI_NOTICE
1512 "please consider using the driver defaults, "
1513 "and refer to up-to-date thinkpad-acpi "
1514 "documentation\n");
1515 }
1516
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001517 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001518 for (i = 0; i < 32; i++) {
1519 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001520 /* enable in firmware mask only keys not in NVRAM
1521 * mode, but enable the key in the cached hotkey_mask
1522 * regardless of mode, or the key will end up
1523 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001524 if (!acpi_evalf(hkey_handle,
1525 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001526 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001527 rc = -EIO;
1528 break;
1529 } else {
1530 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1531 }
1532 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001533 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001534
1535 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001536 if (!hotkey_mask_get() && !rc &&
1537 (hotkey_mask & ~hotkey_source_mask) !=
1538 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001539 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001540 "requested hot key mask 0x%08x, but "
1541 "firmware forced it to 0x%08x\n",
1542 mask, hotkey_mask);
1543 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001544 } else {
1545#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1546 HOTKEY_CONFIG_CRITICAL_START
1547 hotkey_mask = mask & hotkey_source_mask;
1548 HOTKEY_CONFIG_CRITICAL_END
1549 hotkey_mask_get();
1550 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001551 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001552 "requested hot key mask 0x%08x, "
1553 "forced to 0x%08x (NVRAM poll mask is "
1554 "0x%08x): no firmware mask support\n",
1555 mask, hotkey_mask, hotkey_source_mask);
1556 }
1557#else
1558 hotkey_mask_get();
1559 rc = -ENXIO;
1560#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001561 }
1562
1563 return rc;
1564}
1565
1566static int hotkey_status_get(int *status)
1567{
1568 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1569 return -EIO;
1570
1571 return 0;
1572}
1573
1574static int hotkey_status_set(int status)
1575{
1576 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1577 return -EIO;
1578
1579 return 0;
1580}
1581
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001582static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001583{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001584 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001585
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001586 if (tp_features.hotkey_tablet &&
1587 !hotkey_get_tablet_mode(&state)) {
1588 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001589
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001590 input_report_switch(tpacpi_inputdev,
1591 SW_TABLET_MODE, !!state);
1592 input_sync(tpacpi_inputdev);
1593
1594 mutex_unlock(&tpacpi_inputdev_send_mutex);
1595 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001596}
1597
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001598static void tpacpi_input_send_key(unsigned int scancode)
1599{
1600 unsigned int keycode;
1601
1602 keycode = hotkey_keycode_map[scancode];
1603
1604 if (keycode != KEY_RESERVED) {
1605 mutex_lock(&tpacpi_inputdev_send_mutex);
1606
1607 input_report_key(tpacpi_inputdev, keycode, 1);
1608 if (keycode == KEY_UNKNOWN)
1609 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1610 scancode);
1611 input_sync(tpacpi_inputdev);
1612
1613 input_report_key(tpacpi_inputdev, keycode, 0);
1614 if (keycode == KEY_UNKNOWN)
1615 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1616 scancode);
1617 input_sync(tpacpi_inputdev);
1618
1619 mutex_unlock(&tpacpi_inputdev_send_mutex);
1620 }
1621}
1622
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001623#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1624static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1625
1626static void tpacpi_hotkey_send_key(unsigned int scancode)
1627{
1628 tpacpi_input_send_key(scancode);
1629 if (hotkey_report_mode < 2) {
1630 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1631 0x80, 0x1001 + scancode);
1632 }
1633}
1634
1635static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1636{
1637 u8 d;
1638
1639 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1640 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1641 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1642 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1643 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1644 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1645 }
1646 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1647 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1648 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1649 }
1650 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1651 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1652 n->displayexp_toggle =
1653 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1654 }
1655 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1656 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1657 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1658 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1659 n->brightness_toggle =
1660 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1661 }
1662 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1663 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1664 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1665 >> TP_NVRAM_POS_LEVEL_VOLUME;
1666 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1667 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1668 }
1669}
1670
1671#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001672 do { \
1673 if ((mask & (1 << __scancode)) && \
1674 oldn->__member != newn->__member) \
1675 tpacpi_hotkey_send_key(__scancode); \
1676 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001677
1678#define TPACPI_MAY_SEND_KEY(__scancode) \
1679 do { if (mask & (1 << __scancode)) \
1680 tpacpi_hotkey_send_key(__scancode); } while (0)
1681
1682static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001683 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001684 u32 mask)
1685{
1686 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1687 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1688 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1689 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1690
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1692
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1694
1695 /* handle volume */
1696 if (oldn->volume_toggle != newn->volume_toggle) {
1697 if (oldn->mute != newn->mute) {
1698 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1699 }
1700 if (oldn->volume_level > newn->volume_level) {
1701 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1702 } else if (oldn->volume_level < newn->volume_level) {
1703 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1704 } else if (oldn->mute == newn->mute) {
1705 /* repeated key presses that didn't change state */
1706 if (newn->mute) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1708 } else if (newn->volume_level != 0) {
1709 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1710 } else {
1711 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1712 }
1713 }
1714 }
1715
1716 /* handle brightness */
1717 if (oldn->brightness_toggle != newn->brightness_toggle) {
1718 if (oldn->brightness_level < newn->brightness_level) {
1719 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1720 } else if (oldn->brightness_level > newn->brightness_level) {
1721 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1722 } else {
1723 /* repeated key presses that didn't change state */
1724 if (newn->brightness_level != 0) {
1725 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1726 } else {
1727 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1728 }
1729 }
1730 }
1731}
1732
1733#undef TPACPI_COMPARE_KEY
1734#undef TPACPI_MAY_SEND_KEY
1735
1736static int hotkey_kthread(void *data)
1737{
1738 struct tp_nvram_state s[2];
1739 u32 mask;
1740 unsigned int si, so;
1741 unsigned long t;
1742 unsigned int change_detector, must_reset;
1743
1744 mutex_lock(&hotkey_thread_mutex);
1745
1746 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1747 goto exit;
1748
1749 set_freezable();
1750
1751 so = 0;
1752 si = 1;
1753 t = 0;
1754
1755 /* Initial state for compares */
1756 mutex_lock(&hotkey_thread_data_mutex);
1757 change_detector = hotkey_config_change;
1758 mask = hotkey_source_mask & hotkey_mask;
1759 mutex_unlock(&hotkey_thread_data_mutex);
1760 hotkey_read_nvram(&s[so], mask);
1761
1762 while (!kthread_should_stop() && hotkey_poll_freq) {
1763 if (t == 0)
1764 t = 1000/hotkey_poll_freq;
1765 t = msleep_interruptible(t);
1766 if (unlikely(kthread_should_stop()))
1767 break;
1768 must_reset = try_to_freeze();
1769 if (t > 0 && !must_reset)
1770 continue;
1771
1772 mutex_lock(&hotkey_thread_data_mutex);
1773 if (must_reset || hotkey_config_change != change_detector) {
1774 /* forget old state on thaw or config change */
1775 si = so;
1776 t = 0;
1777 change_detector = hotkey_config_change;
1778 }
1779 mask = hotkey_source_mask & hotkey_mask;
1780 mutex_unlock(&hotkey_thread_data_mutex);
1781
1782 if (likely(mask)) {
1783 hotkey_read_nvram(&s[si], mask);
1784 if (likely(si != so)) {
1785 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001786 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001787 }
1788 }
1789
1790 so = si;
1791 si ^= 1;
1792 }
1793
1794exit:
1795 mutex_unlock(&hotkey_thread_mutex);
1796 return 0;
1797}
1798
1799static void hotkey_poll_stop_sync(void)
1800{
1801 if (tpacpi_hotkey_task) {
1802 if (frozen(tpacpi_hotkey_task) ||
1803 freezing(tpacpi_hotkey_task))
1804 thaw_process(tpacpi_hotkey_task);
1805
1806 kthread_stop(tpacpi_hotkey_task);
1807 tpacpi_hotkey_task = NULL;
1808 mutex_lock(&hotkey_thread_mutex);
1809 /* at this point, the thread did exit */
1810 mutex_unlock(&hotkey_thread_mutex);
1811 }
1812}
1813
1814/* call with hotkey_mutex held */
1815static void hotkey_poll_setup(int may_warn)
1816{
1817 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1818 hotkey_poll_freq > 0 &&
1819 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1820 if (!tpacpi_hotkey_task) {
1821 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001822 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001823 if (IS_ERR(tpacpi_hotkey_task)) {
1824 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001825 printk(TPACPI_ERR
1826 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001827 "for hotkey polling\n");
1828 }
1829 }
1830 } else {
1831 hotkey_poll_stop_sync();
1832 if (may_warn &&
1833 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001834 printk(TPACPI_NOTICE
1835 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001836 "which is currently disabled\n",
1837 hotkey_source_mask);
1838 }
1839 }
1840}
1841
1842static void hotkey_poll_setup_safe(int may_warn)
1843{
1844 mutex_lock(&hotkey_mutex);
1845 hotkey_poll_setup(may_warn);
1846 mutex_unlock(&hotkey_mutex);
1847}
1848
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001849#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1850
1851static void hotkey_poll_setup_safe(int __unused)
1852{
1853}
1854
1855#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1856
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001857static int hotkey_inputdev_open(struct input_dev *dev)
1858{
1859 switch (tpacpi_lifecycle) {
1860 case TPACPI_LIFE_INIT:
1861 /*
1862 * hotkey_init will call hotkey_poll_setup_safe
1863 * at the appropriate moment
1864 */
1865 return 0;
1866 case TPACPI_LIFE_EXITING:
1867 return -EBUSY;
1868 case TPACPI_LIFE_RUNNING:
1869 hotkey_poll_setup_safe(0);
1870 return 0;
1871 }
1872
1873 /* Should only happen if tpacpi_lifecycle is corrupt */
1874 BUG();
1875 return -EBUSY;
1876}
1877
1878static void hotkey_inputdev_close(struct input_dev *dev)
1879{
1880 /* disable hotkey polling when possible */
1881 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1882 hotkey_poll_setup_safe(0);
1883}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001884
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001885/* sysfs hotkey enable ------------------------------------------------- */
1886static ssize_t hotkey_enable_show(struct device *dev,
1887 struct device_attribute *attr,
1888 char *buf)
1889{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001890 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001891
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001892 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001893 if (res)
1894 return res;
1895
1896 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1897}
1898
1899static ssize_t hotkey_enable_store(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t count)
1902{
1903 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001904 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001905
1906 if (parse_strtoul(buf, 1, &t))
1907 return -EINVAL;
1908
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001909 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001910
1911 return (res) ? res : count;
1912}
1913
1914static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001915 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001916 hotkey_enable_show, hotkey_enable_store);
1917
1918/* sysfs hotkey mask --------------------------------------------------- */
1919static ssize_t hotkey_mask_show(struct device *dev,
1920 struct device_attribute *attr,
1921 char *buf)
1922{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001923 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001924
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001925 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001926 return -ERESTARTSYS;
1927 res = hotkey_mask_get();
1928 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001929
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001930 return (res)?
1931 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001932}
1933
1934static ssize_t hotkey_mask_store(struct device *dev,
1935 struct device_attribute *attr,
1936 const char *buf, size_t count)
1937{
1938 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001939 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001940
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001941 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001942 return -EINVAL;
1943
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001944 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001945 return -ERESTARTSYS;
1946
1947 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001948
1949#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1950 hotkey_poll_setup(1);
1951#endif
1952
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001953 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001954
1955 return (res) ? res : count;
1956}
1957
1958static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001959 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001960 hotkey_mask_show, hotkey_mask_store);
1961
1962/* sysfs hotkey bios_enabled ------------------------------------------- */
1963static ssize_t hotkey_bios_enabled_show(struct device *dev,
1964 struct device_attribute *attr,
1965 char *buf)
1966{
1967 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1968}
1969
1970static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001971 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001972
1973/* sysfs hotkey bios_mask ---------------------------------------------- */
1974static ssize_t hotkey_bios_mask_show(struct device *dev,
1975 struct device_attribute *attr,
1976 char *buf)
1977{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001978 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001979}
1980
1981static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001982 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001983
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001984/* sysfs hotkey all_mask ----------------------------------------------- */
1985static ssize_t hotkey_all_mask_show(struct device *dev,
1986 struct device_attribute *attr,
1987 char *buf)
1988{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001989 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1990 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001991}
1992
1993static struct device_attribute dev_attr_hotkey_all_mask =
1994 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1995
1996/* sysfs hotkey recommended_mask --------------------------------------- */
1997static ssize_t hotkey_recommended_mask_show(struct device *dev,
1998 struct device_attribute *attr,
1999 char *buf)
2000{
2001 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002 (hotkey_all_mask | hotkey_source_mask)
2003 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002004}
2005
2006static struct device_attribute dev_attr_hotkey_recommended_mask =
2007 __ATTR(hotkey_recommended_mask, S_IRUGO,
2008 hotkey_recommended_mask_show, NULL);
2009
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002010#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2011
2012/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2013static ssize_t hotkey_source_mask_show(struct device *dev,
2014 struct device_attribute *attr,
2015 char *buf)
2016{
2017 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2018}
2019
2020static ssize_t hotkey_source_mask_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
2023{
2024 unsigned long t;
2025
2026 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2027 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2028 return -EINVAL;
2029
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002030 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031 return -ERESTARTSYS;
2032
2033 HOTKEY_CONFIG_CRITICAL_START
2034 hotkey_source_mask = t;
2035 HOTKEY_CONFIG_CRITICAL_END
2036
2037 hotkey_poll_setup(1);
2038
2039 mutex_unlock(&hotkey_mutex);
2040
2041 return count;
2042}
2043
2044static struct device_attribute dev_attr_hotkey_source_mask =
2045 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2046 hotkey_source_mask_show, hotkey_source_mask_store);
2047
2048/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2049static ssize_t hotkey_poll_freq_show(struct device *dev,
2050 struct device_attribute *attr,
2051 char *buf)
2052{
2053 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2054}
2055
2056static ssize_t hotkey_poll_freq_store(struct device *dev,
2057 struct device_attribute *attr,
2058 const char *buf, size_t count)
2059{
2060 unsigned long t;
2061
2062 if (parse_strtoul(buf, 25, &t))
2063 return -EINVAL;
2064
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002065 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002066 return -ERESTARTSYS;
2067
2068 hotkey_poll_freq = t;
2069
2070 hotkey_poll_setup(1);
2071 mutex_unlock(&hotkey_mutex);
2072
2073 return count;
2074}
2075
2076static struct device_attribute dev_attr_hotkey_poll_freq =
2077 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2078 hotkey_poll_freq_show, hotkey_poll_freq_store);
2079
2080#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2081
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002082/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002083static ssize_t hotkey_radio_sw_show(struct device *dev,
2084 struct device_attribute *attr,
2085 char *buf)
2086{
2087 int res, s;
2088 res = hotkey_get_wlsw(&s);
2089 if (res < 0)
2090 return res;
2091
2092 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2093}
2094
2095static struct device_attribute dev_attr_hotkey_radio_sw =
2096 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2097
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002098static void hotkey_radio_sw_notify_change(void)
2099{
2100 if (tp_features.hotkey_wlsw)
2101 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2102 "hotkey_radio_sw");
2103}
2104
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002105/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2106static ssize_t hotkey_tablet_mode_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2109{
2110 int res, s;
2111 res = hotkey_get_tablet_mode(&s);
2112 if (res < 0)
2113 return res;
2114
2115 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2116}
2117
2118static struct device_attribute dev_attr_hotkey_tablet_mode =
2119 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2120
2121static void hotkey_tablet_mode_notify_change(void)
2122{
2123 if (tp_features.hotkey_tablet)
2124 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2125 "hotkey_tablet_mode");
2126}
2127
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002128/* sysfs hotkey report_mode -------------------------------------------- */
2129static ssize_t hotkey_report_mode_show(struct device *dev,
2130 struct device_attribute *attr,
2131 char *buf)
2132{
2133 return snprintf(buf, PAGE_SIZE, "%d\n",
2134 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2135}
2136
2137static struct device_attribute dev_attr_hotkey_report_mode =
2138 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2139
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002140/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002141static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2142 struct device_attribute *attr,
2143 char *buf)
2144{
2145 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2146}
2147
2148static struct device_attribute dev_attr_hotkey_wakeup_reason =
2149 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2150
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002151static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002152{
2153 if (tp_features.hotkey_mask)
2154 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2155 "wakeup_reason");
2156}
2157
2158/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002159static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2160 struct device_attribute *attr,
2161 char *buf)
2162{
2163 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2164}
2165
2166static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2167 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2168 hotkey_wakeup_hotunplug_complete_show, NULL);
2169
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002170static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002171{
2172 if (tp_features.hotkey_mask)
2173 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2174 "wakeup_hotunplug_complete");
2175}
2176
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002177/* --------------------------------------------------------------------- */
2178
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002179static struct attribute *hotkey_attributes[] __initdata = {
2180 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002181 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002182 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002183#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2184 &dev_attr_hotkey_mask.attr,
2185 &dev_attr_hotkey_all_mask.attr,
2186 &dev_attr_hotkey_recommended_mask.attr,
2187 &dev_attr_hotkey_source_mask.attr,
2188 &dev_attr_hotkey_poll_freq.attr,
2189#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002190};
2191
2192static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002193 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002194#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2195 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002196 &dev_attr_hotkey_all_mask.attr,
2197 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002198#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002199 &dev_attr_hotkey_wakeup_reason.attr,
2200 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002201};
2202
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002203static void bluetooth_update_rfk(void);
2204static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002205static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002206static void tpacpi_send_radiosw_update(void)
2207{
2208 int wlsw;
2209
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002210 /* Sync these BEFORE sending any rfkill events */
2211 if (tp_features.bluetooth)
2212 bluetooth_update_rfk();
2213 if (tp_features.wan)
2214 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002215 if (tp_features.uwb)
2216 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002217
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002218 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2219 mutex_lock(&tpacpi_inputdev_send_mutex);
2220
2221 input_report_switch(tpacpi_inputdev,
2222 SW_RFKILL_ALL, !!wlsw);
2223 input_sync(tpacpi_inputdev);
2224
2225 mutex_unlock(&tpacpi_inputdev_send_mutex);
2226 }
2227 hotkey_radio_sw_notify_change();
2228}
2229
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002230static void hotkey_exit(void)
2231{
2232#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2233 hotkey_poll_stop_sync();
2234#endif
2235
2236 if (hotkey_dev_attributes)
2237 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2238
2239 kfree(hotkey_keycode_map);
2240
2241 if (tp_features.hotkey) {
2242 dbg_printk(TPACPI_DBG_EXIT,
2243 "restoring original hot key mask\n");
2244 /* no short-circuit boolean operator below! */
2245 if ((hotkey_mask_set(hotkey_orig_mask) |
2246 hotkey_status_set(hotkey_orig_status)) != 0)
2247 printk(TPACPI_ERR
2248 "failed to restore hot key mask "
2249 "to BIOS defaults\n");
2250 }
2251}
2252
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002253static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002254{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002255 /* Requirements for changing the default keymaps:
2256 *
2257 * 1. Many of the keys are mapped to KEY_RESERVED for very
2258 * good reasons. Do not change them unless you have deep
2259 * knowledge on the IBM and Lenovo ThinkPad firmware for
2260 * the various ThinkPad models. The driver behaves
2261 * differently for KEY_RESERVED: such keys have their
2262 * hot key mask *unset* in mask_recommended, and also
2263 * in the initial hot key mask programmed into the
2264 * firmware at driver load time, which means the firm-
2265 * ware may react very differently if you change them to
2266 * something else;
2267 *
2268 * 2. You must be subscribed to the linux-thinkpad and
2269 * ibm-acpi-devel mailing lists, and you should read the
2270 * list archives since 2007 if you want to change the
2271 * keymaps. This requirement exists so that you will
2272 * know the past history of problems with the thinkpad-
2273 * acpi driver keymaps, and also that you will be
2274 * listening to any bug reports;
2275 *
2276 * 3. Do not send thinkpad-acpi specific patches directly to
2277 * for merging, *ever*. Send them to the linux-acpi
2278 * mailinglist for comments. Merging is to be done only
2279 * through acpi-test and the ACPI maintainer.
2280 *
2281 * If the above is too much to ask, don't change the keymap.
2282 * Ask the thinkpad-acpi maintainer to do it, instead.
2283 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002284 static u16 ibm_keycode_map[] __initdata = {
2285 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2286 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2287 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2288 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002289
2290 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002291 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2292 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2293 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002294
2295 /* brightness: firmware always reacts to them, unless
2296 * X.org did some tricks in the radeon BIOS scratch
2297 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002298 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002299 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002300
2301 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002302 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002303
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002304 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2305 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002306
2307 /* Volume: firmware always react to it and reprograms
2308 * the built-in *extra* mixer. Never map it to control
2309 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002310 KEY_RESERVED, /* 0x14: VOLUME UP */
2311 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2312 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002313
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002314 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002315
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002316 /* (assignments unknown, please report if found) */
2317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2319 };
2320 static u16 lenovo_keycode_map[] __initdata = {
2321 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2322 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2323 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2324 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002325
2326 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002327 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2328 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2329 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002330
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002331 /* These either have to go through ACPI video, or
2332 * act like in the IBM ThinkPads, so don't ever
2333 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002334 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002335 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002336
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002337 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002338
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002339 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2340 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002341
2342 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2343 * react to it and reprograms the built-in *extra* mixer.
2344 * Never map it to control another mixer by default.
2345 *
2346 * T60?, T61, R60?, R61: firmware and EC tries to send
2347 * these over the regular keyboard, so these are no-ops,
2348 * but there are still weird bugs re. MUTE, so do not
2349 * change unless you get test reports from all Lenovo
2350 * models. May cause the BIOS to interfere with the
2351 * HDA mixer.
2352 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002353 KEY_RESERVED, /* 0x14: VOLUME UP */
2354 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2355 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002356
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002357 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002358
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002359 /* (assignments unknown, please report if found) */
2360 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2361 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2362 };
2363
2364#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2365#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2366#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2367
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002368 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002369 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002370 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002371
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002372 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2373
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002374 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002375 BUG_ON(tpacpi_inputdev->open != NULL ||
2376 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002377
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002378 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002379 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002380
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002381#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2382 mutex_init(&hotkey_thread_mutex);
2383 mutex_init(&hotkey_thread_data_mutex);
2384#endif
2385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002386 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002387 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002388
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002389 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002390 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002391
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002392 if (!tp_features.hotkey)
2393 return 1;
2394
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002395 tpacpi_disable_brightness_delay();
2396
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002397 hotkey_dev_attributes = create_attr_set(13, NULL);
2398 if (!hotkey_dev_attributes)
2399 return -ENOMEM;
2400 res = add_many_to_attr_set(hotkey_dev_attributes,
2401 hotkey_attributes,
2402 ARRAY_SIZE(hotkey_attributes));
2403 if (res)
2404 goto err_exit;
2405
2406 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2407 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2408 for HKEY interface version 0x100 */
2409 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2410 if ((hkeyv >> 8) != 1) {
2411 printk(TPACPI_ERR "unknown version of the "
2412 "HKEY interface: 0x%x\n", hkeyv);
2413 printk(TPACPI_ERR "please report this to %s\n",
2414 TPACPI_MAIL);
2415 } else {
2416 /*
2417 * MHKV 0x100 in A31, R40, R40e,
2418 * T4x, X31, and later
2419 */
2420 tp_features.hotkey_mask = 1;
2421 }
2422 }
2423
2424 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2425 str_supported(tp_features.hotkey_mask));
2426
2427 if (tp_features.hotkey_mask) {
2428 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2429 "MHKA", "qd")) {
2430 printk(TPACPI_ERR
2431 "missing MHKA handler, "
2432 "please report this to %s\n",
2433 TPACPI_MAIL);
2434 /* FN+F12, FN+F4, FN+F3 */
2435 hotkey_all_mask = 0x080cU;
2436 }
2437 }
2438
2439 /* hotkey_source_mask *must* be zero for
2440 * the first hotkey_mask_get */
2441 res = hotkey_status_get(&hotkey_orig_status);
2442 if (res)
2443 goto err_exit;
2444
2445 if (tp_features.hotkey_mask) {
2446 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002447 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002448 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002449
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002450 hotkey_orig_mask = hotkey_mask;
2451 res = add_many_to_attr_set(
2452 hotkey_dev_attributes,
2453 hotkey_mask_attributes,
2454 ARRAY_SIZE(hotkey_mask_attributes));
2455 if (res)
2456 goto err_exit;
2457 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002458
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002459#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002460 if (tp_features.hotkey_mask) {
2461 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2462 & ~hotkey_all_mask;
2463 } else {
2464 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2465 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002466
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002467 vdbg_printk(TPACPI_DBG_INIT,
2468 "hotkey source mask 0x%08x, polling freq %d\n",
2469 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002470#endif
2471
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002472#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2473 if (dbg_wlswemul) {
2474 tp_features.hotkey_wlsw = 1;
2475 printk(TPACPI_INFO
2476 "radio switch emulation enabled\n");
2477 } else
2478#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002479 /* Not all thinkpads have a hardware radio switch */
2480 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2481 tp_features.hotkey_wlsw = 1;
2482 printk(TPACPI_INFO
2483 "radio switch found; radios are %s\n",
2484 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002485 }
2486 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002487 res = add_to_attr_set(hotkey_dev_attributes,
2488 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002489
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002490 /* For X41t, X60t, X61t Tablets... */
2491 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2492 tp_features.hotkey_tablet = 1;
2493 printk(TPACPI_INFO
2494 "possible tablet mode switch found; "
2495 "ThinkPad in %s mode\n",
2496 (status & TP_HOTKEY_TABLET_MASK)?
2497 "tablet" : "laptop");
2498 res = add_to_attr_set(hotkey_dev_attributes,
2499 &dev_attr_hotkey_tablet_mode.attr);
2500 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002501
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002502 if (!res)
2503 res = register_attr_set_with_sysfs(
2504 hotkey_dev_attributes,
2505 &tpacpi_pdev->dev.kobj);
2506 if (res)
2507 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002508
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002509 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002510
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002511 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2512 GFP_KERNEL);
2513 if (!hotkey_keycode_map) {
2514 printk(TPACPI_ERR
2515 "failed to allocate memory for key map\n");
2516 res = -ENOMEM;
2517 goto err_exit;
2518 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002519
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002520 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2521 dbg_printk(TPACPI_DBG_INIT,
2522 "using Lenovo default hot key map\n");
2523 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2524 TPACPI_HOTKEY_MAP_SIZE);
2525 } else {
2526 dbg_printk(TPACPI_DBG_INIT,
2527 "using IBM default hot key map\n");
2528 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2529 TPACPI_HOTKEY_MAP_SIZE);
2530 }
2531
2532 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2533 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2534 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2535 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2536 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2537 tpacpi_inputdev->keycode = hotkey_keycode_map;
2538 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2539 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2540 set_bit(hotkey_keycode_map[i],
2541 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002542 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002543 if (i < sizeof(hotkey_reserved_mask)*8)
2544 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002545 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002546 }
2547
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002548 if (tp_features.hotkey_wlsw) {
2549 set_bit(EV_SW, tpacpi_inputdev->evbit);
2550 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2551 }
2552 if (tp_features.hotkey_tablet) {
2553 set_bit(EV_SW, tpacpi_inputdev->evbit);
2554 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002555 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002556
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002557 /* Do not issue duplicate brightness change events to
2558 * userspace */
2559 if (!tp_features.bright_acpimode)
2560 /* update bright_acpimode... */
2561 tpacpi_check_std_acpi_brightness_support();
2562
2563 if (tp_features.bright_acpimode) {
2564 printk(TPACPI_INFO
2565 "This ThinkPad has standard ACPI backlight "
2566 "brightness control, supported by the ACPI "
2567 "video driver\n");
2568 printk(TPACPI_NOTICE
2569 "Disabling thinkpad-acpi brightness events "
2570 "by default...\n");
2571
2572 /* The hotkey_reserved_mask change below is not
2573 * necessary while the keys are at KEY_RESERVED in the
2574 * default map, but better safe than sorry, leave it
2575 * here as a marker of what we have to do, especially
2576 * when we finally become able to set this at runtime
2577 * on response to X.org requests */
2578 hotkey_reserved_mask |=
2579 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2580 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002581 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002582
2583 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2584 res = hotkey_status_set(1);
2585 if (res) {
2586 hotkey_exit();
2587 return res;
2588 }
2589 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2590 & ~hotkey_reserved_mask)
2591 | hotkey_orig_mask);
2592 if (res < 0 && res != -ENXIO) {
2593 hotkey_exit();
2594 return res;
2595 }
2596
2597 dbg_printk(TPACPI_DBG_INIT,
2598 "legacy hot key reporting over procfs %s\n",
2599 (hotkey_report_mode < 2) ?
2600 "enabled" : "disabled");
2601
2602 tpacpi_inputdev->open = &hotkey_inputdev_open;
2603 tpacpi_inputdev->close = &hotkey_inputdev_close;
2604
2605 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002606 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002607 tpacpi_input_send_tabletsw();
2608
2609 return 0;
2610
2611err_exit:
2612 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2613 hotkey_dev_attributes = NULL;
2614
2615 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002616}
2617
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002618static bool hotkey_notify_hotkey(const u32 hkey,
2619 bool *send_acpi_ev,
2620 bool *ignore_acpi_ev)
2621{
2622 /* 0x1000-0x1FFF: key presses */
2623 unsigned int scancode = hkey & 0xfff;
2624 *send_acpi_ev = true;
2625 *ignore_acpi_ev = false;
2626
2627 if (scancode > 0 && scancode < 0x21) {
2628 scancode--;
2629 if (!(hotkey_source_mask & (1 << scancode))) {
2630 tpacpi_input_send_key(scancode);
2631 *send_acpi_ev = false;
2632 } else {
2633 *ignore_acpi_ev = true;
2634 }
2635 return true;
2636 }
2637 return false;
2638}
2639
2640static bool hotkey_notify_wakeup(const u32 hkey,
2641 bool *send_acpi_ev,
2642 bool *ignore_acpi_ev)
2643{
2644 /* 0x2000-0x2FFF: Wakeup reason */
2645 *send_acpi_ev = true;
2646 *ignore_acpi_ev = false;
2647
2648 switch (hkey) {
2649 case 0x2304: /* suspend, undock */
2650 case 0x2404: /* hibernation, undock */
2651 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2652 *ignore_acpi_ev = true;
2653 break;
2654
2655 case 0x2305: /* suspend, bay eject */
2656 case 0x2405: /* hibernation, bay eject */
2657 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2658 *ignore_acpi_ev = true;
2659 break;
2660
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002661 case 0x2313: /* Battery on critical low level (S3) */
2662 case 0x2413: /* Battery on critical low level (S4) */
2663 printk(TPACPI_ALERT
2664 "EMERGENCY WAKEUP: battery almost empty\n");
2665 /* how to auto-heal: */
2666 /* 2313: woke up from S3, go to S4/S5 */
2667 /* 2413: woke up from S4, go to S5 */
2668 break;
2669
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002670 default:
2671 return false;
2672 }
2673
2674 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2675 printk(TPACPI_INFO
2676 "woke up due to a hot-unplug "
2677 "request...\n");
2678 hotkey_wakeup_reason_notify_change();
2679 }
2680 return true;
2681}
2682
2683static bool hotkey_notify_usrevent(const u32 hkey,
2684 bool *send_acpi_ev,
2685 bool *ignore_acpi_ev)
2686{
2687 /* 0x5000-0x5FFF: human interface helpers */
2688 *send_acpi_ev = true;
2689 *ignore_acpi_ev = false;
2690
2691 switch (hkey) {
2692 case 0x5010: /* Lenovo new BIOS: brightness changed */
2693 case 0x500b: /* X61t: tablet pen inserted into bay */
2694 case 0x500c: /* X61t: tablet pen removed from bay */
2695 return true;
2696
2697 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2698 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2699 tpacpi_input_send_tabletsw();
2700 hotkey_tablet_mode_notify_change();
2701 *send_acpi_ev = false;
2702 return true;
2703
2704 case 0x5001:
2705 case 0x5002:
2706 /* LID switch events. Do not propagate */
2707 *ignore_acpi_ev = true;
2708 return true;
2709
2710 default:
2711 return false;
2712 }
2713}
2714
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002715static bool hotkey_notify_thermal(const u32 hkey,
2716 bool *send_acpi_ev,
2717 bool *ignore_acpi_ev)
2718{
2719 /* 0x6000-0x6FFF: thermal alarms */
2720 *send_acpi_ev = true;
2721 *ignore_acpi_ev = false;
2722
2723 switch (hkey) {
2724 case 0x6011:
2725 printk(TPACPI_CRIT
2726 "THERMAL ALARM: battery is too hot!\n");
2727 /* recommended action: warn user through gui */
2728 return true;
2729 case 0x6012:
2730 printk(TPACPI_ALERT
2731 "THERMAL EMERGENCY: battery is extremely hot!\n");
2732 /* recommended action: immediate sleep/hibernate */
2733 return true;
2734 case 0x6021:
2735 printk(TPACPI_CRIT
2736 "THERMAL ALARM: "
2737 "a sensor reports something is too hot!\n");
2738 /* recommended action: warn user through gui, that */
2739 /* some internal component is too hot */
2740 return true;
2741 case 0x6022:
2742 printk(TPACPI_ALERT
2743 "THERMAL EMERGENCY: "
2744 "a sensor reports something is extremely hot!\n");
2745 /* recommended action: immediate sleep/hibernate */
2746 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002747 case 0x6030:
2748 printk(TPACPI_INFO
2749 "EC reports that Thermal Table has changed\n");
2750 /* recommended action: do nothing, we don't have
2751 * Lenovo ATM information */
2752 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002753 default:
2754 printk(TPACPI_ALERT
2755 "THERMAL ALERT: unknown thermal alarm received\n");
2756 return false;
2757 }
2758}
2759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002760static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2761{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002762 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002763 bool send_acpi_ev;
2764 bool ignore_acpi_ev;
2765 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002766
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002767 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002768 printk(TPACPI_ERR
2769 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002770 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002771 acpi_bus_generate_netlink_event(
2772 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002773 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002774 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002775 return;
2776 }
2777
2778 while (1) {
2779 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002780 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002781 return;
2782 }
2783
2784 if (hkey == 0) {
2785 /* queue empty */
2786 return;
2787 }
2788
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002789 send_acpi_ev = true;
2790 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002791
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002792 switch (hkey >> 12) {
2793 case 1:
2794 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002795 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2796 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002797 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002798 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002799 /* 0x2000-0x2FFF: Wakeup reason */
2800 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2801 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002802 break;
2803 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002804 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002805 if (hkey == 0x3003) {
2806 hotkey_autosleep_ack = 1;
2807 printk(TPACPI_INFO
2808 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002809 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002810 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002811 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002812 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002813 }
2814 break;
2815 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002816 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002817 if (hkey == 0x4003) {
2818 hotkey_autosleep_ack = 1;
2819 printk(TPACPI_INFO
2820 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002821 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002822 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002823 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002824 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002825 }
2826 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002827 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002828 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002829 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2830 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002831 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002832 case 6:
2833 /* 0x6000-0x6FFF: thermal alarms */
2834 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2835 &ignore_acpi_ev);
2836 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002837 case 7:
2838 /* 0x7000-0x7FFF: misc */
2839 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002840 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002841 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002842 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002843 break;
2844 }
2845 /* fallthrough to default */
2846 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002847 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002848 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002849 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002850 printk(TPACPI_NOTICE
2851 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002852 printk(TPACPI_NOTICE
2853 "please report the conditions when this "
2854 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002855 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002856
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002857 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002858 if (!ignore_acpi_ev &&
2859 (send_acpi_ev || hotkey_report_mode < 2)) {
2860 acpi_bus_generate_proc_event(ibm->acpi->device,
2861 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002862 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002863
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002864 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002865 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002866 acpi_bus_generate_netlink_event(
2867 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002868 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002869 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002870 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002871 }
2872}
2873
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002874static void hotkey_suspend(pm_message_t state)
2875{
2876 /* Do these on suspend, we get the events on early resume! */
2877 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2878 hotkey_autosleep_ack = 0;
2879}
2880
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002881static void hotkey_resume(void)
2882{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002883 tpacpi_disable_brightness_delay();
2884
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002885 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002886 printk(TPACPI_ERR
2887 "error while trying to read hot key mask "
2888 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002889 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002890 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002891 hotkey_wakeup_reason_notify_change();
2892 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002893 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002894}
2895
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002896/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002897static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002899 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 int len = 0;
2901
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002902 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002903 len += sprintf(p + len, "status:\t\tnot supported\n");
2904 return len;
2905 }
2906
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002907 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002908 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002909 res = hotkey_status_get(&status);
2910 if (!res)
2911 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002912 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002913 if (res)
2914 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002917 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002918 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 len += sprintf(p + len,
2920 "commands:\tenable, disable, reset, <mask>\n");
2921 } else {
2922 len += sprintf(p + len, "mask:\t\tnot supported\n");
2923 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2924 }
2925
2926 return len;
2927}
2928
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002929static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002931 int res, status;
2932 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002935 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return -ENODEV;
2937
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002938 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002939 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002941 status = -1;
2942 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002943
2944 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 while ((cmd = next_cmd(&buf))) {
2946 if (strlencmp(cmd, "enable") == 0) {
2947 status = 1;
2948 } else if (strlencmp(cmd, "disable") == 0) {
2949 status = 0;
2950 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002951 status = hotkey_orig_status;
2952 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2954 /* mask set */
2955 } else if (sscanf(cmd, "%x", &mask) == 1) {
2956 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002957 } else {
2958 res = -EINVAL;
2959 goto errexit;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002962 if (status != -1)
2963 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002965 if (!res && mask != hotkey_mask)
2966 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002968errexit:
2969 mutex_unlock(&hotkey_mutex);
2970 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002973static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002974 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002975 {"", 0},
2976};
2977
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002978static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002979 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002980 .notify = hotkey_notify,
2981 .handle = &hkey_handle,
2982 .type = ACPI_DEVICE_NOTIFY,
2983};
2984
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002985static struct ibm_struct hotkey_driver_data = {
2986 .name = "hotkey",
2987 .read = hotkey_read,
2988 .write = hotkey_write,
2989 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002990 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002991 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002992 .acpi = &ibm_hotkey_acpidriver,
2993};
2994
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002995/*************************************************************************
2996 * Bluetooth subdriver
2997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002999enum {
3000 /* ACPI GBDC/SBDC bits */
3001 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3002 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003003 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3004 off / last state */
3005};
3006
3007enum {
3008 /* ACPI \BLTH commands */
3009 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3010 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3011 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3012 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3013 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003014};
3015
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003016static struct rfkill *tpacpi_bluetooth_rfkill;
3017
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003018static void bluetooth_suspend(pm_message_t state)
3019{
3020 /* Try to make sure radio will resume powered off */
3021 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3022 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
3023}
3024
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003025static int bluetooth_get_radiosw(void)
3026{
3027 int status;
3028
3029 if (!tp_features.bluetooth)
3030 return -ENODEV;
3031
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003032 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3033 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003034 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003035
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003036#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3037 if (dbg_bluetoothemul)
3038 return (tpacpi_bluetooth_emulstate) ?
3039 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3040#endif
3041
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003042 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3043 return -EIO;
3044
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003045 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3046 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003047}
3048
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003049static void bluetooth_update_rfk(void)
3050{
3051 int status;
3052
3053 if (!tpacpi_bluetooth_rfkill)
3054 return;
3055
3056 status = bluetooth_get_radiosw();
3057 if (status < 0)
3058 return;
3059 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3060}
3061
3062static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003063{
3064 int status;
3065
3066 if (!tp_features.bluetooth)
3067 return -ENODEV;
3068
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003069 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3070 * reason to risk weird behaviour. */
3071 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3072 && radio_on)
3073 return -EPERM;
3074
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003075#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3076 if (dbg_bluetoothemul) {
3077 tpacpi_bluetooth_emulstate = !!radio_on;
3078 if (update_rfk)
3079 bluetooth_update_rfk();
3080 return 0;
3081 }
3082#endif
3083
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003084 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003085 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003086 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003087 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003088 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003089 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3090 return -EIO;
3091
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003092 if (update_rfk)
3093 bluetooth_update_rfk();
3094
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003095 return 0;
3096}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003097
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003098/* sysfs bluetooth enable ---------------------------------------------- */
3099static ssize_t bluetooth_enable_show(struct device *dev,
3100 struct device_attribute *attr,
3101 char *buf)
3102{
3103 int status;
3104
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003105 printk_deprecated_rfkill_attribute("bluetooth_enable");
3106
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003107 status = bluetooth_get_radiosw();
3108 if (status < 0)
3109 return status;
3110
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003111 return snprintf(buf, PAGE_SIZE, "%d\n",
3112 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003113}
3114
3115static ssize_t bluetooth_enable_store(struct device *dev,
3116 struct device_attribute *attr,
3117 const char *buf, size_t count)
3118{
3119 unsigned long t;
3120 int res;
3121
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003122 printk_deprecated_rfkill_attribute("bluetooth_enable");
3123
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003124 if (parse_strtoul(buf, 1, &t))
3125 return -EINVAL;
3126
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003127 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003128
3129 return (res) ? res : count;
3130}
3131
3132static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003133 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003134 bluetooth_enable_show, bluetooth_enable_store);
3135
3136/* --------------------------------------------------------------------- */
3137
3138static struct attribute *bluetooth_attributes[] = {
3139 &dev_attr_bluetooth_enable.attr,
3140 NULL
3141};
3142
3143static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003144 .attrs = bluetooth_attributes,
3145};
3146
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003147static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3148{
3149 int bts = bluetooth_get_radiosw();
3150
3151 if (bts < 0)
3152 return bts;
3153
3154 *state = bts;
3155 return 0;
3156}
3157
3158static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3159{
3160 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3161}
3162
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003163static void bluetooth_shutdown(void)
3164{
3165 /* Order firmware to save current state to NVRAM */
3166 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3167 TP_ACPI_BLTH_SAVE_STATE))
3168 printk(TPACPI_NOTICE
3169 "failed to save bluetooth state to NVRAM\n");
3170}
3171
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003172static void bluetooth_exit(void)
3173{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003174 bluetooth_shutdown();
3175
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003176 if (tpacpi_bluetooth_rfkill)
3177 rfkill_unregister(tpacpi_bluetooth_rfkill);
3178
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003179 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3180 &bluetooth_attr_group);
3181}
3182
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003183static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003185 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003186 int status = 0;
3187
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003188 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3189
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003190 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003191
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003192 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3193 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003194 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003195 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003197 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3198 str_supported(tp_features.bluetooth),
3199 status);
3200
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003201#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3202 if (dbg_bluetoothemul) {
3203 tp_features.bluetooth = 1;
3204 printk(TPACPI_INFO
3205 "bluetooth switch emulation enabled\n");
3206 } else
3207#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003208 if (tp_features.bluetooth &&
3209 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3210 /* no bluetooth hardware present in system */
3211 tp_features.bluetooth = 0;
3212 dbg_printk(TPACPI_DBG_INIT,
3213 "bluetooth hardware not installed\n");
3214 }
3215
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003216 if (!tp_features.bluetooth)
3217 return 1;
3218
3219 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003220 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003221 if (res)
3222 return res;
3223
3224 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3225 &tpacpi_bluetooth_rfkill,
3226 RFKILL_TYPE_BLUETOOTH,
3227 "tpacpi_bluetooth_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003228 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003229 tpacpi_bluetooth_rfk_set,
3230 tpacpi_bluetooth_rfk_get);
3231 if (res) {
3232 bluetooth_exit();
3233 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003234 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003235
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003239/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003240static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003243 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003245 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003248 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003249 (status == RFKILL_STATE_UNBLOCKED) ?
3250 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 len += sprintf(p + len, "commands:\tenable, disable\n");
3252 }
3253
3254 return len;
3255}
3256
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003257static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003261 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003262 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 while ((cmd = next_cmd(&buf))) {
3265 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003266 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003268 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 } else
3270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return 0;
3274}
3275
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003276static struct ibm_struct bluetooth_driver_data = {
3277 .name = "bluetooth",
3278 .read = bluetooth_read,
3279 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003280 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003281 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003282 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003283};
3284
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003285/*************************************************************************
3286 * Wan subdriver
3287 */
3288
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003289enum {
3290 /* ACPI GWAN/SWAN bits */
3291 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3292 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003293 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3294 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003295};
3296
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003297static struct rfkill *tpacpi_wan_rfkill;
3298
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003299static void wan_suspend(pm_message_t state)
3300{
3301 /* Try to make sure radio will resume powered off */
3302 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3303 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3304}
3305
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003306static int wan_get_radiosw(void)
3307{
3308 int status;
3309
3310 if (!tp_features.wan)
3311 return -ENODEV;
3312
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003313 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3314 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003315 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003316
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003317#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3318 if (dbg_wwanemul)
3319 return (tpacpi_wwan_emulstate) ?
3320 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3321#endif
3322
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003323 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3324 return -EIO;
3325
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003326 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3327 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003328}
3329
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003330static void wan_update_rfk(void)
3331{
3332 int status;
3333
3334 if (!tpacpi_wan_rfkill)
3335 return;
3336
3337 status = wan_get_radiosw();
3338 if (status < 0)
3339 return;
3340 rfkill_force_state(tpacpi_wan_rfkill, status);
3341}
3342
3343static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003344{
3345 int status;
3346
3347 if (!tp_features.wan)
3348 return -ENODEV;
3349
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003350 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3351 * reason to risk weird behaviour. */
3352 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3353 && radio_on)
3354 return -EPERM;
3355
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003356#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3357 if (dbg_wwanemul) {
3358 tpacpi_wwan_emulstate = !!radio_on;
3359 if (update_rfk)
3360 wan_update_rfk();
3361 return 0;
3362 }
3363#endif
3364
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003365 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003366 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003367 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003368 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003369 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003370 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3371 return -EIO;
3372
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003373 if (update_rfk)
3374 wan_update_rfk();
3375
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003376 return 0;
3377}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003378
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003379/* sysfs wan enable ---------------------------------------------------- */
3380static ssize_t wan_enable_show(struct device *dev,
3381 struct device_attribute *attr,
3382 char *buf)
3383{
3384 int status;
3385
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003386 printk_deprecated_rfkill_attribute("wwan_enable");
3387
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003388 status = wan_get_radiosw();
3389 if (status < 0)
3390 return status;
3391
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003392 return snprintf(buf, PAGE_SIZE, "%d\n",
3393 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003394}
3395
3396static ssize_t wan_enable_store(struct device *dev,
3397 struct device_attribute *attr,
3398 const char *buf, size_t count)
3399{
3400 unsigned long t;
3401 int res;
3402
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003403 printk_deprecated_rfkill_attribute("wwan_enable");
3404
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003405 if (parse_strtoul(buf, 1, &t))
3406 return -EINVAL;
3407
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003408 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003409
3410 return (res) ? res : count;
3411}
3412
3413static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003414 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003415 wan_enable_show, wan_enable_store);
3416
3417/* --------------------------------------------------------------------- */
3418
3419static struct attribute *wan_attributes[] = {
3420 &dev_attr_wan_enable.attr,
3421 NULL
3422};
3423
3424static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003425 .attrs = wan_attributes,
3426};
3427
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003428static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3429{
3430 int wans = wan_get_radiosw();
3431
3432 if (wans < 0)
3433 return wans;
3434
3435 *state = wans;
3436 return 0;
3437}
3438
3439static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3440{
3441 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3442}
3443
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003444static void wan_shutdown(void)
3445{
3446 /* Order firmware to save current state to NVRAM */
3447 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3448 TP_ACPI_WGSV_SAVE_STATE))
3449 printk(TPACPI_NOTICE
3450 "failed to save WWAN state to NVRAM\n");
3451}
3452
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003453static void wan_exit(void)
3454{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003455 wan_shutdown();
3456
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003457 if (tpacpi_wan_rfkill)
3458 rfkill_unregister(tpacpi_wan_rfkill);
3459
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003460 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3461 &wan_attr_group);
3462}
3463
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003464static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003465{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003466 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003467 int status = 0;
3468
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003469 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3470
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003471 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003472
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003473 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003474 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003475
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003476 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3477 str_supported(tp_features.wan),
3478 status);
3479
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003480#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3481 if (dbg_wwanemul) {
3482 tp_features.wan = 1;
3483 printk(TPACPI_INFO
3484 "wwan switch emulation enabled\n");
3485 } else
3486#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003487 if (tp_features.wan &&
3488 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3489 /* no wan hardware present in system */
3490 tp_features.wan = 0;
3491 dbg_printk(TPACPI_DBG_INIT,
3492 "wan hardware not installed\n");
3493 }
3494
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003495 if (!tp_features.wan)
3496 return 1;
3497
3498 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003499 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003500 if (res)
3501 return res;
3502
3503 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3504 &tpacpi_wan_rfkill,
3505 RFKILL_TYPE_WWAN,
3506 "tpacpi_wwan_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003507 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003508 tpacpi_wan_rfk_set,
3509 tpacpi_wan_rfk_get);
3510 if (res) {
3511 wan_exit();
3512 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003513 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003514
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003515 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003516}
3517
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003518/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003519static int wan_read(char *p)
3520{
3521 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003522 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003523
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003524 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003525 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003526 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003527 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003528 (status == RFKILL_STATE_UNBLOCKED) ?
3529 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003530 len += sprintf(p + len, "commands:\tenable, disable\n");
3531 }
3532
3533 return len;
3534}
3535
3536static int wan_write(char *buf)
3537{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003538 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003539
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003540 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003541 return -ENODEV;
3542
3543 while ((cmd = next_cmd(&buf))) {
3544 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003545 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003546 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003547 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003548 } else
3549 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003550 }
3551
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003552 return 0;
3553}
3554
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003555static struct ibm_struct wan_driver_data = {
3556 .name = "wan",
3557 .read = wan_read,
3558 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003559 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003560 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003561 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003562};
3563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003564/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003565 * UWB subdriver
3566 */
3567
3568enum {
3569 /* ACPI GUWB/SUWB bits */
3570 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3571 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3572};
3573
3574static struct rfkill *tpacpi_uwb_rfkill;
3575
3576static int uwb_get_radiosw(void)
3577{
3578 int status;
3579
3580 if (!tp_features.uwb)
3581 return -ENODEV;
3582
3583 /* WLSW overrides UWB in firmware/hardware, reflect that */
3584 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3585 return RFKILL_STATE_HARD_BLOCKED;
3586
3587#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3588 if (dbg_uwbemul)
3589 return (tpacpi_uwb_emulstate) ?
3590 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3591#endif
3592
3593 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3594 return -EIO;
3595
3596 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3597 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3598}
3599
3600static void uwb_update_rfk(void)
3601{
3602 int status;
3603
3604 if (!tpacpi_uwb_rfkill)
3605 return;
3606
3607 status = uwb_get_radiosw();
3608 if (status < 0)
3609 return;
3610 rfkill_force_state(tpacpi_uwb_rfkill, status);
3611}
3612
3613static int uwb_set_radiosw(int radio_on, int update_rfk)
3614{
3615 int status;
3616
3617 if (!tp_features.uwb)
3618 return -ENODEV;
3619
3620 /* WLSW overrides UWB in firmware/hardware, but there is no
3621 * reason to risk weird behaviour. */
3622 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3623 && radio_on)
3624 return -EPERM;
3625
3626#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3627 if (dbg_uwbemul) {
3628 tpacpi_uwb_emulstate = !!radio_on;
3629 if (update_rfk)
3630 uwb_update_rfk();
3631 return 0;
3632 }
3633#endif
3634
3635 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3636 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3637 return -EIO;
3638
3639 if (update_rfk)
3640 uwb_update_rfk();
3641
3642 return 0;
3643}
3644
3645/* --------------------------------------------------------------------- */
3646
3647static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3648{
3649 int uwbs = uwb_get_radiosw();
3650
3651 if (uwbs < 0)
3652 return uwbs;
3653
3654 *state = uwbs;
3655 return 0;
3656}
3657
3658static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3659{
3660 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3661}
3662
3663static void uwb_exit(void)
3664{
3665 if (tpacpi_uwb_rfkill)
3666 rfkill_unregister(tpacpi_uwb_rfkill);
3667}
3668
3669static int __init uwb_init(struct ibm_init_struct *iibm)
3670{
3671 int res;
3672 int status = 0;
3673
3674 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3675
3676 TPACPI_ACPIHANDLE_INIT(hkey);
3677
3678 tp_features.uwb = hkey_handle &&
3679 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3680
3681 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3682 str_supported(tp_features.uwb),
3683 status);
3684
3685#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3686 if (dbg_uwbemul) {
3687 tp_features.uwb = 1;
3688 printk(TPACPI_INFO
3689 "uwb switch emulation enabled\n");
3690 } else
3691#endif
3692 if (tp_features.uwb &&
3693 !(status & TP_ACPI_UWB_HWPRESENT)) {
3694 /* no uwb hardware present in system */
3695 tp_features.uwb = 0;
3696 dbg_printk(TPACPI_DBG_INIT,
3697 "uwb hardware not installed\n");
3698 }
3699
3700 if (!tp_features.uwb)
3701 return 1;
3702
3703 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3704 &tpacpi_uwb_rfkill,
3705 RFKILL_TYPE_UWB,
3706 "tpacpi_uwb_sw",
3707 false,
3708 tpacpi_uwb_rfk_set,
3709 tpacpi_uwb_rfk_get);
3710
3711 return res;
3712}
3713
3714static struct ibm_struct uwb_driver_data = {
3715 .name = "uwb",
3716 .exit = uwb_exit,
3717 .flags.experimental = 1,
3718};
3719
3720/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003721 * Video subdriver
3722 */
3723
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003724#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3725
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003726enum video_access_mode {
3727 TPACPI_VIDEO_NONE = 0,
3728 TPACPI_VIDEO_570, /* 570 */
3729 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3730 TPACPI_VIDEO_NEW, /* all others */
3731};
3732
3733enum { /* video status flags, based on VIDEO_570 */
3734 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3735 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3736 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3737};
3738
3739enum { /* TPACPI_VIDEO_570 constants */
3740 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3741 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3742 * video_status_flags */
3743 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3744 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3745};
3746
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003747static enum video_access_mode video_supported;
3748static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003749
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003750static int video_autosw_get(void);
3751static int video_autosw_set(int enable);
3752
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003753TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003754
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003755static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757 int ivga;
3758
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003759 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3760
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003761 TPACPI_ACPIHANDLE_INIT(vid);
3762 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003763
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003764 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3765 /* G41, assume IVGA doesn't change */
3766 vid_handle = vid2_handle;
3767
3768 if (!vid_handle)
3769 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003770 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003771 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3772 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003773 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003774 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3775 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003776 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003777 else
3778 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003779 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003781 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3782 str_supported(video_supported != TPACPI_VIDEO_NONE),
3783 video_supported);
3784
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003785 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003788static void video_exit(void)
3789{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003790 dbg_printk(TPACPI_DBG_EXIT,
3791 "restoring original video autoswitch mode\n");
3792 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003793 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003794 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003795}
3796
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003797static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 int status = 0;
3800 int i;
3801
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003802 switch (video_supported) {
3803 case TPACPI_VIDEO_570:
3804 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3805 TP_ACPI_VIDEO_570_PHSCMD))
3806 return -EIO;
3807 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3808 break;
3809 case TPACPI_VIDEO_770:
3810 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3811 return -EIO;
3812 if (i)
3813 status |= TP_ACPI_VIDEO_S_LCD;
3814 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3815 return -EIO;
3816 if (i)
3817 status |= TP_ACPI_VIDEO_S_CRT;
3818 break;
3819 case TPACPI_VIDEO_NEW:
3820 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3821 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3822 return -EIO;
3823 if (i)
3824 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003826 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3827 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3828 return -EIO;
3829 if (i)
3830 status |= TP_ACPI_VIDEO_S_LCD;
3831 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3832 return -EIO;
3833 if (i)
3834 status |= TP_ACPI_VIDEO_S_DVI;
3835 break;
3836 default:
3837 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 return status;
3841}
3842
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003843static int video_outputsw_set(int status)
3844{
3845 int autosw;
3846 int res = 0;
3847
3848 switch (video_supported) {
3849 case TPACPI_VIDEO_570:
3850 res = acpi_evalf(NULL, NULL,
3851 "\\_SB.PHS2", "vdd",
3852 TP_ACPI_VIDEO_570_PHS2CMD,
3853 status | TP_ACPI_VIDEO_570_PHS2SET);
3854 break;
3855 case TPACPI_VIDEO_770:
3856 autosw = video_autosw_get();
3857 if (autosw < 0)
3858 return autosw;
3859
3860 res = video_autosw_set(1);
3861 if (res)
3862 return res;
3863 res = acpi_evalf(vid_handle, NULL,
3864 "ASWT", "vdd", status * 0x100, 0);
3865 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003866 printk(TPACPI_ERR
3867 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003868 return -EIO;
3869 }
3870 break;
3871 case TPACPI_VIDEO_NEW:
3872 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003873 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003874 break;
3875 default:
3876 return -ENOSYS;
3877 }
3878
3879 return (res)? 0 : -EIO;
3880}
3881
3882static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003884 int autosw = 0;
3885
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003886 switch (video_supported) {
3887 case TPACPI_VIDEO_570:
3888 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3889 return -EIO;
3890 break;
3891 case TPACPI_VIDEO_770:
3892 case TPACPI_VIDEO_NEW:
3893 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3894 return -EIO;
3895 break;
3896 default:
3897 return -ENOSYS;
3898 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003899
3900 return autosw & 1;
3901}
3902
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003903static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003904{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003905 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003906 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003907 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003908}
3909
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003910static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003911{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003912 int autosw = video_autosw_get();
3913 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003914
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003915 if (autosw < 0)
3916 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003917
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003918 switch (video_supported) {
3919 case TPACPI_VIDEO_570:
3920 res = video_autosw_set(1);
3921 if (res)
3922 return res;
3923 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3924 break;
3925 case TPACPI_VIDEO_770:
3926 case TPACPI_VIDEO_NEW:
3927 res = video_autosw_set(1);
3928 if (res)
3929 return res;
3930 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3931 break;
3932 default:
3933 return -ENOSYS;
3934 }
3935 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003936 printk(TPACPI_ERR
3937 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003938 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003939 }
3940
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003941 return (res)? 0 : -EIO;
3942}
3943
3944static int video_expand_toggle(void)
3945{
3946 switch (video_supported) {
3947 case TPACPI_VIDEO_570:
3948 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3949 0 : -EIO;
3950 case TPACPI_VIDEO_770:
3951 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3952 0 : -EIO;
3953 case TPACPI_VIDEO_NEW:
3954 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3955 0 : -EIO;
3956 default:
3957 return -ENOSYS;
3958 }
3959 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960}
3961
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003962static int video_read(char *p)
3963{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003964 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003965 int len = 0;
3966
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003967 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003968 len += sprintf(p + len, "status:\t\tnot supported\n");
3969 return len;
3970 }
3971
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003972 status = video_outputsw_get();
3973 if (status < 0)
3974 return status;
3975
3976 autosw = video_autosw_get();
3977 if (autosw < 0)
3978 return autosw;
3979
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003980 len += sprintf(p + len, "status:\t\tsupported\n");
3981 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3982 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003983 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003984 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3985 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3986 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3987 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
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, "commands:\tdvi_enable, dvi_disable\n");
3990 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3991 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3992
3993 return len;
3994}
3995
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003996static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
3998 char *cmd;
3999 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004000 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004002 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004003 return -ENODEV;
4004
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004005 enable = 0;
4006 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 while ((cmd = next_cmd(&buf))) {
4009 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004010 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004012 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004014 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004016 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004017 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004018 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004019 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004020 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004022 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004024 res = video_autosw_set(1);
4025 if (res)
4026 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004028 res = video_autosw_set(0);
4029 if (res)
4030 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004032 res = video_outputsw_cycle();
4033 if (res)
4034 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004036 res = video_expand_toggle();
4037 if (res)
4038 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 } else
4040 return -EINVAL;
4041 }
4042
4043 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004044 status = video_outputsw_get();
4045 if (status < 0)
4046 return status;
4047 res = video_outputsw_set((status & ~disable) | enable);
4048 if (res)
4049 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
4051
4052 return 0;
4053}
4054
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004055static struct ibm_struct video_driver_data = {
4056 .name = "video",
4057 .read = video_read,
4058 .write = video_write,
4059 .exit = video_exit,
4060};
4061
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004062#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4063
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004064/*************************************************************************
4065 * Light (thinklight) subdriver
4066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004068TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4069TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004070
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004071static int light_get_status(void)
4072{
4073 int status = 0;
4074
4075 if (tp_features.light_status) {
4076 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4077 return -EIO;
4078 return (!!status);
4079 }
4080
4081 return -ENXIO;
4082}
4083
4084static int light_set_status(int status)
4085{
4086 int rc;
4087
4088 if (tp_features.light) {
4089 if (cmos_handle) {
4090 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4091 (status)?
4092 TP_CMOS_THINKLIGHT_ON :
4093 TP_CMOS_THINKLIGHT_OFF);
4094 } else {
4095 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4096 (status)? 1 : 0);
4097 }
4098 return (rc)? 0 : -EIO;
4099 }
4100
4101 return -ENXIO;
4102}
4103
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004104static void light_set_status_worker(struct work_struct *work)
4105{
4106 struct tpacpi_led_classdev *data =
4107 container_of(work, struct tpacpi_led_classdev, work);
4108
4109 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4110 light_set_status((data->new_brightness != LED_OFF));
4111}
4112
4113static void light_sysfs_set(struct led_classdev *led_cdev,
4114 enum led_brightness brightness)
4115{
4116 struct tpacpi_led_classdev *data =
4117 container_of(led_cdev,
4118 struct tpacpi_led_classdev,
4119 led_classdev);
4120 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004121 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004122}
4123
4124static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4125{
4126 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4127}
4128
4129static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4130 .led_classdev = {
4131 .name = "tpacpi::thinklight",
4132 .brightness_set = &light_sysfs_set,
4133 .brightness_get = &light_sysfs_get,
4134 }
4135};
4136
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004137static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004139 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004140
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004141 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4142
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004143 TPACPI_ACPIHANDLE_INIT(ledb);
4144 TPACPI_ACPIHANDLE_INIT(lght);
4145 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004146 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004148 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004149 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004150
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004151 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004152 /* light status not supported on
4153 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004154 tp_features.light_status =
4155 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004157 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4158 str_supported(tp_features.light),
4159 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004160
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004161 if (!tp_features.light)
4162 return 1;
4163
4164 rc = led_classdev_register(&tpacpi_pdev->dev,
4165 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004166
4167 if (rc < 0) {
4168 tp_features.light = 0;
4169 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004170 } else {
4171 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004172 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004173
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004174 return rc;
4175}
4176
4177static void light_exit(void)
4178{
4179 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4180 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004181 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004184static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
4186 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004187 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004189 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004190 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004191 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004192 len += sprintf(p + len, "status:\t\tunknown\n");
4193 len += sprintf(p + len, "commands:\ton, off\n");
4194 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004195 status = light_get_status();
4196 if (status < 0)
4197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004199 len += sprintf(p + len, "commands:\ton, off\n");
4200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 return len;
4203}
4204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004205static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004208 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004209
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004210 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004211 return -ENODEV;
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 while ((cmd = next_cmd(&buf))) {
4214 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004215 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004217 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 } else
4219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
4221
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004222 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004225static struct ibm_struct light_driver_data = {
4226 .name = "light",
4227 .read = light_read,
4228 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004229 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004230};
4231
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004232/*************************************************************************
4233 * Dock subdriver
4234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004236#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004237
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004238static void dock_notify(struct ibm_struct *ibm, u32 event);
4239static int dock_read(char *p);
4240static int dock_write(char *buf);
4241
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004242TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004243 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4244 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4245 "\\_SB.PCI.ISA.SLCE", /* 570 */
4246 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4247
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004248/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004249TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004250
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004251static const struct acpi_device_id ibm_pci_device_ids[] = {
4252 {PCI_ROOT_HID_STRING, 0},
4253 {"", 0},
4254};
4255
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004256static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4257 {
4258 .notify = dock_notify,
4259 .handle = &dock_handle,
4260 .type = ACPI_SYSTEM_NOTIFY,
4261 },
4262 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004263 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4264 * We just use it to get notifications of dock hotplug
4265 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004266 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004267 .notify = dock_notify,
4268 .handle = &pci_handle,
4269 .type = ACPI_SYSTEM_NOTIFY,
4270 },
4271};
4272
4273static struct ibm_struct dock_driver_data[2] = {
4274 {
4275 .name = "dock",
4276 .read = dock_read,
4277 .write = dock_write,
4278 .acpi = &ibm_dock_acpidriver[0],
4279 },
4280 {
4281 .name = "dock",
4282 .acpi = &ibm_dock_acpidriver[1],
4283 },
4284};
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286#define dock_docked() (_sta(dock_handle) & 1)
4287
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004288static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004289{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004290 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4291
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004292 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004293
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004294 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4295 str_supported(dock_handle != NULL));
4296
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004297 return (dock_handle)? 0 : 1;
4298}
4299
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004300static int __init dock_init2(struct ibm_init_struct *iibm)
4301{
4302 int dock2_needed;
4303
4304 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4305
4306 if (dock_driver_data[0].flags.acpi_driver_registered &&
4307 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004308 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004309 dock2_needed = (pci_handle != NULL);
4310 vdbg_printk(TPACPI_DBG_INIT,
4311 "dock PCI handler for the TP 570 is %s\n",
4312 str_supported(dock2_needed));
4313 } else {
4314 vdbg_printk(TPACPI_DBG_INIT,
4315 "dock subdriver part 2 not required\n");
4316 dock2_needed = 0;
4317 }
4318
4319 return (dock2_needed)? 0 : 1;
4320}
4321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004322static void dock_notify(struct ibm_struct *ibm, u32 event)
4323{
4324 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004325 int pci = ibm->acpi->hid && ibm->acpi->device &&
4326 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004327 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004328
4329 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004330 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004331 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004332 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004333 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004334 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004335 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004336 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004337 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004338 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004339 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004340 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004341 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004342 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004343 }
Len Brown14e04fb2007-08-23 15:20:26 -04004344 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004345 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004346 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004347 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004348}
4349
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
4352 int len = 0;
4353 int docked = dock_docked();
4354
4355 if (!dock_handle)
4356 len += sprintf(p + len, "status:\t\tnot supported\n");
4357 else if (!docked)
4358 len += sprintf(p + len, "status:\t\tundocked\n");
4359 else {
4360 len += sprintf(p + len, "status:\t\tdocked\n");
4361 len += sprintf(p + len, "commands:\tdock, undock\n");
4362 }
4363
4364 return len;
4365}
4366
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004367static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 char *cmd;
4370
4371 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004372 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 while ((cmd = next_cmd(&buf))) {
4375 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004376 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4377 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return -EIO;
4379 } else if (strlencmp(cmd, "dock") == 0) {
4380 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4381 return -EIO;
4382 } else
4383 return -EINVAL;
4384 }
4385
4386 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004387}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004389#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391/*************************************************************************
4392 * Bay subdriver
4393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004395#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004397TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4399 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4400 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4401 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004402TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004403 "_EJ0", /* all others */
4404 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004405TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4407 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004408TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004409 "_EJ0", /* 770x */
4410 ); /* all others */
4411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004414 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4415
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004416 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004417 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004418 TPACPI_ACPIHANDLE_INIT(bay_ej);
4419 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004420 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004421 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004422
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004423 tp_features.bay_status = bay_handle &&
4424 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4425 tp_features.bay_status2 = bay2_handle &&
4426 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004427
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004428 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4429 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4430 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4431 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004433 vdbg_printk(TPACPI_DBG_INIT,
4434 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004435 str_supported(tp_features.bay_status),
4436 str_supported(tp_features.bay_eject),
4437 str_supported(tp_features.bay_status2),
4438 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004439
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004440 return (tp_features.bay_status || tp_features.bay_eject ||
4441 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004444static void bay_notify(struct ibm_struct *ibm, u32 event)
4445{
Len Brown14e04fb2007-08-23 15:20:26 -04004446 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004447 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004448 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004449 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004450}
4451
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004452#define bay_occupied(b) (_sta(b##_handle) & 1)
4453
4454static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004457 int occupied = bay_occupied(bay);
4458 int occupied2 = bay_occupied(bay2);
4459 int eject, eject2;
4460
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004461 len += sprintf(p + len, "status:\t\t%s\n",
4462 tp_features.bay_status ?
4463 (occupied ? "occupied" : "unoccupied") :
4464 "not supported");
4465 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004466 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4467 "occupied" : "unoccupied");
4468
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004469 eject = tp_features.bay_eject && occupied;
4470 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004471
4472 if (eject && eject2)
4473 len += sprintf(p + len, "commands:\teject, eject2\n");
4474 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476 else if (eject2)
4477 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 return len;
4480}
4481
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 char *cmd;
4485
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004486 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487 return -ENODEV;
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004490 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004491 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4492 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004493 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004494 strlencmp(cmd, "eject2") == 0) {
4495 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return -EIO;
4497 } else
4498 return -EINVAL;
4499 }
4500
4501 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004502}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004503
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004504static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4505 .notify = bay_notify,
4506 .handle = &bay_handle,
4507 .type = ACPI_SYSTEM_NOTIFY,
4508};
4509
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004510static struct ibm_struct bay_driver_data = {
4511 .name = "bay",
4512 .read = bay_read,
4513 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004514 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004515};
4516
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004517#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004519/*************************************************************************
4520 * CMOS subdriver
4521 */
4522
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004523/* sysfs cmos_command -------------------------------------------------- */
4524static ssize_t cmos_command_store(struct device *dev,
4525 struct device_attribute *attr,
4526 const char *buf, size_t count)
4527{
4528 unsigned long cmos_cmd;
4529 int res;
4530
4531 if (parse_strtoul(buf, 21, &cmos_cmd))
4532 return -EINVAL;
4533
4534 res = issue_thinkpad_cmos_command(cmos_cmd);
4535 return (res)? res : count;
4536}
4537
4538static struct device_attribute dev_attr_cmos_command =
4539 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4540
4541/* --------------------------------------------------------------------- */
4542
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004543static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004544{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004545 int res;
4546
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004547 vdbg_printk(TPACPI_DBG_INIT,
4548 "initializing cmos commands subdriver\n");
4549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004550 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004551
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004552 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4553 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004554
4555 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4556 if (res)
4557 return res;
4558
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004559 return (cmos_handle)? 0 : 1;
4560}
4561
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004562static void cmos_exit(void)
4563{
4564 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4565}
4566
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004567static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
4569 int len = 0;
4570
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004571 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4572 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (!cmos_handle)
4574 len += sprintf(p + len, "status:\t\tnot supported\n");
4575 else {
4576 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004577 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 }
4579
4580 return len;
4581}
4582
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004583static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
4585 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004586 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004589 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4590 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /* cmos_cmd set */
4592 } else
4593 return -EINVAL;
4594
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004595 res = issue_thinkpad_cmos_command(cmos_cmd);
4596 if (res)
4597 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599
4600 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004601}
4602
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004603static struct ibm_struct cmos_driver_data = {
4604 .name = "cmos",
4605 .read = cmos_read,
4606 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004607 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004608};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004609
4610/*************************************************************************
4611 * LED subdriver
4612 */
4613
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004614enum led_access_mode {
4615 TPACPI_LED_NONE = 0,
4616 TPACPI_LED_570, /* 570 */
4617 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4618 TPACPI_LED_NEW, /* all others */
4619};
4620
4621enum { /* For TPACPI_LED_OLD */
4622 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4623 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4624 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4625};
4626
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004627enum led_status_t {
4628 TPACPI_LED_OFF = 0,
4629 TPACPI_LED_ON,
4630 TPACPI_LED_BLINK,
4631};
4632
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004633static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004634
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004635TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004636 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4637 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004638 "LED", /* all others */
4639 ); /* R30, R31 */
4640
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004641#define TPACPI_LED_NUMLEDS 8
4642static struct tpacpi_led_classdev *tpacpi_leds;
4643static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004644static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004645 /* there's a limit of 19 chars + NULL before 2.6.26 */
4646 "tpacpi::power",
4647 "tpacpi:orange:batt",
4648 "tpacpi:green:batt",
4649 "tpacpi::dock_active",
4650 "tpacpi::bay_active",
4651 "tpacpi::dock_batt",
4652 "tpacpi::unknown_led",
4653 "tpacpi::standby",
4654};
4655
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004656static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004657{
4658 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004659 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004660
4661 switch (led_supported) {
4662 case TPACPI_LED_570:
4663 if (!acpi_evalf(ec_handle,
4664 &status, "GLED", "dd", 1 << led))
4665 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004666 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004667 TPACPI_LED_OFF :
4668 ((status == 1)?
4669 TPACPI_LED_ON :
4670 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004671 tpacpi_led_state_cache[led] = led_s;
4672 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004673 default:
4674 return -ENXIO;
4675 }
4676
4677 /* not reached */
4678}
4679
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004680static int led_set_status(const unsigned int led,
4681 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004682{
4683 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004684 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4685 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004686
4687 int rc = 0;
4688
4689 switch (led_supported) {
4690 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004691 /* 570 */
4692 if (led > 7)
4693 return -EINVAL;
4694 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4695 (1 << led), led_sled_arg1[ledstatus]))
4696 rc = -EIO;
4697 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004698 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004699 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4700 if (led > 7)
4701 return -EINVAL;
4702 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4703 if (rc >= 0)
4704 rc = ec_write(TPACPI_LED_EC_HLBL,
4705 (ledstatus == TPACPI_LED_BLINK) << led);
4706 if (rc >= 0)
4707 rc = ec_write(TPACPI_LED_EC_HLCL,
4708 (ledstatus != TPACPI_LED_OFF) << led);
4709 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004710 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004711 /* all others */
4712 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4713 led, led_led_arg1[ledstatus]))
4714 rc = -EIO;
4715 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004716 default:
4717 rc = -ENXIO;
4718 }
4719
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004720 if (!rc)
4721 tpacpi_led_state_cache[led] = ledstatus;
4722
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004723 return rc;
4724}
4725
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004726static void led_sysfs_set_status(unsigned int led,
4727 enum led_brightness brightness)
4728{
4729 led_set_status(led,
4730 (brightness == LED_OFF) ?
4731 TPACPI_LED_OFF :
4732 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4733 TPACPI_LED_BLINK : TPACPI_LED_ON);
4734}
4735
4736static void led_set_status_worker(struct work_struct *work)
4737{
4738 struct tpacpi_led_classdev *data =
4739 container_of(work, struct tpacpi_led_classdev, work);
4740
4741 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4742 led_sysfs_set_status(data->led, data->new_brightness);
4743}
4744
4745static void led_sysfs_set(struct led_classdev *led_cdev,
4746 enum led_brightness brightness)
4747{
4748 struct tpacpi_led_classdev *data = container_of(led_cdev,
4749 struct tpacpi_led_classdev, led_classdev);
4750
4751 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004752 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004753}
4754
4755static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4756 unsigned long *delay_on, unsigned long *delay_off)
4757{
4758 struct tpacpi_led_classdev *data = container_of(led_cdev,
4759 struct tpacpi_led_classdev, led_classdev);
4760
4761 /* Can we choose the flash rate? */
4762 if (*delay_on == 0 && *delay_off == 0) {
4763 /* yes. set them to the hardware blink rate (1 Hz) */
4764 *delay_on = 500; /* ms */
4765 *delay_off = 500; /* ms */
4766 } else if ((*delay_on != 500) || (*delay_off != 500))
4767 return -EINVAL;
4768
4769 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004770 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004771
4772 return 0;
4773}
4774
4775static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4776{
4777 int rc;
4778
4779 struct tpacpi_led_classdev *data = container_of(led_cdev,
4780 struct tpacpi_led_classdev, led_classdev);
4781
4782 rc = led_get_status(data->led);
4783
4784 if (rc == TPACPI_LED_OFF || rc < 0)
4785 rc = LED_OFF; /* no error handling in led class :( */
4786 else
4787 rc = LED_FULL;
4788
4789 return rc;
4790}
4791
4792static void led_exit(void)
4793{
4794 unsigned int i;
4795
4796 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4797 if (tpacpi_leds[i].led_classdev.name)
4798 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4799 }
4800
4801 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004802}
4803
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004804static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004805{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004806 unsigned int i;
4807 int rc;
4808
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004809 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4810
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004811 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004812
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004813 if (!led_handle)
4814 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004815 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004816 else if (strlencmp(led_path, "SLED") == 0)
4817 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004818 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004819 else if (strlencmp(led_path, "SYSL") == 0)
4820 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004821 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004822 else
4823 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004824 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004825
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004826 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4827 str_supported(led_supported), led_supported);
4828
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004829 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4830 GFP_KERNEL);
4831 if (!tpacpi_leds) {
4832 printk(TPACPI_ERR "Out of memory for LED data\n");
4833 return -ENOMEM;
4834 }
4835
4836 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4837 tpacpi_leds[i].led = i;
4838
4839 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4840 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4841 if (led_supported == TPACPI_LED_570)
4842 tpacpi_leds[i].led_classdev.brightness_get =
4843 &led_sysfs_get;
4844
4845 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4846
4847 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4848
4849 rc = led_classdev_register(&tpacpi_pdev->dev,
4850 &tpacpi_leds[i].led_classdev);
4851 if (rc < 0) {
4852 tpacpi_leds[i].led_classdev.name = NULL;
4853 led_exit();
4854 return rc;
4855 }
4856 }
4857
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004858 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004859}
4860
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004861#define str_led_status(s) \
4862 ((s) == TPACPI_LED_OFF ? "off" : \
4863 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004864
4865static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
4867 int len = 0;
4868
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869 if (!led_supported) {
4870 len += sprintf(p + len, "status:\t\tnot supported\n");
4871 return len;
4872 }
4873 len += sprintf(p + len, "status:\t\tsupported\n");
4874
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004875 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004876 /* 570 */
4877 int i, status;
4878 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004879 status = led_get_status(i);
4880 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004881 return -EIO;
4882 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004883 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004884 }
4885 }
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004888 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
4890 return len;
4891}
4892
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004893static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894{
4895 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004896 int led, rc;
4897 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004898
4899 if (!led_supported)
4900 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004903 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return -EINVAL;
4905
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004906 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004907 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004909 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004910 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004911 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004913 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004914 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004915
4916 rc = led_set_status(led, s);
4917 if (rc < 0)
4918 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920
4921 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004922}
4923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004924static struct ibm_struct led_driver_data = {
4925 .name = "led",
4926 .read = led_read,
4927 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004928 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004929};
4930
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004931/*************************************************************************
4932 * Beep subdriver
4933 */
4934
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004935TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004936
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004937static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004938{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004939 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4940
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004941 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004942
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004943 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4944 str_supported(beep_handle != NULL));
4945
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004946 return (beep_handle)? 0 : 1;
4947}
4948
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004949static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
4951 int len = 0;
4952
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 if (!beep_handle)
4954 len += sprintf(p + len, "status:\t\tnot supported\n");
4955 else {
4956 len += sprintf(p + len, "status:\t\tsupported\n");
4957 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 return len;
4961}
4962
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
4965 char *cmd;
4966 int beep_cmd;
4967
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004968 if (!beep_handle)
4969 return -ENODEV;
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004972 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4973 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 /* beep_cmd set */
4975 } else
4976 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004977 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return -EIO;
4979 }
4980
4981 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004982}
4983
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004984static struct ibm_struct beep_driver_data = {
4985 .name = "beep",
4986 .read = beep_read,
4987 .write = beep_write,
4988};
4989
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004990/*************************************************************************
4991 * Thermal subdriver
4992 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004993
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004994enum thermal_access_mode {
4995 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4996 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4997 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4998 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4999 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5000};
5001
5002enum { /* TPACPI_THERMAL_TPEC_* */
5003 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5004 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5005 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5006};
5007
5008#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5009struct ibm_thermal_sensors_struct {
5010 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5011};
5012
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005013static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005014
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005015/* idx is zero-based */
5016static int thermal_get_sensor(int idx, s32 *value)
5017{
5018 int t;
5019 s8 tmp;
5020 char tmpi[5];
5021
5022 t = TP_EC_THERMAL_TMP0;
5023
5024 switch (thermal_read_mode) {
5025#if TPACPI_MAX_THERMAL_SENSORS >= 16
5026 case TPACPI_THERMAL_TPEC_16:
5027 if (idx >= 8 && idx <= 15) {
5028 t = TP_EC_THERMAL_TMP8;
5029 idx -= 8;
5030 }
5031 /* fallthrough */
5032#endif
5033 case TPACPI_THERMAL_TPEC_8:
5034 if (idx <= 7) {
5035 if (!acpi_ec_read(t + idx, &tmp))
5036 return -EIO;
5037 *value = tmp * 1000;
5038 return 0;
5039 }
5040 break;
5041
5042 case TPACPI_THERMAL_ACPI_UPDT:
5043 if (idx <= 7) {
5044 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5045 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5046 return -EIO;
5047 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5048 return -EIO;
5049 *value = (t - 2732) * 100;
5050 return 0;
5051 }
5052 break;
5053
5054 case TPACPI_THERMAL_ACPI_TMP07:
5055 if (idx <= 7) {
5056 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5057 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5058 return -EIO;
5059 if (t > 127 || t < -127)
5060 t = TP_EC_THERMAL_TMP_NA;
5061 *value = t * 1000;
5062 return 0;
5063 }
5064 break;
5065
5066 case TPACPI_THERMAL_NONE:
5067 default:
5068 return -ENOSYS;
5069 }
5070
5071 return -EINVAL;
5072}
5073
5074static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5075{
5076 int res, i;
5077 int n;
5078
5079 n = 8;
5080 i = 0;
5081
5082 if (!s)
5083 return -EINVAL;
5084
5085 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5086 n = 16;
5087
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005088 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005089 res = thermal_get_sensor(i, &s->temp[i]);
5090 if (res)
5091 return res;
5092 }
5093
5094 return n;
5095}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005096
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005097/* sysfs temp##_input -------------------------------------------------- */
5098
5099static ssize_t thermal_temp_input_show(struct device *dev,
5100 struct device_attribute *attr,
5101 char *buf)
5102{
5103 struct sensor_device_attribute *sensor_attr =
5104 to_sensor_dev_attr(attr);
5105 int idx = sensor_attr->index;
5106 s32 value;
5107 int res;
5108
5109 res = thermal_get_sensor(idx, &value);
5110 if (res)
5111 return res;
5112 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5113 return -ENXIO;
5114
5115 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5116}
5117
5118#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005119 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5120 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005121
5122static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5123 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5124 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5125 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5126 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5127 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5128 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5129 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5130 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5131 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5132 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5133 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5134 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5135 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5136 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5137 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5138 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5139};
5140
5141#define THERMAL_ATTRS(X) \
5142 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5143
5144static struct attribute *thermal_temp_input_attr[] = {
5145 THERMAL_ATTRS(8),
5146 THERMAL_ATTRS(9),
5147 THERMAL_ATTRS(10),
5148 THERMAL_ATTRS(11),
5149 THERMAL_ATTRS(12),
5150 THERMAL_ATTRS(13),
5151 THERMAL_ATTRS(14),
5152 THERMAL_ATTRS(15),
5153 THERMAL_ATTRS(0),
5154 THERMAL_ATTRS(1),
5155 THERMAL_ATTRS(2),
5156 THERMAL_ATTRS(3),
5157 THERMAL_ATTRS(4),
5158 THERMAL_ATTRS(5),
5159 THERMAL_ATTRS(6),
5160 THERMAL_ATTRS(7),
5161 NULL
5162};
5163
5164static const struct attribute_group thermal_temp_input16_group = {
5165 .attrs = thermal_temp_input_attr
5166};
5167
5168static const struct attribute_group thermal_temp_input8_group = {
5169 .attrs = &thermal_temp_input_attr[8]
5170};
5171
5172#undef THERMAL_SENSOR_ATTR_TEMP
5173#undef THERMAL_ATTRS
5174
5175/* --------------------------------------------------------------------- */
5176
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005177static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005178{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005179 u8 t, ta1, ta2;
5180 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005181 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005182 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005183
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005184 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5185
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005186 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005187
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005188 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005189 /*
5190 * Direct EC access mode: sensors at registers
5191 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5192 * non-implemented, thermal sensors return 0x80 when
5193 * not available
5194 */
5195
5196 ta1 = ta2 = 0;
5197 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005198 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005199 ta1 |= t;
5200 } else {
5201 ta1 = 0;
5202 break;
5203 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005204 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005205 ta2 |= t;
5206 } else {
5207 ta1 = 0;
5208 break;
5209 }
5210 }
5211 if (ta1 == 0) {
5212 /* This is sheer paranoia, but we handle it anyway */
5213 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005214 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005215 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005216 "falling back to ACPI TMPx access "
5217 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005218 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005219 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005220 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005221 "ThinkPad ACPI EC access misbehaving, "
5222 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005223 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005224 }
5225 } else {
5226 thermal_read_mode =
5227 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005228 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005229 }
5230 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005231 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5232 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005233 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005234 } else {
5235 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005236 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005237 }
5238 } else {
5239 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005240 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005241 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005242
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005243 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5244 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5245 thermal_read_mode);
5246
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005247 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005248 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005249 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005250 &thermal_temp_input16_group);
5251 if (res)
5252 return res;
5253 break;
5254 case TPACPI_THERMAL_TPEC_8:
5255 case TPACPI_THERMAL_ACPI_TMP07:
5256 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005257 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005258 &thermal_temp_input8_group);
5259 if (res)
5260 return res;
5261 break;
5262 case TPACPI_THERMAL_NONE:
5263 default:
5264 return 1;
5265 }
5266
5267 return 0;
5268}
5269
5270static void thermal_exit(void)
5271{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005272 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005273 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005274 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005275 &thermal_temp_input16_group);
5276 break;
5277 case TPACPI_THERMAL_TPEC_8:
5278 case TPACPI_THERMAL_ACPI_TMP07:
5279 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005280 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005281 &thermal_temp_input16_group);
5282 break;
5283 case TPACPI_THERMAL_NONE:
5284 default:
5285 break;
5286 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005287}
5288
5289static int thermal_read(char *p)
5290{
5291 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005292 int n, i;
5293 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005294
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005295 n = thermal_get_sensors(&t);
5296 if (unlikely(n < 0))
5297 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005298
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005299 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005300
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005301 if (n > 0) {
5302 for (i = 0; i < (n - 1); i++)
5303 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5304 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5305 } else
5306 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005307
5308 return len;
5309}
5310
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005311static struct ibm_struct thermal_driver_data = {
5312 .name = "thermal",
5313 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005314 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005315};
5316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005317/*************************************************************************
5318 * EC Dump subdriver
5319 */
5320
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005321static u8 ecdump_regs[256];
5322
5323static int ecdump_read(char *p)
5324{
5325 int len = 0;
5326 int i, j;
5327 u8 v;
5328
5329 len += sprintf(p + len, "EC "
5330 " +00 +01 +02 +03 +04 +05 +06 +07"
5331 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5332 for (i = 0; i < 256; i += 16) {
5333 len += sprintf(p + len, "EC 0x%02x:", i);
5334 for (j = 0; j < 16; j++) {
5335 if (!acpi_ec_read(i + j, &v))
5336 break;
5337 if (v != ecdump_regs[i + j])
5338 len += sprintf(p + len, " *%02x", v);
5339 else
5340 len += sprintf(p + len, " %02x", v);
5341 ecdump_regs[i + j] = v;
5342 }
5343 len += sprintf(p + len, "\n");
5344 if (j != 16)
5345 break;
5346 }
5347
5348 /* These are way too dangerous to advertise openly... */
5349#if 0
5350 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5351 " (<offset> is 00-ff, <value> is 00-ff)\n");
5352 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5353 " (<offset> is 00-ff, <value> is 0-255)\n");
5354#endif
5355 return len;
5356}
5357
5358static int ecdump_write(char *buf)
5359{
5360 char *cmd;
5361 int i, v;
5362
5363 while ((cmd = next_cmd(&buf))) {
5364 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5365 /* i and v set */
5366 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5367 /* i and v set */
5368 } else
5369 return -EINVAL;
5370 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5371 if (!acpi_ec_write(i, v))
5372 return -EIO;
5373 } else
5374 return -EINVAL;
5375 }
5376
5377 return 0;
5378}
5379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005380static struct ibm_struct ecdump_driver_data = {
5381 .name = "ecdump",
5382 .read = ecdump_read,
5383 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005384 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005385};
5386
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387/*************************************************************************
5388 * Backlight/brightness subdriver
5389 */
Holger Macht8acb0252006-10-20 14:30:28 -07005390
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005391#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5392
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005393enum {
5394 TP_EC_BACKLIGHT = 0x31,
5395
5396 /* TP_EC_BACKLIGHT bitmasks */
5397 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5398 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5399 TP_EC_BACKLIGHT_MAPSW = 0x20,
5400};
5401
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005402static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005403static int brightness_mode;
5404static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5405
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005406static struct mutex brightness_mutex;
5407
5408/*
5409 * ThinkPads can read brightness from two places: EC 0x31, or
5410 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005411 *
5412 * EC 0x31 has the following layout
5413 * Bit 7: unknown function
5414 * Bit 6: unknown function
5415 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5416 * Bit 4: must be set to zero to avoid problems
5417 * Bit 3-0: backlight brightness level
5418 *
5419 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005420 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005421static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005422{
5423 u8 lec = 0, lcmos = 0, level = 0;
5424
5425 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005426 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005427 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005428 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005429 };
5430 if (brightness_mode & 2) {
5431 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5432 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5433 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5434 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5435 level = lcmos;
5436 }
5437
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005438 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005439 *status = lec; /* Prefer EC, CMOS is just a backing store */
5440 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005441 if (lec == lcmos)
5442 tp_warned.bright_cmos_ec_unsync = 0;
5443 else {
5444 if (!tp_warned.bright_cmos_ec_unsync) {
5445 printk(TPACPI_ERR
5446 "CMOS NVRAM (%u) and EC (%u) do not "
5447 "agree on display brightness level\n",
5448 (unsigned int) lcmos,
5449 (unsigned int) lec);
5450 tp_warned.bright_cmos_ec_unsync = 1;
5451 }
5452 return -EIO;
5453 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005454 } else {
5455 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005456 }
5457
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005458 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005459}
5460
5461/* May return EINTR which can always be mapped to ERESTARTSYS */
5462static int brightness_set(int value)
5463{
5464 int cmos_cmd, inc, i, res;
5465 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005466 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005467
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005468 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5469 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005470 return -EINVAL;
5471
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005472 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005473 if (res < 0)
5474 return res;
5475
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005476 res = brightness_get_raw(&current_value);
5477 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005478 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005479
5480 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5481 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005482
5483 cmos_cmd = value > current_value ?
5484 TP_CMOS_BRIGHTNESS_UP :
5485 TP_CMOS_BRIGHTNESS_DOWN;
5486 inc = (value > current_value)? 1 : -1;
5487
5488 res = 0;
5489 for (i = current_value; i != value; i += inc) {
5490 if ((brightness_mode & 2) &&
5491 issue_thinkpad_cmos_command(cmos_cmd)) {
5492 res = -EIO;
5493 goto errout;
5494 }
5495 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005496 !acpi_ec_write(TP_EC_BACKLIGHT,
5497 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005498 res = -EIO;
5499 goto errout;;
5500 }
5501 }
5502
5503errout:
5504 mutex_unlock(&brightness_mutex);
5505 return res;
5506}
5507
5508/* sysfs backlight class ----------------------------------------------- */
5509
5510static int brightness_update_status(struct backlight_device *bd)
5511{
5512 /* it is the backlight class's job (caller) to handle
5513 * EINTR and other errors properly */
5514 return brightness_set(
5515 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5516 bd->props.power == FB_BLANK_UNBLANK) ?
5517 bd->props.brightness : 0);
5518}
Holger Macht8acb0252006-10-20 14:30:28 -07005519
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005520static int brightness_get(struct backlight_device *bd)
5521{
5522 int status, res;
5523
5524 res = brightness_get_raw(&status);
5525 if (res < 0)
5526 return 0; /* FIXME: teach backlight about error handling */
5527
5528 return status & TP_EC_BACKLIGHT_LVLMSK;
5529}
5530
Richard Purdie599a52d2007-02-10 23:07:48 +00005531static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005532 .get_brightness = brightness_get,
5533 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005534};
5535
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005536/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005537
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005538static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005539{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005540 int b;
5541
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005542 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5543
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005544 mutex_init(&brightness_mutex);
5545
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005546 /*
5547 * We always attempt to detect acpi support, so as to switch
5548 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5549 * going to publish a backlight interface
5550 */
5551 b = tpacpi_check_std_acpi_brightness_support();
5552 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005553
5554 if (acpi_video_backlight_support()) {
5555 if (brightness_enable > 1) {
5556 printk(TPACPI_NOTICE
5557 "Standard ACPI backlight interface "
5558 "available, not loading native one.\n");
5559 return 1;
5560 } else if (brightness_enable == 1) {
5561 printk(TPACPI_NOTICE
5562 "Backlight control force enabled, even if standard "
5563 "ACPI backlight interface is available\n");
5564 }
5565 } else {
5566 if (brightness_enable > 1) {
5567 printk(TPACPI_NOTICE
5568 "Standard ACPI backlight interface not "
5569 "available, thinkpad_acpi native "
5570 "brightness control enabled\n");
5571 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005572 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005573 }
5574
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005575 if (!brightness_enable) {
5576 dbg_printk(TPACPI_DBG_INIT,
5577 "brightness support disabled by "
5578 "module parameter\n");
5579 return 1;
5580 }
5581
5582 if (b > 16) {
5583 printk(TPACPI_ERR
5584 "Unsupported brightness interface, "
5585 "please contact %s\n", TPACPI_MAIL);
5586 return 1;
5587 }
5588 if (b == 16)
5589 tp_features.bright_16levels = 1;
5590
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005591 if (!brightness_mode) {
5592 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5593 brightness_mode = 2;
5594 else
5595 brightness_mode = 3;
5596
5597 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5598 brightness_mode);
5599 }
5600
5601 if (brightness_mode > 3)
5602 return -EINVAL;
5603
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005604 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005605 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005606
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005607 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005608 printk(TPACPI_INFO
5609 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005610
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005611 ibm_backlight_device = backlight_device_register(
5612 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5613 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005614 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005615 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005616 return PTR_ERR(ibm_backlight_device);
5617 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005618 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005619
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005620 ibm_backlight_device->props.max_brightness =
5621 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005622 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005623 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005624
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005625 return 0;
5626}
5627
5628static void brightness_exit(void)
5629{
5630 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005631 vdbg_printk(TPACPI_DBG_EXIT,
5632 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005633 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005634 }
5635}
5636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005637static int brightness_read(char *p)
5638{
5639 int len = 0;
5640 int level;
5641
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005642 level = brightness_get(NULL);
5643 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005644 len += sprintf(p + len, "level:\t\tunreadable\n");
5645 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005646 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005647 len += sprintf(p + len, "commands:\tup, down\n");
5648 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005649 " (<level> is 0-%d)\n",
5650 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005651 }
5652
5653 return len;
5654}
5655
5656static int brightness_write(char *buf)
5657{
5658 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005659 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005660 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005661 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005662
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005663 level = brightness_get(NULL);
5664 if (level < 0)
5665 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005666
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005667 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005668 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005669 if (level < max_level)
5670 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005671 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005672 if (level > 0)
5673 level--;
5674 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5675 level >= 0 && level <= max_level) {
5676 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005677 } else
5678 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005679 }
5680
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005681 /*
5682 * Now we know what the final level should be, so we try to set it.
5683 * Doing it this way makes the syscall restartable in case of EINTR
5684 */
5685 rc = brightness_set(level);
5686 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005687}
5688
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005689static struct ibm_struct brightness_driver_data = {
5690 .name = "brightness",
5691 .read = brightness_read,
5692 .write = brightness_write,
5693 .exit = brightness_exit,
5694};
5695
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005696/*************************************************************************
5697 * Volume subdriver
5698 */
5699
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005700static int volume_offset = 0x30;
5701
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005702static int volume_read(char *p)
5703{
5704 int len = 0;
5705 u8 level;
5706
5707 if (!acpi_ec_read(volume_offset, &level)) {
5708 len += sprintf(p + len, "level:\t\tunreadable\n");
5709 } else {
5710 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5711 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5712 len += sprintf(p + len, "commands:\tup, down, mute\n");
5713 len += sprintf(p + len, "commands:\tlevel <level>"
5714 " (<level> is 0-15)\n");
5715 }
5716
5717 return len;
5718}
5719
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005720static int volume_write(char *buf)
5721{
5722 int cmos_cmd, inc, i;
5723 u8 level, mute;
5724 int new_level, new_mute;
5725 char *cmd;
5726
5727 while ((cmd = next_cmd(&buf))) {
5728 if (!acpi_ec_read(volume_offset, &level))
5729 return -EIO;
5730 new_mute = mute = level & 0x40;
5731 new_level = level = level & 0xf;
5732
5733 if (strlencmp(cmd, "up") == 0) {
5734 if (mute)
5735 new_mute = 0;
5736 else
5737 new_level = level == 15 ? 15 : level + 1;
5738 } else if (strlencmp(cmd, "down") == 0) {
5739 if (mute)
5740 new_mute = 0;
5741 else
5742 new_level = level == 0 ? 0 : level - 1;
5743 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5744 new_level >= 0 && new_level <= 15) {
5745 /* new_level set */
5746 } else if (strlencmp(cmd, "mute") == 0) {
5747 new_mute = 0x40;
5748 } else
5749 return -EINVAL;
5750
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005751 if (new_level != level) {
5752 /* mute doesn't change */
5753
5754 cmos_cmd = (new_level > level) ?
5755 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005756 inc = new_level > level ? 1 : -1;
5757
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005758 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005759 !acpi_ec_write(volume_offset, level)))
5760 return -EIO;
5761
5762 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005763 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005764 !acpi_ec_write(volume_offset, i + inc))
5765 return -EIO;
5766
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005767 if (mute &&
5768 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5769 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005770 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005771 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005772 }
5773
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005774 if (new_mute != mute) {
5775 /* level doesn't change */
5776
5777 cmos_cmd = (new_mute) ?
5778 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005779
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005780 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005781 !acpi_ec_write(volume_offset, level + new_mute))
5782 return -EIO;
5783 }
5784 }
5785
5786 return 0;
5787}
5788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005789static struct ibm_struct volume_driver_data = {
5790 .name = "volume",
5791 .read = volume_read,
5792 .write = volume_write,
5793};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005794
5795/*************************************************************************
5796 * Fan subdriver
5797 */
5798
5799/*
5800 * FAN ACCESS MODES
5801 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005802 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005803 * ACPI GFAN method: returns fan level
5804 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005805 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005806 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005807 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005808 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005809 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5810 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005811 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5812 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005813 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005814 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005815 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5816 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005817 *
5818 * Fan speed changes of any sort (including those caused by the
5819 * disengaged mode) are usually done slowly by the firmware as the
5820 * maximum ammount of fan duty cycle change per second seems to be
5821 * limited.
5822 *
5823 * Reading is not available if GFAN exists.
5824 * Writing is not available if SFAN exists.
5825 *
5826 * Bits
5827 * 7 automatic mode engaged;
5828 * (default operation mode of the ThinkPad)
5829 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005830 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005832 * the tachometer while the fan controller ramps up
5833 * the speed (which can take up to a few *minutes*).
5834 * Speeds up fan to 100% duty-cycle, which is far above
5835 * the standard RPM levels. It is not impossible that
5836 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005837 * 5-3 unused in some models. Extra bits for fan level
5838 * in others, but still useless as all values above
5839 * 7 map to the same speed as level 7 in these models.
5840 * 2-0 fan level (0..7 usually)
5841 * 0x00 = stop
5842 * 0x07 = max (set when temperatures critical)
5843 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005844 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005845 *
5846 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5847 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5848 * does so, its initial value is meaningless (0x07).
5849 *
5850 * For firmware bugs, refer to:
5851 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5852 *
5853 * ----
5854 *
5855 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5856 * Main fan tachometer reading (in RPM)
5857 *
5858 * This register is present on all ThinkPads with a new-style EC, and
5859 * it is known not to be present on the A21m/e, and T22, as there is
5860 * something else in offset 0x84 according to the ACPI DSDT. Other
5861 * ThinkPads from this same time period (and earlier) probably lack the
5862 * tachometer as well.
5863 *
5864 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5865 * was never fixed by IBM to report the EC firmware version string
5866 * probably support the tachometer (like the early X models), so
5867 * detecting it is quite hard. We need more data to know for sure.
5868 *
5869 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5870 * might result.
5871 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005872 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5873 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005874 *
5875 * For firmware bugs, refer to:
5876 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5877 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005878 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005879 * ThinkPad X31, X40, X41. Not available in the X60.
5880 *
5881 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5882 * high speed. ACPI DSDT seems to map these three speeds to levels
5883 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5884 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5885 *
5886 * The speeds are stored on handles
5887 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5888 *
5889 * There are three default speed sets, acessible as handles:
5890 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5891 *
5892 * ACPI DSDT switches which set is in use depending on various
5893 * factors.
5894 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005895 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005896 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5897 * but the ACPI tables just mention level 7.
5898 */
5899
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005900enum { /* Fan control constants */
5901 fan_status_offset = 0x2f, /* EC register 0x2f */
5902 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5903 * 0x84 must be read before 0x85 */
5904
5905 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5906 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5907
5908 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5909};
5910
5911enum fan_status_access_mode {
5912 TPACPI_FAN_NONE = 0, /* No fan status or control */
5913 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5914 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5915};
5916
5917enum fan_control_access_mode {
5918 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5919 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5920 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5921 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5922};
5923
5924enum fan_control_commands {
5925 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5926 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5927 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5928 * and also watchdog cmd */
5929};
5930
5931static int fan_control_allowed;
5932
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005933static enum fan_status_access_mode fan_status_access_mode;
5934static enum fan_control_access_mode fan_control_access_mode;
5935static enum fan_control_commands fan_control_commands;
5936
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005937static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005938static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005939static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005940static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005941
5942static struct mutex fan_mutex;
5943
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005944static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005945static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005946
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005947TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5948TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005949 "\\FSPD", /* 600e/x, 770e, 770x */
5950 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005951TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005952 "JFNS", /* 770x-JL */
5953 ); /* all others */
5954
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005955/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02005956 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5957 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5958 * be in auto mode (0x80).
5959 *
5960 * This is corrected by any write to HFSP either by the driver, or
5961 * by the firmware.
5962 *
5963 * We assume 0x07 really means auto mode while this quirk is active,
5964 * as this is far more likely than the ThinkPad being in level 7,
5965 * which is only used by the firmware during thermal emergencies.
5966 */
5967
5968static void fan_quirk1_detect(void)
5969{
5970 /* In some ThinkPads, neither the EC nor the ACPI
5971 * DSDT initialize the HFSP register, and it ends up
5972 * being initially set to 0x07 when it *could* be
5973 * either 0x07 or 0x80.
5974 *
5975 * Enable for TP-1Y (T43), TP-78 (R51e),
5976 * TP-76 (R52), TP-70 (T43, R52), which are known
5977 * to be buggy. */
5978 if (fan_control_initial_status == 0x07) {
5979 switch (thinkpad_id.ec_model) {
5980 case 0x5931: /* TP-1Y */
5981 case 0x3837: /* TP-78 */
5982 case 0x3637: /* TP-76 */
5983 case 0x3037: /* TP-70 */
5984 printk(TPACPI_NOTICE
5985 "fan_init: initial fan status is unknown, "
5986 "assuming it is in auto mode\n");
5987 tp_features.fan_ctrl_status_undef = 1;
5988 ;;
5989 }
5990 }
5991}
5992
5993static void fan_quirk1_handle(u8 *fan_status)
5994{
5995 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5996 if (*fan_status != fan_control_initial_status) {
5997 /* something changed the HFSP regisnter since
5998 * driver init time, so it is not undefined
5999 * anymore */
6000 tp_features.fan_ctrl_status_undef = 0;
6001 } else {
6002 /* Return most likely status. In fact, it
6003 * might be the only possible status */
6004 *fan_status = TP_EC_FAN_AUTO;
6005 }
6006 }
6007}
6008
6009/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006010 * Call with fan_mutex held
6011 */
6012static void fan_update_desired_level(u8 status)
6013{
6014 if ((status &
6015 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6016 if (status > 7)
6017 fan_control_desired_level = 7;
6018 else
6019 fan_control_desired_level = status;
6020 }
6021}
6022
6023static int fan_get_status(u8 *status)
6024{
6025 u8 s;
6026
6027 /* TODO:
6028 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6029
6030 switch (fan_status_access_mode) {
6031 case TPACPI_FAN_RD_ACPI_GFAN:
6032 /* 570, 600e/x, 770e, 770x */
6033
6034 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6035 return -EIO;
6036
6037 if (likely(status))
6038 *status = s & 0x07;
6039
6040 break;
6041
6042 case TPACPI_FAN_RD_TPEC:
6043 /* all except 570, 600e/x, 770e, 770x */
6044 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6045 return -EIO;
6046
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006047 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006048 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006049 fan_quirk1_handle(status);
6050 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006051
6052 break;
6053
6054 default:
6055 return -ENXIO;
6056 }
6057
6058 return 0;
6059}
6060
6061static int fan_get_status_safe(u8 *status)
6062{
6063 int rc;
6064 u8 s;
6065
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006066 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006067 return -ERESTARTSYS;
6068 rc = fan_get_status(&s);
6069 if (!rc)
6070 fan_update_desired_level(s);
6071 mutex_unlock(&fan_mutex);
6072
6073 if (status)
6074 *status = s;
6075
6076 return rc;
6077}
6078
6079static int fan_get_speed(unsigned int *speed)
6080{
6081 u8 hi, lo;
6082
6083 switch (fan_status_access_mode) {
6084 case TPACPI_FAN_RD_TPEC:
6085 /* all except 570, 600e/x, 770e, 770x */
6086 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6087 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6088 return -EIO;
6089
6090 if (likely(speed))
6091 *speed = (hi << 8) | lo;
6092
6093 break;
6094
6095 default:
6096 return -ENXIO;
6097 }
6098
6099 return 0;
6100}
6101
6102static int fan_set_level(int level)
6103{
6104 if (!fan_control_allowed)
6105 return -EPERM;
6106
6107 switch (fan_control_access_mode) {
6108 case TPACPI_FAN_WR_ACPI_SFAN:
6109 if (level >= 0 && level <= 7) {
6110 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6111 return -EIO;
6112 } else
6113 return -EINVAL;
6114 break;
6115
6116 case TPACPI_FAN_WR_ACPI_FANS:
6117 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006118 if (!(level & TP_EC_FAN_AUTO) &&
6119 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006120 ((level < 0) || (level > 7)))
6121 return -EINVAL;
6122
6123 /* safety net should the EC not support AUTO
6124 * or FULLSPEED mode bits and just ignore them */
6125 if (level & TP_EC_FAN_FULLSPEED)
6126 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006127 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006128 level |= 4; /* safety min speed 4 */
6129
6130 if (!acpi_ec_write(fan_status_offset, level))
6131 return -EIO;
6132 else
6133 tp_features.fan_ctrl_status_undef = 0;
6134 break;
6135
6136 default:
6137 return -ENXIO;
6138 }
6139 return 0;
6140}
6141
6142static int fan_set_level_safe(int level)
6143{
6144 int rc;
6145
6146 if (!fan_control_allowed)
6147 return -EPERM;
6148
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006149 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006150 return -ERESTARTSYS;
6151
6152 if (level == TPACPI_FAN_LAST_LEVEL)
6153 level = fan_control_desired_level;
6154
6155 rc = fan_set_level(level);
6156 if (!rc)
6157 fan_update_desired_level(level);
6158
6159 mutex_unlock(&fan_mutex);
6160 return rc;
6161}
6162
6163static int fan_set_enable(void)
6164{
6165 u8 s;
6166 int rc;
6167
6168 if (!fan_control_allowed)
6169 return -EPERM;
6170
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006171 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006172 return -ERESTARTSYS;
6173
6174 switch (fan_control_access_mode) {
6175 case TPACPI_FAN_WR_ACPI_FANS:
6176 case TPACPI_FAN_WR_TPEC:
6177 rc = fan_get_status(&s);
6178 if (rc < 0)
6179 break;
6180
6181 /* Don't go out of emergency fan mode */
6182 if (s != 7) {
6183 s &= 0x07;
6184 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6185 }
6186
6187 if (!acpi_ec_write(fan_status_offset, s))
6188 rc = -EIO;
6189 else {
6190 tp_features.fan_ctrl_status_undef = 0;
6191 rc = 0;
6192 }
6193 break;
6194
6195 case TPACPI_FAN_WR_ACPI_SFAN:
6196 rc = fan_get_status(&s);
6197 if (rc < 0)
6198 break;
6199
6200 s &= 0x07;
6201
6202 /* Set fan to at least level 4 */
6203 s |= 4;
6204
6205 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006206 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006207 else
6208 rc = 0;
6209 break;
6210
6211 default:
6212 rc = -ENXIO;
6213 }
6214
6215 mutex_unlock(&fan_mutex);
6216 return rc;
6217}
6218
6219static int fan_set_disable(void)
6220{
6221 int rc;
6222
6223 if (!fan_control_allowed)
6224 return -EPERM;
6225
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006226 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006227 return -ERESTARTSYS;
6228
6229 rc = 0;
6230 switch (fan_control_access_mode) {
6231 case TPACPI_FAN_WR_ACPI_FANS:
6232 case TPACPI_FAN_WR_TPEC:
6233 if (!acpi_ec_write(fan_status_offset, 0x00))
6234 rc = -EIO;
6235 else {
6236 fan_control_desired_level = 0;
6237 tp_features.fan_ctrl_status_undef = 0;
6238 }
6239 break;
6240
6241 case TPACPI_FAN_WR_ACPI_SFAN:
6242 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6243 rc = -EIO;
6244 else
6245 fan_control_desired_level = 0;
6246 break;
6247
6248 default:
6249 rc = -ENXIO;
6250 }
6251
6252
6253 mutex_unlock(&fan_mutex);
6254 return rc;
6255}
6256
6257static int fan_set_speed(int speed)
6258{
6259 int rc;
6260
6261 if (!fan_control_allowed)
6262 return -EPERM;
6263
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006264 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006265 return -ERESTARTSYS;
6266
6267 rc = 0;
6268 switch (fan_control_access_mode) {
6269 case TPACPI_FAN_WR_ACPI_FANS:
6270 if (speed >= 0 && speed <= 65535) {
6271 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6272 speed, speed, speed))
6273 rc = -EIO;
6274 } else
6275 rc = -EINVAL;
6276 break;
6277
6278 default:
6279 rc = -ENXIO;
6280 }
6281
6282 mutex_unlock(&fan_mutex);
6283 return rc;
6284}
6285
6286static void fan_watchdog_reset(void)
6287{
6288 static int fan_watchdog_active;
6289
6290 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6291 return;
6292
6293 if (fan_watchdog_active)
6294 cancel_delayed_work(&fan_watchdog_task);
6295
6296 if (fan_watchdog_maxinterval > 0 &&
6297 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6298 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006299 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006300 msecs_to_jiffies(fan_watchdog_maxinterval
6301 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006302 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006303 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006304 "watchdog will not trigger\n");
6305 }
6306 } else
6307 fan_watchdog_active = 0;
6308}
6309
6310static void fan_watchdog_fire(struct work_struct *ignored)
6311{
6312 int rc;
6313
6314 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6315 return;
6316
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006317 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006318 rc = fan_set_enable();
6319 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006320 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006321 "will try again later...\n", -rc);
6322 /* reschedule for later */
6323 fan_watchdog_reset();
6324 }
6325}
6326
6327/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006328 * SYSFS fan layout: hwmon compatible (device)
6329 *
6330 * pwm*_enable:
6331 * 0: "disengaged" mode
6332 * 1: manual mode
6333 * 2: native EC "auto" mode (recommended, hardware default)
6334 *
6335 * pwm*: set speed in manual mode, ignored otherwise.
6336 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6337 * interpolation.
6338 *
6339 * fan*_input: tachometer reading, RPM
6340 *
6341 *
6342 * SYSFS fan layout: extensions
6343 *
6344 * fan_watchdog (driver):
6345 * fan watchdog interval in seconds, 0 disables (default), max 120
6346 */
6347
6348/* sysfs fan pwm1_enable ----------------------------------------------- */
6349static ssize_t fan_pwm1_enable_show(struct device *dev,
6350 struct device_attribute *attr,
6351 char *buf)
6352{
6353 int res, mode;
6354 u8 status;
6355
6356 res = fan_get_status_safe(&status);
6357 if (res)
6358 return res;
6359
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006360 if (status & TP_EC_FAN_FULLSPEED) {
6361 mode = 0;
6362 } else if (status & TP_EC_FAN_AUTO) {
6363 mode = 2;
6364 } else
6365 mode = 1;
6366
6367 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6368}
6369
6370static ssize_t fan_pwm1_enable_store(struct device *dev,
6371 struct device_attribute *attr,
6372 const char *buf, size_t count)
6373{
6374 unsigned long t;
6375 int res, level;
6376
6377 if (parse_strtoul(buf, 2, &t))
6378 return -EINVAL;
6379
6380 switch (t) {
6381 case 0:
6382 level = TP_EC_FAN_FULLSPEED;
6383 break;
6384 case 1:
6385 level = TPACPI_FAN_LAST_LEVEL;
6386 break;
6387 case 2:
6388 level = TP_EC_FAN_AUTO;
6389 break;
6390 case 3:
6391 /* reserved for software-controlled auto mode */
6392 return -ENOSYS;
6393 default:
6394 return -EINVAL;
6395 }
6396
6397 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006398 if (res == -ENXIO)
6399 return -EINVAL;
6400 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006401 return res;
6402
6403 fan_watchdog_reset();
6404
6405 return count;
6406}
6407
6408static struct device_attribute dev_attr_fan_pwm1_enable =
6409 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6410 fan_pwm1_enable_show, fan_pwm1_enable_store);
6411
6412/* sysfs fan pwm1 ------------------------------------------------------ */
6413static ssize_t fan_pwm1_show(struct device *dev,
6414 struct device_attribute *attr,
6415 char *buf)
6416{
6417 int res;
6418 u8 status;
6419
6420 res = fan_get_status_safe(&status);
6421 if (res)
6422 return res;
6423
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006424 if ((status &
6425 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6426 status = fan_control_desired_level;
6427
6428 if (status > 7)
6429 status = 7;
6430
6431 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6432}
6433
6434static ssize_t fan_pwm1_store(struct device *dev,
6435 struct device_attribute *attr,
6436 const char *buf, size_t count)
6437{
6438 unsigned long s;
6439 int rc;
6440 u8 status, newlevel;
6441
6442 if (parse_strtoul(buf, 255, &s))
6443 return -EINVAL;
6444
6445 /* scale down from 0-255 to 0-7 */
6446 newlevel = (s >> 5) & 0x07;
6447
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006448 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006449 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006450
6451 rc = fan_get_status(&status);
6452 if (!rc && (status &
6453 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6454 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006455 if (rc == -ENXIO)
6456 rc = -EINVAL;
6457 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006458 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006459 fan_watchdog_reset();
6460 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006461 }
6462
6463 mutex_unlock(&fan_mutex);
6464 return (rc)? rc : count;
6465}
6466
6467static struct device_attribute dev_attr_fan_pwm1 =
6468 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6469 fan_pwm1_show, fan_pwm1_store);
6470
6471/* sysfs fan fan1_input ------------------------------------------------ */
6472static ssize_t fan_fan1_input_show(struct device *dev,
6473 struct device_attribute *attr,
6474 char *buf)
6475{
6476 int res;
6477 unsigned int speed;
6478
6479 res = fan_get_speed(&speed);
6480 if (res < 0)
6481 return res;
6482
6483 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6484}
6485
6486static struct device_attribute dev_attr_fan_fan1_input =
6487 __ATTR(fan1_input, S_IRUGO,
6488 fan_fan1_input_show, NULL);
6489
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006490/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006491static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6492 char *buf)
6493{
6494 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6495}
6496
6497static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6498 const char *buf, size_t count)
6499{
6500 unsigned long t;
6501
6502 if (parse_strtoul(buf, 120, &t))
6503 return -EINVAL;
6504
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006505 if (!fan_control_allowed)
6506 return -EPERM;
6507
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006508 fan_watchdog_maxinterval = t;
6509 fan_watchdog_reset();
6510
6511 return count;
6512}
6513
6514static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6515 fan_fan_watchdog_show, fan_fan_watchdog_store);
6516
6517/* --------------------------------------------------------------------- */
6518static struct attribute *fan_attributes[] = {
6519 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6520 &dev_attr_fan_fan1_input.attr,
6521 NULL
6522};
6523
6524static const struct attribute_group fan_attr_group = {
6525 .attrs = fan_attributes,
6526};
6527
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006528static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006529{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006530 int rc;
6531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006532 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6533
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006534 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006535 fan_status_access_mode = TPACPI_FAN_NONE;
6536 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006537 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006538 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006539 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006540 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006541
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006542 TPACPI_ACPIHANDLE_INIT(fans);
6543 TPACPI_ACPIHANDLE_INIT(gfan);
6544 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006545
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006546 if (gfan_handle) {
6547 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006548 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006549 } else {
6550 /* all other ThinkPads: note that even old-style
6551 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006552 if (likely(acpi_ec_read(fan_status_offset,
6553 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006554 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006555 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006556 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006557 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006558 "ThinkPad ACPI EC access misbehaving, "
6559 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006560 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006561 }
6562 }
6563
6564 if (sfan_handle) {
6565 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006566 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006567 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006568 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006569 } else {
6570 if (!gfan_handle) {
6571 /* gfan without sfan means no fan control */
6572 /* all other models implement TP EC 0x2f control */
6573
6574 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006575 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006576 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006577 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006578 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006579 TPACPI_FAN_CMD_SPEED |
6580 TPACPI_FAN_CMD_LEVEL |
6581 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006582 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006583 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006584 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006585 TPACPI_FAN_CMD_LEVEL |
6586 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006587 }
6588 }
6589 }
6590
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006591 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6592 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6593 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6594 fan_status_access_mode, fan_control_access_mode);
6595
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006596 /* fan control master switch */
6597 if (!fan_control_allowed) {
6598 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6599 fan_control_commands = 0;
6600 dbg_printk(TPACPI_DBG_INIT,
6601 "fan control features disabled by parameter\n");
6602 }
6603
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006604 /* update fan_control_desired_level */
6605 if (fan_status_access_mode != TPACPI_FAN_NONE)
6606 fan_get_status_safe(NULL);
6607
6608 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6609 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006610 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006611 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006612 if (rc < 0)
6613 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006614
6615 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6616 &driver_attr_fan_watchdog);
6617 if (rc < 0) {
6618 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6619 &fan_attr_group);
6620 return rc;
6621 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006622 return 0;
6623 } else
6624 return 1;
6625}
6626
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006627static void fan_exit(void)
6628{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006629 vdbg_printk(TPACPI_DBG_EXIT,
6630 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006631
6632 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006633 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006634 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6635 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006637 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006638 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006639}
6640
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006641static void fan_suspend(pm_message_t state)
6642{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006643 int rc;
6644
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006645 if (!fan_control_allowed)
6646 return;
6647
6648 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006649 fan_control_resume_level = 0;
6650 rc = fan_get_status_safe(&fan_control_resume_level);
6651 if (rc < 0)
6652 printk(TPACPI_NOTICE
6653 "failed to read fan level for later "
6654 "restore during resume: %d\n", rc);
6655
6656 /* if it is undefined, don't attempt to restore it.
6657 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006658 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006659 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006660}
6661
6662static void fan_resume(void)
6663{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006664 u8 current_level = 7;
6665 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006666 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006667
6668 /* DSDT *always* updates status on resume */
6669 tp_features.fan_ctrl_status_undef = 0;
6670
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006671 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006672 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006673 (fan_get_status_safe(&current_level) < 0))
6674 return;
6675
6676 switch (fan_control_access_mode) {
6677 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006678 /* never decrease fan level */
6679 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006680 break;
6681 case TPACPI_FAN_WR_ACPI_FANS:
6682 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006683 /* never decrease fan level, scale is:
6684 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6685 *
6686 * We expect the firmware to set either 7 or AUTO, but we
6687 * handle FULLSPEED out of paranoia.
6688 *
6689 * So, we can safely only restore FULLSPEED or 7, anything
6690 * else could slow the fan. Restoring AUTO is useless, at
6691 * best that's exactly what the DSDT already set (it is the
6692 * slower it uses).
6693 *
6694 * Always keep in mind that the DSDT *will* have set the
6695 * fans to what the vendor supposes is the best level. We
6696 * muck with it only to speed the fan up.
6697 */
6698 if (fan_control_resume_level != 7 &&
6699 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6700 return;
6701 else
6702 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6703 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006704 break;
6705 default:
6706 return;
6707 }
6708 if (do_set) {
6709 printk(TPACPI_NOTICE
6710 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006711 fan_control_resume_level);
6712 rc = fan_set_level_safe(fan_control_resume_level);
6713 if (rc < 0)
6714 printk(TPACPI_NOTICE
6715 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006716 }
6717}
6718
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006719static int fan_read(char *p)
6720{
6721 int len = 0;
6722 int rc;
6723 u8 status;
6724 unsigned int speed = 0;
6725
6726 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006727 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006728 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006729 rc = fan_get_status_safe(&status);
6730 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006731 return rc;
6732
6733 len += sprintf(p + len, "status:\t\t%s\n"
6734 "level:\t\t%d\n",
6735 (status != 0) ? "enabled" : "disabled", status);
6736 break;
6737
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006738 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006739 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006740 rc = fan_get_status_safe(&status);
6741 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006742 return rc;
6743
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006744 len += sprintf(p + len, "status:\t\t%s\n",
6745 (status != 0) ? "enabled" : "disabled");
6746
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006747 rc = fan_get_speed(&speed);
6748 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006749 return rc;
6750
6751 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6752
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006753 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006754 /* Disengaged mode takes precedence */
6755 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006756 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006757 len += sprintf(p + len, "level:\t\tauto\n");
6758 else
6759 len += sprintf(p + len, "level:\t\t%d\n", status);
6760 break;
6761
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006762 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006763 default:
6764 len += sprintf(p + len, "status:\t\tnot supported\n");
6765 }
6766
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006767 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006768 len += sprintf(p + len, "commands:\tlevel <level>");
6769
6770 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006771 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006772 len += sprintf(p + len, " (<level> is 0-7)\n");
6773 break;
6774
6775 default:
6776 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006777 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006778 break;
6779 }
6780 }
6781
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006782 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006783 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006784 "commands:\twatchdog <timeout> (<timeout> "
6785 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006786
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006787 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006788 len += sprintf(p + len, "commands:\tspeed <speed>"
6789 " (<speed> is 0-65535)\n");
6790
6791 return len;
6792}
6793
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006794static int fan_write_cmd_level(const char *cmd, int *rc)
6795{
6796 int level;
6797
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006798 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006799 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006800 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006801 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006802 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006803 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006804 return 0;
6805
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006806 *rc = fan_set_level_safe(level);
6807 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006808 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006809 "access mode %d", fan_control_access_mode);
6810
6811 return 1;
6812}
6813
6814static int fan_write_cmd_enable(const char *cmd, int *rc)
6815{
6816 if (strlencmp(cmd, "enable") != 0)
6817 return 0;
6818
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006819 *rc = fan_set_enable();
6820 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006821 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006822 "access mode %d", fan_control_access_mode);
6823
6824 return 1;
6825}
6826
6827static int fan_write_cmd_disable(const char *cmd, int *rc)
6828{
6829 if (strlencmp(cmd, "disable") != 0)
6830 return 0;
6831
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006832 *rc = fan_set_disable();
6833 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006834 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006835 "access mode %d", fan_control_access_mode);
6836
6837 return 1;
6838}
6839
6840static int fan_write_cmd_speed(const char *cmd, int *rc)
6841{
6842 int speed;
6843
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006844 /* TODO:
6845 * Support speed <low> <medium> <high> ? */
6846
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006847 if (sscanf(cmd, "speed %d", &speed) != 1)
6848 return 0;
6849
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006850 *rc = fan_set_speed(speed);
6851 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006852 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006853 "access mode %d", fan_control_access_mode);
6854
6855 return 1;
6856}
6857
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006858static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6859{
6860 int interval;
6861
6862 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6863 return 0;
6864
6865 if (interval < 0 || interval > 120)
6866 *rc = -EINVAL;
6867 else
6868 fan_watchdog_maxinterval = interval;
6869
6870 return 1;
6871}
6872
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006873static int fan_write(char *buf)
6874{
6875 char *cmd;
6876 int rc = 0;
6877
6878 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006879 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006880 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006881 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006882 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006883 fan_write_cmd_disable(cmd, &rc) ||
6884 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006885 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006886 fan_write_cmd_speed(cmd, &rc))
6887 )
6888 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006889 else if (!rc)
6890 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006891 }
6892
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006893 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006894}
6895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006896static struct ibm_struct fan_driver_data = {
6897 .name = "fan",
6898 .read = fan_read,
6899 .write = fan_write,
6900 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006901 .suspend = fan_suspend,
6902 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006903};
6904
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006905/****************************************************************************
6906 ****************************************************************************
6907 *
6908 * Infrastructure
6909 *
6910 ****************************************************************************
6911 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006913/* sysfs name ---------------------------------------------------------- */
6914static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6915 struct device_attribute *attr,
6916 char *buf)
6917{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006918 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006919}
6920
6921static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6922 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6923
6924/* --------------------------------------------------------------------- */
6925
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006926/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006927static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006928
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006929/*
6930 * Module and infrastructure proble, init and exit handling
6931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006933static int force_load;
6934
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006935#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006936static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006937{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006938 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006939
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006940 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006941}
6942#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6943
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006944static void ibm_exit(struct ibm_struct *ibm)
6945{
6946 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6947
6948 list_del_init(&ibm->all_drivers);
6949
6950 if (ibm->flags.acpi_notify_installed) {
6951 dbg_printk(TPACPI_DBG_EXIT,
6952 "%s: acpi_remove_notify_handler\n", ibm->name);
6953 BUG_ON(!ibm->acpi);
6954 acpi_remove_notify_handler(*ibm->acpi->handle,
6955 ibm->acpi->type,
6956 dispatch_acpi_notify);
6957 ibm->flags.acpi_notify_installed = 0;
6958 ibm->flags.acpi_notify_installed = 0;
6959 }
6960
6961 if (ibm->flags.proc_created) {
6962 dbg_printk(TPACPI_DBG_EXIT,
6963 "%s: remove_proc_entry\n", ibm->name);
6964 remove_proc_entry(ibm->name, proc_dir);
6965 ibm->flags.proc_created = 0;
6966 }
6967
6968 if (ibm->flags.acpi_driver_registered) {
6969 dbg_printk(TPACPI_DBG_EXIT,
6970 "%s: acpi_bus_unregister_driver\n", ibm->name);
6971 BUG_ON(!ibm->acpi);
6972 acpi_bus_unregister_driver(ibm->acpi->driver);
6973 kfree(ibm->acpi->driver);
6974 ibm->acpi->driver = NULL;
6975 ibm->flags.acpi_driver_registered = 0;
6976 }
6977
6978 if (ibm->flags.init_called && ibm->exit) {
6979 ibm->exit();
6980 ibm->flags.init_called = 0;
6981 }
6982
6983 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6984}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006985
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006986static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
6988 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006989 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 struct proc_dir_entry *entry;
6991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006992 BUG_ON(ibm == NULL);
6993
6994 INIT_LIST_HEAD(&ibm->all_drivers);
6995
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006996 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 return 0;
6998
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006999 dbg_printk(TPACPI_DBG_INIT,
7000 "probing for %s\n", ibm->name);
7001
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007002 if (iibm->init) {
7003 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007004 if (ret > 0)
7005 return 0; /* probe failed */
7006 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007008
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007009 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 }
7011
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007012 if (ibm->acpi) {
7013 if (ibm->acpi->hid) {
7014 ret = register_tpacpi_subdriver(ibm);
7015 if (ret)
7016 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007017 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007018
7019 if (ibm->acpi->notify) {
7020 ret = setup_acpi_notify(ibm);
7021 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007022 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007023 ibm->name);
7024 ret = 0;
7025 goto err_out;
7026 }
7027 if (ret < 0)
7028 goto err_out;
7029 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007030 }
7031
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007032 dbg_printk(TPACPI_DBG_INIT,
7033 "%s installed\n", ibm->name);
7034
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007035 if (ibm->read) {
7036 entry = create_proc_entry(ibm->name,
7037 S_IFREG | S_IRUGO | S_IWUSR,
7038 proc_dir);
7039 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007040 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007041 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007042 ret = -ENODEV;
7043 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007044 }
7045 entry->owner = THIS_MODULE;
7046 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007047 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007048 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007049 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007050 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007053 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7054
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007056
7057err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007058 dbg_printk(TPACPI_DBG_INIT,
7059 "%s: at error exit path with result %d\n",
7060 ibm->name, ret);
7061
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007062 ibm_exit(ibm);
7063 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064}
7065
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007066/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007068/* returns 0 - probe ok, or < 0 - probe error.
7069 * Probe ok doesn't mean thinkpad found.
7070 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7071static int __must_check __init get_thinkpad_model_data(
7072 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007073{
Jeff Garzik18552562007-10-03 15:15:40 -04007074 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007075 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007076 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007077
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007078 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007079 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007080
7081 memset(tp, 0, sizeof(*tp));
7082
7083 if (dmi_name_in_vendors("IBM"))
7084 tp->vendor = PCI_VENDOR_ID_IBM;
7085 else if (dmi_name_in_vendors("LENOVO"))
7086 tp->vendor = PCI_VENDOR_ID_LENOVO;
7087 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007088 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007089
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007090 s = dmi_get_system_info(DMI_BIOS_VERSION);
7091 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7092 if (s && !tp->bios_version_str)
7093 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007094 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007095 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007096 tp->bios_model = tp->bios_version_str[0]
7097 | (tp->bios_version_str[1] << 8);
7098
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007099 /*
7100 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7101 * X32 or newer, all Z series; Some models must have an
7102 * up-to-date BIOS or they will not be detected.
7103 *
7104 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7105 */
7106 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007107 if (sscanf(dev->name,
7108 "IBM ThinkPad Embedded Controller -[%17c",
7109 ec_fw_string) == 1) {
7110 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7111 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007112
7113 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007114 if (!tp->ec_version_str)
7115 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007116 tp->ec_model = ec_fw_string[0]
7117 | (ec_fw_string[1] << 8);
7118 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007119 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007120 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007121
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007122 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7123 if (s && !strnicmp(s, "ThinkPad", 8)) {
7124 tp->model_str = kstrdup(s, GFP_KERNEL);
7125 if (!tp->model_str)
7126 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007127 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007128
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007129 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7130 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7131 if (s && !tp->nummodel_str)
7132 return -ENOMEM;
7133
7134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135}
7136
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007137static int __init probe_for_thinkpad(void)
7138{
7139 int is_thinkpad;
7140
7141 if (acpi_disabled)
7142 return -ENODEV;
7143
7144 /*
7145 * Non-ancient models have better DMI tagging, but very old models
7146 * don't.
7147 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007148 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007149
7150 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007151 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007152 if (!ec_handle) {
7153 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007154 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007155 "Not yet supported ThinkPad detected!\n");
7156 return -ENODEV;
7157 }
7158
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007159 /*
7160 * Risks a regression on very old machines, but reduces potential
7161 * false positives a damn great deal
7162 */
7163 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007164 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007165
7166 if (!is_thinkpad && !force_load)
7167 return -ENODEV;
7168
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007169 return 0;
7170}
7171
7172
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007173/* Module init, exit, parameters */
7174
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007175static struct ibm_init_struct ibms_init[] __initdata = {
7176 {
7177 .init = thinkpad_acpi_driver_init,
7178 .data = &thinkpad_acpi_driver_data,
7179 },
7180 {
7181 .init = hotkey_init,
7182 .data = &hotkey_driver_data,
7183 },
7184 {
7185 .init = bluetooth_init,
7186 .data = &bluetooth_driver_data,
7187 },
7188 {
7189 .init = wan_init,
7190 .data = &wan_driver_data,
7191 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007192 {
7193 .init = uwb_init,
7194 .data = &uwb_driver_data,
7195 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007196#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007197 {
7198 .init = video_init,
7199 .data = &video_driver_data,
7200 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007201#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007202 {
7203 .init = light_init,
7204 .data = &light_driver_data,
7205 },
7206#ifdef CONFIG_THINKPAD_ACPI_DOCK
7207 {
7208 .init = dock_init,
7209 .data = &dock_driver_data[0],
7210 },
7211 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007212 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007213 .data = &dock_driver_data[1],
7214 },
7215#endif
7216#ifdef CONFIG_THINKPAD_ACPI_BAY
7217 {
7218 .init = bay_init,
7219 .data = &bay_driver_data,
7220 },
7221#endif
7222 {
7223 .init = cmos_init,
7224 .data = &cmos_driver_data,
7225 },
7226 {
7227 .init = led_init,
7228 .data = &led_driver_data,
7229 },
7230 {
7231 .init = beep_init,
7232 .data = &beep_driver_data,
7233 },
7234 {
7235 .init = thermal_init,
7236 .data = &thermal_driver_data,
7237 },
7238 {
7239 .data = &ecdump_driver_data,
7240 },
7241 {
7242 .init = brightness_init,
7243 .data = &brightness_driver_data,
7244 },
7245 {
7246 .data = &volume_driver_data,
7247 },
7248 {
7249 .init = fan_init,
7250 .data = &fan_driver_data,
7251 },
7252};
7253
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007254static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7255{
7256 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007257 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007258
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007259 if (!kp || !kp->name || !val)
7260 return -EINVAL;
7261
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007262 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7263 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007264 WARN_ON(ibm == NULL);
7265
7266 if (!ibm || !ibm->name)
7267 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007268
7269 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7270 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007271 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007272 strcpy(ibms_init[i].param, val);
7273 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007274 return 0;
7275 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007276 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007277
7278 return -EINVAL;
7279}
7280
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007281module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007282MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007283 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007284
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007285module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007286MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007287
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007288module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007289MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007290 "Attempts to load the driver even on a "
7291 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007292
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007293module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007294MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007295 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007296
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007297module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007298MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007299 "Selects brightness control strategy: "
7300 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007301
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007302module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007303MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007304 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007305
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007306module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007307MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007308 "used for backwards compatibility with userspace, "
7309 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007310
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007311#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007312 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007313 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007314 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007315
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007316TPACPI_PARAM(hotkey);
7317TPACPI_PARAM(bluetooth);
7318TPACPI_PARAM(video);
7319TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007320#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007321TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007322#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007323#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007324TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007325#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007326TPACPI_PARAM(cmos);
7327TPACPI_PARAM(led);
7328TPACPI_PARAM(beep);
7329TPACPI_PARAM(ecdump);
7330TPACPI_PARAM(brightness);
7331TPACPI_PARAM(volume);
7332TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007333
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007334#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7335module_param(dbg_wlswemul, uint, 0);
7336MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7337module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7338MODULE_PARM_DESC(wlsw_state,
7339 "Initial state of the emulated WLSW switch");
7340
7341module_param(dbg_bluetoothemul, uint, 0);
7342MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7343module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7344MODULE_PARM_DESC(bluetooth_state,
7345 "Initial state of the emulated bluetooth switch");
7346
7347module_param(dbg_wwanemul, uint, 0);
7348MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7349module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7350MODULE_PARM_DESC(wwan_state,
7351 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007352
7353module_param(dbg_uwbemul, uint, 0);
7354MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7355module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7356MODULE_PARM_DESC(uwb_state,
7357 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007358#endif
7359
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007360static void thinkpad_acpi_module_exit(void)
7361{
7362 struct ibm_struct *ibm, *itmp;
7363
7364 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7365
7366 list_for_each_entry_safe_reverse(ibm, itmp,
7367 &tpacpi_all_drivers,
7368 all_drivers) {
7369 ibm_exit(ibm);
7370 }
7371
7372 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7373
7374 if (tpacpi_inputdev) {
7375 if (tp_features.input_device_registered)
7376 input_unregister_device(tpacpi_inputdev);
7377 else
7378 input_free_device(tpacpi_inputdev);
7379 }
7380
7381 if (tpacpi_hwmon)
7382 hwmon_device_unregister(tpacpi_hwmon);
7383
7384 if (tp_features.sensors_pdev_attrs_registered)
7385 device_remove_file(&tpacpi_sensors_pdev->dev,
7386 &dev_attr_thinkpad_acpi_pdev_name);
7387 if (tpacpi_sensors_pdev)
7388 platform_device_unregister(tpacpi_sensors_pdev);
7389 if (tpacpi_pdev)
7390 platform_device_unregister(tpacpi_pdev);
7391
7392 if (tp_features.sensors_pdrv_attrs_registered)
7393 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7394 if (tp_features.platform_drv_attrs_registered)
7395 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7396
7397 if (tp_features.sensors_pdrv_registered)
7398 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7399
7400 if (tp_features.platform_drv_registered)
7401 platform_driver_unregister(&tpacpi_pdriver);
7402
7403 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007404 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007405
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007406 if (tpacpi_wq)
7407 destroy_workqueue(tpacpi_wq);
7408
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007409 kfree(thinkpad_id.bios_version_str);
7410 kfree(thinkpad_id.ec_version_str);
7411 kfree(thinkpad_id.model_str);
7412}
7413
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007414
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007415static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416{
7417 int ret, i;
7418
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007419 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7420
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007421 /* Parameter checking */
7422 if (hotkey_report_mode > 2)
7423 return -EINVAL;
7424
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007425 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007426
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007427 ret = get_thinkpad_model_data(&thinkpad_id);
7428 if (ret) {
7429 printk(TPACPI_ERR
7430 "unable to get DMI data: %d\n", ret);
7431 thinkpad_acpi_module_exit();
7432 return ret;
7433 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007434 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007435 if (ret) {
7436 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007437 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007440 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007441
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007442 TPACPI_ACPIHANDLE_INIT(ecrd);
7443 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007445 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7446 if (!tpacpi_wq) {
7447 thinkpad_acpi_module_exit();
7448 return -ENOMEM;
7449 }
7450
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007451 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007453 printk(TPACPI_ERR
7454 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007455 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 return -ENODEV;
7457 }
7458 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007459
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007460 ret = platform_driver_register(&tpacpi_pdriver);
7461 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007462 printk(TPACPI_ERR
7463 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007464 thinkpad_acpi_module_exit();
7465 return ret;
7466 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007467 tp_features.platform_drv_registered = 1;
7468
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007469 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7470 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007471 printk(TPACPI_ERR
7472 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007473 thinkpad_acpi_module_exit();
7474 return ret;
7475 }
7476 tp_features.sensors_pdrv_registered = 1;
7477
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007478 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007479 if (!ret) {
7480 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007481 ret = tpacpi_create_driver_attributes(
7482 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007483 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007484 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007485 printk(TPACPI_ERR
7486 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007487 thinkpad_acpi_module_exit();
7488 return ret;
7489 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007490 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007491
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007492
7493 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007494 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007495 NULL, 0);
7496 if (IS_ERR(tpacpi_pdev)) {
7497 ret = PTR_ERR(tpacpi_pdev);
7498 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007499 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007500 thinkpad_acpi_module_exit();
7501 return ret;
7502 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007503 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007504 TPACPI_HWMON_DRVR_NAME,
7505 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007506 if (IS_ERR(tpacpi_sensors_pdev)) {
7507 ret = PTR_ERR(tpacpi_sensors_pdev);
7508 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007509 printk(TPACPI_ERR
7510 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007511 thinkpad_acpi_module_exit();
7512 return ret;
7513 }
7514 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7515 &dev_attr_thinkpad_acpi_pdev_name);
7516 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007517 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007518 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007519 thinkpad_acpi_module_exit();
7520 return ret;
7521 }
7522 tp_features.sensors_pdev_attrs_registered = 1;
7523 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007524 if (IS_ERR(tpacpi_hwmon)) {
7525 ret = PTR_ERR(tpacpi_hwmon);
7526 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007527 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007528 thinkpad_acpi_module_exit();
7529 return ret;
7530 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007531 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007532 tpacpi_inputdev = input_allocate_device();
7533 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007534 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007535 thinkpad_acpi_module_exit();
7536 return -ENOMEM;
7537 } else {
7538 /* Prepare input device, but don't register */
7539 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007540 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007541 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007542 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7543 thinkpad_id.vendor :
7544 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007545 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7546 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7547 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007548 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7549 ret = ibm_init(&ibms_init[i]);
7550 if (ret >= 0 && *ibms_init[i].param)
7551 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007553 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 return ret;
7555 }
7556 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007557 ret = input_register_device(tpacpi_inputdev);
7558 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007559 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007560 thinkpad_acpi_module_exit();
7561 return ret;
7562 } else {
7563 tp_features.input_device_registered = 1;
7564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007566 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 return 0;
7568}
7569
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007570MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7571
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007572/*
7573 * DMI matching for module autoloading
7574 *
7575 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7576 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7577 *
7578 * Only models listed in thinkwiki will be supported, so add yours
7579 * if it is not there yet.
7580 */
7581#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007582 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007583
7584/* Non-ancient thinkpads */
7585MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7586MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7587
7588/* Ancient thinkpad BIOSes have to be identified by
7589 * BIOS type or model number, and there are far less
7590 * BIOS types than model numbers... */
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007591IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7592IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7593IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007594
7595MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007596MODULE_DESCRIPTION(TPACPI_DESC);
7597MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007598MODULE_LICENSE("GPL");
7599
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007600module_init(thinkpad_acpi_module_init);
7601module_exit(thinkpad_acpi_module_exit);