blob: 3981b060b7d5f39cdb9a8460a7307e53b11515e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb57f7e72009-04-14 02:44:14 +000024#define TPACPI_VERSION "0.23"
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +000025#define TPACPI_SYSFS_VERSION 0x020300
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300169/* rfkill switches */
170enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200173 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300174};
175
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000176/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200177#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000178#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181#define TPACPI_ERR KERN_ERR TPACPI_LOG
182#define TPACPI_WARN KERN_WARNING TPACPI_LOG
183#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184#define TPACPI_INFO KERN_INFO TPACPI_LOG
185#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200186
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000187/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200188#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000189#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200190#define TPACPI_DBG_INIT 0x0001
191#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000192#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000193#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000194#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000195#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200196
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200197#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200201
202/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200203 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200204 */
205
206struct ibm_struct;
207
208struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
211
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
216};
217
218struct ibm_struct {
219 char *name;
220
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200225 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200226 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200227
228 struct list_head all_drivers;
229
230 struct tp_acpi_drv_struct *acpi;
231
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
239};
240
241struct ibm_init_struct {
242 char param[32];
243
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
246};
247
248static struct {
249#ifdef CONFIG_THINKPAD_ACPI_BAY
250 u32 bay_status:1;
251 u32 bay_eject:1;
252 u32 bay_status2:1;
253 u32 bay_eject2:1;
254#endif
255 u32 bluetooth:1;
256 u32 hotkey:1;
257 u32 hotkey_mask:1;
258 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200259 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 u32 light:1;
261 u32 light_status:1;
262 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300263 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200265 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
274} tp_features;
275
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300276static struct {
277 u16 hotkey_mask_ff:1;
278} tp_warned;
279
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280struct thinkpad_id_data {
281 unsigned int vendor; /* ThinkPad vendor:
282 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283
284 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
285 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300287 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200288 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300289 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
290 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200291
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300292 char *model_str; /* ThinkPad T43 */
293 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200294};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200295static struct thinkpad_id_data thinkpad_id;
296
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300297static enum {
298 TPACPI_LIFE_INIT = 0,
299 TPACPI_LIFE_RUNNING,
300 TPACPI_LIFE_EXITING,
301} tpacpi_lifecycle;
302
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200303static int experimental;
304static u32 dbg_level;
305
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300306static struct workqueue_struct *tpacpi_wq;
307
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000308enum led_status_t {
309 TPACPI_LED_OFF = 0,
310 TPACPI_LED_ON,
311 TPACPI_LED_BLINK,
312};
313
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300314/* Special LED class that can defer work */
315struct tpacpi_led_classdev {
316 struct led_classdev led_classdev;
317 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000318 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300319 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300320};
321
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200322#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
323static int dbg_wlswemul;
324static int tpacpi_wlsw_emulstate;
325static int dbg_bluetoothemul;
326static int tpacpi_bluetooth_emulstate;
327static int dbg_wwanemul;
328static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200329static int dbg_uwbemul;
330static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200331#endif
332
333
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000334/*************************************************************************
335 * Debugging helpers
336 */
337
338#define dbg_printk(a_dbg_level, format, arg...) \
339 do { if (dbg_level & (a_dbg_level)) \
340 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
341 } while (0)
342
343#ifdef CONFIG_THINKPAD_ACPI_DEBUG
344#define vdbg_printk dbg_printk
345static const char *str_supported(int is_supported);
346#else
347#define vdbg_printk(a_dbg_level, format, arg...) \
348 do { } while (0)
349#endif
350
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000351static void tpacpi_log_usertask(const char * const what)
352{
353 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
354 what, task_tgid_vnr(current));
355}
356
357#define tpacpi_disclose_usertask(what, format, arg...) \
358 do { \
359 if (unlikely( \
360 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
361 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
362 printk(TPACPI_DEBUG "%s: PID %d: " format, \
363 what, task_tgid_vnr(current), ## arg); \
364 } \
365 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000366
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300367/*
368 * Quirk handling helpers
369 *
370 * ThinkPad IDs and versions seen in the field so far
371 * are two-characters from the set [0-9A-Z], i.e. base 36.
372 *
373 * We use values well outside that range as specials.
374 */
375
376#define TPACPI_MATCH_ANY 0xffffU
377#define TPACPI_MATCH_UNKNOWN 0U
378
379/* TPID('1', 'Y') == 0x5931 */
380#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
381
382#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
383 { .vendor = PCI_VENDOR_ID_IBM, \
384 .bios = TPID(__id1, __id2), \
385 .ec = TPACPI_MATCH_ANY, \
386 .quirks = (__quirk) }
387
388#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
389 { .vendor = PCI_VENDOR_ID_LENOVO, \
390 .bios = TPID(__id1, __id2), \
391 .ec = TPACPI_MATCH_ANY, \
392 .quirks = (__quirk) }
393
394struct tpacpi_quirk {
395 unsigned int vendor;
396 u16 bios;
397 u16 ec;
398 unsigned long quirks;
399};
400
401/**
402 * tpacpi_check_quirks() - search BIOS/EC version on a list
403 * @qlist: array of &struct tpacpi_quirk
404 * @qlist_size: number of elements in @qlist
405 *
406 * Iterates over a quirks list until one is found that matches the
407 * ThinkPad's vendor, BIOS and EC model.
408 *
409 * Returns 0 if nothing matches, otherwise returns the quirks field of
410 * the matching &struct tpacpi_quirk entry.
411 *
412 * The match criteria is: vendor, ec and bios much match.
413 */
414static unsigned long __init tpacpi_check_quirks(
415 const struct tpacpi_quirk *qlist,
416 unsigned int qlist_size)
417{
418 while (qlist_size) {
419 if ((qlist->vendor == thinkpad_id.vendor ||
420 qlist->vendor == TPACPI_MATCH_ANY) &&
421 (qlist->bios == thinkpad_id.bios_model ||
422 qlist->bios == TPACPI_MATCH_ANY) &&
423 (qlist->ec == thinkpad_id.ec_model ||
424 qlist->ec == TPACPI_MATCH_ANY))
425 return qlist->quirks;
426
427 qlist_size--;
428 qlist++;
429 }
430 return 0;
431}
432
433
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300434/****************************************************************************
435 ****************************************************************************
436 *
437 * ACPI Helpers and device model
438 *
439 ****************************************************************************
440 ****************************************************************************/
441
442/*************************************************************************
443 * ACPI basic handles
444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300446static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200448#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 static acpi_handle object##_handle; \
450 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400451 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 static char *object##_paths[] = { paths }
453
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200454TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400455 "\\_SB.PCI.ISA.EC", /* 570 */
456 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
457 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
458 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
459 "\\_SB.PCI0.ICH3.EC0", /* R31 */
460 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300461 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200463TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
464TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200466TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
467 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400468 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
469 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300470 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400471
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200472TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400473 "^HKEY", /* R30, R31 */
474 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400476
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200477TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
478 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
479 "\\_SB.PCI0.VID0", /* 770e */
480 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
481 "\\_SB.PCI0.AGP.VID", /* all others */
482 ); /* R30, R31 */
483
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485/*************************************************************************
486 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200487 */
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static int acpi_evalf(acpi_handle handle,
490 void *res, char *method, char *fmt, ...)
491{
492 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400493 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200494 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400495 struct acpi_buffer result, *resultp;
496 union acpi_object out_obj;
497 acpi_status status;
498 va_list ap;
499 char res_type;
500 int success;
501 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200504 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506 }
507
508 if (*fmt == 'q') {
509 quiet = 1;
510 fmt++;
511 } else
512 quiet = 0;
513
514 res_type = *(fmt++);
515
516 params.count = 0;
517 params.pointer = &in_objs[0];
518
519 va_start(ap, fmt);
520 while (*fmt) {
521 char c = *(fmt++);
522 switch (c) {
523 case 'd': /* int */
524 in_objs[params.count].integer.value = va_arg(ap, int);
525 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
526 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400527 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 "with invalid format character '%c'\n", c);
531 return 0;
532 }
533 }
534 va_end(ap);
535
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536 if (res_type != 'v') {
537 result.length = sizeof(out_obj);
538 result.pointer = &out_obj;
539 resultp = &result;
540 } else
541 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400546 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (res)
548 *(int *)res = out_obj.integer.value;
549 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
550 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400551 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 success = status == AE_OK;
553 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400554 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200556 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 "with invalid format character '%c'\n", res_type);
558 return 0;
559 }
560
561 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200562 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 method, fmt0, status);
564
565 return success;
566}
567
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200568static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300569{
570 int v;
571
572 if (ecrd_handle) {
573 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
574 return 0;
575 *p = v;
576 } else {
577 if (ec_read(i, p) < 0)
578 return 0;
579 }
580
581 return 1;
582}
583
584static int acpi_ec_write(int i, u8 v)
585{
586 if (ecwr_handle) {
587 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
588 return 0;
589 } else {
590 if (ec_write(i, v) < 0)
591 return 0;
592 }
593
594 return 1;
595}
596
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200597#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300598static int _sta(acpi_handle handle)
599{
600 int status;
601
602 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
603 status = 0;
604
605 return status;
606}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200607#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300609static int issue_thinkpad_cmos_command(int cmos_cmd)
610{
611 if (!cmos_handle)
612 return -ENXIO;
613
614 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
615 return -EIO;
616
617 return 0;
618}
619
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300620/*************************************************************************
621 * ACPI device model
622 */
623
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200624#define TPACPI_ACPIHANDLE_INIT(object) \
625 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200626 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
627
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300628static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300629 acpi_handle *handle, acpi_handle parent,
630 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300631{
632 int i;
633 acpi_status status;
634
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300635 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
636 name);
637
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300638 for (i = 0; i < num_paths; i++) {
639 status = acpi_get_handle(parent, paths[i], handle);
640 if (ACPI_SUCCESS(status)) {
641 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300642 dbg_printk(TPACPI_DBG_INIT,
643 "Found ACPI handle %s for %s\n",
644 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300645 return;
646 }
647 }
648
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300649 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
650 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300651 *handle = NULL;
652}
653
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300654static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300655{
656 struct ibm_struct *ibm = data;
657
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300658 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
659 return;
660
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300661 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300662 return;
663
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300664 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300665}
666
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300667static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668{
669 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300670 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300671
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300672 BUG_ON(!ibm->acpi);
673
674 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300675 return 0;
676
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300677 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300678 "setting up ACPI notify for %s\n", ibm->name);
679
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300680 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
681 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200682 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300683 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684 return -ENODEV;
685 }
686
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700687 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300688 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200689 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300690 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300691
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300692 status = acpi_install_notify_handler(*ibm->acpi->handle,
693 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300694 if (ACPI_FAILURE(status)) {
695 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200696 printk(TPACPI_NOTICE
697 "another device driver is already "
698 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200700 printk(TPACPI_ERR
701 "acpi_install_notify_handler(%s) failed: %d\n",
702 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300703 }
704 return -ENODEV;
705 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300706 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300707 return 0;
708}
709
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300710static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300711{
712 return 0;
713}
714
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300715static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300716{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300717 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300718
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300719 dbg_printk(TPACPI_DBG_INIT,
720 "registering %s as an ACPI driver\n", ibm->name);
721
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300722 BUG_ON(!ibm->acpi);
723
724 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
725 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300726 printk(TPACPI_ERR
727 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300728 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729 }
730
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200731 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300732 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300734 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300735
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300736 rc = acpi_bus_register_driver(ibm->acpi->driver);
737 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200738 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200739 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300740 kfree(ibm->acpi->driver);
741 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300742 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300743 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300744
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300745 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300746}
747
748
749/****************************************************************************
750 ****************************************************************************
751 *
752 * Procfs Helpers
753 *
754 ****************************************************************************
755 ****************************************************************************/
756
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300757static int dispatch_procfs_read(char *page, char **start, off_t off,
758 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759{
760 struct ibm_struct *ibm = data;
761 int len;
762
763 if (!ibm || !ibm->read)
764 return -EINVAL;
765
766 len = ibm->read(page);
767 if (len < 0)
768 return len;
769
770 if (len <= off + count)
771 *eof = 1;
772 *start = page + off;
773 len -= off;
774 if (len > count)
775 len = count;
776 if (len < 0)
777 len = 0;
778
779 return len;
780}
781
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300782static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200783 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300784 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300785{
786 struct ibm_struct *ibm = data;
787 char *kernbuf;
788 int ret;
789
790 if (!ibm || !ibm->write)
791 return -EINVAL;
792
793 kernbuf = kmalloc(count + 2, GFP_KERNEL);
794 if (!kernbuf)
795 return -ENOMEM;
796
797 if (copy_from_user(kernbuf, userbuf, count)) {
798 kfree(kernbuf);
799 return -EFAULT;
800 }
801
802 kernbuf[count] = 0;
803 strcat(kernbuf, ",");
804 ret = ibm->write(kernbuf);
805 if (ret == 0)
806 ret = count;
807
808 kfree(kernbuf);
809
810 return ret;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static char *next_cmd(char **cmds)
814{
815 char *start = *cmds;
816 char *end;
817
818 while ((end = strchr(start, ',')) && end == start)
819 start = end + 1;
820
821 if (!end)
822 return NULL;
823
824 *end = 0;
825 *cmds = end + 1;
826 return start;
827}
828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300829
830/****************************************************************************
831 ****************************************************************************
832 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300833 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300834 *
835 ****************************************************************************
836 ****************************************************************************/
837
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300838static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300839static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700840static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300841static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300842static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200843static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300844
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200845static int tpacpi_suspend_handler(struct platform_device *pdev,
846 pm_message_t state)
847{
848 struct ibm_struct *ibm, *itmp;
849
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->suspend)
854 (ibm->suspend)(state);
855 }
856
857 return 0;
858}
859
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300860static int tpacpi_resume_handler(struct platform_device *pdev)
861{
862 struct ibm_struct *ibm, *itmp;
863
864 list_for_each_entry_safe(ibm, itmp,
865 &tpacpi_all_drivers,
866 all_drivers) {
867 if (ibm->resume)
868 (ibm->resume)();
869 }
870
871 return 0;
872}
873
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200874static void tpacpi_shutdown_handler(struct platform_device *pdev)
875{
876 struct ibm_struct *ibm, *itmp;
877
878 list_for_each_entry_safe(ibm, itmp,
879 &tpacpi_all_drivers,
880 all_drivers) {
881 if (ibm->shutdown)
882 (ibm->shutdown)();
883 }
884}
885
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300886static struct platform_driver tpacpi_pdriver = {
887 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200888 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300889 .owner = THIS_MODULE,
890 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200891 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300892 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200893 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300894};
895
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300896static struct platform_driver tpacpi_hwmon_pdriver = {
897 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200898 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300899 .owner = THIS_MODULE,
900 },
901};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300902
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300903/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300904 * sysfs support helpers
905 */
906
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200907struct attribute_set {
908 unsigned int members, max_members;
909 struct attribute_group group;
910};
911
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300912struct attribute_set_obj {
913 struct attribute_set s;
914 struct attribute *a;
915} __attribute__((packed));
916
917static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200918 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300919{
920 struct attribute_set_obj *sobj;
921
922 if (max_members == 0)
923 return NULL;
924
925 /* Allocates space for implicit NULL at the end too */
926 sobj = kzalloc(sizeof(struct attribute_set_obj) +
927 max_members * sizeof(struct attribute *),
928 GFP_KERNEL);
929 if (!sobj)
930 return NULL;
931 sobj->s.max_members = max_members;
932 sobj->s.group.attrs = &sobj->a;
933 sobj->s.group.name = name;
934
935 return &sobj->s;
936}
937
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200938#define destroy_attr_set(_set) \
939 kfree(_set);
940
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300941/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200942static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300943{
944 if (!s || !attr)
945 return -EINVAL;
946
947 if (s->members >= s->max_members)
948 return -ENOMEM;
949
950 s->group.attrs[s->members] = attr;
951 s->members++;
952
953 return 0;
954}
955
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200956static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300957 struct attribute **attr,
958 unsigned int count)
959{
960 int i, res;
961
962 for (i = 0; i < count; i++) {
963 res = add_to_attr_set(s, attr[i]);
964 if (res)
965 return res;
966 }
967
968 return 0;
969}
970
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200971static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300972{
973 sysfs_remove_group(kobj, &s->group);
974 destroy_attr_set(s);
975}
976
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200977#define register_attr_set_with_sysfs(_attr_set, _kobj) \
978 sysfs_create_group(_kobj, &_attr_set->group)
979
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300980static int parse_strtoul(const char *buf,
981 unsigned long max, unsigned long *value)
982{
983 char *endp;
984
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300985 while (*buf && isspace(*buf))
986 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300987 *value = simple_strtoul(buf, &endp, 0);
988 while (*endp && isspace(*endp))
989 endp++;
990 if (*endp || *value > max)
991 return -EINVAL;
992
993 return 0;
994}
995
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300996static void tpacpi_disable_brightness_delay(void)
997{
998 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
999 printk(TPACPI_NOTICE
1000 "ACPI backlight control delay disabled\n");
1001}
1002
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001003static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1004{
1005 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1006 union acpi_object *obj;
1007 int rc;
1008
1009 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1010 obj = (union acpi_object *)buffer.pointer;
1011 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1012 printk(TPACPI_ERR "Unknown _BCL data, "
1013 "please report this to %s\n", TPACPI_MAIL);
1014 rc = 0;
1015 } else {
1016 rc = obj->package.count;
1017 }
1018 } else {
1019 return 0;
1020 }
1021
1022 kfree(buffer.pointer);
1023 return rc;
1024}
1025
1026static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1027 u32 lvl, void *context, void **rv)
1028{
1029 char name[ACPI_PATH_SEGMENT_LENGTH];
1030 struct acpi_buffer buffer = { sizeof(name), &name };
1031
1032 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1033 !strncmp("_BCL", name, sizeof(name) - 1)) {
1034 BUG_ON(!rv || !*rv);
1035 **(int **)rv = tpacpi_query_bcl_levels(handle);
1036 return AE_CTRL_TERMINATE;
1037 } else {
1038 return AE_OK;
1039 }
1040}
1041
1042/*
1043 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1044 */
1045static int __init tpacpi_check_std_acpi_brightness_support(void)
1046{
1047 int status;
1048 int bcl_levels = 0;
1049 void *bcl_ptr = &bcl_levels;
1050
1051 if (!vid_handle) {
1052 TPACPI_ACPIHANDLE_INIT(vid);
1053 }
1054 if (!vid_handle)
1055 return 0;
1056
1057 /*
1058 * Search for a _BCL method, and execute it. This is safe on all
1059 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1060 * BIOS in ACPI backlight control mode. We do NOT have to care
1061 * about calling the _BCL method in an enabled video device, any
1062 * will do for our purposes.
1063 */
1064
1065 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1066 tpacpi_acpi_walk_find_bcl, NULL,
1067 &bcl_ptr);
1068
1069 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1070 tp_features.bright_acpimode = 1;
1071 return (bcl_levels - 2);
1072 }
1073
1074 return 0;
1075}
1076
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001077static int __init tpacpi_new_rfkill(const unsigned int id,
1078 struct rfkill **rfk,
1079 const enum rfkill_type rfktype,
1080 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001081 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001082 int (*toggle_radio)(void *, enum rfkill_state),
1083 int (*get_state)(void *, enum rfkill_state *))
1084{
1085 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001086 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1087
1088 res = get_state(NULL, &initial_state);
1089 if (res < 0) {
1090 printk(TPACPI_ERR
1091 "failed to read initial state for %s, error %d; "
1092 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001093 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001094 /* try to set the initial state as the default for the rfkill
1095 * type, since we ask the firmware to preserve it across S5 in
1096 * NVRAM */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001097 if (rfkill_set_default(rfktype,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001098 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1099 RFKILL_STATE_UNBLOCKED :
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00001100 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1101 vdbg_printk(TPACPI_DBG_RFKILL,
1102 "Default state for %s cannot be changed\n",
1103 name);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001104 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001105
1106 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1107 if (!*rfk) {
1108 printk(TPACPI_ERR
1109 "failed to allocate memory for rfkill class\n");
1110 return -ENOMEM;
1111 }
1112
1113 (*rfk)->name = name;
1114 (*rfk)->get_state = get_state;
1115 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001116 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001117
1118 res = rfkill_register(*rfk);
1119 if (res < 0) {
1120 printk(TPACPI_ERR
1121 "failed to register %s rfkill switch: %d\n",
1122 name, res);
1123 rfkill_free(*rfk);
1124 *rfk = NULL;
1125 return res;
1126 }
1127
1128 return 0;
1129}
1130
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001131static void printk_deprecated_attribute(const char * const what,
1132 const char * const details)
1133{
1134 tpacpi_log_usertask("deprecated sysfs attribute");
1135 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1136 "will be removed. %s\n",
1137 what, details);
1138}
1139
1140static void printk_deprecated_rfkill_attribute(const char * const what)
1141{
1142 printk_deprecated_attribute(what,
1143 "Please switch to generic rfkill before year 2010");
1144}
1145
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001146/*************************************************************************
1147 * thinkpad-acpi driver attributes
1148 */
1149
1150/* interface_version --------------------------------------------------- */
1151static ssize_t tpacpi_driver_interface_version_show(
1152 struct device_driver *drv,
1153 char *buf)
1154{
1155 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1156}
1157
1158static DRIVER_ATTR(interface_version, S_IRUGO,
1159 tpacpi_driver_interface_version_show, NULL);
1160
1161/* debug_level --------------------------------------------------------- */
1162static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1163 char *buf)
1164{
1165 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1166}
1167
1168static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1169 const char *buf, size_t count)
1170{
1171 unsigned long t;
1172
1173 if (parse_strtoul(buf, 0xffff, &t))
1174 return -EINVAL;
1175
1176 dbg_level = t;
1177
1178 return count;
1179}
1180
1181static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1182 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1183
1184/* version ------------------------------------------------------------- */
1185static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1186 char *buf)
1187{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001188 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1189 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001190}
1191
1192static DRIVER_ATTR(version, S_IRUGO,
1193 tpacpi_driver_version_show, NULL);
1194
1195/* --------------------------------------------------------------------- */
1196
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001197#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1198
1199static void tpacpi_send_radiosw_update(void);
1200
1201/* wlsw_emulstate ------------------------------------------------------ */
1202static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1203 char *buf)
1204{
1205 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1206}
1207
1208static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1209 const char *buf, size_t count)
1210{
1211 unsigned long t;
1212
1213 if (parse_strtoul(buf, 1, &t))
1214 return -EINVAL;
1215
1216 if (tpacpi_wlsw_emulstate != t) {
1217 tpacpi_wlsw_emulstate = !!t;
1218 tpacpi_send_radiosw_update();
1219 } else
1220 tpacpi_wlsw_emulstate = !!t;
1221
1222 return count;
1223}
1224
1225static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1226 tpacpi_driver_wlsw_emulstate_show,
1227 tpacpi_driver_wlsw_emulstate_store);
1228
1229/* bluetooth_emulstate ------------------------------------------------- */
1230static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1231 struct device_driver *drv,
1232 char *buf)
1233{
1234 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1235}
1236
1237static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1238 struct device_driver *drv,
1239 const char *buf, size_t count)
1240{
1241 unsigned long t;
1242
1243 if (parse_strtoul(buf, 1, &t))
1244 return -EINVAL;
1245
1246 tpacpi_bluetooth_emulstate = !!t;
1247
1248 return count;
1249}
1250
1251static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1252 tpacpi_driver_bluetooth_emulstate_show,
1253 tpacpi_driver_bluetooth_emulstate_store);
1254
1255/* wwan_emulstate ------------------------------------------------- */
1256static ssize_t tpacpi_driver_wwan_emulstate_show(
1257 struct device_driver *drv,
1258 char *buf)
1259{
1260 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1261}
1262
1263static ssize_t tpacpi_driver_wwan_emulstate_store(
1264 struct device_driver *drv,
1265 const char *buf, size_t count)
1266{
1267 unsigned long t;
1268
1269 if (parse_strtoul(buf, 1, &t))
1270 return -EINVAL;
1271
1272 tpacpi_wwan_emulstate = !!t;
1273
1274 return count;
1275}
1276
1277static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1278 tpacpi_driver_wwan_emulstate_show,
1279 tpacpi_driver_wwan_emulstate_store);
1280
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001281/* uwb_emulstate ------------------------------------------------- */
1282static ssize_t tpacpi_driver_uwb_emulstate_show(
1283 struct device_driver *drv,
1284 char *buf)
1285{
1286 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1287}
1288
1289static ssize_t tpacpi_driver_uwb_emulstate_store(
1290 struct device_driver *drv,
1291 const char *buf, size_t count)
1292{
1293 unsigned long t;
1294
1295 if (parse_strtoul(buf, 1, &t))
1296 return -EINVAL;
1297
1298 tpacpi_uwb_emulstate = !!t;
1299
1300 return count;
1301}
1302
1303static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1304 tpacpi_driver_uwb_emulstate_show,
1305 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001306#endif
1307
1308/* --------------------------------------------------------------------- */
1309
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001310static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001311 &driver_attr_debug_level, &driver_attr_version,
1312 &driver_attr_interface_version,
1313};
1314
1315static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1316{
1317 int i, res;
1318
1319 i = 0;
1320 res = 0;
1321 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1322 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1323 i++;
1324 }
1325
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001326#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1327 if (!res && dbg_wlswemul)
1328 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1329 if (!res && dbg_bluetoothemul)
1330 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1331 if (!res && dbg_wwanemul)
1332 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001333 if (!res && dbg_uwbemul)
1334 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001335#endif
1336
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001337 return res;
1338}
1339
1340static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1341{
1342 int i;
1343
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001344 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001345 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001346
1347#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1348 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1349 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1350 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001351 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001352#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001353}
1354
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001355/****************************************************************************
1356 ****************************************************************************
1357 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001358 * Subdrivers
1359 *
1360 ****************************************************************************
1361 ****************************************************************************/
1362
1363/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001364 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001365 */
1366
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001367static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001369 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1370 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001372 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001373 (thinkpad_id.bios_version_str) ?
1374 thinkpad_id.bios_version_str : "unknown",
1375 (thinkpad_id.ec_version_str) ?
1376 thinkpad_id.ec_version_str : "unknown");
1377
1378 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001379 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001380 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1381 "IBM" : ((thinkpad_id.vendor ==
1382 PCI_VENDOR_ID_LENOVO) ?
1383 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001384 thinkpad_id.model_str,
1385 (thinkpad_id.nummodel_str) ?
1386 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
1390
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001391static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 int len = 0;
1394
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001395 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1396 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 return len;
1399}
1400
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001401static struct ibm_struct thinkpad_acpi_driver_data = {
1402 .name = "driver",
1403 .read = thinkpad_acpi_driver_read,
1404};
1405
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001406/*************************************************************************
1407 * Hotkey subdriver
1408 */
1409
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001410enum { /* hot key scan codes (derived from ACPI DSDT) */
1411 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1412 TP_ACPI_HOTKEYSCAN_FNF2,
1413 TP_ACPI_HOTKEYSCAN_FNF3,
1414 TP_ACPI_HOTKEYSCAN_FNF4,
1415 TP_ACPI_HOTKEYSCAN_FNF5,
1416 TP_ACPI_HOTKEYSCAN_FNF6,
1417 TP_ACPI_HOTKEYSCAN_FNF7,
1418 TP_ACPI_HOTKEYSCAN_FNF8,
1419 TP_ACPI_HOTKEYSCAN_FNF9,
1420 TP_ACPI_HOTKEYSCAN_FNF10,
1421 TP_ACPI_HOTKEYSCAN_FNF11,
1422 TP_ACPI_HOTKEYSCAN_FNF12,
1423 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1424 TP_ACPI_HOTKEYSCAN_FNINSERT,
1425 TP_ACPI_HOTKEYSCAN_FNDELETE,
1426 TP_ACPI_HOTKEYSCAN_FNHOME,
1427 TP_ACPI_HOTKEYSCAN_FNEND,
1428 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1429 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1430 TP_ACPI_HOTKEYSCAN_FNSPACE,
1431 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1432 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1433 TP_ACPI_HOTKEYSCAN_MUTE,
1434 TP_ACPI_HOTKEYSCAN_THINKPAD,
1435};
1436
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001437enum { /* Keys available through NVRAM polling */
1438 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1439 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1440};
1441
1442enum { /* Positions of some of the keys in hotkey masks */
1443 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1444 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1445 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1446 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1447 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1448 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1449 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1450 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1451 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1452 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1453 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1454};
1455
1456enum { /* NVRAM to ACPI HKEY group map */
1457 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1458 TP_ACPI_HKEY_ZOOM_MASK |
1459 TP_ACPI_HKEY_DISPSWTCH_MASK |
1460 TP_ACPI_HKEY_HIBERNATE_MASK,
1461 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1462 TP_ACPI_HKEY_BRGHTDWN_MASK,
1463 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1464 TP_ACPI_HKEY_VOLDWN_MASK |
1465 TP_ACPI_HKEY_MUTE_MASK,
1466};
1467
1468#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1469struct tp_nvram_state {
1470 u16 thinkpad_toggle:1;
1471 u16 zoom_toggle:1;
1472 u16 display_toggle:1;
1473 u16 thinklight_toggle:1;
1474 u16 hibernate_toggle:1;
1475 u16 displayexp_toggle:1;
1476 u16 display_state:1;
1477 u16 brightness_toggle:1;
1478 u16 volume_toggle:1;
1479 u16 mute:1;
1480
1481 u8 brightness_level;
1482 u8 volume_level;
1483};
1484
1485static struct task_struct *tpacpi_hotkey_task;
1486static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1487static int hotkey_poll_freq = 10; /* Hz */
1488static struct mutex hotkey_thread_mutex;
1489static struct mutex hotkey_thread_data_mutex;
1490static unsigned int hotkey_config_change;
1491
1492#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1493
1494#define hotkey_source_mask 0U
1495
1496#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1497
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001498static struct mutex hotkey_mutex;
1499
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001500static enum { /* Reasons for waking up */
1501 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1502 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1503 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1504} hotkey_wakeup_reason;
1505
1506static int hotkey_autosleep_ack;
1507
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001508static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001509static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001510static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001511static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001512
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001513static unsigned int hotkey_report_mode;
1514
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001515static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001516
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001517static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001519#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1520#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001521 do { \
1522 mutex_lock(&hotkey_thread_data_mutex); \
1523 hotkey_config_change++; \
1524 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001525#define HOTKEY_CONFIG_CRITICAL_END \
1526 mutex_unlock(&hotkey_thread_data_mutex);
1527#else
1528#define HOTKEY_CONFIG_CRITICAL_START
1529#define HOTKEY_CONFIG_CRITICAL_END
1530#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1531
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001532/* HKEY.MHKG() return bits */
1533#define TP_HOTKEY_TABLET_MASK (1 << 3)
1534
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001535static int hotkey_get_wlsw(int *status)
1536{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001537#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1538 if (dbg_wlswemul) {
1539 *status = !!tpacpi_wlsw_emulstate;
1540 return 0;
1541 }
1542#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001543 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1544 return -EIO;
1545 return 0;
1546}
1547
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001548static int hotkey_get_tablet_mode(int *status)
1549{
1550 int s;
1551
1552 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1553 return -EIO;
1554
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001555 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1556 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001557}
1558
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001559/*
1560 * Call with hotkey_mutex held
1561 */
1562static int hotkey_mask_get(void)
1563{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001564 u32 m = 0;
1565
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001566 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001567 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001568 return -EIO;
1569 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001570 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001571
1572 return 0;
1573}
1574
1575/*
1576 * Call with hotkey_mutex held
1577 */
1578static int hotkey_mask_set(u32 mask)
1579{
1580 int i;
1581 int rc = 0;
1582
1583 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001584 if (!tp_warned.hotkey_mask_ff &&
1585 (mask == 0xffff || mask == 0xffffff ||
1586 mask == 0xffffffff)) {
1587 tp_warned.hotkey_mask_ff = 1;
1588 printk(TPACPI_NOTICE
1589 "setting the hotkey mask to 0x%08x is likely "
1590 "not the best way to go about it\n", mask);
1591 printk(TPACPI_NOTICE
1592 "please consider using the driver defaults, "
1593 "and refer to up-to-date thinkpad-acpi "
1594 "documentation\n");
1595 }
1596
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001597 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001598 for (i = 0; i < 32; i++) {
1599 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001600 /* enable in firmware mask only keys not in NVRAM
1601 * mode, but enable the key in the cached hotkey_mask
1602 * regardless of mode, or the key will end up
1603 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001604 if (!acpi_evalf(hkey_handle,
1605 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001606 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001607 rc = -EIO;
1608 break;
1609 } else {
1610 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1611 }
1612 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001613 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001614
1615 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001616 if (!hotkey_mask_get() && !rc &&
1617 (hotkey_mask & ~hotkey_source_mask) !=
1618 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001619 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001620 "requested hot key mask 0x%08x, but "
1621 "firmware forced it to 0x%08x\n",
1622 mask, hotkey_mask);
1623 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001624 } else {
1625#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1626 HOTKEY_CONFIG_CRITICAL_START
1627 hotkey_mask = mask & hotkey_source_mask;
1628 HOTKEY_CONFIG_CRITICAL_END
1629 hotkey_mask_get();
1630 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001631 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001632 "requested hot key mask 0x%08x, "
1633 "forced to 0x%08x (NVRAM poll mask is "
1634 "0x%08x): no firmware mask support\n",
1635 mask, hotkey_mask, hotkey_source_mask);
1636 }
1637#else
1638 hotkey_mask_get();
1639 rc = -ENXIO;
1640#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001641 }
1642
1643 return rc;
1644}
1645
1646static int hotkey_status_get(int *status)
1647{
1648 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1649 return -EIO;
1650
1651 return 0;
1652}
1653
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001654static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001655{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001656 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001657 return -EIO;
1658
1659 return 0;
1660}
1661
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001662static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001663{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001664 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001665
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001666 if (tp_features.hotkey_tablet &&
1667 !hotkey_get_tablet_mode(&state)) {
1668 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001669
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001670 input_report_switch(tpacpi_inputdev,
1671 SW_TABLET_MODE, !!state);
1672 input_sync(tpacpi_inputdev);
1673
1674 mutex_unlock(&tpacpi_inputdev_send_mutex);
1675 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001676}
1677
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001678static void tpacpi_input_send_key(unsigned int scancode)
1679{
1680 unsigned int keycode;
1681
1682 keycode = hotkey_keycode_map[scancode];
1683
1684 if (keycode != KEY_RESERVED) {
1685 mutex_lock(&tpacpi_inputdev_send_mutex);
1686
1687 input_report_key(tpacpi_inputdev, keycode, 1);
1688 if (keycode == KEY_UNKNOWN)
1689 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1690 scancode);
1691 input_sync(tpacpi_inputdev);
1692
1693 input_report_key(tpacpi_inputdev, keycode, 0);
1694 if (keycode == KEY_UNKNOWN)
1695 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1696 scancode);
1697 input_sync(tpacpi_inputdev);
1698
1699 mutex_unlock(&tpacpi_inputdev_send_mutex);
1700 }
1701}
1702
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001703#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1704static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1705
1706static void tpacpi_hotkey_send_key(unsigned int scancode)
1707{
1708 tpacpi_input_send_key(scancode);
1709 if (hotkey_report_mode < 2) {
1710 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1711 0x80, 0x1001 + scancode);
1712 }
1713}
1714
1715static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1716{
1717 u8 d;
1718
1719 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1720 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1721 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1722 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1723 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1724 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1725 }
1726 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1727 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1728 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1729 }
1730 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1731 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1732 n->displayexp_toggle =
1733 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1734 }
1735 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1736 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1737 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1738 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1739 n->brightness_toggle =
1740 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1741 }
1742 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1743 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1744 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1745 >> TP_NVRAM_POS_LEVEL_VOLUME;
1746 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1747 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1748 }
1749}
1750
1751#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001752 do { \
1753 if ((mask & (1 << __scancode)) && \
1754 oldn->__member != newn->__member) \
1755 tpacpi_hotkey_send_key(__scancode); \
1756 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001757
1758#define TPACPI_MAY_SEND_KEY(__scancode) \
1759 do { if (mask & (1 << __scancode)) \
1760 tpacpi_hotkey_send_key(__scancode); } while (0)
1761
1762static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001763 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001764 u32 mask)
1765{
1766 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1767 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1768 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1769 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1770
1771 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1772
1773 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1774
1775 /* handle volume */
1776 if (oldn->volume_toggle != newn->volume_toggle) {
1777 if (oldn->mute != newn->mute) {
1778 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1779 }
1780 if (oldn->volume_level > newn->volume_level) {
1781 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1782 } else if (oldn->volume_level < newn->volume_level) {
1783 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1784 } else if (oldn->mute == newn->mute) {
1785 /* repeated key presses that didn't change state */
1786 if (newn->mute) {
1787 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1788 } else if (newn->volume_level != 0) {
1789 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1790 } else {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1792 }
1793 }
1794 }
1795
1796 /* handle brightness */
1797 if (oldn->brightness_toggle != newn->brightness_toggle) {
1798 if (oldn->brightness_level < newn->brightness_level) {
1799 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1800 } else if (oldn->brightness_level > newn->brightness_level) {
1801 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1802 } else {
1803 /* repeated key presses that didn't change state */
1804 if (newn->brightness_level != 0) {
1805 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1806 } else {
1807 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1808 }
1809 }
1810 }
1811}
1812
1813#undef TPACPI_COMPARE_KEY
1814#undef TPACPI_MAY_SEND_KEY
1815
1816static int hotkey_kthread(void *data)
1817{
1818 struct tp_nvram_state s[2];
1819 u32 mask;
1820 unsigned int si, so;
1821 unsigned long t;
1822 unsigned int change_detector, must_reset;
1823
1824 mutex_lock(&hotkey_thread_mutex);
1825
1826 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1827 goto exit;
1828
1829 set_freezable();
1830
1831 so = 0;
1832 si = 1;
1833 t = 0;
1834
1835 /* Initial state for compares */
1836 mutex_lock(&hotkey_thread_data_mutex);
1837 change_detector = hotkey_config_change;
1838 mask = hotkey_source_mask & hotkey_mask;
1839 mutex_unlock(&hotkey_thread_data_mutex);
1840 hotkey_read_nvram(&s[so], mask);
1841
1842 while (!kthread_should_stop() && hotkey_poll_freq) {
1843 if (t == 0)
1844 t = 1000/hotkey_poll_freq;
1845 t = msleep_interruptible(t);
1846 if (unlikely(kthread_should_stop()))
1847 break;
1848 must_reset = try_to_freeze();
1849 if (t > 0 && !must_reset)
1850 continue;
1851
1852 mutex_lock(&hotkey_thread_data_mutex);
1853 if (must_reset || hotkey_config_change != change_detector) {
1854 /* forget old state on thaw or config change */
1855 si = so;
1856 t = 0;
1857 change_detector = hotkey_config_change;
1858 }
1859 mask = hotkey_source_mask & hotkey_mask;
1860 mutex_unlock(&hotkey_thread_data_mutex);
1861
1862 if (likely(mask)) {
1863 hotkey_read_nvram(&s[si], mask);
1864 if (likely(si != so)) {
1865 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001866 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001867 }
1868 }
1869
1870 so = si;
1871 si ^= 1;
1872 }
1873
1874exit:
1875 mutex_unlock(&hotkey_thread_mutex);
1876 return 0;
1877}
1878
1879static void hotkey_poll_stop_sync(void)
1880{
1881 if (tpacpi_hotkey_task) {
1882 if (frozen(tpacpi_hotkey_task) ||
1883 freezing(tpacpi_hotkey_task))
1884 thaw_process(tpacpi_hotkey_task);
1885
1886 kthread_stop(tpacpi_hotkey_task);
1887 tpacpi_hotkey_task = NULL;
1888 mutex_lock(&hotkey_thread_mutex);
1889 /* at this point, the thread did exit */
1890 mutex_unlock(&hotkey_thread_mutex);
1891 }
1892}
1893
1894/* call with hotkey_mutex held */
1895static void hotkey_poll_setup(int may_warn)
1896{
1897 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1898 hotkey_poll_freq > 0 &&
1899 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1900 if (!tpacpi_hotkey_task) {
1901 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001902 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001903 if (IS_ERR(tpacpi_hotkey_task)) {
1904 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001905 printk(TPACPI_ERR
1906 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001907 "for hotkey polling\n");
1908 }
1909 }
1910 } else {
1911 hotkey_poll_stop_sync();
1912 if (may_warn &&
1913 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001914 printk(TPACPI_NOTICE
1915 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001916 "which is currently disabled\n",
1917 hotkey_source_mask);
1918 }
1919 }
1920}
1921
1922static void hotkey_poll_setup_safe(int may_warn)
1923{
1924 mutex_lock(&hotkey_mutex);
1925 hotkey_poll_setup(may_warn);
1926 mutex_unlock(&hotkey_mutex);
1927}
1928
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001929#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1930
1931static void hotkey_poll_setup_safe(int __unused)
1932{
1933}
1934
1935#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1936
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937static int hotkey_inputdev_open(struct input_dev *dev)
1938{
1939 switch (tpacpi_lifecycle) {
1940 case TPACPI_LIFE_INIT:
1941 /*
1942 * hotkey_init will call hotkey_poll_setup_safe
1943 * at the appropriate moment
1944 */
1945 return 0;
1946 case TPACPI_LIFE_EXITING:
1947 return -EBUSY;
1948 case TPACPI_LIFE_RUNNING:
1949 hotkey_poll_setup_safe(0);
1950 return 0;
1951 }
1952
1953 /* Should only happen if tpacpi_lifecycle is corrupt */
1954 BUG();
1955 return -EBUSY;
1956}
1957
1958static void hotkey_inputdev_close(struct input_dev *dev)
1959{
1960 /* disable hotkey polling when possible */
1961 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1962 hotkey_poll_setup_safe(0);
1963}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001964
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001965/* sysfs hotkey enable ------------------------------------------------- */
1966static ssize_t hotkey_enable_show(struct device *dev,
1967 struct device_attribute *attr,
1968 char *buf)
1969{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001970 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001971
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001972 printk_deprecated_attribute("hotkey_enable",
1973 "Hotkey reporting is always enabled");
1974
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001975 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001976 if (res)
1977 return res;
1978
1979 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1980}
1981
1982static ssize_t hotkey_enable_store(struct device *dev,
1983 struct device_attribute *attr,
1984 const char *buf, size_t count)
1985{
1986 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001987
1988 printk_deprecated_attribute("hotkey_enable",
1989 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001990
1991 if (parse_strtoul(buf, 1, &t))
1992 return -EINVAL;
1993
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001994 if (t == 0)
1995 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001996
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001997 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001998}
1999
2000static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002001 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002002 hotkey_enable_show, hotkey_enable_store);
2003
2004/* sysfs hotkey mask --------------------------------------------------- */
2005static ssize_t hotkey_mask_show(struct device *dev,
2006 struct device_attribute *attr,
2007 char *buf)
2008{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002009 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002010
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002011 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002012 return -ERESTARTSYS;
2013 res = hotkey_mask_get();
2014 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002015
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002016 return (res)?
2017 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002018}
2019
2020static ssize_t hotkey_mask_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
2023{
2024 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002025 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002026
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002027 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002028 return -EINVAL;
2029
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002030 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002031 return -ERESTARTSYS;
2032
2033 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002034
2035#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2036 hotkey_poll_setup(1);
2037#endif
2038
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002039 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002040
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002041 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2042
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002043 return (res) ? res : count;
2044}
2045
2046static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002047 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002048 hotkey_mask_show, hotkey_mask_store);
2049
2050/* sysfs hotkey bios_enabled ------------------------------------------- */
2051static ssize_t hotkey_bios_enabled_show(struct device *dev,
2052 struct device_attribute *attr,
2053 char *buf)
2054{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002055 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002056}
2057
2058static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002059 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002060
2061/* sysfs hotkey bios_mask ---------------------------------------------- */
2062static ssize_t hotkey_bios_mask_show(struct device *dev,
2063 struct device_attribute *attr,
2064 char *buf)
2065{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002066 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002067}
2068
2069static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002070 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002071
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002072/* sysfs hotkey all_mask ----------------------------------------------- */
2073static ssize_t hotkey_all_mask_show(struct device *dev,
2074 struct device_attribute *attr,
2075 char *buf)
2076{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002077 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2078 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002079}
2080
2081static struct device_attribute dev_attr_hotkey_all_mask =
2082 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2083
2084/* sysfs hotkey recommended_mask --------------------------------------- */
2085static ssize_t hotkey_recommended_mask_show(struct device *dev,
2086 struct device_attribute *attr,
2087 char *buf)
2088{
2089 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002090 (hotkey_all_mask | hotkey_source_mask)
2091 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002092}
2093
2094static struct device_attribute dev_attr_hotkey_recommended_mask =
2095 __ATTR(hotkey_recommended_mask, S_IRUGO,
2096 hotkey_recommended_mask_show, NULL);
2097
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002098#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2099
2100/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2101static ssize_t hotkey_source_mask_show(struct device *dev,
2102 struct device_attribute *attr,
2103 char *buf)
2104{
2105 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2106}
2107
2108static ssize_t hotkey_source_mask_store(struct device *dev,
2109 struct device_attribute *attr,
2110 const char *buf, size_t count)
2111{
2112 unsigned long t;
2113
2114 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2115 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2116 return -EINVAL;
2117
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002118 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002119 return -ERESTARTSYS;
2120
2121 HOTKEY_CONFIG_CRITICAL_START
2122 hotkey_source_mask = t;
2123 HOTKEY_CONFIG_CRITICAL_END
2124
2125 hotkey_poll_setup(1);
2126
2127 mutex_unlock(&hotkey_mutex);
2128
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002129 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2130
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002131 return count;
2132}
2133
2134static struct device_attribute dev_attr_hotkey_source_mask =
2135 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2136 hotkey_source_mask_show, hotkey_source_mask_store);
2137
2138/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2139static ssize_t hotkey_poll_freq_show(struct device *dev,
2140 struct device_attribute *attr,
2141 char *buf)
2142{
2143 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2144}
2145
2146static ssize_t hotkey_poll_freq_store(struct device *dev,
2147 struct device_attribute *attr,
2148 const char *buf, size_t count)
2149{
2150 unsigned long t;
2151
2152 if (parse_strtoul(buf, 25, &t))
2153 return -EINVAL;
2154
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002155 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002156 return -ERESTARTSYS;
2157
2158 hotkey_poll_freq = t;
2159
2160 hotkey_poll_setup(1);
2161 mutex_unlock(&hotkey_mutex);
2162
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002163 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2164
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002165 return count;
2166}
2167
2168static struct device_attribute dev_attr_hotkey_poll_freq =
2169 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2170 hotkey_poll_freq_show, hotkey_poll_freq_store);
2171
2172#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2173
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002174/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002175static ssize_t hotkey_radio_sw_show(struct device *dev,
2176 struct device_attribute *attr,
2177 char *buf)
2178{
2179 int res, s;
2180 res = hotkey_get_wlsw(&s);
2181 if (res < 0)
2182 return res;
2183
2184 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2185}
2186
2187static struct device_attribute dev_attr_hotkey_radio_sw =
2188 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2189
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002190static void hotkey_radio_sw_notify_change(void)
2191{
2192 if (tp_features.hotkey_wlsw)
2193 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2194 "hotkey_radio_sw");
2195}
2196
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002197/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2198static ssize_t hotkey_tablet_mode_show(struct device *dev,
2199 struct device_attribute *attr,
2200 char *buf)
2201{
2202 int res, s;
2203 res = hotkey_get_tablet_mode(&s);
2204 if (res < 0)
2205 return res;
2206
2207 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2208}
2209
2210static struct device_attribute dev_attr_hotkey_tablet_mode =
2211 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2212
2213static void hotkey_tablet_mode_notify_change(void)
2214{
2215 if (tp_features.hotkey_tablet)
2216 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2217 "hotkey_tablet_mode");
2218}
2219
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002220/* sysfs hotkey report_mode -------------------------------------------- */
2221static ssize_t hotkey_report_mode_show(struct device *dev,
2222 struct device_attribute *attr,
2223 char *buf)
2224{
2225 return snprintf(buf, PAGE_SIZE, "%d\n",
2226 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2227}
2228
2229static struct device_attribute dev_attr_hotkey_report_mode =
2230 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2231
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002232/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002233static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2234 struct device_attribute *attr,
2235 char *buf)
2236{
2237 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2238}
2239
2240static struct device_attribute dev_attr_hotkey_wakeup_reason =
2241 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2242
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002243static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002244{
2245 if (tp_features.hotkey_mask)
2246 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2247 "wakeup_reason");
2248}
2249
2250/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002251static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2252 struct device_attribute *attr,
2253 char *buf)
2254{
2255 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2256}
2257
2258static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2259 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2260 hotkey_wakeup_hotunplug_complete_show, NULL);
2261
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002262static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002263{
2264 if (tp_features.hotkey_mask)
2265 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2266 "wakeup_hotunplug_complete");
2267}
2268
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002269/* --------------------------------------------------------------------- */
2270
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002271static struct attribute *hotkey_attributes[] __initdata = {
2272 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002273 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002274 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002275#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276 &dev_attr_hotkey_mask.attr,
2277 &dev_attr_hotkey_all_mask.attr,
2278 &dev_attr_hotkey_recommended_mask.attr,
2279 &dev_attr_hotkey_source_mask.attr,
2280 &dev_attr_hotkey_poll_freq.attr,
2281#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002282};
2283
2284static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002285 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002286#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2287 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002288 &dev_attr_hotkey_all_mask.attr,
2289 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002290#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002291 &dev_attr_hotkey_wakeup_reason.attr,
2292 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002293};
2294
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002295static void bluetooth_update_rfk(void);
2296static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002297static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002298static void tpacpi_send_radiosw_update(void)
2299{
2300 int wlsw;
2301
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002302 /* Sync these BEFORE sending any rfkill events */
2303 if (tp_features.bluetooth)
2304 bluetooth_update_rfk();
2305 if (tp_features.wan)
2306 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002307 if (tp_features.uwb)
2308 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002309
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002310 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2311 mutex_lock(&tpacpi_inputdev_send_mutex);
2312
2313 input_report_switch(tpacpi_inputdev,
2314 SW_RFKILL_ALL, !!wlsw);
2315 input_sync(tpacpi_inputdev);
2316
2317 mutex_unlock(&tpacpi_inputdev_send_mutex);
2318 }
2319 hotkey_radio_sw_notify_change();
2320}
2321
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002322static void hotkey_exit(void)
2323{
2324#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2325 hotkey_poll_stop_sync();
2326#endif
2327
2328 if (hotkey_dev_attributes)
2329 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2330
2331 kfree(hotkey_keycode_map);
2332
2333 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002334 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002335 "restoring original hot key mask\n");
2336 /* no short-circuit boolean operator below! */
2337 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002338 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002339 printk(TPACPI_ERR
2340 "failed to restore hot key mask "
2341 "to BIOS defaults\n");
2342 }
2343}
2344
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002345static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002346{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002347 /* Requirements for changing the default keymaps:
2348 *
2349 * 1. Many of the keys are mapped to KEY_RESERVED for very
2350 * good reasons. Do not change them unless you have deep
2351 * knowledge on the IBM and Lenovo ThinkPad firmware for
2352 * the various ThinkPad models. The driver behaves
2353 * differently for KEY_RESERVED: such keys have their
2354 * hot key mask *unset* in mask_recommended, and also
2355 * in the initial hot key mask programmed into the
2356 * firmware at driver load time, which means the firm-
2357 * ware may react very differently if you change them to
2358 * something else;
2359 *
2360 * 2. You must be subscribed to the linux-thinkpad and
2361 * ibm-acpi-devel mailing lists, and you should read the
2362 * list archives since 2007 if you want to change the
2363 * keymaps. This requirement exists so that you will
2364 * know the past history of problems with the thinkpad-
2365 * acpi driver keymaps, and also that you will be
2366 * listening to any bug reports;
2367 *
2368 * 3. Do not send thinkpad-acpi specific patches directly to
2369 * for merging, *ever*. Send them to the linux-acpi
2370 * mailinglist for comments. Merging is to be done only
2371 * through acpi-test and the ACPI maintainer.
2372 *
2373 * If the above is too much to ask, don't change the keymap.
2374 * Ask the thinkpad-acpi maintainer to do it, instead.
2375 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002376 static u16 ibm_keycode_map[] __initdata = {
2377 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2378 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2379 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2380 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002381
2382 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002383 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2384 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2385 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002386
2387 /* brightness: firmware always reacts to them, unless
2388 * X.org did some tricks in the radeon BIOS scratch
2389 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002390 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002391 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002392
2393 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002394 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002395
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002396 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2397 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002398
2399 /* Volume: firmware always react to it and reprograms
2400 * the built-in *extra* mixer. Never map it to control
2401 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002402 KEY_RESERVED, /* 0x14: VOLUME UP */
2403 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2404 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002405
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002406 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002407
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002408 /* (assignments unknown, please report if found) */
2409 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2410 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2411 };
2412 static u16 lenovo_keycode_map[] __initdata = {
2413 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2414 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2415 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2416 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002417
2418 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002419 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2420 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2421 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002422
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002423 /* These either have to go through ACPI video, or
2424 * act like in the IBM ThinkPads, so don't ever
2425 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002426 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002427 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002428
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002429 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002430
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002431 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2432 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002433
2434 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2435 * react to it and reprograms the built-in *extra* mixer.
2436 * Never map it to control another mixer by default.
2437 *
2438 * T60?, T61, R60?, R61: firmware and EC tries to send
2439 * these over the regular keyboard, so these are no-ops,
2440 * but there are still weird bugs re. MUTE, so do not
2441 * change unless you get test reports from all Lenovo
2442 * models. May cause the BIOS to interfere with the
2443 * HDA mixer.
2444 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002445 KEY_RESERVED, /* 0x14: VOLUME UP */
2446 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2447 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002448
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002449 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002450
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002451 /* (assignments unknown, please report if found) */
2452 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2453 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2454 };
2455
2456#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2457#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2458#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2459
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002460 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002461 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002462 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002463
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002464 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2465 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002466
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002467 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002468 BUG_ON(tpacpi_inputdev->open != NULL ||
2469 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002470
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002471 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002472 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002473
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2475 mutex_init(&hotkey_thread_mutex);
2476 mutex_init(&hotkey_thread_data_mutex);
2477#endif
2478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002479 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002480 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002481
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002482 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2483 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002484 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002485
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002486 if (!tp_features.hotkey)
2487 return 1;
2488
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002489 tpacpi_disable_brightness_delay();
2490
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002491 hotkey_dev_attributes = create_attr_set(13, NULL);
2492 if (!hotkey_dev_attributes)
2493 return -ENOMEM;
2494 res = add_many_to_attr_set(hotkey_dev_attributes,
2495 hotkey_attributes,
2496 ARRAY_SIZE(hotkey_attributes));
2497 if (res)
2498 goto err_exit;
2499
2500 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2501 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2502 for HKEY interface version 0x100 */
2503 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2504 if ((hkeyv >> 8) != 1) {
2505 printk(TPACPI_ERR "unknown version of the "
2506 "HKEY interface: 0x%x\n", hkeyv);
2507 printk(TPACPI_ERR "please report this to %s\n",
2508 TPACPI_MAIL);
2509 } else {
2510 /*
2511 * MHKV 0x100 in A31, R40, R40e,
2512 * T4x, X31, and later
2513 */
2514 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002515 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2516 "firmware HKEY interface version: 0x%x\n",
2517 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002518 }
2519 }
2520
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002521 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2522 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002523 str_supported(tp_features.hotkey_mask));
2524
2525 if (tp_features.hotkey_mask) {
2526 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2527 "MHKA", "qd")) {
2528 printk(TPACPI_ERR
2529 "missing MHKA handler, "
2530 "please report this to %s\n",
2531 TPACPI_MAIL);
2532 /* FN+F12, FN+F4, FN+F3 */
2533 hotkey_all_mask = 0x080cU;
2534 }
2535 }
2536
2537 /* hotkey_source_mask *must* be zero for
2538 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002539 if (tp_features.hotkey_mask) {
2540 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002541 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002542 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002543
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002544 hotkey_orig_mask = hotkey_mask;
2545 res = add_many_to_attr_set(
2546 hotkey_dev_attributes,
2547 hotkey_mask_attributes,
2548 ARRAY_SIZE(hotkey_mask_attributes));
2549 if (res)
2550 goto err_exit;
2551 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002552
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002554 if (tp_features.hotkey_mask) {
2555 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2556 & ~hotkey_all_mask;
2557 } else {
2558 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2559 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002560
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002561 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002562 "hotkey source mask 0x%08x, polling freq %d\n",
2563 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002564#endif
2565
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002566#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2567 if (dbg_wlswemul) {
2568 tp_features.hotkey_wlsw = 1;
2569 printk(TPACPI_INFO
2570 "radio switch emulation enabled\n");
2571 } else
2572#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002573 /* Not all thinkpads have a hardware radio switch */
2574 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2575 tp_features.hotkey_wlsw = 1;
2576 printk(TPACPI_INFO
2577 "radio switch found; radios are %s\n",
2578 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002579 }
2580 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002581 res = add_to_attr_set(hotkey_dev_attributes,
2582 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002583
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002584 /* For X41t, X60t, X61t Tablets... */
2585 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2586 tp_features.hotkey_tablet = 1;
2587 printk(TPACPI_INFO
2588 "possible tablet mode switch found; "
2589 "ThinkPad in %s mode\n",
2590 (status & TP_HOTKEY_TABLET_MASK)?
2591 "tablet" : "laptop");
2592 res = add_to_attr_set(hotkey_dev_attributes,
2593 &dev_attr_hotkey_tablet_mode.attr);
2594 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002595
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002596 if (!res)
2597 res = register_attr_set_with_sysfs(
2598 hotkey_dev_attributes,
2599 &tpacpi_pdev->dev.kobj);
2600 if (res)
2601 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002602
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002603 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002604
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002605 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2606 GFP_KERNEL);
2607 if (!hotkey_keycode_map) {
2608 printk(TPACPI_ERR
2609 "failed to allocate memory for key map\n");
2610 res = -ENOMEM;
2611 goto err_exit;
2612 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002613
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002614 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002615 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002616 "using Lenovo default hot key map\n");
2617 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2618 TPACPI_HOTKEY_MAP_SIZE);
2619 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002620 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002621 "using IBM default hot key map\n");
2622 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2623 TPACPI_HOTKEY_MAP_SIZE);
2624 }
2625
2626 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2627 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2628 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2629 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2630 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2631 tpacpi_inputdev->keycode = hotkey_keycode_map;
2632 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2633 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2634 set_bit(hotkey_keycode_map[i],
2635 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002636 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002637 if (i < sizeof(hotkey_reserved_mask)*8)
2638 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002639 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002640 }
2641
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002642 if (tp_features.hotkey_wlsw) {
2643 set_bit(EV_SW, tpacpi_inputdev->evbit);
2644 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2645 }
2646 if (tp_features.hotkey_tablet) {
2647 set_bit(EV_SW, tpacpi_inputdev->evbit);
2648 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002649 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002650
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002651 /* Do not issue duplicate brightness change events to
2652 * userspace */
2653 if (!tp_features.bright_acpimode)
2654 /* update bright_acpimode... */
2655 tpacpi_check_std_acpi_brightness_support();
2656
2657 if (tp_features.bright_acpimode) {
2658 printk(TPACPI_INFO
2659 "This ThinkPad has standard ACPI backlight "
2660 "brightness control, supported by the ACPI "
2661 "video driver\n");
2662 printk(TPACPI_NOTICE
2663 "Disabling thinkpad-acpi brightness events "
2664 "by default...\n");
2665
2666 /* The hotkey_reserved_mask change below is not
2667 * necessary while the keys are at KEY_RESERVED in the
2668 * default map, but better safe than sorry, leave it
2669 * here as a marker of what we have to do, especially
2670 * when we finally become able to set this at runtime
2671 * on response to X.org requests */
2672 hotkey_reserved_mask |=
2673 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2674 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002675 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002676
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002677 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2678 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002679 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002680 if (res) {
2681 hotkey_exit();
2682 return res;
2683 }
2684 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2685 & ~hotkey_reserved_mask)
2686 | hotkey_orig_mask);
2687 if (res < 0 && res != -ENXIO) {
2688 hotkey_exit();
2689 return res;
2690 }
2691
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002692 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2693 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002694 (hotkey_report_mode < 2) ?
2695 "enabled" : "disabled");
2696
2697 tpacpi_inputdev->open = &hotkey_inputdev_open;
2698 tpacpi_inputdev->close = &hotkey_inputdev_close;
2699
2700 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002701 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002702 tpacpi_input_send_tabletsw();
2703
2704 return 0;
2705
2706err_exit:
2707 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2708 hotkey_dev_attributes = NULL;
2709
2710 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002711}
2712
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002713static bool hotkey_notify_hotkey(const u32 hkey,
2714 bool *send_acpi_ev,
2715 bool *ignore_acpi_ev)
2716{
2717 /* 0x1000-0x1FFF: key presses */
2718 unsigned int scancode = hkey & 0xfff;
2719 *send_acpi_ev = true;
2720 *ignore_acpi_ev = false;
2721
2722 if (scancode > 0 && scancode < 0x21) {
2723 scancode--;
2724 if (!(hotkey_source_mask & (1 << scancode))) {
2725 tpacpi_input_send_key(scancode);
2726 *send_acpi_ev = false;
2727 } else {
2728 *ignore_acpi_ev = true;
2729 }
2730 return true;
2731 }
2732 return false;
2733}
2734
2735static bool hotkey_notify_wakeup(const u32 hkey,
2736 bool *send_acpi_ev,
2737 bool *ignore_acpi_ev)
2738{
2739 /* 0x2000-0x2FFF: Wakeup reason */
2740 *send_acpi_ev = true;
2741 *ignore_acpi_ev = false;
2742
2743 switch (hkey) {
2744 case 0x2304: /* suspend, undock */
2745 case 0x2404: /* hibernation, undock */
2746 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2747 *ignore_acpi_ev = true;
2748 break;
2749
2750 case 0x2305: /* suspend, bay eject */
2751 case 0x2405: /* hibernation, bay eject */
2752 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2753 *ignore_acpi_ev = true;
2754 break;
2755
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002756 case 0x2313: /* Battery on critical low level (S3) */
2757 case 0x2413: /* Battery on critical low level (S4) */
2758 printk(TPACPI_ALERT
2759 "EMERGENCY WAKEUP: battery almost empty\n");
2760 /* how to auto-heal: */
2761 /* 2313: woke up from S3, go to S4/S5 */
2762 /* 2413: woke up from S4, go to S5 */
2763 break;
2764
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002765 default:
2766 return false;
2767 }
2768
2769 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2770 printk(TPACPI_INFO
2771 "woke up due to a hot-unplug "
2772 "request...\n");
2773 hotkey_wakeup_reason_notify_change();
2774 }
2775 return true;
2776}
2777
2778static bool hotkey_notify_usrevent(const u32 hkey,
2779 bool *send_acpi_ev,
2780 bool *ignore_acpi_ev)
2781{
2782 /* 0x5000-0x5FFF: human interface helpers */
2783 *send_acpi_ev = true;
2784 *ignore_acpi_ev = false;
2785
2786 switch (hkey) {
2787 case 0x5010: /* Lenovo new BIOS: brightness changed */
2788 case 0x500b: /* X61t: tablet pen inserted into bay */
2789 case 0x500c: /* X61t: tablet pen removed from bay */
2790 return true;
2791
2792 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2793 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2794 tpacpi_input_send_tabletsw();
2795 hotkey_tablet_mode_notify_change();
2796 *send_acpi_ev = false;
2797 return true;
2798
2799 case 0x5001:
2800 case 0x5002:
2801 /* LID switch events. Do not propagate */
2802 *ignore_acpi_ev = true;
2803 return true;
2804
2805 default:
2806 return false;
2807 }
2808}
2809
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002810static bool hotkey_notify_thermal(const u32 hkey,
2811 bool *send_acpi_ev,
2812 bool *ignore_acpi_ev)
2813{
2814 /* 0x6000-0x6FFF: thermal alarms */
2815 *send_acpi_ev = true;
2816 *ignore_acpi_ev = false;
2817
2818 switch (hkey) {
2819 case 0x6011:
2820 printk(TPACPI_CRIT
2821 "THERMAL ALARM: battery is too hot!\n");
2822 /* recommended action: warn user through gui */
2823 return true;
2824 case 0x6012:
2825 printk(TPACPI_ALERT
2826 "THERMAL EMERGENCY: battery is extremely hot!\n");
2827 /* recommended action: immediate sleep/hibernate */
2828 return true;
2829 case 0x6021:
2830 printk(TPACPI_CRIT
2831 "THERMAL ALARM: "
2832 "a sensor reports something is too hot!\n");
2833 /* recommended action: warn user through gui, that */
2834 /* some internal component is too hot */
2835 return true;
2836 case 0x6022:
2837 printk(TPACPI_ALERT
2838 "THERMAL EMERGENCY: "
2839 "a sensor reports something is extremely hot!\n");
2840 /* recommended action: immediate sleep/hibernate */
2841 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002842 case 0x6030:
2843 printk(TPACPI_INFO
2844 "EC reports that Thermal Table has changed\n");
2845 /* recommended action: do nothing, we don't have
2846 * Lenovo ATM information */
2847 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002848 default:
2849 printk(TPACPI_ALERT
2850 "THERMAL ALERT: unknown thermal alarm received\n");
2851 return false;
2852 }
2853}
2854
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002855static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2856{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002857 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002858 bool send_acpi_ev;
2859 bool ignore_acpi_ev;
2860 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002861
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002862 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002863 printk(TPACPI_ERR
2864 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002865 /* forward it to userspace, maybe it knows how to handle it */
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, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002870 return;
2871 }
2872
2873 while (1) {
2874 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002875 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002876 return;
2877 }
2878
2879 if (hkey == 0) {
2880 /* queue empty */
2881 return;
2882 }
2883
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002884 send_acpi_ev = true;
2885 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002886
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002887 switch (hkey >> 12) {
2888 case 1:
2889 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002890 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2891 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002892 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002893 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002894 /* 0x2000-0x2FFF: Wakeup reason */
2895 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2896 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002897 break;
2898 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002899 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002900 if (hkey == 0x3003) {
2901 hotkey_autosleep_ack = 1;
2902 printk(TPACPI_INFO
2903 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002904 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002905 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002906 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002907 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002908 }
2909 break;
2910 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002911 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002912 if (hkey == 0x4003) {
2913 hotkey_autosleep_ack = 1;
2914 printk(TPACPI_INFO
2915 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002916 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002917 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002918 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002919 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002920 }
2921 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002922 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002923 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002924 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2925 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002926 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002927 case 6:
2928 /* 0x6000-0x6FFF: thermal alarms */
2929 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2930 &ignore_acpi_ev);
2931 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002932 case 7:
2933 /* 0x7000-0x7FFF: misc */
2934 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002935 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002936 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002937 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002938 break;
2939 }
2940 /* fallthrough to default */
2941 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002942 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002943 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002944 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002945 printk(TPACPI_NOTICE
2946 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002947 printk(TPACPI_NOTICE
2948 "please report the conditions when this "
2949 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002950 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002951
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002952 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002953 if (!ignore_acpi_ev &&
2954 (send_acpi_ev || hotkey_report_mode < 2)) {
2955 acpi_bus_generate_proc_event(ibm->acpi->device,
2956 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002957 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002958
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002959 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002960 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002961 acpi_bus_generate_netlink_event(
2962 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002963 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002964 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002965 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002966 }
2967}
2968
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002969static void hotkey_suspend(pm_message_t state)
2970{
2971 /* Do these on suspend, we get the events on early resume! */
2972 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2973 hotkey_autosleep_ack = 0;
2974}
2975
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002976static void hotkey_resume(void)
2977{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002978 tpacpi_disable_brightness_delay();
2979
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002980 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002981 printk(TPACPI_ERR
2982 "error while trying to read hot key mask "
2983 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002984 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002985 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002986 hotkey_wakeup_reason_notify_change();
2987 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002988 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002989}
2990
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002991/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002992static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002994 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 int len = 0;
2996
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002997 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002998 len += sprintf(p + len, "status:\t\tnot supported\n");
2999 return len;
3000 }
3001
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003002 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003003 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003004 res = hotkey_status_get(&status);
3005 if (!res)
3006 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003007 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003008 if (res)
3009 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003012 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003013 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 len += sprintf(p + len,
3015 "commands:\tenable, disable, reset, <mask>\n");
3016 } else {
3017 len += sprintf(p + len, "mask:\t\tnot supported\n");
3018 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3019 }
3020
3021 return len;
3022}
3023
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003024static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003025{
3026 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003027 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3028 TPACPI_WARN
3029 "hotkey enable/disable functionality has been "
3030 "removed from the driver. Hotkeys are always "
3031 "enabled\n"))
3032 printk(TPACPI_ERR
3033 "Please remove the hotkey=enable module "
3034 "parameter, it is deprecated. Hotkeys are always "
3035 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003036}
3037
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003038static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003040 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003041 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003044 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return -ENODEV;
3046
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003047 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003048 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003049
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003050 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003051
3052 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 while ((cmd = next_cmd(&buf))) {
3054 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003055 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003057 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003058 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003060 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3062 /* mask set */
3063 } else if (sscanf(cmd, "%x", &mask) == 1) {
3064 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003065 } else {
3066 res = -EINVAL;
3067 goto errexit;
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003070
3071 if (!res)
3072 tpacpi_disclose_usertask("procfs hotkey",
3073 "set mask to 0x%08x\n", mask);
3074
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003075 if (!res && mask != hotkey_mask)
3076 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003078errexit:
3079 mutex_unlock(&hotkey_mutex);
3080 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003083static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003084 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003085 {"", 0},
3086};
3087
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003088static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003089 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003090 .notify = hotkey_notify,
3091 .handle = &hkey_handle,
3092 .type = ACPI_DEVICE_NOTIFY,
3093};
3094
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003095static struct ibm_struct hotkey_driver_data = {
3096 .name = "hotkey",
3097 .read = hotkey_read,
3098 .write = hotkey_write,
3099 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003100 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003101 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003102 .acpi = &ibm_hotkey_acpidriver,
3103};
3104
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003105/*************************************************************************
3106 * Bluetooth subdriver
3107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003109enum {
3110 /* ACPI GBDC/SBDC bits */
3111 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3112 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003113 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3114 off / last state */
3115};
3116
3117enum {
3118 /* ACPI \BLTH commands */
3119 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3120 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3121 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3122 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3123 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003124};
3125
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003126#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3127
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003128static struct rfkill *tpacpi_bluetooth_rfkill;
3129
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003130static void bluetooth_suspend(pm_message_t state)
3131{
3132 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003133 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3134 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3135 vdbg_printk(TPACPI_DBG_RFKILL,
3136 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003137}
3138
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003139static int bluetooth_get_radiosw(void)
3140{
3141 int status;
3142
3143 if (!tp_features.bluetooth)
3144 return -ENODEV;
3145
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003146 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3147 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003148 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003149
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003150#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3151 if (dbg_bluetoothemul)
3152 return (tpacpi_bluetooth_emulstate) ?
3153 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3154#endif
3155
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003156 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3157 return -EIO;
3158
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003159 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3160 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003161}
3162
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003163static void bluetooth_update_rfk(void)
3164{
3165 int status;
3166
3167 if (!tpacpi_bluetooth_rfkill)
3168 return;
3169
3170 status = bluetooth_get_radiosw();
3171 if (status < 0)
3172 return;
3173 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003174
3175 vdbg_printk(TPACPI_DBG_RFKILL,
3176 "forced rfkill state to %d\n",
3177 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003178}
3179
3180static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003181{
3182 int status;
3183
3184 if (!tp_features.bluetooth)
3185 return -ENODEV;
3186
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003187 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3188 * reason to risk weird behaviour. */
3189 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3190 && radio_on)
3191 return -EPERM;
3192
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003193 vdbg_printk(TPACPI_DBG_RFKILL,
3194 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3195
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003196#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3197 if (dbg_bluetoothemul) {
3198 tpacpi_bluetooth_emulstate = !!radio_on;
3199 if (update_rfk)
3200 bluetooth_update_rfk();
3201 return 0;
3202 }
3203#endif
3204
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003205 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003206 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003207 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003208 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003209 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003210 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3211 return -EIO;
3212
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003213 if (update_rfk)
3214 bluetooth_update_rfk();
3215
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003216 return 0;
3217}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003218
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003219/* sysfs bluetooth enable ---------------------------------------------- */
3220static ssize_t bluetooth_enable_show(struct device *dev,
3221 struct device_attribute *attr,
3222 char *buf)
3223{
3224 int status;
3225
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003226 printk_deprecated_rfkill_attribute("bluetooth_enable");
3227
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003228 status = bluetooth_get_radiosw();
3229 if (status < 0)
3230 return status;
3231
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003232 return snprintf(buf, PAGE_SIZE, "%d\n",
3233 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003234}
3235
3236static ssize_t bluetooth_enable_store(struct device *dev,
3237 struct device_attribute *attr,
3238 const char *buf, size_t count)
3239{
3240 unsigned long t;
3241 int res;
3242
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003243 printk_deprecated_rfkill_attribute("bluetooth_enable");
3244
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003245 if (parse_strtoul(buf, 1, &t))
3246 return -EINVAL;
3247
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003248 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3249
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003250 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003251
3252 return (res) ? res : count;
3253}
3254
3255static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003256 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003257 bluetooth_enable_show, bluetooth_enable_store);
3258
3259/* --------------------------------------------------------------------- */
3260
3261static struct attribute *bluetooth_attributes[] = {
3262 &dev_attr_bluetooth_enable.attr,
3263 NULL
3264};
3265
3266static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003267 .attrs = bluetooth_attributes,
3268};
3269
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003270static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3271{
3272 int bts = bluetooth_get_radiosw();
3273
3274 if (bts < 0)
3275 return bts;
3276
3277 *state = bts;
3278 return 0;
3279}
3280
3281static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3282{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003283 dbg_printk(TPACPI_DBG_RFKILL,
3284 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003285 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3286}
3287
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003288static void bluetooth_shutdown(void)
3289{
3290 /* Order firmware to save current state to NVRAM */
3291 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3292 TP_ACPI_BLTH_SAVE_STATE))
3293 printk(TPACPI_NOTICE
3294 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003295 else
3296 vdbg_printk(TPACPI_DBG_RFKILL,
3297 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003298}
3299
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003300static void bluetooth_exit(void)
3301{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003302 bluetooth_shutdown();
3303
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003304 if (tpacpi_bluetooth_rfkill)
3305 rfkill_unregister(tpacpi_bluetooth_rfkill);
3306
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003307 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3308 &bluetooth_attr_group);
3309}
3310
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003311static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003313 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003314 int status = 0;
3315
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003316 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3317 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003318
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003319 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003320
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003321 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3322 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003323 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003324 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003326 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3327 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003328 str_supported(tp_features.bluetooth),
3329 status);
3330
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003331#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3332 if (dbg_bluetoothemul) {
3333 tp_features.bluetooth = 1;
3334 printk(TPACPI_INFO
3335 "bluetooth switch emulation enabled\n");
3336 } else
3337#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003338 if (tp_features.bluetooth &&
3339 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3340 /* no bluetooth hardware present in system */
3341 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003342 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003343 "bluetooth hardware not installed\n");
3344 }
3345
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003346 if (!tp_features.bluetooth)
3347 return 1;
3348
3349 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003350 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003351 if (res)
3352 return res;
3353
3354 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3355 &tpacpi_bluetooth_rfkill,
3356 RFKILL_TYPE_BLUETOOTH,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003357 TPACPI_RFK_BLUETOOTH_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003358 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003359 tpacpi_bluetooth_rfk_set,
3360 tpacpi_bluetooth_rfk_get);
3361 if (res) {
3362 bluetooth_exit();
3363 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003364 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003365
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003369/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003370static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003373 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003375 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003378 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003379 (status == RFKILL_STATE_UNBLOCKED) ?
3380 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 len += sprintf(p + len, "commands:\tenable, disable\n");
3382 }
3383
3384 return len;
3385}
3386
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003387static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003390 int state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003392 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003393 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 while ((cmd = next_cmd(&buf))) {
3396 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003397 state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003399 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 } else
3401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003404 if (state != -1) {
3405 tpacpi_disclose_usertask("procfs bluetooth",
3406 "attempt to %s\n",
3407 state ? "enable" : "disable");
3408 bluetooth_set_radiosw(state, 1);
3409 }
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412}
3413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003414static struct ibm_struct bluetooth_driver_data = {
3415 .name = "bluetooth",
3416 .read = bluetooth_read,
3417 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003418 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003419 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003420 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003421};
3422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003423/*************************************************************************
3424 * Wan subdriver
3425 */
3426
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003427enum {
3428 /* ACPI GWAN/SWAN bits */
3429 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3430 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003431 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3432 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003433};
3434
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003435#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3436
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003437static struct rfkill *tpacpi_wan_rfkill;
3438
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003439static void wan_suspend(pm_message_t state)
3440{
3441 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003442 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3443 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3444 vdbg_printk(TPACPI_DBG_RFKILL,
3445 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003446}
3447
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003448static int wan_get_radiosw(void)
3449{
3450 int status;
3451
3452 if (!tp_features.wan)
3453 return -ENODEV;
3454
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003455 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3456 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003457 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003458
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003459#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3460 if (dbg_wwanemul)
3461 return (tpacpi_wwan_emulstate) ?
3462 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3463#endif
3464
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003465 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3466 return -EIO;
3467
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003468 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3469 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003470}
3471
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003472static void wan_update_rfk(void)
3473{
3474 int status;
3475
3476 if (!tpacpi_wan_rfkill)
3477 return;
3478
3479 status = wan_get_radiosw();
3480 if (status < 0)
3481 return;
3482 rfkill_force_state(tpacpi_wan_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003483
3484 vdbg_printk(TPACPI_DBG_RFKILL,
3485 "forced rfkill state to %d\n",
3486 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003487}
3488
3489static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003490{
3491 int status;
3492
3493 if (!tp_features.wan)
3494 return -ENODEV;
3495
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003496 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3497 * reason to risk weird behaviour. */
3498 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3499 && radio_on)
3500 return -EPERM;
3501
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003502 vdbg_printk(TPACPI_DBG_RFKILL,
3503 "will %s WWAN\n", radio_on ? "enable" : "disable");
3504
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003505#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3506 if (dbg_wwanemul) {
3507 tpacpi_wwan_emulstate = !!radio_on;
3508 if (update_rfk)
3509 wan_update_rfk();
3510 return 0;
3511 }
3512#endif
3513
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003514 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003515 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003516 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003517 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003518 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003519 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3520 return -EIO;
3521
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003522 if (update_rfk)
3523 wan_update_rfk();
3524
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003525 return 0;
3526}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003527
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003528/* sysfs wan enable ---------------------------------------------------- */
3529static ssize_t wan_enable_show(struct device *dev,
3530 struct device_attribute *attr,
3531 char *buf)
3532{
3533 int status;
3534
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003535 printk_deprecated_rfkill_attribute("wwan_enable");
3536
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003537 status = wan_get_radiosw();
3538 if (status < 0)
3539 return status;
3540
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003541 return snprintf(buf, PAGE_SIZE, "%d\n",
3542 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003543}
3544
3545static ssize_t wan_enable_store(struct device *dev,
3546 struct device_attribute *attr,
3547 const char *buf, size_t count)
3548{
3549 unsigned long t;
3550 int res;
3551
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003552 printk_deprecated_rfkill_attribute("wwan_enable");
3553
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003554 if (parse_strtoul(buf, 1, &t))
3555 return -EINVAL;
3556
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003557 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3558
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003559 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003560
3561 return (res) ? res : count;
3562}
3563
3564static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003565 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003566 wan_enable_show, wan_enable_store);
3567
3568/* --------------------------------------------------------------------- */
3569
3570static struct attribute *wan_attributes[] = {
3571 &dev_attr_wan_enable.attr,
3572 NULL
3573};
3574
3575static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003576 .attrs = wan_attributes,
3577};
3578
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003579static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3580{
3581 int wans = wan_get_radiosw();
3582
3583 if (wans < 0)
3584 return wans;
3585
3586 *state = wans;
3587 return 0;
3588}
3589
3590static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3591{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003592 dbg_printk(TPACPI_DBG_RFKILL,
3593 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003594 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3595}
3596
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003597static void wan_shutdown(void)
3598{
3599 /* Order firmware to save current state to NVRAM */
3600 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3601 TP_ACPI_WGSV_SAVE_STATE))
3602 printk(TPACPI_NOTICE
3603 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003604 else
3605 vdbg_printk(TPACPI_DBG_RFKILL,
3606 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003607}
3608
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003609static void wan_exit(void)
3610{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003611 wan_shutdown();
3612
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003613 if (tpacpi_wan_rfkill)
3614 rfkill_unregister(tpacpi_wan_rfkill);
3615
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003616 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3617 &wan_attr_group);
3618}
3619
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003620static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003621{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003622 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003623 int status = 0;
3624
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003625 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3626 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003627
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003628 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003629
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003630 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003631 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003632
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003633 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3634 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003635 str_supported(tp_features.wan),
3636 status);
3637
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003638#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3639 if (dbg_wwanemul) {
3640 tp_features.wan = 1;
3641 printk(TPACPI_INFO
3642 "wwan switch emulation enabled\n");
3643 } else
3644#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003645 if (tp_features.wan &&
3646 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3647 /* no wan hardware present in system */
3648 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003649 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003650 "wan hardware not installed\n");
3651 }
3652
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003653 if (!tp_features.wan)
3654 return 1;
3655
3656 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003657 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003658 if (res)
3659 return res;
3660
3661 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3662 &tpacpi_wan_rfkill,
3663 RFKILL_TYPE_WWAN,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003664 TPACPI_RFK_WWAN_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003665 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003666 tpacpi_wan_rfk_set,
3667 tpacpi_wan_rfk_get);
3668 if (res) {
3669 wan_exit();
3670 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003671 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003672
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003673 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003674}
3675
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003676/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003677static int wan_read(char *p)
3678{
3679 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003680 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003681
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003682 tpacpi_disclose_usertask("procfs wan", "read");
3683
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003684 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003685 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003686 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003687 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003688 (status == RFKILL_STATE_UNBLOCKED) ?
3689 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003690 len += sprintf(p + len, "commands:\tenable, disable\n");
3691 }
3692
3693 return len;
3694}
3695
3696static int wan_write(char *buf)
3697{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003698 char *cmd;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003699 int state = -1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003700
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003701 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003702 return -ENODEV;
3703
3704 while ((cmd = next_cmd(&buf))) {
3705 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003706 state = 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003707 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003708 state = 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003709 } else
3710 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003711 }
3712
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003713 if (state != -1) {
3714 tpacpi_disclose_usertask("procfs wan",
3715 "attempt to %s\n",
3716 state ? "enable" : "disable");
3717 wan_set_radiosw(state, 1);
3718 }
3719
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003720 return 0;
3721}
3722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003723static struct ibm_struct wan_driver_data = {
3724 .name = "wan",
3725 .read = wan_read,
3726 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003727 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003728 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003729 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003730};
3731
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003732/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003733 * UWB subdriver
3734 */
3735
3736enum {
3737 /* ACPI GUWB/SUWB bits */
3738 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3739 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3740};
3741
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003742#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3743
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003744static struct rfkill *tpacpi_uwb_rfkill;
3745
3746static int uwb_get_radiosw(void)
3747{
3748 int status;
3749
3750 if (!tp_features.uwb)
3751 return -ENODEV;
3752
3753 /* WLSW overrides UWB in firmware/hardware, reflect that */
3754 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3755 return RFKILL_STATE_HARD_BLOCKED;
3756
3757#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3758 if (dbg_uwbemul)
3759 return (tpacpi_uwb_emulstate) ?
3760 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3761#endif
3762
3763 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3764 return -EIO;
3765
3766 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3767 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3768}
3769
3770static void uwb_update_rfk(void)
3771{
3772 int status;
3773
3774 if (!tpacpi_uwb_rfkill)
3775 return;
3776
3777 status = uwb_get_radiosw();
3778 if (status < 0)
3779 return;
3780 rfkill_force_state(tpacpi_uwb_rfkill, status);
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003781
3782 vdbg_printk(TPACPI_DBG_RFKILL,
3783 "forced rfkill state to %d\n",
3784 status);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003785}
3786
3787static int uwb_set_radiosw(int radio_on, int update_rfk)
3788{
3789 int status;
3790
3791 if (!tp_features.uwb)
3792 return -ENODEV;
3793
3794 /* WLSW overrides UWB in firmware/hardware, but there is no
3795 * reason to risk weird behaviour. */
3796 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3797 && radio_on)
3798 return -EPERM;
3799
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003800 vdbg_printk(TPACPI_DBG_RFKILL,
3801 "will %s UWB\n", radio_on ? "enable" : "disable");
3802
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003803#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3804 if (dbg_uwbemul) {
3805 tpacpi_uwb_emulstate = !!radio_on;
3806 if (update_rfk)
3807 uwb_update_rfk();
3808 return 0;
3809 }
3810#endif
3811
3812 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3813 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3814 return -EIO;
3815
3816 if (update_rfk)
3817 uwb_update_rfk();
3818
3819 return 0;
3820}
3821
3822/* --------------------------------------------------------------------- */
3823
3824static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3825{
3826 int uwbs = uwb_get_radiosw();
3827
3828 if (uwbs < 0)
3829 return uwbs;
3830
3831 *state = uwbs;
3832 return 0;
3833}
3834
3835static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3836{
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003837 dbg_printk(TPACPI_DBG_RFKILL,
3838 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003839 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3840}
3841
3842static void uwb_exit(void)
3843{
3844 if (tpacpi_uwb_rfkill)
3845 rfkill_unregister(tpacpi_uwb_rfkill);
3846}
3847
3848static int __init uwb_init(struct ibm_init_struct *iibm)
3849{
3850 int res;
3851 int status = 0;
3852
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003853 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3854 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003855
3856 TPACPI_ACPIHANDLE_INIT(hkey);
3857
3858 tp_features.uwb = hkey_handle &&
3859 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3860
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3862 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003863 str_supported(tp_features.uwb),
3864 status);
3865
3866#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3867 if (dbg_uwbemul) {
3868 tp_features.uwb = 1;
3869 printk(TPACPI_INFO
3870 "uwb switch emulation enabled\n");
3871 } else
3872#endif
3873 if (tp_features.uwb &&
3874 !(status & TP_ACPI_UWB_HWPRESENT)) {
3875 /* no uwb hardware present in system */
3876 tp_features.uwb = 0;
3877 dbg_printk(TPACPI_DBG_INIT,
3878 "uwb hardware not installed\n");
3879 }
3880
3881 if (!tp_features.uwb)
3882 return 1;
3883
3884 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3885 &tpacpi_uwb_rfkill,
3886 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003887 TPACPI_RFK_UWB_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003888 false,
3889 tpacpi_uwb_rfk_set,
3890 tpacpi_uwb_rfk_get);
3891
3892 return res;
3893}
3894
3895static struct ibm_struct uwb_driver_data = {
3896 .name = "uwb",
3897 .exit = uwb_exit,
3898 .flags.experimental = 1,
3899};
3900
3901/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003902 * Video subdriver
3903 */
3904
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003905#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3906
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003907enum video_access_mode {
3908 TPACPI_VIDEO_NONE = 0,
3909 TPACPI_VIDEO_570, /* 570 */
3910 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3911 TPACPI_VIDEO_NEW, /* all others */
3912};
3913
3914enum { /* video status flags, based on VIDEO_570 */
3915 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3916 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3917 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3918};
3919
3920enum { /* TPACPI_VIDEO_570 constants */
3921 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3922 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3923 * video_status_flags */
3924 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3925 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3926};
3927
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003928static enum video_access_mode video_supported;
3929static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003930
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003931static int video_autosw_get(void);
3932static int video_autosw_set(int enable);
3933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003934TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003935
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003936static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003938 int ivga;
3939
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003940 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3941
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003942 TPACPI_ACPIHANDLE_INIT(vid);
3943 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003944
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003945 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3946 /* G41, assume IVGA doesn't change */
3947 vid_handle = vid2_handle;
3948
3949 if (!vid_handle)
3950 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003951 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003952 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3953 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003954 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003955 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3956 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003957 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003958 else
3959 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003960 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003962 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3963 str_supported(video_supported != TPACPI_VIDEO_NONE),
3964 video_supported);
3965
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003966 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003969static void video_exit(void)
3970{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003971 dbg_printk(TPACPI_DBG_EXIT,
3972 "restoring original video autoswitch mode\n");
3973 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003974 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003975 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003976}
3977
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003978static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
3980 int status = 0;
3981 int i;
3982
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003983 switch (video_supported) {
3984 case TPACPI_VIDEO_570:
3985 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3986 TP_ACPI_VIDEO_570_PHSCMD))
3987 return -EIO;
3988 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3989 break;
3990 case TPACPI_VIDEO_770:
3991 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3992 return -EIO;
3993 if (i)
3994 status |= TP_ACPI_VIDEO_S_LCD;
3995 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3996 return -EIO;
3997 if (i)
3998 status |= TP_ACPI_VIDEO_S_CRT;
3999 break;
4000 case TPACPI_VIDEO_NEW:
4001 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4002 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4003 return -EIO;
4004 if (i)
4005 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004007 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4008 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4009 return -EIO;
4010 if (i)
4011 status |= TP_ACPI_VIDEO_S_LCD;
4012 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4013 return -EIO;
4014 if (i)
4015 status |= TP_ACPI_VIDEO_S_DVI;
4016 break;
4017 default:
4018 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 return status;
4022}
4023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004024static int video_outputsw_set(int status)
4025{
4026 int autosw;
4027 int res = 0;
4028
4029 switch (video_supported) {
4030 case TPACPI_VIDEO_570:
4031 res = acpi_evalf(NULL, NULL,
4032 "\\_SB.PHS2", "vdd",
4033 TP_ACPI_VIDEO_570_PHS2CMD,
4034 status | TP_ACPI_VIDEO_570_PHS2SET);
4035 break;
4036 case TPACPI_VIDEO_770:
4037 autosw = video_autosw_get();
4038 if (autosw < 0)
4039 return autosw;
4040
4041 res = video_autosw_set(1);
4042 if (res)
4043 return res;
4044 res = acpi_evalf(vid_handle, NULL,
4045 "ASWT", "vdd", status * 0x100, 0);
4046 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004047 printk(TPACPI_ERR
4048 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004049 return -EIO;
4050 }
4051 break;
4052 case TPACPI_VIDEO_NEW:
4053 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004054 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004055 break;
4056 default:
4057 return -ENOSYS;
4058 }
4059
4060 return (res)? 0 : -EIO;
4061}
4062
4063static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004065 int autosw = 0;
4066
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004067 switch (video_supported) {
4068 case TPACPI_VIDEO_570:
4069 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4070 return -EIO;
4071 break;
4072 case TPACPI_VIDEO_770:
4073 case TPACPI_VIDEO_NEW:
4074 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4075 return -EIO;
4076 break;
4077 default:
4078 return -ENOSYS;
4079 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004080
4081 return autosw & 1;
4082}
4083
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004084static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004085{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004086 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004087 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004088 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004089}
4090
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004091static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004092{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004093 int autosw = video_autosw_get();
4094 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004095
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004096 if (autosw < 0)
4097 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004098
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004099 switch (video_supported) {
4100 case TPACPI_VIDEO_570:
4101 res = video_autosw_set(1);
4102 if (res)
4103 return res;
4104 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4105 break;
4106 case TPACPI_VIDEO_770:
4107 case TPACPI_VIDEO_NEW:
4108 res = video_autosw_set(1);
4109 if (res)
4110 return res;
4111 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4112 break;
4113 default:
4114 return -ENOSYS;
4115 }
4116 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004117 printk(TPACPI_ERR
4118 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004119 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004120 }
4121
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004122 return (res)? 0 : -EIO;
4123}
4124
4125static int video_expand_toggle(void)
4126{
4127 switch (video_supported) {
4128 case TPACPI_VIDEO_570:
4129 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4130 0 : -EIO;
4131 case TPACPI_VIDEO_770:
4132 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4133 0 : -EIO;
4134 case TPACPI_VIDEO_NEW:
4135 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4136 0 : -EIO;
4137 default:
4138 return -ENOSYS;
4139 }
4140 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004141}
4142
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004143static int video_read(char *p)
4144{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004145 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004146 int len = 0;
4147
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004148 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004149 len += sprintf(p + len, "status:\t\tnot supported\n");
4150 return len;
4151 }
4152
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004153 status = video_outputsw_get();
4154 if (status < 0)
4155 return status;
4156
4157 autosw = video_autosw_get();
4158 if (autosw < 0)
4159 return autosw;
4160
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004161 len += sprintf(p + len, "status:\t\tsupported\n");
4162 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4163 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004164 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004165 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4166 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4167 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4168 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004169 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004170 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4171 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4172 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4173
4174 return len;
4175}
4176
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004177static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178{
4179 char *cmd;
4180 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004181 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004183 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004184 return -ENODEV;
4185
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004186 enable = 0;
4187 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 while ((cmd = next_cmd(&buf))) {
4190 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004191 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004193 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004195 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004197 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004198 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004199 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004200 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004201 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004202 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004203 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004205 res = video_autosw_set(1);
4206 if (res)
4207 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004209 res = video_autosw_set(0);
4210 if (res)
4211 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004213 res = video_outputsw_cycle();
4214 if (res)
4215 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004217 res = video_expand_toggle();
4218 if (res)
4219 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 } else
4221 return -EINVAL;
4222 }
4223
4224 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004225 status = video_outputsw_get();
4226 if (status < 0)
4227 return status;
4228 res = video_outputsw_set((status & ~disable) | enable);
4229 if (res)
4230 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
4232
4233 return 0;
4234}
4235
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004236static struct ibm_struct video_driver_data = {
4237 .name = "video",
4238 .read = video_read,
4239 .write = video_write,
4240 .exit = video_exit,
4241};
4242
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004243#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4244
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004245/*************************************************************************
4246 * Light (thinklight) subdriver
4247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004249TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4250TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004251
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004252static int light_get_status(void)
4253{
4254 int status = 0;
4255
4256 if (tp_features.light_status) {
4257 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4258 return -EIO;
4259 return (!!status);
4260 }
4261
4262 return -ENXIO;
4263}
4264
4265static int light_set_status(int status)
4266{
4267 int rc;
4268
4269 if (tp_features.light) {
4270 if (cmos_handle) {
4271 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4272 (status)?
4273 TP_CMOS_THINKLIGHT_ON :
4274 TP_CMOS_THINKLIGHT_OFF);
4275 } else {
4276 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4277 (status)? 1 : 0);
4278 }
4279 return (rc)? 0 : -EIO;
4280 }
4281
4282 return -ENXIO;
4283}
4284
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004285static void light_set_status_worker(struct work_struct *work)
4286{
4287 struct tpacpi_led_classdev *data =
4288 container_of(work, struct tpacpi_led_classdev, work);
4289
4290 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004291 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004292}
4293
4294static void light_sysfs_set(struct led_classdev *led_cdev,
4295 enum led_brightness brightness)
4296{
4297 struct tpacpi_led_classdev *data =
4298 container_of(led_cdev,
4299 struct tpacpi_led_classdev,
4300 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004301 data->new_state = (brightness != LED_OFF) ?
4302 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004303 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004304}
4305
4306static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4307{
4308 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4309}
4310
4311static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4312 .led_classdev = {
4313 .name = "tpacpi::thinklight",
4314 .brightness_set = &light_sysfs_set,
4315 .brightness_get = &light_sysfs_get,
4316 }
4317};
4318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004319static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004321 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004322
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004323 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4324
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004325 TPACPI_ACPIHANDLE_INIT(ledb);
4326 TPACPI_ACPIHANDLE_INIT(lght);
4327 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004328 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004330 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004331 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004332
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004333 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004334 /* light status not supported on
4335 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004336 tp_features.light_status =
4337 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004339 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4340 str_supported(tp_features.light),
4341 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004343 if (!tp_features.light)
4344 return 1;
4345
4346 rc = led_classdev_register(&tpacpi_pdev->dev,
4347 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004348
4349 if (rc < 0) {
4350 tp_features.light = 0;
4351 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004352 } else {
4353 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004354 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004355
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004356 return rc;
4357}
4358
4359static void light_exit(void)
4360{
4361 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4362 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004363 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004366static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
4368 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004369 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004371 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004372 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004373 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004374 len += sprintf(p + len, "status:\t\tunknown\n");
4375 len += sprintf(p + len, "commands:\ton, off\n");
4376 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004377 status = light_get_status();
4378 if (status < 0)
4379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004381 len += sprintf(p + len, "commands:\ton, off\n");
4382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 return len;
4385}
4386
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004387static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004390 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004391
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004392 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004393 return -ENODEV;
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 while ((cmd = next_cmd(&buf))) {
4396 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004397 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004399 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 } else
4401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004404 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004407static struct ibm_struct light_driver_data = {
4408 .name = "light",
4409 .read = light_read,
4410 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004411 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412};
4413
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004414/*************************************************************************
4415 * Dock subdriver
4416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004418#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004419
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004420static void dock_notify(struct ibm_struct *ibm, u32 event);
4421static int dock_read(char *p);
4422static int dock_write(char *buf);
4423
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004424TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004425 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4426 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4427 "\\_SB.PCI.ISA.SLCE", /* 570 */
4428 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4429
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004430/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004431TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004432
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004433static const struct acpi_device_id ibm_pci_device_ids[] = {
4434 {PCI_ROOT_HID_STRING, 0},
4435 {"", 0},
4436};
4437
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004438static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4439 {
4440 .notify = dock_notify,
4441 .handle = &dock_handle,
4442 .type = ACPI_SYSTEM_NOTIFY,
4443 },
4444 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004445 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4446 * We just use it to get notifications of dock hotplug
4447 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004448 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004449 .notify = dock_notify,
4450 .handle = &pci_handle,
4451 .type = ACPI_SYSTEM_NOTIFY,
4452 },
4453};
4454
4455static struct ibm_struct dock_driver_data[2] = {
4456 {
4457 .name = "dock",
4458 .read = dock_read,
4459 .write = dock_write,
4460 .acpi = &ibm_dock_acpidriver[0],
4461 },
4462 {
4463 .name = "dock",
4464 .acpi = &ibm_dock_acpidriver[1],
4465 },
4466};
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468#define dock_docked() (_sta(dock_handle) & 1)
4469
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004470static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004471{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004472 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4473
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004474 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004475
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004476 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4477 str_supported(dock_handle != NULL));
4478
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004479 return (dock_handle)? 0 : 1;
4480}
4481
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004482static int __init dock_init2(struct ibm_init_struct *iibm)
4483{
4484 int dock2_needed;
4485
4486 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4487
4488 if (dock_driver_data[0].flags.acpi_driver_registered &&
4489 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004490 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004491 dock2_needed = (pci_handle != NULL);
4492 vdbg_printk(TPACPI_DBG_INIT,
4493 "dock PCI handler for the TP 570 is %s\n",
4494 str_supported(dock2_needed));
4495 } else {
4496 vdbg_printk(TPACPI_DBG_INIT,
4497 "dock subdriver part 2 not required\n");
4498 dock2_needed = 0;
4499 }
4500
4501 return (dock2_needed)? 0 : 1;
4502}
4503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004504static void dock_notify(struct ibm_struct *ibm, u32 event)
4505{
4506 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004507 int pci = ibm->acpi->hid && ibm->acpi->device &&
4508 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004509 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004510
4511 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004512 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004513 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004514 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004515 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004516 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004517 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004518 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004519 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004520 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004522 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004523 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004524 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004525 }
Len Brown14e04fb2007-08-23 15:20:26 -04004526 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004527 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004528 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004529 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004530}
4531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004532static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
4534 int len = 0;
4535 int docked = dock_docked();
4536
4537 if (!dock_handle)
4538 len += sprintf(p + len, "status:\t\tnot supported\n");
4539 else if (!docked)
4540 len += sprintf(p + len, "status:\t\tundocked\n");
4541 else {
4542 len += sprintf(p + len, "status:\t\tdocked\n");
4543 len += sprintf(p + len, "commands:\tdock, undock\n");
4544 }
4545
4546 return len;
4547}
4548
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004549static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550{
4551 char *cmd;
4552
4553 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004554 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 while ((cmd = next_cmd(&buf))) {
4557 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4559 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return -EIO;
4561 } else if (strlencmp(cmd, "dock") == 0) {
4562 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4563 return -EIO;
4564 } else
4565 return -EINVAL;
4566 }
4567
4568 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004571#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004573/*************************************************************************
4574 * Bay subdriver
4575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004577#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004578
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004579TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004580 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4581 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4582 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4583 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004584TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004585 "_EJ0", /* all others */
4586 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004587TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004588 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4589 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004590TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004591 "_EJ0", /* 770x */
4592 ); /* all others */
4593
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004594static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004596 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4597
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004598 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004599 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004600 TPACPI_ACPIHANDLE_INIT(bay_ej);
4601 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004602 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004603 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004604
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004605 tp_features.bay_status = bay_handle &&
4606 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4607 tp_features.bay_status2 = bay2_handle &&
4608 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004609
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004610 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4611 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4612 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4613 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004615 vdbg_printk(TPACPI_DBG_INIT,
4616 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004617 str_supported(tp_features.bay_status),
4618 str_supported(tp_features.bay_eject),
4619 str_supported(tp_features.bay_status2),
4620 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004621
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004622 return (tp_features.bay_status || tp_features.bay_eject ||
4623 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624}
4625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626static void bay_notify(struct ibm_struct *ibm, u32 event)
4627{
Len Brown14e04fb2007-08-23 15:20:26 -04004628 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004629 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004630 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004631 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632}
4633
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004634#define bay_occupied(b) (_sta(b##_handle) & 1)
4635
4636static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
4638 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004639 int occupied = bay_occupied(bay);
4640 int occupied2 = bay_occupied(bay2);
4641 int eject, eject2;
4642
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004643 len += sprintf(p + len, "status:\t\t%s\n",
4644 tp_features.bay_status ?
4645 (occupied ? "occupied" : "unoccupied") :
4646 "not supported");
4647 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004648 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4649 "occupied" : "unoccupied");
4650
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004651 eject = tp_features.bay_eject && occupied;
4652 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004653
4654 if (eject && eject2)
4655 len += sprintf(p + len, "commands:\teject, eject2\n");
4656 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004658 else if (eject2)
4659 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 return len;
4662}
4663
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004664static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
4666 char *cmd;
4667
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004668 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669 return -ENODEV;
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004672 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004673 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4674 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004675 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004676 strlencmp(cmd, "eject2") == 0) {
4677 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return -EIO;
4679 } else
4680 return -EINVAL;
4681 }
4682
4683 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004684}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004685
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004686static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4687 .notify = bay_notify,
4688 .handle = &bay_handle,
4689 .type = ACPI_SYSTEM_NOTIFY,
4690};
4691
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004692static struct ibm_struct bay_driver_data = {
4693 .name = "bay",
4694 .read = bay_read,
4695 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004696 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004697};
4698
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004699#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004701/*************************************************************************
4702 * CMOS subdriver
4703 */
4704
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004705/* sysfs cmos_command -------------------------------------------------- */
4706static ssize_t cmos_command_store(struct device *dev,
4707 struct device_attribute *attr,
4708 const char *buf, size_t count)
4709{
4710 unsigned long cmos_cmd;
4711 int res;
4712
4713 if (parse_strtoul(buf, 21, &cmos_cmd))
4714 return -EINVAL;
4715
4716 res = issue_thinkpad_cmos_command(cmos_cmd);
4717 return (res)? res : count;
4718}
4719
4720static struct device_attribute dev_attr_cmos_command =
4721 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4722
4723/* --------------------------------------------------------------------- */
4724
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004725static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004726{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004727 int res;
4728
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004729 vdbg_printk(TPACPI_DBG_INIT,
4730 "initializing cmos commands subdriver\n");
4731
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004732 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004733
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004734 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4735 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004736
4737 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4738 if (res)
4739 return res;
4740
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004741 return (cmos_handle)? 0 : 1;
4742}
4743
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004744static void cmos_exit(void)
4745{
4746 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4747}
4748
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004749static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
4751 int len = 0;
4752
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004753 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4754 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (!cmos_handle)
4756 len += sprintf(p + len, "status:\t\tnot supported\n");
4757 else {
4758 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004759 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761
4762 return len;
4763}
4764
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004765static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
4767 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004768 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
4770 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004771 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4772 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 /* cmos_cmd set */
4774 } else
4775 return -EINVAL;
4776
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004777 res = issue_thinkpad_cmos_command(cmos_cmd);
4778 if (res)
4779 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
4781
4782 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004783}
4784
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004785static struct ibm_struct cmos_driver_data = {
4786 .name = "cmos",
4787 .read = cmos_read,
4788 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004789 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004790};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004791
4792/*************************************************************************
4793 * LED subdriver
4794 */
4795
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004796enum led_access_mode {
4797 TPACPI_LED_NONE = 0,
4798 TPACPI_LED_570, /* 570 */
4799 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4800 TPACPI_LED_NEW, /* all others */
4801};
4802
4803enum { /* For TPACPI_LED_OLD */
4804 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4805 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4806 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4807};
4808
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004809static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004810
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004811TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004812 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4813 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004814 "LED", /* all others */
4815 ); /* R30, R31 */
4816
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004817#define TPACPI_LED_NUMLEDS 8
4818static struct tpacpi_led_classdev *tpacpi_leds;
4819static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004820static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004821 /* there's a limit of 19 chars + NULL before 2.6.26 */
4822 "tpacpi::power",
4823 "tpacpi:orange:batt",
4824 "tpacpi:green:batt",
4825 "tpacpi::dock_active",
4826 "tpacpi::bay_active",
4827 "tpacpi::dock_batt",
4828 "tpacpi::unknown_led",
4829 "tpacpi::standby",
4830};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004831#define TPACPI_SAFE_LEDS 0x0081U
4832
4833static inline bool tpacpi_is_led_restricted(const unsigned int led)
4834{
4835#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4836 return false;
4837#else
4838 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4839#endif
4840}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004841
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004842static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004843{
4844 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004845 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004846
4847 switch (led_supported) {
4848 case TPACPI_LED_570:
4849 if (!acpi_evalf(ec_handle,
4850 &status, "GLED", "dd", 1 << led))
4851 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004852 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853 TPACPI_LED_OFF :
4854 ((status == 1)?
4855 TPACPI_LED_ON :
4856 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004857 tpacpi_led_state_cache[led] = led_s;
4858 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 default:
4860 return -ENXIO;
4861 }
4862
4863 /* not reached */
4864}
4865
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004866static int led_set_status(const unsigned int led,
4867 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868{
4869 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004870 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4871 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004872
4873 int rc = 0;
4874
4875 switch (led_supported) {
4876 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004877 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004878 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004879 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004880 if (unlikely(tpacpi_is_led_restricted(led)))
4881 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004882 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4883 (1 << led), led_sled_arg1[ledstatus]))
4884 rc = -EIO;
4885 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004886 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004887 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004888 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004889 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004890 if (unlikely(tpacpi_is_led_restricted(led)))
4891 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004892 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4893 if (rc >= 0)
4894 rc = ec_write(TPACPI_LED_EC_HLBL,
4895 (ledstatus == TPACPI_LED_BLINK) << led);
4896 if (rc >= 0)
4897 rc = ec_write(TPACPI_LED_EC_HLCL,
4898 (ledstatus != TPACPI_LED_OFF) << led);
4899 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004900 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004901 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004902 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4903 return -EINVAL;
4904 if (unlikely(tpacpi_is_led_restricted(led)))
4905 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004906 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4907 led, led_led_arg1[ledstatus]))
4908 rc = -EIO;
4909 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004910 default:
4911 rc = -ENXIO;
4912 }
4913
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004914 if (!rc)
4915 tpacpi_led_state_cache[led] = ledstatus;
4916
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004917 return rc;
4918}
4919
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004920static void led_set_status_worker(struct work_struct *work)
4921{
4922 struct tpacpi_led_classdev *data =
4923 container_of(work, struct tpacpi_led_classdev, work);
4924
4925 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004926 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004927}
4928
4929static void led_sysfs_set(struct led_classdev *led_cdev,
4930 enum led_brightness brightness)
4931{
4932 struct tpacpi_led_classdev *data = container_of(led_cdev,
4933 struct tpacpi_led_classdev, led_classdev);
4934
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004935 if (brightness == LED_OFF)
4936 data->new_state = TPACPI_LED_OFF;
4937 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4938 data->new_state = TPACPI_LED_ON;
4939 else
4940 data->new_state = TPACPI_LED_BLINK;
4941
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004942 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004943}
4944
4945static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4946 unsigned long *delay_on, unsigned long *delay_off)
4947{
4948 struct tpacpi_led_classdev *data = container_of(led_cdev,
4949 struct tpacpi_led_classdev, led_classdev);
4950
4951 /* Can we choose the flash rate? */
4952 if (*delay_on == 0 && *delay_off == 0) {
4953 /* yes. set them to the hardware blink rate (1 Hz) */
4954 *delay_on = 500; /* ms */
4955 *delay_off = 500; /* ms */
4956 } else if ((*delay_on != 500) || (*delay_off != 500))
4957 return -EINVAL;
4958
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004959 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004960 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004961
4962 return 0;
4963}
4964
4965static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4966{
4967 int rc;
4968
4969 struct tpacpi_led_classdev *data = container_of(led_cdev,
4970 struct tpacpi_led_classdev, led_classdev);
4971
4972 rc = led_get_status(data->led);
4973
4974 if (rc == TPACPI_LED_OFF || rc < 0)
4975 rc = LED_OFF; /* no error handling in led class :( */
4976 else
4977 rc = LED_FULL;
4978
4979 return rc;
4980}
4981
4982static void led_exit(void)
4983{
4984 unsigned int i;
4985
4986 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4987 if (tpacpi_leds[i].led_classdev.name)
4988 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4989 }
4990
4991 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004992}
4993
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004994static int __init tpacpi_init_led(unsigned int led)
4995{
4996 int rc;
4997
4998 tpacpi_leds[led].led = led;
4999
5000 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5001 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5002 if (led_supported == TPACPI_LED_570)
5003 tpacpi_leds[led].led_classdev.brightness_get =
5004 &led_sysfs_get;
5005
5006 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5007
5008 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5009
5010 rc = led_classdev_register(&tpacpi_pdev->dev,
5011 &tpacpi_leds[led].led_classdev);
5012 if (rc < 0)
5013 tpacpi_leds[led].led_classdev.name = NULL;
5014
5015 return rc;
5016}
5017
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005018static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005019{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005020 unsigned int i;
5021 int rc;
5022
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005023 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5024
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005025 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005026
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005027 if (!led_handle)
5028 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005029 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005030 else if (strlencmp(led_path, "SLED") == 0)
5031 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005032 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005033 else if (strlencmp(led_path, "SYSL") == 0)
5034 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005035 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005036 else
5037 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005038 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005039
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005040 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5041 str_supported(led_supported), led_supported);
5042
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005043 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5044 GFP_KERNEL);
5045 if (!tpacpi_leds) {
5046 printk(TPACPI_ERR "Out of memory for LED data\n");
5047 return -ENOMEM;
5048 }
5049
5050 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005051 if (!tpacpi_is_led_restricted(i)) {
5052 rc = tpacpi_init_led(i);
5053 if (rc < 0) {
5054 led_exit();
5055 return rc;
5056 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005057 }
5058 }
5059
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005060#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5061 if (led_supported != TPACPI_LED_NONE)
5062 printk(TPACPI_NOTICE
5063 "warning: userspace override of important "
5064 "firmware LEDs is enabled\n");
5065#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005066 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005067}
5068
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005069#define str_led_status(s) \
5070 ((s) == TPACPI_LED_OFF ? "off" : \
5071 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005072
5073static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
5075 int len = 0;
5076
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005077 if (!led_supported) {
5078 len += sprintf(p + len, "status:\t\tnot supported\n");
5079 return len;
5080 }
5081 len += sprintf(p + len, "status:\t\tsupported\n");
5082
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005083 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005084 /* 570 */
5085 int i, status;
5086 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005087 status = led_get_status(i);
5088 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005089 return -EIO;
5090 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005091 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005092 }
5093 }
5094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005096 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 return len;
5099}
5100
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005101static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102{
5103 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005104 int led, rc;
5105 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005106
5107 if (!led_supported)
5108 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
5110 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005111 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 return -EINVAL;
5113
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005114 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005115 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005117 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005118 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005119 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005120 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005121 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005122 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005123
5124 rc = led_set_status(led, s);
5125 if (rc < 0)
5126 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 }
5128
5129 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005130}
5131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005132static struct ibm_struct led_driver_data = {
5133 .name = "led",
5134 .read = led_read,
5135 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005136 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005137};
5138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005139/*************************************************************************
5140 * Beep subdriver
5141 */
5142
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005143TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005144
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005145static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005146{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005147 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5148
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005149 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005150
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005151 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5152 str_supported(beep_handle != NULL));
5153
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005154 return (beep_handle)? 0 : 1;
5155}
5156
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005157static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
5159 int len = 0;
5160
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005161 if (!beep_handle)
5162 len += sprintf(p + len, "status:\t\tnot supported\n");
5163 else {
5164 len += sprintf(p + len, "status:\t\tsupported\n");
5165 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 return len;
5169}
5170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005171static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172{
5173 char *cmd;
5174 int beep_cmd;
5175
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005176 if (!beep_handle)
5177 return -ENODEV;
5178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005180 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5181 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 /* beep_cmd set */
5183 } else
5184 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005185 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 return -EIO;
5187 }
5188
5189 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005190}
5191
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005192static struct ibm_struct beep_driver_data = {
5193 .name = "beep",
5194 .read = beep_read,
5195 .write = beep_write,
5196};
5197
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005198/*************************************************************************
5199 * Thermal subdriver
5200 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005201
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005202enum thermal_access_mode {
5203 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5204 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5205 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5206 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5207 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5208};
5209
5210enum { /* TPACPI_THERMAL_TPEC_* */
5211 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5212 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5213 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5214};
5215
5216#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5217struct ibm_thermal_sensors_struct {
5218 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5219};
5220
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005221static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005222
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005223/* idx is zero-based */
5224static int thermal_get_sensor(int idx, s32 *value)
5225{
5226 int t;
5227 s8 tmp;
5228 char tmpi[5];
5229
5230 t = TP_EC_THERMAL_TMP0;
5231
5232 switch (thermal_read_mode) {
5233#if TPACPI_MAX_THERMAL_SENSORS >= 16
5234 case TPACPI_THERMAL_TPEC_16:
5235 if (idx >= 8 && idx <= 15) {
5236 t = TP_EC_THERMAL_TMP8;
5237 idx -= 8;
5238 }
5239 /* fallthrough */
5240#endif
5241 case TPACPI_THERMAL_TPEC_8:
5242 if (idx <= 7) {
5243 if (!acpi_ec_read(t + idx, &tmp))
5244 return -EIO;
5245 *value = tmp * 1000;
5246 return 0;
5247 }
5248 break;
5249
5250 case TPACPI_THERMAL_ACPI_UPDT:
5251 if (idx <= 7) {
5252 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5253 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5254 return -EIO;
5255 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5256 return -EIO;
5257 *value = (t - 2732) * 100;
5258 return 0;
5259 }
5260 break;
5261
5262 case TPACPI_THERMAL_ACPI_TMP07:
5263 if (idx <= 7) {
5264 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5265 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5266 return -EIO;
5267 if (t > 127 || t < -127)
5268 t = TP_EC_THERMAL_TMP_NA;
5269 *value = t * 1000;
5270 return 0;
5271 }
5272 break;
5273
5274 case TPACPI_THERMAL_NONE:
5275 default:
5276 return -ENOSYS;
5277 }
5278
5279 return -EINVAL;
5280}
5281
5282static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5283{
5284 int res, i;
5285 int n;
5286
5287 n = 8;
5288 i = 0;
5289
5290 if (!s)
5291 return -EINVAL;
5292
5293 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5294 n = 16;
5295
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005296 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005297 res = thermal_get_sensor(i, &s->temp[i]);
5298 if (res)
5299 return res;
5300 }
5301
5302 return n;
5303}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005304
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005305/* sysfs temp##_input -------------------------------------------------- */
5306
5307static ssize_t thermal_temp_input_show(struct device *dev,
5308 struct device_attribute *attr,
5309 char *buf)
5310{
5311 struct sensor_device_attribute *sensor_attr =
5312 to_sensor_dev_attr(attr);
5313 int idx = sensor_attr->index;
5314 s32 value;
5315 int res;
5316
5317 res = thermal_get_sensor(idx, &value);
5318 if (res)
5319 return res;
5320 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5321 return -ENXIO;
5322
5323 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5324}
5325
5326#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005327 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5328 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005329
5330static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5331 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5332 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5333 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5334 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5335 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5336 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5337 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5338 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5339 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5340 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5341 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5342 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5343 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5344 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5345 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5346 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5347};
5348
5349#define THERMAL_ATTRS(X) \
5350 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5351
5352static struct attribute *thermal_temp_input_attr[] = {
5353 THERMAL_ATTRS(8),
5354 THERMAL_ATTRS(9),
5355 THERMAL_ATTRS(10),
5356 THERMAL_ATTRS(11),
5357 THERMAL_ATTRS(12),
5358 THERMAL_ATTRS(13),
5359 THERMAL_ATTRS(14),
5360 THERMAL_ATTRS(15),
5361 THERMAL_ATTRS(0),
5362 THERMAL_ATTRS(1),
5363 THERMAL_ATTRS(2),
5364 THERMAL_ATTRS(3),
5365 THERMAL_ATTRS(4),
5366 THERMAL_ATTRS(5),
5367 THERMAL_ATTRS(6),
5368 THERMAL_ATTRS(7),
5369 NULL
5370};
5371
5372static const struct attribute_group thermal_temp_input16_group = {
5373 .attrs = thermal_temp_input_attr
5374};
5375
5376static const struct attribute_group thermal_temp_input8_group = {
5377 .attrs = &thermal_temp_input_attr[8]
5378};
5379
5380#undef THERMAL_SENSOR_ATTR_TEMP
5381#undef THERMAL_ATTRS
5382
5383/* --------------------------------------------------------------------- */
5384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005385static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005386{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005387 u8 t, ta1, ta2;
5388 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005389 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005390 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005391
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005392 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5393
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005394 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005395
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005396 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005397 /*
5398 * Direct EC access mode: sensors at registers
5399 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5400 * non-implemented, thermal sensors return 0x80 when
5401 * not available
5402 */
5403
5404 ta1 = ta2 = 0;
5405 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005406 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005407 ta1 |= t;
5408 } else {
5409 ta1 = 0;
5410 break;
5411 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005412 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005413 ta2 |= t;
5414 } else {
5415 ta1 = 0;
5416 break;
5417 }
5418 }
5419 if (ta1 == 0) {
5420 /* This is sheer paranoia, but we handle it anyway */
5421 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005422 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005423 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005424 "falling back to ACPI TMPx access "
5425 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005426 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005427 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005428 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005429 "ThinkPad ACPI EC access misbehaving, "
5430 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005431 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005432 }
5433 } else {
5434 thermal_read_mode =
5435 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005436 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005437 }
5438 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005439 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5440 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005441 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005442 } else {
5443 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005444 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005445 }
5446 } else {
5447 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005448 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005449 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005450
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005451 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5452 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5453 thermal_read_mode);
5454
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005455 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005456 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005457 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005458 &thermal_temp_input16_group);
5459 if (res)
5460 return res;
5461 break;
5462 case TPACPI_THERMAL_TPEC_8:
5463 case TPACPI_THERMAL_ACPI_TMP07:
5464 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005465 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005466 &thermal_temp_input8_group);
5467 if (res)
5468 return res;
5469 break;
5470 case TPACPI_THERMAL_NONE:
5471 default:
5472 return 1;
5473 }
5474
5475 return 0;
5476}
5477
5478static void thermal_exit(void)
5479{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005480 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005481 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005482 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005483 &thermal_temp_input16_group);
5484 break;
5485 case TPACPI_THERMAL_TPEC_8:
5486 case TPACPI_THERMAL_ACPI_TMP07:
5487 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005488 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005489 &thermal_temp_input16_group);
5490 break;
5491 case TPACPI_THERMAL_NONE:
5492 default:
5493 break;
5494 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495}
5496
5497static int thermal_read(char *p)
5498{
5499 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005500 int n, i;
5501 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005502
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005503 n = thermal_get_sensors(&t);
5504 if (unlikely(n < 0))
5505 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005506
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005507 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005508
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005509 if (n > 0) {
5510 for (i = 0; i < (n - 1); i++)
5511 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5512 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5513 } else
5514 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005515
5516 return len;
5517}
5518
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005519static struct ibm_struct thermal_driver_data = {
5520 .name = "thermal",
5521 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005522 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005523};
5524
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005525/*************************************************************************
5526 * EC Dump subdriver
5527 */
5528
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005529static u8 ecdump_regs[256];
5530
5531static int ecdump_read(char *p)
5532{
5533 int len = 0;
5534 int i, j;
5535 u8 v;
5536
5537 len += sprintf(p + len, "EC "
5538 " +00 +01 +02 +03 +04 +05 +06 +07"
5539 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5540 for (i = 0; i < 256; i += 16) {
5541 len += sprintf(p + len, "EC 0x%02x:", i);
5542 for (j = 0; j < 16; j++) {
5543 if (!acpi_ec_read(i + j, &v))
5544 break;
5545 if (v != ecdump_regs[i + j])
5546 len += sprintf(p + len, " *%02x", v);
5547 else
5548 len += sprintf(p + len, " %02x", v);
5549 ecdump_regs[i + j] = v;
5550 }
5551 len += sprintf(p + len, "\n");
5552 if (j != 16)
5553 break;
5554 }
5555
5556 /* These are way too dangerous to advertise openly... */
5557#if 0
5558 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5559 " (<offset> is 00-ff, <value> is 00-ff)\n");
5560 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5561 " (<offset> is 00-ff, <value> is 0-255)\n");
5562#endif
5563 return len;
5564}
5565
5566static int ecdump_write(char *buf)
5567{
5568 char *cmd;
5569 int i, v;
5570
5571 while ((cmd = next_cmd(&buf))) {
5572 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5573 /* i and v set */
5574 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5575 /* i and v set */
5576 } else
5577 return -EINVAL;
5578 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5579 if (!acpi_ec_write(i, v))
5580 return -EIO;
5581 } else
5582 return -EINVAL;
5583 }
5584
5585 return 0;
5586}
5587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005588static struct ibm_struct ecdump_driver_data = {
5589 .name = "ecdump",
5590 .read = ecdump_read,
5591 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005592 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005593};
5594
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005595/*************************************************************************
5596 * Backlight/brightness subdriver
5597 */
Holger Macht8acb0252006-10-20 14:30:28 -07005598
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005599#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5600
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005601/*
5602 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5603 * CMOS NVRAM byte 0x5E, bits 0-3.
5604 *
5605 * EC HBRV (0x31) has the following layout
5606 * Bit 7: unknown function
5607 * Bit 6: unknown function
5608 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5609 * Bit 4: must be set to zero to avoid problems
5610 * Bit 3-0: backlight brightness level
5611 *
5612 * brightness_get_raw returns status data in the HBRV layout
5613 */
5614
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005615enum {
5616 TP_EC_BACKLIGHT = 0x31,
5617
5618 /* TP_EC_BACKLIGHT bitmasks */
5619 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5620 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5621 TP_EC_BACKLIGHT_MAPSW = 0x20,
5622};
5623
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005624enum tpacpi_brightness_access_mode {
5625 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5626 TPACPI_BRGHT_MODE_EC, /* EC control */
5627 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5628 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5629 TPACPI_BRGHT_MODE_MAX
5630};
5631
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005632static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005633
5634static enum tpacpi_brightness_access_mode brightness_mode =
5635 TPACPI_BRGHT_MODE_MAX;
5636
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005637static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5638
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005639static struct mutex brightness_mutex;
5640
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005641/* NVRAM brightness access,
5642 * call with brightness_mutex held! */
5643static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005644{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005645 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005646
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005647 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5648 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5649 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5650 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005651
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005652 return lnvram;
5653}
5654
5655static void tpacpi_brightness_checkpoint_nvram(void)
5656{
5657 u8 lec = 0;
5658 u8 b_nvram;
5659
5660 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5661 return;
5662
5663 vdbg_printk(TPACPI_DBG_BRGHT,
5664 "trying to checkpoint backlight level to NVRAM...\n");
5665
5666 if (mutex_lock_killable(&brightness_mutex) < 0)
5667 return;
5668
5669 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5670 goto unlock;
5671 lec &= TP_EC_BACKLIGHT_LVLMSK;
5672 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5673
5674 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5675 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5676 /* NVRAM needs update */
5677 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5678 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5679 b_nvram |= lec;
5680 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5681 dbg_printk(TPACPI_DBG_BRGHT,
5682 "updated NVRAM backlight level to %u (0x%02x)\n",
5683 (unsigned int) lec, (unsigned int) b_nvram);
5684 } else
5685 vdbg_printk(TPACPI_DBG_BRGHT,
5686 "NVRAM backlight level already is %u (0x%02x)\n",
5687 (unsigned int) lec, (unsigned int) b_nvram);
5688
5689unlock:
5690 mutex_unlock(&brightness_mutex);
5691}
5692
5693
5694/* call with brightness_mutex held! */
5695static int tpacpi_brightness_get_raw(int *status)
5696{
5697 u8 lec = 0;
5698
5699 switch (brightness_mode) {
5700 case TPACPI_BRGHT_MODE_UCMS_STEP:
5701 *status = tpacpi_brightness_nvram_get();
5702 return 0;
5703 case TPACPI_BRGHT_MODE_EC:
5704 case TPACPI_BRGHT_MODE_ECNVRAM:
5705 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005706 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005707 *status = lec;
5708 return 0;
5709 default:
5710 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005711 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005712}
5713
5714/* call with brightness_mutex held! */
5715/* do NOT call with illegal backlight level value */
5716static int tpacpi_brightness_set_ec(unsigned int value)
5717{
5718 u8 lec = 0;
5719
5720 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5721 return -EIO;
5722
5723 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5724 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5725 (value & TP_EC_BACKLIGHT_LVLMSK))))
5726 return -EIO;
5727
5728 return 0;
5729}
5730
5731/* call with brightness_mutex held! */
5732static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5733{
5734 int cmos_cmd, inc;
5735 unsigned int current_value, i;
5736
5737 current_value = tpacpi_brightness_nvram_get();
5738
5739 if (value == current_value)
5740 return 0;
5741
5742 cmos_cmd = (value > current_value) ?
5743 TP_CMOS_BRIGHTNESS_UP :
5744 TP_CMOS_BRIGHTNESS_DOWN;
5745 inc = (value > current_value) ? 1 : -1;
5746
5747 for (i = current_value; i != value; i += inc)
5748 if (issue_thinkpad_cmos_command(cmos_cmd))
5749 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005750
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005751 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005752}
5753
5754/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005755static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005756{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005757 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005758
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005759 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5760 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005761 return -EINVAL;
5762
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005763 vdbg_printk(TPACPI_DBG_BRGHT,
5764 "set backlight level to %d\n", value);
5765
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005766 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005767 if (res < 0)
5768 return res;
5769
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005770 switch (brightness_mode) {
5771 case TPACPI_BRGHT_MODE_EC:
5772 case TPACPI_BRGHT_MODE_ECNVRAM:
5773 res = tpacpi_brightness_set_ec(value);
5774 break;
5775 case TPACPI_BRGHT_MODE_UCMS_STEP:
5776 res = tpacpi_brightness_set_ucmsstep(value);
5777 break;
5778 default:
5779 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005780 }
5781
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005782 mutex_unlock(&brightness_mutex);
5783 return res;
5784}
5785
5786/* sysfs backlight class ----------------------------------------------- */
5787
5788static int brightness_update_status(struct backlight_device *bd)
5789{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005790 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005791 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5792 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005793 bd->props.brightness : 0;
5794
5795 dbg_printk(TPACPI_DBG_BRGHT,
5796 "backlight: attempt to set level to %d\n",
5797 level);
5798
5799 /* it is the backlight class's job (caller) to handle
5800 * EINTR and other errors properly */
5801 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005802}
Holger Macht8acb0252006-10-20 14:30:28 -07005803
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005804static int brightness_get(struct backlight_device *bd)
5805{
5806 int status, res;
5807
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005808 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005809 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005810 return 0;
5811
5812 res = tpacpi_brightness_get_raw(&status);
5813
5814 mutex_unlock(&brightness_mutex);
5815
5816 if (res < 0)
5817 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005818
5819 return status & TP_EC_BACKLIGHT_LVLMSK;
5820}
5821
Richard Purdie599a52d2007-02-10 23:07:48 +00005822static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005823 .get_brightness = brightness_get,
5824 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005825};
5826
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005827/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005829static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005830{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005831 int b;
5832
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005833 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5834
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005835 mutex_init(&brightness_mutex);
5836
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005837 /*
5838 * We always attempt to detect acpi support, so as to switch
5839 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5840 * going to publish a backlight interface
5841 */
5842 b = tpacpi_check_std_acpi_brightness_support();
5843 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005844
5845 if (acpi_video_backlight_support()) {
5846 if (brightness_enable > 1) {
5847 printk(TPACPI_NOTICE
5848 "Standard ACPI backlight interface "
5849 "available, not loading native one.\n");
5850 return 1;
5851 } else if (brightness_enable == 1) {
5852 printk(TPACPI_NOTICE
5853 "Backlight control force enabled, even if standard "
5854 "ACPI backlight interface is available\n");
5855 }
5856 } else {
5857 if (brightness_enable > 1) {
5858 printk(TPACPI_NOTICE
5859 "Standard ACPI backlight interface not "
5860 "available, thinkpad_acpi native "
5861 "brightness control enabled\n");
5862 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005863 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005864 }
5865
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005866 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005867 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005868 "brightness support disabled by "
5869 "module parameter\n");
5870 return 1;
5871 }
5872
5873 if (b > 16) {
5874 printk(TPACPI_ERR
5875 "Unsupported brightness interface, "
5876 "please contact %s\n", TPACPI_MAIL);
5877 return 1;
5878 }
5879 if (b == 16)
5880 tp_features.bright_16levels = 1;
5881
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005882 /*
5883 * Check for module parameter bogosity, note that we
5884 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5885 * able to detect "unspecified"
5886 */
5887 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005888 return -EINVAL;
5889
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005890 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5891 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5892 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5893 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5894 /*
5895 * IBM models that define HBRV probably have
5896 * EC-based backlight level control
5897 */
5898 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5899 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5900 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5901 else
5902 /* all other IBM ThinkPads */
5903 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5904 } else
5905 /* All Lenovo ThinkPads */
5906 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5907
5908 dbg_printk(TPACPI_DBG_BRGHT,
5909 "selected brightness_mode=%d\n",
5910 brightness_mode);
5911 }
5912
5913 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005914 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005915
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005916 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005917 printk(TPACPI_INFO
5918 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005919
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005920 ibm_backlight_device = backlight_device_register(
5921 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5922 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005923 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005924 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005925 return PTR_ERR(ibm_backlight_device);
5926 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005927 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5928 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005929
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005930 ibm_backlight_device->props.max_brightness =
5931 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005932 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005933 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005934
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005935 return 0;
5936}
5937
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005938static void brightness_suspend(pm_message_t state)
5939{
5940 tpacpi_brightness_checkpoint_nvram();
5941}
5942
5943static void brightness_shutdown(void)
5944{
5945 tpacpi_brightness_checkpoint_nvram();
5946}
5947
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005948static void brightness_exit(void)
5949{
5950 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005951 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005952 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005953 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005954 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005955
5956 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005957}
5958
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005959static int brightness_read(char *p)
5960{
5961 int len = 0;
5962 int level;
5963
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005964 level = brightness_get(NULL);
5965 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005966 len += sprintf(p + len, "level:\t\tunreadable\n");
5967 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005968 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005969 len += sprintf(p + len, "commands:\tup, down\n");
5970 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005971 " (<level> is 0-%d)\n",
5972 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005973 }
5974
5975 return len;
5976}
5977
5978static int brightness_write(char *buf)
5979{
5980 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005981 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005982 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005983 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005984
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005985 level = brightness_get(NULL);
5986 if (level < 0)
5987 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005988
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005989 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005990 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005991 if (level < max_level)
5992 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005994 if (level > 0)
5995 level--;
5996 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5997 level >= 0 && level <= max_level) {
5998 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005999 } else
6000 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006001 }
6002
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006003 tpacpi_disclose_usertask("procfs brightness",
6004 "set level to %d\n", level);
6005
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006006 /*
6007 * Now we know what the final level should be, so we try to set it.
6008 * Doing it this way makes the syscall restartable in case of EINTR
6009 */
6010 rc = brightness_set(level);
6011 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006012}
6013
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006014static struct ibm_struct brightness_driver_data = {
6015 .name = "brightness",
6016 .read = brightness_read,
6017 .write = brightness_write,
6018 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006019 .suspend = brightness_suspend,
6020 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006021};
6022
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006023/*************************************************************************
6024 * Volume subdriver
6025 */
6026
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006027static int volume_offset = 0x30;
6028
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006029static int volume_read(char *p)
6030{
6031 int len = 0;
6032 u8 level;
6033
6034 if (!acpi_ec_read(volume_offset, &level)) {
6035 len += sprintf(p + len, "level:\t\tunreadable\n");
6036 } else {
6037 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6038 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6039 len += sprintf(p + len, "commands:\tup, down, mute\n");
6040 len += sprintf(p + len, "commands:\tlevel <level>"
6041 " (<level> is 0-15)\n");
6042 }
6043
6044 return len;
6045}
6046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006047static int volume_write(char *buf)
6048{
6049 int cmos_cmd, inc, i;
6050 u8 level, mute;
6051 int new_level, new_mute;
6052 char *cmd;
6053
6054 while ((cmd = next_cmd(&buf))) {
6055 if (!acpi_ec_read(volume_offset, &level))
6056 return -EIO;
6057 new_mute = mute = level & 0x40;
6058 new_level = level = level & 0xf;
6059
6060 if (strlencmp(cmd, "up") == 0) {
6061 if (mute)
6062 new_mute = 0;
6063 else
6064 new_level = level == 15 ? 15 : level + 1;
6065 } else if (strlencmp(cmd, "down") == 0) {
6066 if (mute)
6067 new_mute = 0;
6068 else
6069 new_level = level == 0 ? 0 : level - 1;
6070 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6071 new_level >= 0 && new_level <= 15) {
6072 /* new_level set */
6073 } else if (strlencmp(cmd, "mute") == 0) {
6074 new_mute = 0x40;
6075 } else
6076 return -EINVAL;
6077
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006078 if (new_level != level) {
6079 /* mute doesn't change */
6080
6081 cmos_cmd = (new_level > level) ?
6082 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006083 inc = new_level > level ? 1 : -1;
6084
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006085 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006086 !acpi_ec_write(volume_offset, level)))
6087 return -EIO;
6088
6089 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006090 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006091 !acpi_ec_write(volume_offset, i + inc))
6092 return -EIO;
6093
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006094 if (mute &&
6095 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6096 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006097 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006098 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006099 }
6100
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006101 if (new_mute != mute) {
6102 /* level doesn't change */
6103
6104 cmos_cmd = (new_mute) ?
6105 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006106
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006107 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006108 !acpi_ec_write(volume_offset, level + new_mute))
6109 return -EIO;
6110 }
6111 }
6112
6113 return 0;
6114}
6115
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006116static struct ibm_struct volume_driver_data = {
6117 .name = "volume",
6118 .read = volume_read,
6119 .write = volume_write,
6120};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006121
6122/*************************************************************************
6123 * Fan subdriver
6124 */
6125
6126/*
6127 * FAN ACCESS MODES
6128 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006129 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006130 * ACPI GFAN method: returns fan level
6131 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006132 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006133 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006134 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006135 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006136 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6137 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006138 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6139 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006140 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006141 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006142 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6143 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006144 *
6145 * Fan speed changes of any sort (including those caused by the
6146 * disengaged mode) are usually done slowly by the firmware as the
6147 * maximum ammount of fan duty cycle change per second seems to be
6148 * limited.
6149 *
6150 * Reading is not available if GFAN exists.
6151 * Writing is not available if SFAN exists.
6152 *
6153 * Bits
6154 * 7 automatic mode engaged;
6155 * (default operation mode of the ThinkPad)
6156 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006157 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006158 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006159 * the tachometer while the fan controller ramps up
6160 * the speed (which can take up to a few *minutes*).
6161 * Speeds up fan to 100% duty-cycle, which is far above
6162 * the standard RPM levels. It is not impossible that
6163 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006164 * 5-3 unused in some models. Extra bits for fan level
6165 * in others, but still useless as all values above
6166 * 7 map to the same speed as level 7 in these models.
6167 * 2-0 fan level (0..7 usually)
6168 * 0x00 = stop
6169 * 0x07 = max (set when temperatures critical)
6170 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006171 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006172 *
6173 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6174 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6175 * does so, its initial value is meaningless (0x07).
6176 *
6177 * For firmware bugs, refer to:
6178 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6179 *
6180 * ----
6181 *
6182 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6183 * Main fan tachometer reading (in RPM)
6184 *
6185 * This register is present on all ThinkPads with a new-style EC, and
6186 * it is known not to be present on the A21m/e, and T22, as there is
6187 * something else in offset 0x84 according to the ACPI DSDT. Other
6188 * ThinkPads from this same time period (and earlier) probably lack the
6189 * tachometer as well.
6190 *
Nick Andrew877d0312009-01-26 11:06:57 +01006191 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006192 * was never fixed by IBM to report the EC firmware version string
6193 * probably support the tachometer (like the early X models), so
6194 * detecting it is quite hard. We need more data to know for sure.
6195 *
6196 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6197 * might result.
6198 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006199 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6200 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006201 *
6202 * For firmware bugs, refer to:
6203 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6204 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006205 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006206 * ThinkPad X31, X40, X41. Not available in the X60.
6207 *
6208 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6209 * high speed. ACPI DSDT seems to map these three speeds to levels
6210 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6211 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6212 *
6213 * The speeds are stored on handles
6214 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6215 *
6216 * There are three default speed sets, acessible as handles:
6217 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6218 *
6219 * ACPI DSDT switches which set is in use depending on various
6220 * factors.
6221 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006222 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006223 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6224 * but the ACPI tables just mention level 7.
6225 */
6226
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006227enum { /* Fan control constants */
6228 fan_status_offset = 0x2f, /* EC register 0x2f */
6229 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6230 * 0x84 must be read before 0x85 */
6231
6232 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6233 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6234
6235 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6236};
6237
6238enum fan_status_access_mode {
6239 TPACPI_FAN_NONE = 0, /* No fan status or control */
6240 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6241 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6242};
6243
6244enum fan_control_access_mode {
6245 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6246 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6247 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6248 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6249};
6250
6251enum fan_control_commands {
6252 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6253 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6254 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6255 * and also watchdog cmd */
6256};
6257
6258static int fan_control_allowed;
6259
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006260static enum fan_status_access_mode fan_status_access_mode;
6261static enum fan_control_access_mode fan_control_access_mode;
6262static enum fan_control_commands fan_control_commands;
6263
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006264static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006265static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006266static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006267static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006268
6269static struct mutex fan_mutex;
6270
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006271static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006272static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006273
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006274TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6275TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006276 "\\FSPD", /* 600e/x, 770e, 770x */
6277 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006278TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006279 "JFNS", /* 770x-JL */
6280 ); /* all others */
6281
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006282/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006283 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6284 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6285 * be in auto mode (0x80).
6286 *
6287 * This is corrected by any write to HFSP either by the driver, or
6288 * by the firmware.
6289 *
6290 * We assume 0x07 really means auto mode while this quirk is active,
6291 * as this is far more likely than the ThinkPad being in level 7,
6292 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006293 *
6294 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6295 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006296 */
6297
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006298static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006299{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006300 if (fan_control_initial_status == 0x07) {
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006301 printk(TPACPI_NOTICE
6302 "fan_init: initial fan status is unknown, "
6303 "assuming it is in auto mode\n");
6304 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006305 }
6306}
6307
6308static void fan_quirk1_handle(u8 *fan_status)
6309{
6310 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6311 if (*fan_status != fan_control_initial_status) {
6312 /* something changed the HFSP regisnter since
6313 * driver init time, so it is not undefined
6314 * anymore */
6315 tp_features.fan_ctrl_status_undef = 0;
6316 } else {
6317 /* Return most likely status. In fact, it
6318 * might be the only possible status */
6319 *fan_status = TP_EC_FAN_AUTO;
6320 }
6321 }
6322}
6323
6324/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006325 * Call with fan_mutex held
6326 */
6327static void fan_update_desired_level(u8 status)
6328{
6329 if ((status &
6330 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6331 if (status > 7)
6332 fan_control_desired_level = 7;
6333 else
6334 fan_control_desired_level = status;
6335 }
6336}
6337
6338static int fan_get_status(u8 *status)
6339{
6340 u8 s;
6341
6342 /* TODO:
6343 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6344
6345 switch (fan_status_access_mode) {
6346 case TPACPI_FAN_RD_ACPI_GFAN:
6347 /* 570, 600e/x, 770e, 770x */
6348
6349 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6350 return -EIO;
6351
6352 if (likely(status))
6353 *status = s & 0x07;
6354
6355 break;
6356
6357 case TPACPI_FAN_RD_TPEC:
6358 /* all except 570, 600e/x, 770e, 770x */
6359 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6360 return -EIO;
6361
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006362 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006363 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006364 fan_quirk1_handle(status);
6365 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006366
6367 break;
6368
6369 default:
6370 return -ENXIO;
6371 }
6372
6373 return 0;
6374}
6375
6376static int fan_get_status_safe(u8 *status)
6377{
6378 int rc;
6379 u8 s;
6380
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006381 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006382 return -ERESTARTSYS;
6383 rc = fan_get_status(&s);
6384 if (!rc)
6385 fan_update_desired_level(s);
6386 mutex_unlock(&fan_mutex);
6387
6388 if (status)
6389 *status = s;
6390
6391 return rc;
6392}
6393
6394static int fan_get_speed(unsigned int *speed)
6395{
6396 u8 hi, lo;
6397
6398 switch (fan_status_access_mode) {
6399 case TPACPI_FAN_RD_TPEC:
6400 /* all except 570, 600e/x, 770e, 770x */
6401 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6402 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6403 return -EIO;
6404
6405 if (likely(speed))
6406 *speed = (hi << 8) | lo;
6407
6408 break;
6409
6410 default:
6411 return -ENXIO;
6412 }
6413
6414 return 0;
6415}
6416
6417static int fan_set_level(int level)
6418{
6419 if (!fan_control_allowed)
6420 return -EPERM;
6421
6422 switch (fan_control_access_mode) {
6423 case TPACPI_FAN_WR_ACPI_SFAN:
6424 if (level >= 0 && level <= 7) {
6425 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6426 return -EIO;
6427 } else
6428 return -EINVAL;
6429 break;
6430
6431 case TPACPI_FAN_WR_ACPI_FANS:
6432 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006433 if (!(level & TP_EC_FAN_AUTO) &&
6434 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006435 ((level < 0) || (level > 7)))
6436 return -EINVAL;
6437
6438 /* safety net should the EC not support AUTO
6439 * or FULLSPEED mode bits and just ignore them */
6440 if (level & TP_EC_FAN_FULLSPEED)
6441 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006442 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006443 level |= 4; /* safety min speed 4 */
6444
6445 if (!acpi_ec_write(fan_status_offset, level))
6446 return -EIO;
6447 else
6448 tp_features.fan_ctrl_status_undef = 0;
6449 break;
6450
6451 default:
6452 return -ENXIO;
6453 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006454
6455 vdbg_printk(TPACPI_DBG_FAN,
6456 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006457 return 0;
6458}
6459
6460static int fan_set_level_safe(int level)
6461{
6462 int rc;
6463
6464 if (!fan_control_allowed)
6465 return -EPERM;
6466
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006467 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006468 return -ERESTARTSYS;
6469
6470 if (level == TPACPI_FAN_LAST_LEVEL)
6471 level = fan_control_desired_level;
6472
6473 rc = fan_set_level(level);
6474 if (!rc)
6475 fan_update_desired_level(level);
6476
6477 mutex_unlock(&fan_mutex);
6478 return rc;
6479}
6480
6481static int fan_set_enable(void)
6482{
6483 u8 s;
6484 int rc;
6485
6486 if (!fan_control_allowed)
6487 return -EPERM;
6488
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006489 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006490 return -ERESTARTSYS;
6491
6492 switch (fan_control_access_mode) {
6493 case TPACPI_FAN_WR_ACPI_FANS:
6494 case TPACPI_FAN_WR_TPEC:
6495 rc = fan_get_status(&s);
6496 if (rc < 0)
6497 break;
6498
6499 /* Don't go out of emergency fan mode */
6500 if (s != 7) {
6501 s &= 0x07;
6502 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6503 }
6504
6505 if (!acpi_ec_write(fan_status_offset, s))
6506 rc = -EIO;
6507 else {
6508 tp_features.fan_ctrl_status_undef = 0;
6509 rc = 0;
6510 }
6511 break;
6512
6513 case TPACPI_FAN_WR_ACPI_SFAN:
6514 rc = fan_get_status(&s);
6515 if (rc < 0)
6516 break;
6517
6518 s &= 0x07;
6519
6520 /* Set fan to at least level 4 */
6521 s |= 4;
6522
6523 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006524 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006525 else
6526 rc = 0;
6527 break;
6528
6529 default:
6530 rc = -ENXIO;
6531 }
6532
6533 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006534
6535 if (!rc)
6536 vdbg_printk(TPACPI_DBG_FAN,
6537 "fan control: set fan control register to 0x%02x\n",
6538 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006539 return rc;
6540}
6541
6542static int fan_set_disable(void)
6543{
6544 int rc;
6545
6546 if (!fan_control_allowed)
6547 return -EPERM;
6548
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006549 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006550 return -ERESTARTSYS;
6551
6552 rc = 0;
6553 switch (fan_control_access_mode) {
6554 case TPACPI_FAN_WR_ACPI_FANS:
6555 case TPACPI_FAN_WR_TPEC:
6556 if (!acpi_ec_write(fan_status_offset, 0x00))
6557 rc = -EIO;
6558 else {
6559 fan_control_desired_level = 0;
6560 tp_features.fan_ctrl_status_undef = 0;
6561 }
6562 break;
6563
6564 case TPACPI_FAN_WR_ACPI_SFAN:
6565 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6566 rc = -EIO;
6567 else
6568 fan_control_desired_level = 0;
6569 break;
6570
6571 default:
6572 rc = -ENXIO;
6573 }
6574
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006575 if (!rc)
6576 vdbg_printk(TPACPI_DBG_FAN,
6577 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006578
6579 mutex_unlock(&fan_mutex);
6580 return rc;
6581}
6582
6583static int fan_set_speed(int speed)
6584{
6585 int rc;
6586
6587 if (!fan_control_allowed)
6588 return -EPERM;
6589
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006590 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006591 return -ERESTARTSYS;
6592
6593 rc = 0;
6594 switch (fan_control_access_mode) {
6595 case TPACPI_FAN_WR_ACPI_FANS:
6596 if (speed >= 0 && speed <= 65535) {
6597 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6598 speed, speed, speed))
6599 rc = -EIO;
6600 } else
6601 rc = -EINVAL;
6602 break;
6603
6604 default:
6605 rc = -ENXIO;
6606 }
6607
6608 mutex_unlock(&fan_mutex);
6609 return rc;
6610}
6611
6612static void fan_watchdog_reset(void)
6613{
6614 static int fan_watchdog_active;
6615
6616 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6617 return;
6618
6619 if (fan_watchdog_active)
6620 cancel_delayed_work(&fan_watchdog_task);
6621
6622 if (fan_watchdog_maxinterval > 0 &&
6623 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6624 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006625 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006626 msecs_to_jiffies(fan_watchdog_maxinterval
6627 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006628 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006629 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006630 "watchdog will not trigger\n");
6631 }
6632 } else
6633 fan_watchdog_active = 0;
6634}
6635
6636static void fan_watchdog_fire(struct work_struct *ignored)
6637{
6638 int rc;
6639
6640 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6641 return;
6642
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006643 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006644 rc = fan_set_enable();
6645 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006646 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006647 "will try again later...\n", -rc);
6648 /* reschedule for later */
6649 fan_watchdog_reset();
6650 }
6651}
6652
6653/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006654 * SYSFS fan layout: hwmon compatible (device)
6655 *
6656 * pwm*_enable:
6657 * 0: "disengaged" mode
6658 * 1: manual mode
6659 * 2: native EC "auto" mode (recommended, hardware default)
6660 *
6661 * pwm*: set speed in manual mode, ignored otherwise.
6662 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6663 * interpolation.
6664 *
6665 * fan*_input: tachometer reading, RPM
6666 *
6667 *
6668 * SYSFS fan layout: extensions
6669 *
6670 * fan_watchdog (driver):
6671 * fan watchdog interval in seconds, 0 disables (default), max 120
6672 */
6673
6674/* sysfs fan pwm1_enable ----------------------------------------------- */
6675static ssize_t fan_pwm1_enable_show(struct device *dev,
6676 struct device_attribute *attr,
6677 char *buf)
6678{
6679 int res, mode;
6680 u8 status;
6681
6682 res = fan_get_status_safe(&status);
6683 if (res)
6684 return res;
6685
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006686 if (status & TP_EC_FAN_FULLSPEED) {
6687 mode = 0;
6688 } else if (status & TP_EC_FAN_AUTO) {
6689 mode = 2;
6690 } else
6691 mode = 1;
6692
6693 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6694}
6695
6696static ssize_t fan_pwm1_enable_store(struct device *dev,
6697 struct device_attribute *attr,
6698 const char *buf, size_t count)
6699{
6700 unsigned long t;
6701 int res, level;
6702
6703 if (parse_strtoul(buf, 2, &t))
6704 return -EINVAL;
6705
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006706 tpacpi_disclose_usertask("hwmon pwm1_enable",
6707 "set fan mode to %lu\n", t);
6708
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006709 switch (t) {
6710 case 0:
6711 level = TP_EC_FAN_FULLSPEED;
6712 break;
6713 case 1:
6714 level = TPACPI_FAN_LAST_LEVEL;
6715 break;
6716 case 2:
6717 level = TP_EC_FAN_AUTO;
6718 break;
6719 case 3:
6720 /* reserved for software-controlled auto mode */
6721 return -ENOSYS;
6722 default:
6723 return -EINVAL;
6724 }
6725
6726 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006727 if (res == -ENXIO)
6728 return -EINVAL;
6729 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006730 return res;
6731
6732 fan_watchdog_reset();
6733
6734 return count;
6735}
6736
6737static struct device_attribute dev_attr_fan_pwm1_enable =
6738 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6739 fan_pwm1_enable_show, fan_pwm1_enable_store);
6740
6741/* sysfs fan pwm1 ------------------------------------------------------ */
6742static ssize_t fan_pwm1_show(struct device *dev,
6743 struct device_attribute *attr,
6744 char *buf)
6745{
6746 int res;
6747 u8 status;
6748
6749 res = fan_get_status_safe(&status);
6750 if (res)
6751 return res;
6752
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006753 if ((status &
6754 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6755 status = fan_control_desired_level;
6756
6757 if (status > 7)
6758 status = 7;
6759
6760 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6761}
6762
6763static ssize_t fan_pwm1_store(struct device *dev,
6764 struct device_attribute *attr,
6765 const char *buf, size_t count)
6766{
6767 unsigned long s;
6768 int rc;
6769 u8 status, newlevel;
6770
6771 if (parse_strtoul(buf, 255, &s))
6772 return -EINVAL;
6773
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006774 tpacpi_disclose_usertask("hwmon pwm1",
6775 "set fan speed to %lu\n", s);
6776
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006777 /* scale down from 0-255 to 0-7 */
6778 newlevel = (s >> 5) & 0x07;
6779
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006780 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006781 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006782
6783 rc = fan_get_status(&status);
6784 if (!rc && (status &
6785 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6786 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006787 if (rc == -ENXIO)
6788 rc = -EINVAL;
6789 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006790 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006791 fan_watchdog_reset();
6792 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006793 }
6794
6795 mutex_unlock(&fan_mutex);
6796 return (rc)? rc : count;
6797}
6798
6799static struct device_attribute dev_attr_fan_pwm1 =
6800 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6801 fan_pwm1_show, fan_pwm1_store);
6802
6803/* sysfs fan fan1_input ------------------------------------------------ */
6804static ssize_t fan_fan1_input_show(struct device *dev,
6805 struct device_attribute *attr,
6806 char *buf)
6807{
6808 int res;
6809 unsigned int speed;
6810
6811 res = fan_get_speed(&speed);
6812 if (res < 0)
6813 return res;
6814
6815 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6816}
6817
6818static struct device_attribute dev_attr_fan_fan1_input =
6819 __ATTR(fan1_input, S_IRUGO,
6820 fan_fan1_input_show, NULL);
6821
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006822/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006823static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6824 char *buf)
6825{
6826 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6827}
6828
6829static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6830 const char *buf, size_t count)
6831{
6832 unsigned long t;
6833
6834 if (parse_strtoul(buf, 120, &t))
6835 return -EINVAL;
6836
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006837 if (!fan_control_allowed)
6838 return -EPERM;
6839
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006840 fan_watchdog_maxinterval = t;
6841 fan_watchdog_reset();
6842
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006843 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6844
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006845 return count;
6846}
6847
6848static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6849 fan_fan_watchdog_show, fan_fan_watchdog_store);
6850
6851/* --------------------------------------------------------------------- */
6852static struct attribute *fan_attributes[] = {
6853 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6854 &dev_attr_fan_fan1_input.attr,
6855 NULL
6856};
6857
6858static const struct attribute_group fan_attr_group = {
6859 .attrs = fan_attributes,
6860};
6861
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006862#define TPACPI_FAN_Q1 0x0001
6863
6864#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6865 { .vendor = PCI_VENDOR_ID_IBM, \
6866 .bios = TPACPI_MATCH_ANY, \
6867 .ec = TPID(__id1, __id2), \
6868 .quirks = __quirks }
6869
6870static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6871 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6872 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6873 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6874 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6875};
6876
6877#undef TPACPI_FAN_QI
6878
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006879static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006880{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006881 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006882 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006883
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006884 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6885 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006886
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006887 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006888 fan_status_access_mode = TPACPI_FAN_NONE;
6889 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006890 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006891 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006892 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006893 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006894
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006895 TPACPI_ACPIHANDLE_INIT(fans);
6896 TPACPI_ACPIHANDLE_INIT(gfan);
6897 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006898
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006899 quirks = tpacpi_check_quirks(fan_quirk_table,
6900 ARRAY_SIZE(fan_quirk_table));
6901
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006902 if (gfan_handle) {
6903 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006904 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006905 } else {
6906 /* all other ThinkPads: note that even old-style
6907 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006908 if (likely(acpi_ec_read(fan_status_offset,
6909 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006910 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03006911 if (quirks & TPACPI_FAN_Q1)
6912 fan_quirk1_setup();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006913 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006914 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006915 "ThinkPad ACPI EC access misbehaving, "
6916 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006917 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006918 }
6919 }
6920
6921 if (sfan_handle) {
6922 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006923 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006924 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006925 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006926 } else {
6927 if (!gfan_handle) {
6928 /* gfan without sfan means no fan control */
6929 /* all other models implement TP EC 0x2f control */
6930
6931 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006932 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006933 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006934 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006935 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006936 TPACPI_FAN_CMD_SPEED |
6937 TPACPI_FAN_CMD_LEVEL |
6938 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006939 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006940 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006941 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006942 TPACPI_FAN_CMD_LEVEL |
6943 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006944 }
6945 }
6946 }
6947
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006948 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6949 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006950 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6951 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6952 fan_status_access_mode, fan_control_access_mode);
6953
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006954 /* fan control master switch */
6955 if (!fan_control_allowed) {
6956 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6957 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006959 "fan control features disabled by parameter\n");
6960 }
6961
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006962 /* update fan_control_desired_level */
6963 if (fan_status_access_mode != TPACPI_FAN_NONE)
6964 fan_get_status_safe(NULL);
6965
6966 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6967 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006968 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006969 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006970 if (rc < 0)
6971 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006972
6973 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6974 &driver_attr_fan_watchdog);
6975 if (rc < 0) {
6976 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6977 &fan_attr_group);
6978 return rc;
6979 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006980 return 0;
6981 } else
6982 return 1;
6983}
6984
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006985static void fan_exit(void)
6986{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006987 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006988 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006989
6990 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006991 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006992 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6993 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006994
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006995 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006996 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006997}
6998
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006999static void fan_suspend(pm_message_t state)
7000{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007001 int rc;
7002
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007003 if (!fan_control_allowed)
7004 return;
7005
7006 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007007 fan_control_resume_level = 0;
7008 rc = fan_get_status_safe(&fan_control_resume_level);
7009 if (rc < 0)
7010 printk(TPACPI_NOTICE
7011 "failed to read fan level for later "
7012 "restore during resume: %d\n", rc);
7013
7014 /* if it is undefined, don't attempt to restore it.
7015 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007016 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007017 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007018}
7019
7020static void fan_resume(void)
7021{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007022 u8 current_level = 7;
7023 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007024 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007025
7026 /* DSDT *always* updates status on resume */
7027 tp_features.fan_ctrl_status_undef = 0;
7028
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007029 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007030 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007031 (fan_get_status_safe(&current_level) < 0))
7032 return;
7033
7034 switch (fan_control_access_mode) {
7035 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007036 /* never decrease fan level */
7037 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007038 break;
7039 case TPACPI_FAN_WR_ACPI_FANS:
7040 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007041 /* never decrease fan level, scale is:
7042 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7043 *
7044 * We expect the firmware to set either 7 or AUTO, but we
7045 * handle FULLSPEED out of paranoia.
7046 *
7047 * So, we can safely only restore FULLSPEED or 7, anything
7048 * else could slow the fan. Restoring AUTO is useless, at
7049 * best that's exactly what the DSDT already set (it is the
7050 * slower it uses).
7051 *
7052 * Always keep in mind that the DSDT *will* have set the
7053 * fans to what the vendor supposes is the best level. We
7054 * muck with it only to speed the fan up.
7055 */
7056 if (fan_control_resume_level != 7 &&
7057 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7058 return;
7059 else
7060 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7061 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007062 break;
7063 default:
7064 return;
7065 }
7066 if (do_set) {
7067 printk(TPACPI_NOTICE
7068 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007069 fan_control_resume_level);
7070 rc = fan_set_level_safe(fan_control_resume_level);
7071 if (rc < 0)
7072 printk(TPACPI_NOTICE
7073 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007074 }
7075}
7076
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007077static int fan_read(char *p)
7078{
7079 int len = 0;
7080 int rc;
7081 u8 status;
7082 unsigned int speed = 0;
7083
7084 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007085 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007086 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007087 rc = fan_get_status_safe(&status);
7088 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007089 return rc;
7090
7091 len += sprintf(p + len, "status:\t\t%s\n"
7092 "level:\t\t%d\n",
7093 (status != 0) ? "enabled" : "disabled", status);
7094 break;
7095
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007096 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007097 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007098 rc = fan_get_status_safe(&status);
7099 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007100 return rc;
7101
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007102 len += sprintf(p + len, "status:\t\t%s\n",
7103 (status != 0) ? "enabled" : "disabled");
7104
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007105 rc = fan_get_speed(&speed);
7106 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007107 return rc;
7108
7109 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7110
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007111 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007112 /* Disengaged mode takes precedence */
7113 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007114 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007115 len += sprintf(p + len, "level:\t\tauto\n");
7116 else
7117 len += sprintf(p + len, "level:\t\t%d\n", status);
7118 break;
7119
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007120 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007121 default:
7122 len += sprintf(p + len, "status:\t\tnot supported\n");
7123 }
7124
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007125 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007126 len += sprintf(p + len, "commands:\tlevel <level>");
7127
7128 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007129 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007130 len += sprintf(p + len, " (<level> is 0-7)\n");
7131 break;
7132
7133 default:
7134 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007135 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007136 break;
7137 }
7138 }
7139
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007140 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007141 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007142 "commands:\twatchdog <timeout> (<timeout> "
7143 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007144
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007145 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007146 len += sprintf(p + len, "commands:\tspeed <speed>"
7147 " (<speed> is 0-65535)\n");
7148
7149 return len;
7150}
7151
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007152static int fan_write_cmd_level(const char *cmd, int *rc)
7153{
7154 int level;
7155
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007156 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007157 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007158 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007159 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007160 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007161 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007162 return 0;
7163
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007164 *rc = fan_set_level_safe(level);
7165 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007166 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007167 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007168 else if (!*rc)
7169 tpacpi_disclose_usertask("procfs fan",
7170 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007171
7172 return 1;
7173}
7174
7175static int fan_write_cmd_enable(const char *cmd, int *rc)
7176{
7177 if (strlencmp(cmd, "enable") != 0)
7178 return 0;
7179
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007180 *rc = fan_set_enable();
7181 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007182 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007183 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007184 else if (!*rc)
7185 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007186
7187 return 1;
7188}
7189
7190static int fan_write_cmd_disable(const char *cmd, int *rc)
7191{
7192 if (strlencmp(cmd, "disable") != 0)
7193 return 0;
7194
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007195 *rc = fan_set_disable();
7196 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007197 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007198 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007199 else if (!*rc)
7200 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007201
7202 return 1;
7203}
7204
7205static int fan_write_cmd_speed(const char *cmd, int *rc)
7206{
7207 int speed;
7208
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007209 /* TODO:
7210 * Support speed <low> <medium> <high> ? */
7211
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007212 if (sscanf(cmd, "speed %d", &speed) != 1)
7213 return 0;
7214
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007215 *rc = fan_set_speed(speed);
7216 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007217 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007218 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007219 else if (!*rc)
7220 tpacpi_disclose_usertask("procfs fan",
7221 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007222
7223 return 1;
7224}
7225
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007226static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7227{
7228 int interval;
7229
7230 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7231 return 0;
7232
7233 if (interval < 0 || interval > 120)
7234 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007235 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007236 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007237 tpacpi_disclose_usertask("procfs fan",
7238 "set watchdog timer to %d\n",
7239 interval);
7240 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007241
7242 return 1;
7243}
7244
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007245static int fan_write(char *buf)
7246{
7247 char *cmd;
7248 int rc = 0;
7249
7250 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007251 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007252 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007253 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007254 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007255 fan_write_cmd_disable(cmd, &rc) ||
7256 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007257 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007258 fan_write_cmd_speed(cmd, &rc))
7259 )
7260 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007261 else if (!rc)
7262 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007263 }
7264
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007265 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007266}
7267
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007268static struct ibm_struct fan_driver_data = {
7269 .name = "fan",
7270 .read = fan_read,
7271 .write = fan_write,
7272 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007273 .suspend = fan_suspend,
7274 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007275};
7276
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007277/****************************************************************************
7278 ****************************************************************************
7279 *
7280 * Infrastructure
7281 *
7282 ****************************************************************************
7283 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007285/* sysfs name ---------------------------------------------------------- */
7286static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7287 struct device_attribute *attr,
7288 char *buf)
7289{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007290 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007291}
7292
7293static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7294 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7295
7296/* --------------------------------------------------------------------- */
7297
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007298/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03007299static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007300
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007301/*
7302 * Module and infrastructure proble, init and exit handling
7303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007305static int force_load;
7306
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007307#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007308static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007309{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007310 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007312 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007313}
7314#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7315
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007316static void ibm_exit(struct ibm_struct *ibm)
7317{
7318 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7319
7320 list_del_init(&ibm->all_drivers);
7321
7322 if (ibm->flags.acpi_notify_installed) {
7323 dbg_printk(TPACPI_DBG_EXIT,
7324 "%s: acpi_remove_notify_handler\n", ibm->name);
7325 BUG_ON(!ibm->acpi);
7326 acpi_remove_notify_handler(*ibm->acpi->handle,
7327 ibm->acpi->type,
7328 dispatch_acpi_notify);
7329 ibm->flags.acpi_notify_installed = 0;
7330 ibm->flags.acpi_notify_installed = 0;
7331 }
7332
7333 if (ibm->flags.proc_created) {
7334 dbg_printk(TPACPI_DBG_EXIT,
7335 "%s: remove_proc_entry\n", ibm->name);
7336 remove_proc_entry(ibm->name, proc_dir);
7337 ibm->flags.proc_created = 0;
7338 }
7339
7340 if (ibm->flags.acpi_driver_registered) {
7341 dbg_printk(TPACPI_DBG_EXIT,
7342 "%s: acpi_bus_unregister_driver\n", ibm->name);
7343 BUG_ON(!ibm->acpi);
7344 acpi_bus_unregister_driver(ibm->acpi->driver);
7345 kfree(ibm->acpi->driver);
7346 ibm->acpi->driver = NULL;
7347 ibm->flags.acpi_driver_registered = 0;
7348 }
7349
7350 if (ibm->flags.init_called && ibm->exit) {
7351 ibm->exit();
7352 ibm->flags.init_called = 0;
7353 }
7354
7355 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7356}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007357
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007358static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359{
7360 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007361 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 struct proc_dir_entry *entry;
7363
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007364 BUG_ON(ibm == NULL);
7365
7366 INIT_LIST_HEAD(&ibm->all_drivers);
7367
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007368 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 return 0;
7370
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007371 dbg_printk(TPACPI_DBG_INIT,
7372 "probing for %s\n", ibm->name);
7373
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007374 if (iibm->init) {
7375 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007376 if (ret > 0)
7377 return 0; /* probe failed */
7378 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007380
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007381 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 }
7383
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007384 if (ibm->acpi) {
7385 if (ibm->acpi->hid) {
7386 ret = register_tpacpi_subdriver(ibm);
7387 if (ret)
7388 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007389 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007390
7391 if (ibm->acpi->notify) {
7392 ret = setup_acpi_notify(ibm);
7393 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007394 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007395 ibm->name);
7396 ret = 0;
7397 goto err_out;
7398 }
7399 if (ret < 0)
7400 goto err_out;
7401 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007402 }
7403
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007404 dbg_printk(TPACPI_DBG_INIT,
7405 "%s installed\n", ibm->name);
7406
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007407 if (ibm->read) {
7408 entry = create_proc_entry(ibm->name,
7409 S_IFREG | S_IRUGO | S_IWUSR,
7410 proc_dir);
7411 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007412 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007413 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007414 ret = -ENODEV;
7415 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007416 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007417 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007418 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007419 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007420 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007421 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007424 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007427
7428err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007429 dbg_printk(TPACPI_DBG_INIT,
7430 "%s: at error exit path with result %d\n",
7431 ibm->name, ret);
7432
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007433 ibm_exit(ibm);
7434 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435}
7436
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007437/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007439static bool __pure __init tpacpi_is_fw_digit(const char c)
7440{
7441 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7442}
7443
7444/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7445static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7446 const char t)
7447{
7448 return s && strlen(s) >= 8 &&
7449 tpacpi_is_fw_digit(s[0]) &&
7450 tpacpi_is_fw_digit(s[1]) &&
7451 s[2] == t && s[3] == 'T' &&
7452 tpacpi_is_fw_digit(s[4]) &&
7453 tpacpi_is_fw_digit(s[5]) &&
7454 s[6] == 'W' && s[7] == 'W';
7455}
7456
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007457/* returns 0 - probe ok, or < 0 - probe error.
7458 * Probe ok doesn't mean thinkpad found.
7459 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7460static int __must_check __init get_thinkpad_model_data(
7461 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007462{
Jeff Garzik18552562007-10-03 15:15:40 -04007463 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007464 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007465 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007466
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007467 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007468 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007469
7470 memset(tp, 0, sizeof(*tp));
7471
7472 if (dmi_name_in_vendors("IBM"))
7473 tp->vendor = PCI_VENDOR_ID_IBM;
7474 else if (dmi_name_in_vendors("LENOVO"))
7475 tp->vendor = PCI_VENDOR_ID_LENOVO;
7476 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007477 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007478
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007479 s = dmi_get_system_info(DMI_BIOS_VERSION);
7480 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7481 if (s && !tp->bios_version_str)
7482 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007483
7484 /* Really ancient ThinkPad 240X will fail this, which is fine */
7485 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007486 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007487
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007488 tp->bios_model = tp->bios_version_str[0]
7489 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007490 tp->bios_release = (tp->bios_version_str[4] << 8)
7491 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007492
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007493 /*
7494 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7495 * X32 or newer, all Z series; Some models must have an
7496 * up-to-date BIOS or they will not be detected.
7497 *
7498 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7499 */
7500 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007501 if (sscanf(dev->name,
7502 "IBM ThinkPad Embedded Controller -[%17c",
7503 ec_fw_string) == 1) {
7504 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7505 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007506
7507 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007508 if (!tp->ec_version_str)
7509 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03007510
7511 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7512 tp->ec_model = ec_fw_string[0]
7513 | (ec_fw_string[1] << 8);
7514 tp->ec_release = (ec_fw_string[4] << 8)
7515 | ec_fw_string[5];
7516 } else {
7517 printk(TPACPI_NOTICE
7518 "ThinkPad firmware release %s "
7519 "doesn't match the known patterns\n",
7520 ec_fw_string);
7521 printk(TPACPI_NOTICE
7522 "please report this to %s\n",
7523 TPACPI_MAIL);
7524 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007525 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007526 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007527 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007528
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007529 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7530 if (s && !strnicmp(s, "ThinkPad", 8)) {
7531 tp->model_str = kstrdup(s, GFP_KERNEL);
7532 if (!tp->model_str)
7533 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007534 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007535
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007536 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7537 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7538 if (s && !tp->nummodel_str)
7539 return -ENOMEM;
7540
7541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542}
7543
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007544static int __init probe_for_thinkpad(void)
7545{
7546 int is_thinkpad;
7547
7548 if (acpi_disabled)
7549 return -ENODEV;
7550
7551 /*
7552 * Non-ancient models have better DMI tagging, but very old models
7553 * don't.
7554 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007555 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007556
7557 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007558 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007559 if (!ec_handle) {
7560 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007561 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007562 "Not yet supported ThinkPad detected!\n");
7563 return -ENODEV;
7564 }
7565
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007566 /*
7567 * Risks a regression on very old machines, but reduces potential
7568 * false positives a damn great deal
7569 */
7570 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007571 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007572
7573 if (!is_thinkpad && !force_load)
7574 return -ENODEV;
7575
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007576 return 0;
7577}
7578
7579
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007580/* Module init, exit, parameters */
7581
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007582static struct ibm_init_struct ibms_init[] __initdata = {
7583 {
7584 .init = thinkpad_acpi_driver_init,
7585 .data = &thinkpad_acpi_driver_data,
7586 },
7587 {
7588 .init = hotkey_init,
7589 .data = &hotkey_driver_data,
7590 },
7591 {
7592 .init = bluetooth_init,
7593 .data = &bluetooth_driver_data,
7594 },
7595 {
7596 .init = wan_init,
7597 .data = &wan_driver_data,
7598 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007599 {
7600 .init = uwb_init,
7601 .data = &uwb_driver_data,
7602 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007603#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007604 {
7605 .init = video_init,
7606 .data = &video_driver_data,
7607 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007608#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007609 {
7610 .init = light_init,
7611 .data = &light_driver_data,
7612 },
7613#ifdef CONFIG_THINKPAD_ACPI_DOCK
7614 {
7615 .init = dock_init,
7616 .data = &dock_driver_data[0],
7617 },
7618 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007619 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007620 .data = &dock_driver_data[1],
7621 },
7622#endif
7623#ifdef CONFIG_THINKPAD_ACPI_BAY
7624 {
7625 .init = bay_init,
7626 .data = &bay_driver_data,
7627 },
7628#endif
7629 {
7630 .init = cmos_init,
7631 .data = &cmos_driver_data,
7632 },
7633 {
7634 .init = led_init,
7635 .data = &led_driver_data,
7636 },
7637 {
7638 .init = beep_init,
7639 .data = &beep_driver_data,
7640 },
7641 {
7642 .init = thermal_init,
7643 .data = &thermal_driver_data,
7644 },
7645 {
7646 .data = &ecdump_driver_data,
7647 },
7648 {
7649 .init = brightness_init,
7650 .data = &brightness_driver_data,
7651 },
7652 {
7653 .data = &volume_driver_data,
7654 },
7655 {
7656 .init = fan_init,
7657 .data = &fan_driver_data,
7658 },
7659};
7660
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007661static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7662{
7663 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007664 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007665
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007666 if (!kp || !kp->name || !val)
7667 return -EINVAL;
7668
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007669 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7670 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007671 WARN_ON(ibm == NULL);
7672
7673 if (!ibm || !ibm->name)
7674 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007675
7676 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7677 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007678 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007679 strcpy(ibms_init[i].param, val);
7680 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007681 return 0;
7682 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007683 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007684
7685 return -EINVAL;
7686}
7687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007688module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007689MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007690 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007691
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007692module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007693MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007694
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007695module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007696MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007697 "Attempts to load the driver even on a "
7698 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007699
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007700module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007701MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007702 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007703
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007704module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007705MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007706 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007707 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007708
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007709module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007710MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007711 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007712
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007713module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007714MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007715 "used for backwards compatibility with userspace, "
7716 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007717
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007718#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007719 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007720 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007721 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007722
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007723TPACPI_PARAM(hotkey);
7724TPACPI_PARAM(bluetooth);
7725TPACPI_PARAM(video);
7726TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007727#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007728TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007729#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007730#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007731TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007732#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007733TPACPI_PARAM(cmos);
7734TPACPI_PARAM(led);
7735TPACPI_PARAM(beep);
7736TPACPI_PARAM(ecdump);
7737TPACPI_PARAM(brightness);
7738TPACPI_PARAM(volume);
7739TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007740
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007741#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7742module_param(dbg_wlswemul, uint, 0);
7743MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7744module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7745MODULE_PARM_DESC(wlsw_state,
7746 "Initial state of the emulated WLSW switch");
7747
7748module_param(dbg_bluetoothemul, uint, 0);
7749MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7750module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7751MODULE_PARM_DESC(bluetooth_state,
7752 "Initial state of the emulated bluetooth switch");
7753
7754module_param(dbg_wwanemul, uint, 0);
7755MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7756module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7757MODULE_PARM_DESC(wwan_state,
7758 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007759
7760module_param(dbg_uwbemul, uint, 0);
7761MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7762module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7763MODULE_PARM_DESC(uwb_state,
7764 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007765#endif
7766
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007767static void thinkpad_acpi_module_exit(void)
7768{
7769 struct ibm_struct *ibm, *itmp;
7770
7771 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7772
7773 list_for_each_entry_safe_reverse(ibm, itmp,
7774 &tpacpi_all_drivers,
7775 all_drivers) {
7776 ibm_exit(ibm);
7777 }
7778
7779 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7780
7781 if (tpacpi_inputdev) {
7782 if (tp_features.input_device_registered)
7783 input_unregister_device(tpacpi_inputdev);
7784 else
7785 input_free_device(tpacpi_inputdev);
7786 }
7787
7788 if (tpacpi_hwmon)
7789 hwmon_device_unregister(tpacpi_hwmon);
7790
7791 if (tp_features.sensors_pdev_attrs_registered)
7792 device_remove_file(&tpacpi_sensors_pdev->dev,
7793 &dev_attr_thinkpad_acpi_pdev_name);
7794 if (tpacpi_sensors_pdev)
7795 platform_device_unregister(tpacpi_sensors_pdev);
7796 if (tpacpi_pdev)
7797 platform_device_unregister(tpacpi_pdev);
7798
7799 if (tp_features.sensors_pdrv_attrs_registered)
7800 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7801 if (tp_features.platform_drv_attrs_registered)
7802 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7803
7804 if (tp_features.sensors_pdrv_registered)
7805 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7806
7807 if (tp_features.platform_drv_registered)
7808 platform_driver_unregister(&tpacpi_pdriver);
7809
7810 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007811 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007812
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007813 if (tpacpi_wq)
7814 destroy_workqueue(tpacpi_wq);
7815
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007816 kfree(thinkpad_id.bios_version_str);
7817 kfree(thinkpad_id.ec_version_str);
7818 kfree(thinkpad_id.model_str);
7819}
7820
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007821
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007822static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823{
7824 int ret, i;
7825
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007826 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7827
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007828 /* Parameter checking */
7829 if (hotkey_report_mode > 2)
7830 return -EINVAL;
7831
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007832 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007833
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007834 ret = get_thinkpad_model_data(&thinkpad_id);
7835 if (ret) {
7836 printk(TPACPI_ERR
7837 "unable to get DMI data: %d\n", ret);
7838 thinkpad_acpi_module_exit();
7839 return ret;
7840 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007841 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007842 if (ret) {
7843 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007844 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007847 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007848
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007849 TPACPI_ACPIHANDLE_INIT(ecrd);
7850 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007852 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7853 if (!tpacpi_wq) {
7854 thinkpad_acpi_module_exit();
7855 return -ENOMEM;
7856 }
7857
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007858 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007860 printk(TPACPI_ERR
7861 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007862 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 return -ENODEV;
7864 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007865
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007866 ret = platform_driver_register(&tpacpi_pdriver);
7867 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007868 printk(TPACPI_ERR
7869 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007870 thinkpad_acpi_module_exit();
7871 return ret;
7872 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007873 tp_features.platform_drv_registered = 1;
7874
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007875 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7876 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007877 printk(TPACPI_ERR
7878 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007879 thinkpad_acpi_module_exit();
7880 return ret;
7881 }
7882 tp_features.sensors_pdrv_registered = 1;
7883
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007884 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007885 if (!ret) {
7886 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007887 ret = tpacpi_create_driver_attributes(
7888 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007889 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007890 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007891 printk(TPACPI_ERR
7892 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007893 thinkpad_acpi_module_exit();
7894 return ret;
7895 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007896 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007897
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007898
7899 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007900 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007901 NULL, 0);
7902 if (IS_ERR(tpacpi_pdev)) {
7903 ret = PTR_ERR(tpacpi_pdev);
7904 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007905 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007906 thinkpad_acpi_module_exit();
7907 return ret;
7908 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007909 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007910 TPACPI_HWMON_DRVR_NAME,
7911 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007912 if (IS_ERR(tpacpi_sensors_pdev)) {
7913 ret = PTR_ERR(tpacpi_sensors_pdev);
7914 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007915 printk(TPACPI_ERR
7916 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007917 thinkpad_acpi_module_exit();
7918 return ret;
7919 }
7920 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7921 &dev_attr_thinkpad_acpi_pdev_name);
7922 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007923 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007924 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007925 thinkpad_acpi_module_exit();
7926 return ret;
7927 }
7928 tp_features.sensors_pdev_attrs_registered = 1;
7929 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007930 if (IS_ERR(tpacpi_hwmon)) {
7931 ret = PTR_ERR(tpacpi_hwmon);
7932 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007933 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007934 thinkpad_acpi_module_exit();
7935 return ret;
7936 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007937 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007938 tpacpi_inputdev = input_allocate_device();
7939 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007940 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007941 thinkpad_acpi_module_exit();
7942 return -ENOMEM;
7943 } else {
7944 /* Prepare input device, but don't register */
7945 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007946 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007947 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007948 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7949 thinkpad_id.vendor :
7950 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007951 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7952 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7953 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007954 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7955 ret = ibm_init(&ibms_init[i]);
7956 if (ret >= 0 && *ibms_init[i].param)
7957 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007959 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960 return ret;
7961 }
7962 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007963 ret = input_register_device(tpacpi_inputdev);
7964 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007965 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007966 thinkpad_acpi_module_exit();
7967 return ret;
7968 } else {
7969 tp_features.input_device_registered = 1;
7970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007972 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 return 0;
7974}
7975
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007976MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7977
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007978/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007979 * This will autoload the driver in almost every ThinkPad
7980 * in widespread use.
7981 *
7982 * Only _VERY_ old models, like the 240, 240x and 570 lack
7983 * the HKEY event interface.
7984 */
7985MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7986
7987/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007988 * DMI matching for module autoloading
7989 *
7990 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7991 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7992 *
7993 * Only models listed in thinkwiki will be supported, so add yours
7994 * if it is not there yet.
7995 */
7996#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007997 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007998
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007999/* Ancient thinkpad BIOSes have to be identified by
8000 * BIOS type or model number, and there are far less
8001 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00008002IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008003
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00008004MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8005MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008006MODULE_DESCRIPTION(TPACPI_DESC);
8007MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008008MODULE_LICENSE("GPL");
8009
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008010module_init(thinkpad_acpi_module_init);
8011module_exit(thinkpad_acpi_module_exit);