blob: 40d64c03278ca4bd18db3cf8214e11a9412cc0e7 [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 Holschuh7ff8d622009-04-04 04:25:46 +0000169/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200170#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000171#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174#define TPACPI_ERR KERN_ERR TPACPI_LOG
175#define TPACPI_WARN KERN_WARNING TPACPI_LOG
176#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177#define TPACPI_INFO KERN_INFO TPACPI_LOG
178#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200179
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000180/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000182#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183#define TPACPI_DBG_INIT 0x0001
184#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000185#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000186#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000187#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000188#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200189
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200190#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200193
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200194
195/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200196 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197 */
198
199struct ibm_struct;
200
201struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
204
205 void (*notify) (struct ibm_struct *, u32);
206 acpi_handle *handle;
207 u32 type;
208 struct acpi_device *device;
209};
210
211struct ibm_struct {
212 char *name;
213
214 int (*read) (char *);
215 int (*write) (char *);
216 void (*exit) (void);
217 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200218 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200219 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200220
221 struct list_head all_drivers;
222
223 struct tp_acpi_drv_struct *acpi;
224
225 struct {
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
228 u8 proc_created:1;
229 u8 init_called:1;
230 u8 experimental:1;
231 } flags;
232};
233
234struct ibm_init_struct {
235 char param[32];
236
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
239};
240
241static struct {
242#ifdef CONFIG_THINKPAD_ACPI_BAY
243 u32 bay_status:1;
244 u32 bay_eject:1;
245 u32 bay_status2:1;
246 u32 bay_eject2:1;
247#endif
248 u32 bluetooth:1;
249 u32 hotkey:1;
250 u32 hotkey_mask:1;
251 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200252 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200253 u32 light:1;
254 u32 light_status:1;
255 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300256 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200258 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259 u32 fan_ctrl_status_undef:1;
260 u32 input_device_registered:1;
261 u32 platform_drv_registered:1;
262 u32 platform_drv_attrs_registered:1;
263 u32 sensors_pdrv_registered:1;
264 u32 sensors_pdrv_attrs_registered:1;
265 u32 sensors_pdev_attrs_registered:1;
266 u32 hotkey_poll_active:1;
267} tp_features;
268
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300269static struct {
270 u16 hotkey_mask_ff:1;
271} tp_warned;
272
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200273struct thinkpad_id_data {
274 unsigned int vendor; /* ThinkPad vendor:
275 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
276
277 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
278 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
279
280 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
281 u16 ec_model;
282
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300283 char *model_str; /* ThinkPad T43 */
284 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200285};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200286static struct thinkpad_id_data thinkpad_id;
287
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300288static enum {
289 TPACPI_LIFE_INIT = 0,
290 TPACPI_LIFE_RUNNING,
291 TPACPI_LIFE_EXITING,
292} tpacpi_lifecycle;
293
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200294static int experimental;
295static u32 dbg_level;
296
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300297static struct workqueue_struct *tpacpi_wq;
298
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000299enum led_status_t {
300 TPACPI_LED_OFF = 0,
301 TPACPI_LED_ON,
302 TPACPI_LED_BLINK,
303};
304
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300305/* Special LED class that can defer work */
306struct tpacpi_led_classdev {
307 struct led_classdev led_classdev;
308 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000309 enum led_status_t new_state;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300310 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300311};
312
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200313#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
314static int dbg_wlswemul;
315static int tpacpi_wlsw_emulstate;
316static int dbg_bluetoothemul;
317static int tpacpi_bluetooth_emulstate;
318static int dbg_wwanemul;
319static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200320static int dbg_uwbemul;
321static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200322#endif
323
324
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000325/*************************************************************************
326 * Debugging helpers
327 */
328
329#define dbg_printk(a_dbg_level, format, arg...) \
330 do { if (dbg_level & (a_dbg_level)) \
331 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
332 } while (0)
333
334#ifdef CONFIG_THINKPAD_ACPI_DEBUG
335#define vdbg_printk dbg_printk
336static const char *str_supported(int is_supported);
337#else
338#define vdbg_printk(a_dbg_level, format, arg...) \
339 do { } while (0)
340#endif
341
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000342static void tpacpi_log_usertask(const char * const what)
343{
344 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
345 what, task_tgid_vnr(current));
346}
347
348#define tpacpi_disclose_usertask(what, format, arg...) \
349 do { \
350 if (unlikely( \
351 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
352 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
353 printk(TPACPI_DEBUG "%s: PID %d: " format, \
354 what, task_tgid_vnr(current), ## arg); \
355 } \
356 } while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000357
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358/****************************************************************************
359 ****************************************************************************
360 *
361 * ACPI Helpers and device model
362 *
363 ****************************************************************************
364 ****************************************************************************/
365
366/*************************************************************************
367 * ACPI basic handles
368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300370static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200372#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 static acpi_handle object##_handle; \
374 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400375 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 static char *object##_paths[] = { paths }
377
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200378TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400379 "\\_SB.PCI.ISA.EC", /* 570 */
380 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
381 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
382 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
383 "\\_SB.PCI0.ICH3.EC0", /* R31 */
384 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300385 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200387TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
388TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200390TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
391 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400392 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
393 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300394 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200396TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400397 "^HKEY", /* R30, R31 */
398 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300399 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400400
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200401TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
403 "\\_SB.PCI0.VID0", /* 770e */
404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
405 "\\_SB.PCI0.AGP.VID", /* all others */
406 ); /* R30, R31 */
407
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300409/*************************************************************************
410 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200411 */
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static int acpi_evalf(acpi_handle handle,
414 void *res, char *method, char *fmt, ...)
415{
416 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400417 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200418 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400419 struct acpi_buffer result, *resultp;
420 union acpi_object out_obj;
421 acpi_status status;
422 va_list ap;
423 char res_type;
424 int success;
425 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200428 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430 }
431
432 if (*fmt == 'q') {
433 quiet = 1;
434 fmt++;
435 } else
436 quiet = 0;
437
438 res_type = *(fmt++);
439
440 params.count = 0;
441 params.pointer = &in_objs[0];
442
443 va_start(ap, fmt);
444 while (*fmt) {
445 char c = *(fmt++);
446 switch (c) {
447 case 'd': /* int */
448 in_objs[params.count].integer.value = va_arg(ap, int);
449 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
450 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400451 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200453 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 "with invalid format character '%c'\n", c);
455 return 0;
456 }
457 }
458 va_end(ap);
459
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460 if (res_type != 'v') {
461 result.length = sizeof(out_obj);
462 result.pointer = &out_obj;
463 resultp = &result;
464 } else
465 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400467 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400470 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (res)
472 *(int *)res = out_obj.integer.value;
473 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
474 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400475 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 success = status == AE_OK;
477 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400478 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200480 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 "with invalid format character '%c'\n", res_type);
482 return 0;
483 }
484
485 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200486 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 method, fmt0, status);
488
489 return success;
490}
491
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200492static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300493{
494 int v;
495
496 if (ecrd_handle) {
497 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
498 return 0;
499 *p = v;
500 } else {
501 if (ec_read(i, p) < 0)
502 return 0;
503 }
504
505 return 1;
506}
507
508static int acpi_ec_write(int i, u8 v)
509{
510 if (ecwr_handle) {
511 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
512 return 0;
513 } else {
514 if (ec_write(i, v) < 0)
515 return 0;
516 }
517
518 return 1;
519}
520
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200521#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300522static int _sta(acpi_handle handle)
523{
524 int status;
525
526 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
527 status = 0;
528
529 return status;
530}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200531#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300533static int issue_thinkpad_cmos_command(int cmos_cmd)
534{
535 if (!cmos_handle)
536 return -ENXIO;
537
538 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
539 return -EIO;
540
541 return 0;
542}
543
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300544/*************************************************************************
545 * ACPI device model
546 */
547
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200548#define TPACPI_ACPIHANDLE_INIT(object) \
549 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200550 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
551
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300552static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300553 acpi_handle *handle, acpi_handle parent,
554 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555{
556 int i;
557 acpi_status status;
558
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300559 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
560 name);
561
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562 for (i = 0; i < num_paths; i++) {
563 status = acpi_get_handle(parent, paths[i], handle);
564 if (ACPI_SUCCESS(status)) {
565 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300566 dbg_printk(TPACPI_DBG_INIT,
567 "Found ACPI handle %s for %s\n",
568 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300569 return;
570 }
571 }
572
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300573 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
574 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300575 *handle = NULL;
576}
577
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300578static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300579{
580 struct ibm_struct *ibm = data;
581
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300582 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
583 return;
584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300585 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586 return;
587
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300588 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300589}
590
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300592{
593 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300594 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300595
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300596 BUG_ON(!ibm->acpi);
597
598 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300599 return 0;
600
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300601 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300602 "setting up ACPI notify for %s\n", ibm->name);
603
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300604 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
605 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200606 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300607 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300608 return -ENODEV;
609 }
610
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700611 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300612 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200613 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300614 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300615
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300616 status = acpi_install_notify_handler(*ibm->acpi->handle,
617 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300618 if (ACPI_FAILURE(status)) {
619 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200620 printk(TPACPI_NOTICE
621 "another device driver is already "
622 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300623 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200624 printk(TPACPI_ERR
625 "acpi_install_notify_handler(%s) failed: %d\n",
626 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300627 }
628 return -ENODEV;
629 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300630 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300631 return 0;
632}
633
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300634static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300635{
636 return 0;
637}
638
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300639static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300640{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300641 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300643 dbg_printk(TPACPI_DBG_INIT,
644 "registering %s as an ACPI driver\n", ibm->name);
645
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300646 BUG_ON(!ibm->acpi);
647
648 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
649 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300650 printk(TPACPI_ERR
651 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300652 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300653 }
654
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200655 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300656 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200657
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300658 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300659
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300660 rc = acpi_bus_register_driver(ibm->acpi->driver);
661 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200662 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200663 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300664 kfree(ibm->acpi->driver);
665 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300666 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300667 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300669 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300670}
671
672
673/****************************************************************************
674 ****************************************************************************
675 *
676 * Procfs Helpers
677 *
678 ****************************************************************************
679 ****************************************************************************/
680
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300681static int dispatch_procfs_read(char *page, char **start, off_t off,
682 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300683{
684 struct ibm_struct *ibm = data;
685 int len;
686
687 if (!ibm || !ibm->read)
688 return -EINVAL;
689
690 len = ibm->read(page);
691 if (len < 0)
692 return len;
693
694 if (len <= off + count)
695 *eof = 1;
696 *start = page + off;
697 len -= off;
698 if (len > count)
699 len = count;
700 if (len < 0)
701 len = 0;
702
703 return len;
704}
705
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300706static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200707 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300708 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300709{
710 struct ibm_struct *ibm = data;
711 char *kernbuf;
712 int ret;
713
714 if (!ibm || !ibm->write)
715 return -EINVAL;
716
717 kernbuf = kmalloc(count + 2, GFP_KERNEL);
718 if (!kernbuf)
719 return -ENOMEM;
720
721 if (copy_from_user(kernbuf, userbuf, count)) {
722 kfree(kernbuf);
723 return -EFAULT;
724 }
725
726 kernbuf[count] = 0;
727 strcat(kernbuf, ",");
728 ret = ibm->write(kernbuf);
729 if (ret == 0)
730 ret = count;
731
732 kfree(kernbuf);
733
734 return ret;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static char *next_cmd(char **cmds)
738{
739 char *start = *cmds;
740 char *end;
741
742 while ((end = strchr(start, ',')) && end == start)
743 start = end + 1;
744
745 if (!end)
746 return NULL;
747
748 *end = 0;
749 *cmds = end + 1;
750 return start;
751}
752
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753
754/****************************************************************************
755 ****************************************************************************
756 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300757 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300758 *
759 ****************************************************************************
760 ****************************************************************************/
761
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300762static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300763static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700764static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300765static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300766static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200767static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300768
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200769static int tpacpi_suspend_handler(struct platform_device *pdev,
770 pm_message_t state)
771{
772 struct ibm_struct *ibm, *itmp;
773
774 list_for_each_entry_safe(ibm, itmp,
775 &tpacpi_all_drivers,
776 all_drivers) {
777 if (ibm->suspend)
778 (ibm->suspend)(state);
779 }
780
781 return 0;
782}
783
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300784static int tpacpi_resume_handler(struct platform_device *pdev)
785{
786 struct ibm_struct *ibm, *itmp;
787
788 list_for_each_entry_safe(ibm, itmp,
789 &tpacpi_all_drivers,
790 all_drivers) {
791 if (ibm->resume)
792 (ibm->resume)();
793 }
794
795 return 0;
796}
797
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200798static void tpacpi_shutdown_handler(struct platform_device *pdev)
799{
800 struct ibm_struct *ibm, *itmp;
801
802 list_for_each_entry_safe(ibm, itmp,
803 &tpacpi_all_drivers,
804 all_drivers) {
805 if (ibm->shutdown)
806 (ibm->shutdown)();
807 }
808}
809
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300810static struct platform_driver tpacpi_pdriver = {
811 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200812 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300813 .owner = THIS_MODULE,
814 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200815 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300816 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200817 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300818};
819
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300820static struct platform_driver tpacpi_hwmon_pdriver = {
821 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200822 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300823 .owner = THIS_MODULE,
824 },
825};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300826
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300827/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300828 * sysfs support helpers
829 */
830
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200831struct attribute_set {
832 unsigned int members, max_members;
833 struct attribute_group group;
834};
835
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300836struct attribute_set_obj {
837 struct attribute_set s;
838 struct attribute *a;
839} __attribute__((packed));
840
841static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200842 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300843{
844 struct attribute_set_obj *sobj;
845
846 if (max_members == 0)
847 return NULL;
848
849 /* Allocates space for implicit NULL at the end too */
850 sobj = kzalloc(sizeof(struct attribute_set_obj) +
851 max_members * sizeof(struct attribute *),
852 GFP_KERNEL);
853 if (!sobj)
854 return NULL;
855 sobj->s.max_members = max_members;
856 sobj->s.group.attrs = &sobj->a;
857 sobj->s.group.name = name;
858
859 return &sobj->s;
860}
861
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200862#define destroy_attr_set(_set) \
863 kfree(_set);
864
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300865/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200866static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300867{
868 if (!s || !attr)
869 return -EINVAL;
870
871 if (s->members >= s->max_members)
872 return -ENOMEM;
873
874 s->group.attrs[s->members] = attr;
875 s->members++;
876
877 return 0;
878}
879
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200880static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300881 struct attribute **attr,
882 unsigned int count)
883{
884 int i, res;
885
886 for (i = 0; i < count; i++) {
887 res = add_to_attr_set(s, attr[i]);
888 if (res)
889 return res;
890 }
891
892 return 0;
893}
894
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200895static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300896{
897 sysfs_remove_group(kobj, &s->group);
898 destroy_attr_set(s);
899}
900
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200901#define register_attr_set_with_sysfs(_attr_set, _kobj) \
902 sysfs_create_group(_kobj, &_attr_set->group)
903
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300904static int parse_strtoul(const char *buf,
905 unsigned long max, unsigned long *value)
906{
907 char *endp;
908
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300909 while (*buf && isspace(*buf))
910 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300911 *value = simple_strtoul(buf, &endp, 0);
912 while (*endp && isspace(*endp))
913 endp++;
914 if (*endp || *value > max)
915 return -EINVAL;
916
917 return 0;
918}
919
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300920static void tpacpi_disable_brightness_delay(void)
921{
922 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
923 printk(TPACPI_NOTICE
924 "ACPI backlight control delay disabled\n");
925}
926
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300927static int __init tpacpi_query_bcl_levels(acpi_handle handle)
928{
929 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
930 union acpi_object *obj;
931 int rc;
932
933 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
934 obj = (union acpi_object *)buffer.pointer;
935 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
936 printk(TPACPI_ERR "Unknown _BCL data, "
937 "please report this to %s\n", TPACPI_MAIL);
938 rc = 0;
939 } else {
940 rc = obj->package.count;
941 }
942 } else {
943 return 0;
944 }
945
946 kfree(buffer.pointer);
947 return rc;
948}
949
950static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
951 u32 lvl, void *context, void **rv)
952{
953 char name[ACPI_PATH_SEGMENT_LENGTH];
954 struct acpi_buffer buffer = { sizeof(name), &name };
955
956 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
957 !strncmp("_BCL", name, sizeof(name) - 1)) {
958 BUG_ON(!rv || !*rv);
959 **(int **)rv = tpacpi_query_bcl_levels(handle);
960 return AE_CTRL_TERMINATE;
961 } else {
962 return AE_OK;
963 }
964}
965
966/*
967 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
968 */
969static int __init tpacpi_check_std_acpi_brightness_support(void)
970{
971 int status;
972 int bcl_levels = 0;
973 void *bcl_ptr = &bcl_levels;
974
975 if (!vid_handle) {
976 TPACPI_ACPIHANDLE_INIT(vid);
977 }
978 if (!vid_handle)
979 return 0;
980
981 /*
982 * Search for a _BCL method, and execute it. This is safe on all
983 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
984 * BIOS in ACPI backlight control mode. We do NOT have to care
985 * about calling the _BCL method in an enabled video device, any
986 * will do for our purposes.
987 */
988
989 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
990 tpacpi_acpi_walk_find_bcl, NULL,
991 &bcl_ptr);
992
993 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
994 tp_features.bright_acpimode = 1;
995 return (bcl_levels - 2);
996 }
997
998 return 0;
999}
1000
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001001static void printk_deprecated_attribute(const char * const what,
1002 const char * const details)
1003{
1004 tpacpi_log_usertask("deprecated sysfs attribute");
1005 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1006 "will be removed. %s\n",
1007 what, details);
1008}
1009
Johannes Berg19d337d2009-06-02 13:01:37 +02001010/*************************************************************************
1011 * rfkill and radio control support helpers
1012 */
1013
1014/*
1015 * ThinkPad-ACPI firmware handling model:
1016 *
1017 * WLSW (master wireless switch) is event-driven, and is common to all
1018 * firmware-controlled radios. It cannot be controlled, just monitored,
1019 * as expected. It overrides all radio state in firmware
1020 *
1021 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1022 * (TODO: verify how WLSW interacts with the returned radio state).
1023 *
1024 * The only time there are shadow radio state changes, is when
1025 * masked-off hotkeys are used.
1026 */
1027
1028/*
1029 * Internal driver API for radio state:
1030 *
1031 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1032 * bool: true means radio blocked (off)
1033 */
1034enum tpacpi_rfkill_state {
1035 TPACPI_RFK_RADIO_OFF = 0,
1036 TPACPI_RFK_RADIO_ON
1037};
1038
1039/* rfkill switches */
1040enum tpacpi_rfk_id {
1041 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1042 TPACPI_RFK_WWAN_SW_ID,
1043 TPACPI_RFK_UWB_SW_ID,
1044 TPACPI_RFK_SW_MAX
1045};
1046
1047static const char *tpacpi_rfkill_names[] = {
1048 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1049 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1050 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1051 [TPACPI_RFK_SW_MAX] = NULL
1052};
1053
1054/* ThinkPad-ACPI rfkill subdriver */
1055struct tpacpi_rfk {
1056 struct rfkill *rfkill;
1057 enum tpacpi_rfk_id id;
1058 const struct tpacpi_rfk_ops *ops;
1059};
1060
1061struct tpacpi_rfk_ops {
1062 /* firmware interface */
1063 int (*get_status)(void);
1064 int (*set_status)(const enum tpacpi_rfkill_state);
1065};
1066
1067static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1068
1069/* Query FW and update rfkill sw state for a given rfkill switch */
1070static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1071{
1072 int status;
1073
1074 if (!tp_rfk)
1075 return -ENODEV;
1076
1077 status = (tp_rfk->ops->get_status)();
1078 if (status < 0)
1079 return status;
1080
1081 rfkill_set_sw_state(tp_rfk->rfkill,
1082 (status == TPACPI_RFK_RADIO_OFF));
1083
1084 return status;
1085}
1086
1087/* Query FW and update rfkill sw state for all rfkill switches */
1088static void tpacpi_rfk_update_swstate_all(void)
1089{
1090 unsigned int i;
1091
1092 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1093 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1094}
1095
1096/*
1097 * Sync the HW-blocking state of all rfkill switches,
1098 * do notice it causes the rfkill core to schedule uevents
1099 */
1100static void tpacpi_rfk_update_hwblock_state(bool blocked)
1101{
1102 unsigned int i;
1103 struct tpacpi_rfk *tp_rfk;
1104
1105 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1106 tp_rfk = tpacpi_rfkill_switches[i];
1107 if (tp_rfk) {
1108 if (rfkill_set_hw_state(tp_rfk->rfkill,
1109 blocked)) {
1110 /* ignore -- we track sw block */
1111 }
1112 }
1113 }
1114}
1115
1116/* Call to get the WLSW state from the firmware */
1117static int hotkey_get_wlsw(void);
1118
1119/* Call to query WLSW state and update all rfkill switches */
1120static bool tpacpi_rfk_check_hwblock_state(void)
1121{
1122 int res = hotkey_get_wlsw();
1123 int hw_blocked;
1124
1125 /* When unknown or unsupported, we have to assume it is unblocked */
1126 if (res < 0)
1127 return false;
1128
1129 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1130 tpacpi_rfk_update_hwblock_state(hw_blocked);
1131
1132 return hw_blocked;
1133}
1134
1135static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1136{
1137 struct tpacpi_rfk *tp_rfk = data;
1138 int res;
1139
1140 dbg_printk(TPACPI_DBG_RFKILL,
1141 "request to change radio state to %s\n",
1142 blocked ? "blocked" : "unblocked");
1143
1144 /* try to set radio state */
1145 res = (tp_rfk->ops->set_status)(blocked ?
1146 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1147
1148 /* and update the rfkill core with whatever the FW really did */
1149 tpacpi_rfk_update_swstate(tp_rfk);
1150
1151 return (res < 0) ? res : 0;
1152}
1153
1154static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1155 .set_block = tpacpi_rfk_hook_set_block,
1156};
1157
1158static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1159 const struct tpacpi_rfk_ops *tp_rfkops,
1160 const enum rfkill_type rfktype,
1161 const char *name,
1162 const bool set_default)
1163{
1164 struct tpacpi_rfk *atp_rfk;
1165 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001166 bool sw_state = false;
1167 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001168
1169 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1170
Johannes Berg19d337d2009-06-02 13:01:37 +02001171 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1172 if (atp_rfk)
1173 atp_rfk->rfkill = rfkill_alloc(name,
1174 &tpacpi_pdev->dev,
1175 rfktype,
1176 &tpacpi_rfk_rfkill_ops,
1177 atp_rfk);
1178 if (!atp_rfk || !atp_rfk->rfkill) {
1179 printk(TPACPI_ERR
1180 "failed to allocate memory for rfkill class\n");
1181 kfree(atp_rfk);
1182 return -ENOMEM;
1183 }
1184
1185 atp_rfk->id = id;
1186 atp_rfk->ops = tp_rfkops;
1187
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001188 sw_status = (tp_rfkops->get_status)();
1189 if (sw_status < 0) {
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001190 printk(TPACPI_ERR
1191 "failed to read initial state for %s, error %d\n",
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001192 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001193 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001194 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001195 if (set_default) {
1196 /* try to keep the initial state, since we ask the
1197 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001198 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001199 }
1200 }
1201 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
Johannes Berg19d337d2009-06-02 13:01:37 +02001202
1203 res = rfkill_register(atp_rfk->rfkill);
1204 if (res < 0) {
1205 printk(TPACPI_ERR
1206 "failed to register %s rfkill switch: %d\n",
1207 name, res);
1208 rfkill_destroy(atp_rfk->rfkill);
1209 kfree(atp_rfk);
1210 return res;
1211 }
1212
1213 tpacpi_rfkill_switches[id] = atp_rfk;
1214 return 0;
1215}
1216
1217static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1218{
1219 struct tpacpi_rfk *tp_rfk;
1220
1221 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1222
1223 tp_rfk = tpacpi_rfkill_switches[id];
1224 if (tp_rfk) {
1225 rfkill_unregister(tp_rfk->rfkill);
1226 tpacpi_rfkill_switches[id] = NULL;
1227 kfree(tp_rfk);
1228 }
1229}
1230
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001231static void printk_deprecated_rfkill_attribute(const char * const what)
1232{
1233 printk_deprecated_attribute(what,
1234 "Please switch to generic rfkill before year 2010");
1235}
1236
Johannes Berg19d337d2009-06-02 13:01:37 +02001237/* sysfs <radio> enable ------------------------------------------------ */
1238static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1239 struct device_attribute *attr,
1240 char *buf)
1241{
1242 int status;
1243
1244 printk_deprecated_rfkill_attribute(attr->attr.name);
1245
1246 /* This is in the ABI... */
1247 if (tpacpi_rfk_check_hwblock_state()) {
1248 status = TPACPI_RFK_RADIO_OFF;
1249 } else {
1250 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1251 if (status < 0)
1252 return status;
1253 }
1254
1255 return snprintf(buf, PAGE_SIZE, "%d\n",
1256 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1257}
1258
1259static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1262{
1263 unsigned long t;
1264 int res;
1265
1266 printk_deprecated_rfkill_attribute(attr->attr.name);
1267
1268 if (parse_strtoul(buf, 1, &t))
1269 return -EINVAL;
1270
1271 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1272
1273 /* This is in the ABI... */
1274 if (tpacpi_rfk_check_hwblock_state() && !!t)
1275 return -EPERM;
1276
1277 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1278 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1279 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1280
1281 return (res < 0) ? res : count;
1282}
1283
1284/* procfs -------------------------------------------------------------- */
1285static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1286{
1287 int len = 0;
1288
1289 if (id >= TPACPI_RFK_SW_MAX)
1290 len += sprintf(p + len, "status:\t\tnot supported\n");
1291 else {
1292 int status;
1293
1294 /* This is in the ABI... */
1295 if (tpacpi_rfk_check_hwblock_state()) {
1296 status = TPACPI_RFK_RADIO_OFF;
1297 } else {
1298 status = tpacpi_rfk_update_swstate(
1299 tpacpi_rfkill_switches[id]);
1300 if (status < 0)
1301 return status;
1302 }
1303
1304 len += sprintf(p + len, "status:\t\t%s\n",
1305 (status == TPACPI_RFK_RADIO_ON) ?
1306 "enabled" : "disabled");
1307 len += sprintf(p + len, "commands:\tenable, disable\n");
1308 }
1309
1310 return len;
1311}
1312
1313static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1314{
1315 char *cmd;
1316 int status = -1;
1317 int res = 0;
1318
1319 if (id >= TPACPI_RFK_SW_MAX)
1320 return -ENODEV;
1321
1322 while ((cmd = next_cmd(&buf))) {
1323 if (strlencmp(cmd, "enable") == 0)
1324 status = TPACPI_RFK_RADIO_ON;
1325 else if (strlencmp(cmd, "disable") == 0)
1326 status = TPACPI_RFK_RADIO_OFF;
1327 else
1328 return -EINVAL;
1329 }
1330
1331 if (status != -1) {
1332 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1333 (status == TPACPI_RFK_RADIO_ON) ?
1334 "enable" : "disable",
1335 tpacpi_rfkill_names[id]);
1336 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1337 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1338 }
1339
1340 return res;
1341}
1342
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001343/*************************************************************************
1344 * thinkpad-acpi driver attributes
1345 */
1346
1347/* interface_version --------------------------------------------------- */
1348static ssize_t tpacpi_driver_interface_version_show(
1349 struct device_driver *drv,
1350 char *buf)
1351{
1352 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1353}
1354
1355static DRIVER_ATTR(interface_version, S_IRUGO,
1356 tpacpi_driver_interface_version_show, NULL);
1357
1358/* debug_level --------------------------------------------------------- */
1359static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1360 char *buf)
1361{
1362 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1363}
1364
1365static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1366 const char *buf, size_t count)
1367{
1368 unsigned long t;
1369
1370 if (parse_strtoul(buf, 0xffff, &t))
1371 return -EINVAL;
1372
1373 dbg_level = t;
1374
1375 return count;
1376}
1377
1378static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1379 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1380
1381/* version ------------------------------------------------------------- */
1382static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1383 char *buf)
1384{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001385 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1386 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001387}
1388
1389static DRIVER_ATTR(version, S_IRUGO,
1390 tpacpi_driver_version_show, NULL);
1391
1392/* --------------------------------------------------------------------- */
1393
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001394#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1395
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001396/* wlsw_emulstate ------------------------------------------------------ */
1397static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1398 char *buf)
1399{
1400 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1401}
1402
1403static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1404 const char *buf, size_t count)
1405{
1406 unsigned long t;
1407
1408 if (parse_strtoul(buf, 1, &t))
1409 return -EINVAL;
1410
Johannes Berg19d337d2009-06-02 13:01:37 +02001411 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001412 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001413 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1414 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001415
1416 return count;
1417}
1418
1419static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1420 tpacpi_driver_wlsw_emulstate_show,
1421 tpacpi_driver_wlsw_emulstate_store);
1422
1423/* bluetooth_emulstate ------------------------------------------------- */
1424static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1425 struct device_driver *drv,
1426 char *buf)
1427{
1428 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1429}
1430
1431static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1432 struct device_driver *drv,
1433 const char *buf, size_t count)
1434{
1435 unsigned long t;
1436
1437 if (parse_strtoul(buf, 1, &t))
1438 return -EINVAL;
1439
1440 tpacpi_bluetooth_emulstate = !!t;
1441
1442 return count;
1443}
1444
1445static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1446 tpacpi_driver_bluetooth_emulstate_show,
1447 tpacpi_driver_bluetooth_emulstate_store);
1448
1449/* wwan_emulstate ------------------------------------------------- */
1450static ssize_t tpacpi_driver_wwan_emulstate_show(
1451 struct device_driver *drv,
1452 char *buf)
1453{
1454 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1455}
1456
1457static ssize_t tpacpi_driver_wwan_emulstate_store(
1458 struct device_driver *drv,
1459 const char *buf, size_t count)
1460{
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 1, &t))
1464 return -EINVAL;
1465
1466 tpacpi_wwan_emulstate = !!t;
1467
1468 return count;
1469}
1470
1471static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_wwan_emulstate_show,
1473 tpacpi_driver_wwan_emulstate_store);
1474
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001475/* uwb_emulstate ------------------------------------------------- */
1476static ssize_t tpacpi_driver_uwb_emulstate_show(
1477 struct device_driver *drv,
1478 char *buf)
1479{
1480 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1481}
1482
1483static ssize_t tpacpi_driver_uwb_emulstate_store(
1484 struct device_driver *drv,
1485 const char *buf, size_t count)
1486{
1487 unsigned long t;
1488
1489 if (parse_strtoul(buf, 1, &t))
1490 return -EINVAL;
1491
1492 tpacpi_uwb_emulstate = !!t;
1493
1494 return count;
1495}
1496
1497static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1498 tpacpi_driver_uwb_emulstate_show,
1499 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001500#endif
1501
1502/* --------------------------------------------------------------------- */
1503
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001504static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001505 &driver_attr_debug_level, &driver_attr_version,
1506 &driver_attr_interface_version,
1507};
1508
1509static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1510{
1511 int i, res;
1512
1513 i = 0;
1514 res = 0;
1515 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1516 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1517 i++;
1518 }
1519
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001520#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1521 if (!res && dbg_wlswemul)
1522 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1523 if (!res && dbg_bluetoothemul)
1524 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1525 if (!res && dbg_wwanemul)
1526 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001527 if (!res && dbg_uwbemul)
1528 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001529#endif
1530
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001531 return res;
1532}
1533
1534static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1535{
1536 int i;
1537
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001538 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001539 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001540
1541#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1542 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1543 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1544 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001545 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001546#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001547}
1548
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001549/****************************************************************************
1550 ****************************************************************************
1551 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001552 * Subdrivers
1553 *
1554 ****************************************************************************
1555 ****************************************************************************/
1556
1557/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001558 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001559 */
1560
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001561static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001563 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1564 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001566 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001567 (thinkpad_id.bios_version_str) ?
1568 thinkpad_id.bios_version_str : "unknown",
1569 (thinkpad_id.ec_version_str) ?
1570 thinkpad_id.ec_version_str : "unknown");
1571
1572 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001573 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001574 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1575 "IBM" : ((thinkpad_id.vendor ==
1576 PCI_VENDOR_ID_LENOVO) ?
1577 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001578 thinkpad_id.model_str,
1579 (thinkpad_id.nummodel_str) ?
1580 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
1583}
1584
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001585static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 int len = 0;
1588
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001589 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1590 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 return len;
1593}
1594
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001595static struct ibm_struct thinkpad_acpi_driver_data = {
1596 .name = "driver",
1597 .read = thinkpad_acpi_driver_read,
1598};
1599
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001600/*************************************************************************
1601 * Hotkey subdriver
1602 */
1603
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001604enum { /* hot key scan codes (derived from ACPI DSDT) */
1605 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1606 TP_ACPI_HOTKEYSCAN_FNF2,
1607 TP_ACPI_HOTKEYSCAN_FNF3,
1608 TP_ACPI_HOTKEYSCAN_FNF4,
1609 TP_ACPI_HOTKEYSCAN_FNF5,
1610 TP_ACPI_HOTKEYSCAN_FNF6,
1611 TP_ACPI_HOTKEYSCAN_FNF7,
1612 TP_ACPI_HOTKEYSCAN_FNF8,
1613 TP_ACPI_HOTKEYSCAN_FNF9,
1614 TP_ACPI_HOTKEYSCAN_FNF10,
1615 TP_ACPI_HOTKEYSCAN_FNF11,
1616 TP_ACPI_HOTKEYSCAN_FNF12,
1617 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1618 TP_ACPI_HOTKEYSCAN_FNINSERT,
1619 TP_ACPI_HOTKEYSCAN_FNDELETE,
1620 TP_ACPI_HOTKEYSCAN_FNHOME,
1621 TP_ACPI_HOTKEYSCAN_FNEND,
1622 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1623 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1624 TP_ACPI_HOTKEYSCAN_FNSPACE,
1625 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1626 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1627 TP_ACPI_HOTKEYSCAN_MUTE,
1628 TP_ACPI_HOTKEYSCAN_THINKPAD,
1629};
1630
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001631enum { /* Keys available through NVRAM polling */
1632 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1633 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1634};
1635
1636enum { /* Positions of some of the keys in hotkey masks */
1637 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1638 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1639 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1640 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1641 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1642 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1643 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1644 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1645 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1646 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1647 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1648};
1649
1650enum { /* NVRAM to ACPI HKEY group map */
1651 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1652 TP_ACPI_HKEY_ZOOM_MASK |
1653 TP_ACPI_HKEY_DISPSWTCH_MASK |
1654 TP_ACPI_HKEY_HIBERNATE_MASK,
1655 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1656 TP_ACPI_HKEY_BRGHTDWN_MASK,
1657 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1658 TP_ACPI_HKEY_VOLDWN_MASK |
1659 TP_ACPI_HKEY_MUTE_MASK,
1660};
1661
1662#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1663struct tp_nvram_state {
1664 u16 thinkpad_toggle:1;
1665 u16 zoom_toggle:1;
1666 u16 display_toggle:1;
1667 u16 thinklight_toggle:1;
1668 u16 hibernate_toggle:1;
1669 u16 displayexp_toggle:1;
1670 u16 display_state:1;
1671 u16 brightness_toggle:1;
1672 u16 volume_toggle:1;
1673 u16 mute:1;
1674
1675 u8 brightness_level;
1676 u8 volume_level;
1677};
1678
1679static struct task_struct *tpacpi_hotkey_task;
1680static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1681static int hotkey_poll_freq = 10; /* Hz */
1682static struct mutex hotkey_thread_mutex;
1683static struct mutex hotkey_thread_data_mutex;
1684static unsigned int hotkey_config_change;
1685
1686#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1687
1688#define hotkey_source_mask 0U
1689
1690#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1691
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001692static struct mutex hotkey_mutex;
1693
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001694static enum { /* Reasons for waking up */
1695 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1696 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1697 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1698} hotkey_wakeup_reason;
1699
1700static int hotkey_autosleep_ack;
1701
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001702static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001703static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001704static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001705static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001706
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001707static unsigned int hotkey_report_mode;
1708
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001709static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001710
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001711static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001712
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001713#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1714#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001715 do { \
1716 mutex_lock(&hotkey_thread_data_mutex); \
1717 hotkey_config_change++; \
1718 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001719#define HOTKEY_CONFIG_CRITICAL_END \
1720 mutex_unlock(&hotkey_thread_data_mutex);
1721#else
1722#define HOTKEY_CONFIG_CRITICAL_START
1723#define HOTKEY_CONFIG_CRITICAL_END
1724#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1725
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001726/* HKEY.MHKG() return bits */
1727#define TP_HOTKEY_TABLET_MASK (1 << 3)
1728
Johannes Berg19d337d2009-06-02 13:01:37 +02001729static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001730{
Johannes Berg19d337d2009-06-02 13:01:37 +02001731 int status;
1732
1733 if (!tp_features.hotkey_wlsw)
1734 return -ENODEV;
1735
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001736#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02001737 if (dbg_wlswemul)
1738 return (tpacpi_wlsw_emulstate) ?
1739 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001740#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02001741
1742 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001743 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001744
1745 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001746}
1747
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001748static int hotkey_get_tablet_mode(int *status)
1749{
1750 int s;
1751
1752 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1753 return -EIO;
1754
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001755 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1756 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001757}
1758
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001759/*
1760 * Call with hotkey_mutex held
1761 */
1762static int hotkey_mask_get(void)
1763{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001764 u32 m = 0;
1765
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001766 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001767 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001768 return -EIO;
1769 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001770 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001771
1772 return 0;
1773}
1774
1775/*
1776 * Call with hotkey_mutex held
1777 */
1778static int hotkey_mask_set(u32 mask)
1779{
1780 int i;
1781 int rc = 0;
1782
1783 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001784 if (!tp_warned.hotkey_mask_ff &&
1785 (mask == 0xffff || mask == 0xffffff ||
1786 mask == 0xffffffff)) {
1787 tp_warned.hotkey_mask_ff = 1;
1788 printk(TPACPI_NOTICE
1789 "setting the hotkey mask to 0x%08x is likely "
1790 "not the best way to go about it\n", mask);
1791 printk(TPACPI_NOTICE
1792 "please consider using the driver defaults, "
1793 "and refer to up-to-date thinkpad-acpi "
1794 "documentation\n");
1795 }
1796
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001797 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001798 for (i = 0; i < 32; i++) {
1799 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001800 /* enable in firmware mask only keys not in NVRAM
1801 * mode, but enable the key in the cached hotkey_mask
1802 * regardless of mode, or the key will end up
1803 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001804 if (!acpi_evalf(hkey_handle,
1805 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001806 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001807 rc = -EIO;
1808 break;
1809 } else {
1810 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1811 }
1812 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001813 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001814
1815 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001816 if (!hotkey_mask_get() && !rc &&
1817 (hotkey_mask & ~hotkey_source_mask) !=
1818 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001819 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001820 "requested hot key mask 0x%08x, but "
1821 "firmware forced it to 0x%08x\n",
1822 mask, hotkey_mask);
1823 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001824 } else {
1825#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1826 HOTKEY_CONFIG_CRITICAL_START
1827 hotkey_mask = mask & hotkey_source_mask;
1828 HOTKEY_CONFIG_CRITICAL_END
1829 hotkey_mask_get();
1830 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001831 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001832 "requested hot key mask 0x%08x, "
1833 "forced to 0x%08x (NVRAM poll mask is "
1834 "0x%08x): no firmware mask support\n",
1835 mask, hotkey_mask, hotkey_source_mask);
1836 }
1837#else
1838 hotkey_mask_get();
1839 rc = -ENXIO;
1840#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001841 }
1842
1843 return rc;
1844}
1845
1846static int hotkey_status_get(int *status)
1847{
1848 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1849 return -EIO;
1850
1851 return 0;
1852}
1853
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001854static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001855{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001856 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001857 return -EIO;
1858
1859 return 0;
1860}
1861
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001862static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001863{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001864 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001865
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001866 if (tp_features.hotkey_tablet &&
1867 !hotkey_get_tablet_mode(&state)) {
1868 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001869
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001870 input_report_switch(tpacpi_inputdev,
1871 SW_TABLET_MODE, !!state);
1872 input_sync(tpacpi_inputdev);
1873
1874 mutex_unlock(&tpacpi_inputdev_send_mutex);
1875 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001876}
1877
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001878static void tpacpi_input_send_key(unsigned int scancode)
1879{
1880 unsigned int keycode;
1881
1882 keycode = hotkey_keycode_map[scancode];
1883
1884 if (keycode != KEY_RESERVED) {
1885 mutex_lock(&tpacpi_inputdev_send_mutex);
1886
1887 input_report_key(tpacpi_inputdev, keycode, 1);
1888 if (keycode == KEY_UNKNOWN)
1889 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1890 scancode);
1891 input_sync(tpacpi_inputdev);
1892
1893 input_report_key(tpacpi_inputdev, keycode, 0);
1894 if (keycode == KEY_UNKNOWN)
1895 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1896 scancode);
1897 input_sync(tpacpi_inputdev);
1898
1899 mutex_unlock(&tpacpi_inputdev_send_mutex);
1900 }
1901}
1902
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001903#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1904static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1905
1906static void tpacpi_hotkey_send_key(unsigned int scancode)
1907{
1908 tpacpi_input_send_key(scancode);
1909 if (hotkey_report_mode < 2) {
1910 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1911 0x80, 0x1001 + scancode);
1912 }
1913}
1914
1915static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1916{
1917 u8 d;
1918
1919 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1920 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1921 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1922 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1923 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1924 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1925 }
1926 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1927 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1928 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1929 }
1930 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1931 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1932 n->displayexp_toggle =
1933 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1934 }
1935 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1936 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1937 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1938 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1939 n->brightness_toggle =
1940 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1941 }
1942 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1943 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1944 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1945 >> TP_NVRAM_POS_LEVEL_VOLUME;
1946 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1947 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1948 }
1949}
1950
1951#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001952 do { \
1953 if ((mask & (1 << __scancode)) && \
1954 oldn->__member != newn->__member) \
1955 tpacpi_hotkey_send_key(__scancode); \
1956 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001957
1958#define TPACPI_MAY_SEND_KEY(__scancode) \
1959 do { if (mask & (1 << __scancode)) \
1960 tpacpi_hotkey_send_key(__scancode); } while (0)
1961
1962static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001963 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001964 u32 mask)
1965{
1966 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1967 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1968 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1969 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1970
1971 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1972
1973 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1974
1975 /* handle volume */
1976 if (oldn->volume_toggle != newn->volume_toggle) {
1977 if (oldn->mute != newn->mute) {
1978 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1979 }
1980 if (oldn->volume_level > newn->volume_level) {
1981 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1982 } else if (oldn->volume_level < newn->volume_level) {
1983 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1984 } else if (oldn->mute == newn->mute) {
1985 /* repeated key presses that didn't change state */
1986 if (newn->mute) {
1987 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1988 } else if (newn->volume_level != 0) {
1989 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1990 } else {
1991 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1992 }
1993 }
1994 }
1995
1996 /* handle brightness */
1997 if (oldn->brightness_toggle != newn->brightness_toggle) {
1998 if (oldn->brightness_level < newn->brightness_level) {
1999 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2000 } else if (oldn->brightness_level > newn->brightness_level) {
2001 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2002 } else {
2003 /* repeated key presses that didn't change state */
2004 if (newn->brightness_level != 0) {
2005 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2006 } else {
2007 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2008 }
2009 }
2010 }
2011}
2012
2013#undef TPACPI_COMPARE_KEY
2014#undef TPACPI_MAY_SEND_KEY
2015
2016static int hotkey_kthread(void *data)
2017{
2018 struct tp_nvram_state s[2];
2019 u32 mask;
2020 unsigned int si, so;
2021 unsigned long t;
2022 unsigned int change_detector, must_reset;
2023
2024 mutex_lock(&hotkey_thread_mutex);
2025
2026 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2027 goto exit;
2028
2029 set_freezable();
2030
2031 so = 0;
2032 si = 1;
2033 t = 0;
2034
2035 /* Initial state for compares */
2036 mutex_lock(&hotkey_thread_data_mutex);
2037 change_detector = hotkey_config_change;
2038 mask = hotkey_source_mask & hotkey_mask;
2039 mutex_unlock(&hotkey_thread_data_mutex);
2040 hotkey_read_nvram(&s[so], mask);
2041
2042 while (!kthread_should_stop() && hotkey_poll_freq) {
2043 if (t == 0)
2044 t = 1000/hotkey_poll_freq;
2045 t = msleep_interruptible(t);
2046 if (unlikely(kthread_should_stop()))
2047 break;
2048 must_reset = try_to_freeze();
2049 if (t > 0 && !must_reset)
2050 continue;
2051
2052 mutex_lock(&hotkey_thread_data_mutex);
2053 if (must_reset || hotkey_config_change != change_detector) {
2054 /* forget old state on thaw or config change */
2055 si = so;
2056 t = 0;
2057 change_detector = hotkey_config_change;
2058 }
2059 mask = hotkey_source_mask & hotkey_mask;
2060 mutex_unlock(&hotkey_thread_data_mutex);
2061
2062 if (likely(mask)) {
2063 hotkey_read_nvram(&s[si], mask);
2064 if (likely(si != so)) {
2065 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002066 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002067 }
2068 }
2069
2070 so = si;
2071 si ^= 1;
2072 }
2073
2074exit:
2075 mutex_unlock(&hotkey_thread_mutex);
2076 return 0;
2077}
2078
2079static void hotkey_poll_stop_sync(void)
2080{
2081 if (tpacpi_hotkey_task) {
2082 if (frozen(tpacpi_hotkey_task) ||
2083 freezing(tpacpi_hotkey_task))
2084 thaw_process(tpacpi_hotkey_task);
2085
2086 kthread_stop(tpacpi_hotkey_task);
2087 tpacpi_hotkey_task = NULL;
2088 mutex_lock(&hotkey_thread_mutex);
2089 /* at this point, the thread did exit */
2090 mutex_unlock(&hotkey_thread_mutex);
2091 }
2092}
2093
2094/* call with hotkey_mutex held */
2095static void hotkey_poll_setup(int may_warn)
2096{
2097 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2098 hotkey_poll_freq > 0 &&
2099 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2100 if (!tpacpi_hotkey_task) {
2101 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002102 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002103 if (IS_ERR(tpacpi_hotkey_task)) {
2104 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002105 printk(TPACPI_ERR
2106 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002107 "for hotkey polling\n");
2108 }
2109 }
2110 } else {
2111 hotkey_poll_stop_sync();
2112 if (may_warn &&
2113 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002114 printk(TPACPI_NOTICE
2115 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002116 "which is currently disabled\n",
2117 hotkey_source_mask);
2118 }
2119 }
2120}
2121
2122static void hotkey_poll_setup_safe(int may_warn)
2123{
2124 mutex_lock(&hotkey_mutex);
2125 hotkey_poll_setup(may_warn);
2126 mutex_unlock(&hotkey_mutex);
2127}
2128
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002129#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2130
2131static void hotkey_poll_setup_safe(int __unused)
2132{
2133}
2134
2135#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2136
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002137static int hotkey_inputdev_open(struct input_dev *dev)
2138{
2139 switch (tpacpi_lifecycle) {
2140 case TPACPI_LIFE_INIT:
2141 /*
2142 * hotkey_init will call hotkey_poll_setup_safe
2143 * at the appropriate moment
2144 */
2145 return 0;
2146 case TPACPI_LIFE_EXITING:
2147 return -EBUSY;
2148 case TPACPI_LIFE_RUNNING:
2149 hotkey_poll_setup_safe(0);
2150 return 0;
2151 }
2152
2153 /* Should only happen if tpacpi_lifecycle is corrupt */
2154 BUG();
2155 return -EBUSY;
2156}
2157
2158static void hotkey_inputdev_close(struct input_dev *dev)
2159{
2160 /* disable hotkey polling when possible */
2161 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2162 hotkey_poll_setup_safe(0);
2163}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002164
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002165/* sysfs hotkey enable ------------------------------------------------- */
2166static ssize_t hotkey_enable_show(struct device *dev,
2167 struct device_attribute *attr,
2168 char *buf)
2169{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002170 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002171
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002172 printk_deprecated_attribute("hotkey_enable",
2173 "Hotkey reporting is always enabled");
2174
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002175 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002176 if (res)
2177 return res;
2178
2179 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2180}
2181
2182static ssize_t hotkey_enable_store(struct device *dev,
2183 struct device_attribute *attr,
2184 const char *buf, size_t count)
2185{
2186 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002187
2188 printk_deprecated_attribute("hotkey_enable",
2189 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002190
2191 if (parse_strtoul(buf, 1, &t))
2192 return -EINVAL;
2193
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002194 if (t == 0)
2195 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002196
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002197 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002198}
2199
2200static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002201 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002202 hotkey_enable_show, hotkey_enable_store);
2203
2204/* sysfs hotkey mask --------------------------------------------------- */
2205static ssize_t hotkey_mask_show(struct device *dev,
2206 struct device_attribute *attr,
2207 char *buf)
2208{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002209 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002210
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002211 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002212 return -ERESTARTSYS;
2213 res = hotkey_mask_get();
2214 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002215
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216 return (res)?
2217 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002218}
2219
2220static ssize_t hotkey_mask_store(struct device *dev,
2221 struct device_attribute *attr,
2222 const char *buf, size_t count)
2223{
2224 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002225 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002226
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002227 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002228 return -EINVAL;
2229
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002230 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002231 return -ERESTARTSYS;
2232
2233 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002234
2235#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2236 hotkey_poll_setup(1);
2237#endif
2238
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002239 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002240
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002241 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2242
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002243 return (res) ? res : count;
2244}
2245
2246static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002247 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002248 hotkey_mask_show, hotkey_mask_store);
2249
2250/* sysfs hotkey bios_enabled ------------------------------------------- */
2251static ssize_t hotkey_bios_enabled_show(struct device *dev,
2252 struct device_attribute *attr,
2253 char *buf)
2254{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002255 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002256}
2257
2258static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002259 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002260
2261/* sysfs hotkey bios_mask ---------------------------------------------- */
2262static ssize_t hotkey_bios_mask_show(struct device *dev,
2263 struct device_attribute *attr,
2264 char *buf)
2265{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002266 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002267}
2268
2269static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002270 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002271
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002272/* sysfs hotkey all_mask ----------------------------------------------- */
2273static ssize_t hotkey_all_mask_show(struct device *dev,
2274 struct device_attribute *attr,
2275 char *buf)
2276{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002277 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2278 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002279}
2280
2281static struct device_attribute dev_attr_hotkey_all_mask =
2282 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2283
2284/* sysfs hotkey recommended_mask --------------------------------------- */
2285static ssize_t hotkey_recommended_mask_show(struct device *dev,
2286 struct device_attribute *attr,
2287 char *buf)
2288{
2289 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002290 (hotkey_all_mask | hotkey_source_mask)
2291 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002292}
2293
2294static struct device_attribute dev_attr_hotkey_recommended_mask =
2295 __ATTR(hotkey_recommended_mask, S_IRUGO,
2296 hotkey_recommended_mask_show, NULL);
2297
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002298#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2299
2300/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2301static ssize_t hotkey_source_mask_show(struct device *dev,
2302 struct device_attribute *attr,
2303 char *buf)
2304{
2305 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2306}
2307
2308static ssize_t hotkey_source_mask_store(struct device *dev,
2309 struct device_attribute *attr,
2310 const char *buf, size_t count)
2311{
2312 unsigned long t;
2313
2314 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2315 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2316 return -EINVAL;
2317
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002318 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002319 return -ERESTARTSYS;
2320
2321 HOTKEY_CONFIG_CRITICAL_START
2322 hotkey_source_mask = t;
2323 HOTKEY_CONFIG_CRITICAL_END
2324
2325 hotkey_poll_setup(1);
2326
2327 mutex_unlock(&hotkey_mutex);
2328
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002329 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2330
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002331 return count;
2332}
2333
2334static struct device_attribute dev_attr_hotkey_source_mask =
2335 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2336 hotkey_source_mask_show, hotkey_source_mask_store);
2337
2338/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2339static ssize_t hotkey_poll_freq_show(struct device *dev,
2340 struct device_attribute *attr,
2341 char *buf)
2342{
2343 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2344}
2345
2346static ssize_t hotkey_poll_freq_store(struct device *dev,
2347 struct device_attribute *attr,
2348 const char *buf, size_t count)
2349{
2350 unsigned long t;
2351
2352 if (parse_strtoul(buf, 25, &t))
2353 return -EINVAL;
2354
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002355 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002356 return -ERESTARTSYS;
2357
2358 hotkey_poll_freq = t;
2359
2360 hotkey_poll_setup(1);
2361 mutex_unlock(&hotkey_mutex);
2362
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002363 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2364
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002365 return count;
2366}
2367
2368static struct device_attribute dev_attr_hotkey_poll_freq =
2369 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2370 hotkey_poll_freq_show, hotkey_poll_freq_store);
2371
2372#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2373
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002374/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002375static ssize_t hotkey_radio_sw_show(struct device *dev,
2376 struct device_attribute *attr,
2377 char *buf)
2378{
Johannes Berg19d337d2009-06-02 13:01:37 +02002379 int res;
2380 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002381 if (res < 0)
2382 return res;
2383
Johannes Berg19d337d2009-06-02 13:01:37 +02002384 /* Opportunistic update */
2385 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2386
2387 return snprintf(buf, PAGE_SIZE, "%d\n",
2388 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002389}
2390
2391static struct device_attribute dev_attr_hotkey_radio_sw =
2392 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2393
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002394static void hotkey_radio_sw_notify_change(void)
2395{
2396 if (tp_features.hotkey_wlsw)
2397 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2398 "hotkey_radio_sw");
2399}
2400
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002401/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2402static ssize_t hotkey_tablet_mode_show(struct device *dev,
2403 struct device_attribute *attr,
2404 char *buf)
2405{
2406 int res, s;
2407 res = hotkey_get_tablet_mode(&s);
2408 if (res < 0)
2409 return res;
2410
2411 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2412}
2413
2414static struct device_attribute dev_attr_hotkey_tablet_mode =
2415 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2416
2417static void hotkey_tablet_mode_notify_change(void)
2418{
2419 if (tp_features.hotkey_tablet)
2420 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2421 "hotkey_tablet_mode");
2422}
2423
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002424/* sysfs hotkey report_mode -------------------------------------------- */
2425static ssize_t hotkey_report_mode_show(struct device *dev,
2426 struct device_attribute *attr,
2427 char *buf)
2428{
2429 return snprintf(buf, PAGE_SIZE, "%d\n",
2430 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2431}
2432
2433static struct device_attribute dev_attr_hotkey_report_mode =
2434 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2435
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002436/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002437static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2438 struct device_attribute *attr,
2439 char *buf)
2440{
2441 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2442}
2443
2444static struct device_attribute dev_attr_hotkey_wakeup_reason =
2445 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2446
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002447static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002448{
2449 if (tp_features.hotkey_mask)
2450 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2451 "wakeup_reason");
2452}
2453
2454/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002455static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2456 struct device_attribute *attr,
2457 char *buf)
2458{
2459 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2460}
2461
2462static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2463 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2464 hotkey_wakeup_hotunplug_complete_show, NULL);
2465
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002466static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002467{
2468 if (tp_features.hotkey_mask)
2469 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2470 "wakeup_hotunplug_complete");
2471}
2472
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002473/* --------------------------------------------------------------------- */
2474
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002475static struct attribute *hotkey_attributes[] __initdata = {
2476 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002477 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002478 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002479#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2480 &dev_attr_hotkey_mask.attr,
2481 &dev_attr_hotkey_all_mask.attr,
2482 &dev_attr_hotkey_recommended_mask.attr,
2483 &dev_attr_hotkey_source_mask.attr,
2484 &dev_attr_hotkey_poll_freq.attr,
2485#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002486};
2487
2488static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002489 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002490#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2491 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002492 &dev_attr_hotkey_all_mask.attr,
2493 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002494#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002495 &dev_attr_hotkey_wakeup_reason.attr,
2496 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002497};
2498
Johannes Berg19d337d2009-06-02 13:01:37 +02002499/*
2500 * Sync both the hw and sw blocking state of all switches
2501 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002502static void tpacpi_send_radiosw_update(void)
2503{
2504 int wlsw;
2505
Johannes Berg19d337d2009-06-02 13:01:37 +02002506 /*
2507 * We must sync all rfkill controllers *before* issuing any
2508 * rfkill input events, or we will race the rfkill core input
2509 * handler.
2510 *
2511 * tpacpi_inputdev_send_mutex works as a syncronization point
2512 * for the above.
2513 *
2514 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2515 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002516
Johannes Berg19d337d2009-06-02 13:01:37 +02002517 wlsw = hotkey_get_wlsw();
2518
2519 /* Sync hw blocking state first if it is hw-blocked */
2520 if (wlsw == TPACPI_RFK_RADIO_OFF)
2521 tpacpi_rfk_update_hwblock_state(true);
2522
2523 /* Sync sw blocking state */
2524 tpacpi_rfk_update_swstate_all();
2525
2526 /* Sync hw blocking state last if it is hw-unblocked */
2527 if (wlsw == TPACPI_RFK_RADIO_ON)
2528 tpacpi_rfk_update_hwblock_state(false);
2529
2530 /* Issue rfkill input event for WLSW switch */
2531 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002532 mutex_lock(&tpacpi_inputdev_send_mutex);
2533
2534 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002535 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002536 input_sync(tpacpi_inputdev);
2537
2538 mutex_unlock(&tpacpi_inputdev_send_mutex);
2539 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002540
2541 /*
2542 * this can be unconditional, as we will poll state again
2543 * if userspace uses the notify to read data
2544 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002545 hotkey_radio_sw_notify_change();
2546}
2547
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002548static void hotkey_exit(void)
2549{
2550#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2551 hotkey_poll_stop_sync();
2552#endif
2553
2554 if (hotkey_dev_attributes)
2555 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2556
2557 kfree(hotkey_keycode_map);
2558
2559 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002560 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002561 "restoring original hot key mask\n");
2562 /* no short-circuit boolean operator below! */
2563 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002564 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002565 printk(TPACPI_ERR
2566 "failed to restore hot key mask "
2567 "to BIOS defaults\n");
2568 }
2569}
2570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002571static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002572{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002573 /* Requirements for changing the default keymaps:
2574 *
2575 * 1. Many of the keys are mapped to KEY_RESERVED for very
2576 * good reasons. Do not change them unless you have deep
2577 * knowledge on the IBM and Lenovo ThinkPad firmware for
2578 * the various ThinkPad models. The driver behaves
2579 * differently for KEY_RESERVED: such keys have their
2580 * hot key mask *unset* in mask_recommended, and also
2581 * in the initial hot key mask programmed into the
2582 * firmware at driver load time, which means the firm-
2583 * ware may react very differently if you change them to
2584 * something else;
2585 *
2586 * 2. You must be subscribed to the linux-thinkpad and
2587 * ibm-acpi-devel mailing lists, and you should read the
2588 * list archives since 2007 if you want to change the
2589 * keymaps. This requirement exists so that you will
2590 * know the past history of problems with the thinkpad-
2591 * acpi driver keymaps, and also that you will be
2592 * listening to any bug reports;
2593 *
2594 * 3. Do not send thinkpad-acpi specific patches directly to
2595 * for merging, *ever*. Send them to the linux-acpi
2596 * mailinglist for comments. Merging is to be done only
2597 * through acpi-test and the ACPI maintainer.
2598 *
2599 * If the above is too much to ask, don't change the keymap.
2600 * Ask the thinkpad-acpi maintainer to do it, instead.
2601 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002602 static u16 ibm_keycode_map[] __initdata = {
2603 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2604 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2605 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2606 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002607
2608 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002609 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2610 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2611 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002612
2613 /* brightness: firmware always reacts to them, unless
2614 * X.org did some tricks in the radeon BIOS scratch
2615 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002616 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002617 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002618
2619 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002620 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002621
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002622 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2623 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002624
2625 /* Volume: firmware always react to it and reprograms
2626 * the built-in *extra* mixer. Never map it to control
2627 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002628 KEY_RESERVED, /* 0x14: VOLUME UP */
2629 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2630 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002631
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002632 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002633
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002634 /* (assignments unknown, please report if found) */
2635 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2636 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2637 };
2638 static u16 lenovo_keycode_map[] __initdata = {
2639 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2640 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2641 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2642 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002643
2644 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002645 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2646 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2647 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002648
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002649 /* These either have to go through ACPI video, or
2650 * act like in the IBM ThinkPads, so don't ever
2651 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002652 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002653 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002654
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002655 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002656
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002657 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2658 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002659
2660 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2661 * react to it and reprograms the built-in *extra* mixer.
2662 * Never map it to control another mixer by default.
2663 *
2664 * T60?, T61, R60?, R61: firmware and EC tries to send
2665 * these over the regular keyboard, so these are no-ops,
2666 * but there are still weird bugs re. MUTE, so do not
2667 * change unless you get test reports from all Lenovo
2668 * models. May cause the BIOS to interfere with the
2669 * HDA mixer.
2670 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002671 KEY_RESERVED, /* 0x14: VOLUME UP */
2672 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2673 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002674
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002675 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002676
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002677 /* (assignments unknown, please report if found) */
2678 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2679 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2680 };
2681
2682#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2683#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2684#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2685
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002686 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002687 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002688 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002689
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002690 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2691 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002692
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002693 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002694 BUG_ON(tpacpi_inputdev->open != NULL ||
2695 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002696
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002697 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002698 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002699
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2701 mutex_init(&hotkey_thread_mutex);
2702 mutex_init(&hotkey_thread_data_mutex);
2703#endif
2704
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002705 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002706 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002707
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002708 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2709 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002710 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002711
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002712 if (!tp_features.hotkey)
2713 return 1;
2714
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002715 tpacpi_disable_brightness_delay();
2716
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002717 hotkey_dev_attributes = create_attr_set(13, NULL);
2718 if (!hotkey_dev_attributes)
2719 return -ENOMEM;
2720 res = add_many_to_attr_set(hotkey_dev_attributes,
2721 hotkey_attributes,
2722 ARRAY_SIZE(hotkey_attributes));
2723 if (res)
2724 goto err_exit;
2725
2726 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2727 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2728 for HKEY interface version 0x100 */
2729 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2730 if ((hkeyv >> 8) != 1) {
2731 printk(TPACPI_ERR "unknown version of the "
2732 "HKEY interface: 0x%x\n", hkeyv);
2733 printk(TPACPI_ERR "please report this to %s\n",
2734 TPACPI_MAIL);
2735 } else {
2736 /*
2737 * MHKV 0x100 in A31, R40, R40e,
2738 * T4x, X31, and later
2739 */
2740 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002741 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2742 "firmware HKEY interface version: 0x%x\n",
2743 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002744 }
2745 }
2746
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2748 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002749 str_supported(tp_features.hotkey_mask));
2750
2751 if (tp_features.hotkey_mask) {
2752 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2753 "MHKA", "qd")) {
2754 printk(TPACPI_ERR
2755 "missing MHKA handler, "
2756 "please report this to %s\n",
2757 TPACPI_MAIL);
2758 /* FN+F12, FN+F4, FN+F3 */
2759 hotkey_all_mask = 0x080cU;
2760 }
2761 }
2762
2763 /* hotkey_source_mask *must* be zero for
2764 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002765 if (tp_features.hotkey_mask) {
2766 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002767 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002768 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002769
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002770 hotkey_orig_mask = hotkey_mask;
2771 res = add_many_to_attr_set(
2772 hotkey_dev_attributes,
2773 hotkey_mask_attributes,
2774 ARRAY_SIZE(hotkey_mask_attributes));
2775 if (res)
2776 goto err_exit;
2777 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002778
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002779#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002780 if (tp_features.hotkey_mask) {
2781 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2782 & ~hotkey_all_mask;
2783 } else {
2784 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2785 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002786
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002787 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002788 "hotkey source mask 0x%08x, polling freq %d\n",
2789 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002790#endif
2791
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002792#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2793 if (dbg_wlswemul) {
2794 tp_features.hotkey_wlsw = 1;
2795 printk(TPACPI_INFO
2796 "radio switch emulation enabled\n");
2797 } else
2798#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002799 /* Not all thinkpads have a hardware radio switch */
2800 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2801 tp_features.hotkey_wlsw = 1;
2802 printk(TPACPI_INFO
2803 "radio switch found; radios are %s\n",
2804 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002805 }
2806 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002807 res = add_to_attr_set(hotkey_dev_attributes,
2808 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002809
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002810 /* For X41t, X60t, X61t Tablets... */
2811 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2812 tp_features.hotkey_tablet = 1;
2813 printk(TPACPI_INFO
2814 "possible tablet mode switch found; "
2815 "ThinkPad in %s mode\n",
2816 (status & TP_HOTKEY_TABLET_MASK)?
2817 "tablet" : "laptop");
2818 res = add_to_attr_set(hotkey_dev_attributes,
2819 &dev_attr_hotkey_tablet_mode.attr);
2820 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002821
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002822 if (!res)
2823 res = register_attr_set_with_sysfs(
2824 hotkey_dev_attributes,
2825 &tpacpi_pdev->dev.kobj);
2826 if (res)
2827 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002828
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002829 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002830
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002831 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2832 GFP_KERNEL);
2833 if (!hotkey_keycode_map) {
2834 printk(TPACPI_ERR
2835 "failed to allocate memory for key map\n");
2836 res = -ENOMEM;
2837 goto err_exit;
2838 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002839
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002840 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002841 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002842 "using Lenovo default hot key map\n");
2843 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2844 TPACPI_HOTKEY_MAP_SIZE);
2845 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002846 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002847 "using IBM default hot key map\n");
2848 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2849 TPACPI_HOTKEY_MAP_SIZE);
2850 }
2851
2852 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2853 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2854 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2855 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2856 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2857 tpacpi_inputdev->keycode = hotkey_keycode_map;
2858 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2859 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2860 set_bit(hotkey_keycode_map[i],
2861 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002862 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002863 if (i < sizeof(hotkey_reserved_mask)*8)
2864 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002865 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002866 }
2867
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002868 if (tp_features.hotkey_wlsw) {
2869 set_bit(EV_SW, tpacpi_inputdev->evbit);
2870 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2871 }
2872 if (tp_features.hotkey_tablet) {
2873 set_bit(EV_SW, tpacpi_inputdev->evbit);
2874 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002875 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002876
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002877 /* Do not issue duplicate brightness change events to
2878 * userspace */
2879 if (!tp_features.bright_acpimode)
2880 /* update bright_acpimode... */
2881 tpacpi_check_std_acpi_brightness_support();
2882
2883 if (tp_features.bright_acpimode) {
2884 printk(TPACPI_INFO
2885 "This ThinkPad has standard ACPI backlight "
2886 "brightness control, supported by the ACPI "
2887 "video driver\n");
2888 printk(TPACPI_NOTICE
2889 "Disabling thinkpad-acpi brightness events "
2890 "by default...\n");
2891
2892 /* The hotkey_reserved_mask change below is not
2893 * necessary while the keys are at KEY_RESERVED in the
2894 * default map, but better safe than sorry, leave it
2895 * here as a marker of what we have to do, especially
2896 * when we finally become able to set this at runtime
2897 * on response to X.org requests */
2898 hotkey_reserved_mask |=
2899 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2900 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002901 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002902
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002903 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2904 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002905 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002906 if (res) {
2907 hotkey_exit();
2908 return res;
2909 }
2910 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2911 & ~hotkey_reserved_mask)
2912 | hotkey_orig_mask);
2913 if (res < 0 && res != -ENXIO) {
2914 hotkey_exit();
2915 return res;
2916 }
2917
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002918 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2919 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002920 (hotkey_report_mode < 2) ?
2921 "enabled" : "disabled");
2922
2923 tpacpi_inputdev->open = &hotkey_inputdev_open;
2924 tpacpi_inputdev->close = &hotkey_inputdev_close;
2925
2926 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002927 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002928 tpacpi_input_send_tabletsw();
2929
2930 return 0;
2931
2932err_exit:
2933 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2934 hotkey_dev_attributes = NULL;
2935
2936 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002937}
2938
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002939static bool hotkey_notify_hotkey(const u32 hkey,
2940 bool *send_acpi_ev,
2941 bool *ignore_acpi_ev)
2942{
2943 /* 0x1000-0x1FFF: key presses */
2944 unsigned int scancode = hkey & 0xfff;
2945 *send_acpi_ev = true;
2946 *ignore_acpi_ev = false;
2947
2948 if (scancode > 0 && scancode < 0x21) {
2949 scancode--;
2950 if (!(hotkey_source_mask & (1 << scancode))) {
2951 tpacpi_input_send_key(scancode);
2952 *send_acpi_ev = false;
2953 } else {
2954 *ignore_acpi_ev = true;
2955 }
2956 return true;
2957 }
2958 return false;
2959}
2960
2961static bool hotkey_notify_wakeup(const u32 hkey,
2962 bool *send_acpi_ev,
2963 bool *ignore_acpi_ev)
2964{
2965 /* 0x2000-0x2FFF: Wakeup reason */
2966 *send_acpi_ev = true;
2967 *ignore_acpi_ev = false;
2968
2969 switch (hkey) {
2970 case 0x2304: /* suspend, undock */
2971 case 0x2404: /* hibernation, undock */
2972 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2973 *ignore_acpi_ev = true;
2974 break;
2975
2976 case 0x2305: /* suspend, bay eject */
2977 case 0x2405: /* hibernation, bay eject */
2978 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2979 *ignore_acpi_ev = true;
2980 break;
2981
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002982 case 0x2313: /* Battery on critical low level (S3) */
2983 case 0x2413: /* Battery on critical low level (S4) */
2984 printk(TPACPI_ALERT
2985 "EMERGENCY WAKEUP: battery almost empty\n");
2986 /* how to auto-heal: */
2987 /* 2313: woke up from S3, go to S4/S5 */
2988 /* 2413: woke up from S4, go to S5 */
2989 break;
2990
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002991 default:
2992 return false;
2993 }
2994
2995 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2996 printk(TPACPI_INFO
2997 "woke up due to a hot-unplug "
2998 "request...\n");
2999 hotkey_wakeup_reason_notify_change();
3000 }
3001 return true;
3002}
3003
3004static bool hotkey_notify_usrevent(const u32 hkey,
3005 bool *send_acpi_ev,
3006 bool *ignore_acpi_ev)
3007{
3008 /* 0x5000-0x5FFF: human interface helpers */
3009 *send_acpi_ev = true;
3010 *ignore_acpi_ev = false;
3011
3012 switch (hkey) {
3013 case 0x5010: /* Lenovo new BIOS: brightness changed */
3014 case 0x500b: /* X61t: tablet pen inserted into bay */
3015 case 0x500c: /* X61t: tablet pen removed from bay */
3016 return true;
3017
3018 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3019 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3020 tpacpi_input_send_tabletsw();
3021 hotkey_tablet_mode_notify_change();
3022 *send_acpi_ev = false;
3023 return true;
3024
3025 case 0x5001:
3026 case 0x5002:
3027 /* LID switch events. Do not propagate */
3028 *ignore_acpi_ev = true;
3029 return true;
3030
3031 default:
3032 return false;
3033 }
3034}
3035
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003036static bool hotkey_notify_thermal(const u32 hkey,
3037 bool *send_acpi_ev,
3038 bool *ignore_acpi_ev)
3039{
3040 /* 0x6000-0x6FFF: thermal alarms */
3041 *send_acpi_ev = true;
3042 *ignore_acpi_ev = false;
3043
3044 switch (hkey) {
3045 case 0x6011:
3046 printk(TPACPI_CRIT
3047 "THERMAL ALARM: battery is too hot!\n");
3048 /* recommended action: warn user through gui */
3049 return true;
3050 case 0x6012:
3051 printk(TPACPI_ALERT
3052 "THERMAL EMERGENCY: battery is extremely hot!\n");
3053 /* recommended action: immediate sleep/hibernate */
3054 return true;
3055 case 0x6021:
3056 printk(TPACPI_CRIT
3057 "THERMAL ALARM: "
3058 "a sensor reports something is too hot!\n");
3059 /* recommended action: warn user through gui, that */
3060 /* some internal component is too hot */
3061 return true;
3062 case 0x6022:
3063 printk(TPACPI_ALERT
3064 "THERMAL EMERGENCY: "
3065 "a sensor reports something is extremely hot!\n");
3066 /* recommended action: immediate sleep/hibernate */
3067 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003068 case 0x6030:
3069 printk(TPACPI_INFO
3070 "EC reports that Thermal Table has changed\n");
3071 /* recommended action: do nothing, we don't have
3072 * Lenovo ATM information */
3073 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003074 default:
3075 printk(TPACPI_ALERT
3076 "THERMAL ALERT: unknown thermal alarm received\n");
3077 return false;
3078 }
3079}
3080
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003081static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3082{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003083 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003084 bool send_acpi_ev;
3085 bool ignore_acpi_ev;
3086 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003087
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003088 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003089 printk(TPACPI_ERR
3090 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003091 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003092 acpi_bus_generate_netlink_event(
3093 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003094 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003095 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003096 return;
3097 }
3098
3099 while (1) {
3100 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003101 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003102 return;
3103 }
3104
3105 if (hkey == 0) {
3106 /* queue empty */
3107 return;
3108 }
3109
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003110 send_acpi_ev = true;
3111 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003112
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003113 switch (hkey >> 12) {
3114 case 1:
3115 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003116 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3117 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003118 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003119 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003120 /* 0x2000-0x2FFF: Wakeup reason */
3121 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3122 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003123 break;
3124 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003125 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003126 if (hkey == 0x3003) {
3127 hotkey_autosleep_ack = 1;
3128 printk(TPACPI_INFO
3129 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003130 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003131 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003132 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003133 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003134 }
3135 break;
3136 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003137 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003138 if (hkey == 0x4003) {
3139 hotkey_autosleep_ack = 1;
3140 printk(TPACPI_INFO
3141 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003142 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003143 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003144 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003145 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003146 }
3147 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003148 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003149 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003150 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3151 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003152 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003153 case 6:
3154 /* 0x6000-0x6FFF: thermal alarms */
3155 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3156 &ignore_acpi_ev);
3157 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003158 case 7:
3159 /* 0x7000-0x7FFF: misc */
3160 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003161 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003162 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003163 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003164 break;
3165 }
3166 /* fallthrough to default */
3167 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003168 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003169 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003170 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003171 printk(TPACPI_NOTICE
3172 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02003173 printk(TPACPI_NOTICE
3174 "please report the conditions when this "
3175 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003176 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003177
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003178 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003179 if (!ignore_acpi_ev &&
3180 (send_acpi_ev || hotkey_report_mode < 2)) {
3181 acpi_bus_generate_proc_event(ibm->acpi->device,
3182 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003183 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003184
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003185 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003186 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003187 acpi_bus_generate_netlink_event(
3188 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003189 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003190 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003191 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003192 }
3193}
3194
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003195static void hotkey_suspend(pm_message_t state)
3196{
3197 /* Do these on suspend, we get the events on early resume! */
3198 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3199 hotkey_autosleep_ack = 0;
3200}
3201
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003202static void hotkey_resume(void)
3203{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003204 tpacpi_disable_brightness_delay();
3205
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003206 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003207 printk(TPACPI_ERR
3208 "error while trying to read hot key mask "
3209 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003210 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003211 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003212 hotkey_wakeup_reason_notify_change();
3213 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003214 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003215}
3216
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003217/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003218static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003220 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 int len = 0;
3222
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003223 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003224 len += sprintf(p + len, "status:\t\tnot supported\n");
3225 return len;
3226 }
3227
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003228 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003229 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003230 res = hotkey_status_get(&status);
3231 if (!res)
3232 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003233 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003234 if (res)
3235 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003238 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003239 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 len += sprintf(p + len,
3241 "commands:\tenable, disable, reset, <mask>\n");
3242 } else {
3243 len += sprintf(p + len, "mask:\t\tnot supported\n");
3244 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3245 }
3246
3247 return len;
3248}
3249
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003250static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003251{
3252 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003253 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3254 TPACPI_WARN
3255 "hotkey enable/disable functionality has been "
3256 "removed from the driver. Hotkeys are always "
3257 "enabled\n"))
3258 printk(TPACPI_ERR
3259 "Please remove the hotkey=enable module "
3260 "parameter, it is deprecated. Hotkeys are always "
3261 "enabled\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003262}
3263
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003264static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003266 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003267 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003270 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return -ENODEV;
3272
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003273 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003274 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003275
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003276 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003277
3278 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 while ((cmd = next_cmd(&buf))) {
3280 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003281 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003283 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003284 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003286 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3288 /* mask set */
3289 } else if (sscanf(cmd, "%x", &mask) == 1) {
3290 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003291 } else {
3292 res = -EINVAL;
3293 goto errexit;
3294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003296
3297 if (!res)
3298 tpacpi_disclose_usertask("procfs hotkey",
3299 "set mask to 0x%08x\n", mask);
3300
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003301 if (!res && mask != hotkey_mask)
3302 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003304errexit:
3305 mutex_unlock(&hotkey_mutex);
3306 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003309static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003310 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003311 {"", 0},
3312};
3313
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003314static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003315 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003316 .notify = hotkey_notify,
3317 .handle = &hkey_handle,
3318 .type = ACPI_DEVICE_NOTIFY,
3319};
3320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003321static struct ibm_struct hotkey_driver_data = {
3322 .name = "hotkey",
3323 .read = hotkey_read,
3324 .write = hotkey_write,
3325 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003326 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003327 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003328 .acpi = &ibm_hotkey_acpidriver,
3329};
3330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003331/*************************************************************************
3332 * Bluetooth subdriver
3333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003335enum {
3336 /* ACPI GBDC/SBDC bits */
3337 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3338 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003339 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3340 off / last state */
3341};
3342
3343enum {
3344 /* ACPI \BLTH commands */
3345 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3346 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3347 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3348 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3349 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003350};
3351
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003352#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3353
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003354static void bluetooth_suspend(pm_message_t state)
3355{
3356 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003357 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3358 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3359 vdbg_printk(TPACPI_DBG_RFKILL,
3360 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003361}
3362
Johannes Berg19d337d2009-06-02 13:01:37 +02003363static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003364{
3365 int status;
3366
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003367#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3368 if (dbg_bluetoothemul)
3369 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003371#endif
3372
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003373 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3374 return -EIO;
3375
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003376 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003377 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003378}
3379
Johannes Berg19d337d2009-06-02 13:01:37 +02003380static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003381{
3382 int status;
3383
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003384 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003385 "will attempt to %s bluetooth\n",
3386 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003387
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003388#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3389 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003390 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003391 return 0;
3392 }
3393#endif
3394
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003395 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003396 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003397 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003398 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003399 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003400
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003401 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3402 return -EIO;
3403
3404 return 0;
3405}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003406
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003407/* sysfs bluetooth enable ---------------------------------------------- */
3408static ssize_t bluetooth_enable_show(struct device *dev,
3409 struct device_attribute *attr,
3410 char *buf)
3411{
Johannes Berg19d337d2009-06-02 13:01:37 +02003412 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3413 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003414}
3415
3416static ssize_t bluetooth_enable_store(struct device *dev,
3417 struct device_attribute *attr,
3418 const char *buf, size_t count)
3419{
Johannes Berg19d337d2009-06-02 13:01:37 +02003420 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3421 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003422}
3423
3424static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003425 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003426 bluetooth_enable_show, bluetooth_enable_store);
3427
3428/* --------------------------------------------------------------------- */
3429
3430static struct attribute *bluetooth_attributes[] = {
3431 &dev_attr_bluetooth_enable.attr,
3432 NULL
3433};
3434
3435static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003436 .attrs = bluetooth_attributes,
3437};
3438
Johannes Berg19d337d2009-06-02 13:01:37 +02003439static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3440 .get_status = bluetooth_get_status,
3441 .set_status = bluetooth_set_status,
3442};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003443
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003444static void bluetooth_shutdown(void)
3445{
3446 /* Order firmware to save current state to NVRAM */
3447 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3448 TP_ACPI_BLTH_SAVE_STATE))
3449 printk(TPACPI_NOTICE
3450 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003451 else
3452 vdbg_printk(TPACPI_DBG_RFKILL,
3453 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003454}
3455
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003456static void bluetooth_exit(void)
3457{
3458 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3459 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003460
3461 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3462
3463 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003464}
3465
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003466static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003468 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003469 int status = 0;
3470
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003471 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3472 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003473
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003474 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003475
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003476 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3477 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003478 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003479 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003481 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3482 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003483 str_supported(tp_features.bluetooth),
3484 status);
3485
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003486#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3487 if (dbg_bluetoothemul) {
3488 tp_features.bluetooth = 1;
3489 printk(TPACPI_INFO
3490 "bluetooth switch emulation enabled\n");
3491 } else
3492#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003493 if (tp_features.bluetooth &&
3494 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3495 /* no bluetooth hardware present in system */
3496 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003497 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003498 "bluetooth hardware not installed\n");
3499 }
3500
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003501 if (!tp_features.bluetooth)
3502 return 1;
3503
Johannes Berg19d337d2009-06-02 13:01:37 +02003504 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3505 &bluetooth_tprfk_ops,
3506 RFKILL_TYPE_BLUETOOTH,
3507 TPACPI_RFK_BLUETOOTH_SW_NAME,
3508 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003509 if (res)
3510 return res;
3511
Johannes Berg19d337d2009-06-02 13:01:37 +02003512 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3513 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003514 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003515 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003516 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003517 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003518
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003522/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003523static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
Johannes Berg19d337d2009-06-02 13:01:37 +02003525 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003528static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Johannes Berg19d337d2009-06-02 13:01:37 +02003530 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003533static struct ibm_struct bluetooth_driver_data = {
3534 .name = "bluetooth",
3535 .read = bluetooth_read,
3536 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003537 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003538 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003539 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003540};
3541
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003542/*************************************************************************
3543 * Wan subdriver
3544 */
3545
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003546enum {
3547 /* ACPI GWAN/SWAN bits */
3548 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3549 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003550 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3551 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003552};
3553
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003554#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3555
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003556static void wan_suspend(pm_message_t state)
3557{
3558 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003559 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3560 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3561 vdbg_printk(TPACPI_DBG_RFKILL,
3562 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003563}
3564
Johannes Berg19d337d2009-06-02 13:01:37 +02003565static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003566{
3567 int status;
3568
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003569#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3570 if (dbg_wwanemul)
3571 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003572 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003573#endif
3574
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003575 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3576 return -EIO;
3577
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003578 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003579 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003580}
3581
Johannes Berg19d337d2009-06-02 13:01:37 +02003582static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003583{
3584 int status;
3585
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003586 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003587 "will attempt to %s wwan\n",
3588 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003589
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003590#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3591 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003592 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003593 return 0;
3594 }
3595#endif
3596
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003597 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Johannes Berg19d337d2009-06-02 13:01:37 +02003598 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003599 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003600 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003601 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003602
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003603 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3604 return -EIO;
3605
3606 return 0;
3607}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003608
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003609/* sysfs wan enable ---------------------------------------------------- */
3610static ssize_t wan_enable_show(struct device *dev,
3611 struct device_attribute *attr,
3612 char *buf)
3613{
Johannes Berg19d337d2009-06-02 13:01:37 +02003614 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3615 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003616}
3617
3618static ssize_t wan_enable_store(struct device *dev,
3619 struct device_attribute *attr,
3620 const char *buf, size_t count)
3621{
Johannes Berg19d337d2009-06-02 13:01:37 +02003622 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3623 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003624}
3625
3626static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003627 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003628 wan_enable_show, wan_enable_store);
3629
3630/* --------------------------------------------------------------------- */
3631
3632static struct attribute *wan_attributes[] = {
3633 &dev_attr_wan_enable.attr,
3634 NULL
3635};
3636
3637static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003638 .attrs = wan_attributes,
3639};
3640
Johannes Berg19d337d2009-06-02 13:01:37 +02003641static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3642 .get_status = wan_get_status,
3643 .set_status = wan_set_status,
3644};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003645
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003646static void wan_shutdown(void)
3647{
3648 /* Order firmware to save current state to NVRAM */
3649 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3650 TP_ACPI_WGSV_SAVE_STATE))
3651 printk(TPACPI_NOTICE
3652 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003653 else
3654 vdbg_printk(TPACPI_DBG_RFKILL,
3655 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003656}
3657
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003658static void wan_exit(void)
3659{
3660 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3661 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003662
3663 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3664
3665 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003666}
3667
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003668static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003669{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003670 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003671 int status = 0;
3672
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003673 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3674 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003675
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003676 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003677
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003678 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003679 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003680
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003681 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3682 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003683 str_supported(tp_features.wan),
3684 status);
3685
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003686#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3687 if (dbg_wwanemul) {
3688 tp_features.wan = 1;
3689 printk(TPACPI_INFO
3690 "wwan switch emulation enabled\n");
3691 } else
3692#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003693 if (tp_features.wan &&
3694 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3695 /* no wan hardware present in system */
3696 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003697 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003698 "wan hardware not installed\n");
3699 }
3700
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003701 if (!tp_features.wan)
3702 return 1;
3703
Johannes Berg19d337d2009-06-02 13:01:37 +02003704 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3705 &wan_tprfk_ops,
3706 RFKILL_TYPE_WWAN,
3707 TPACPI_RFK_WWAN_SW_NAME,
3708 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003709 if (res)
3710 return res;
3711
Johannes Berg19d337d2009-06-02 13:01:37 +02003712 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3713 &wan_attr_group);
3714
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003715 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003716 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003717 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003718 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003719
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003720 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003721}
3722
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003723/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003724static int wan_read(char *p)
3725{
Johannes Berg19d337d2009-06-02 13:01:37 +02003726 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003727}
3728
3729static int wan_write(char *buf)
3730{
Johannes Berg19d337d2009-06-02 13:01:37 +02003731 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003732}
3733
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003734static struct ibm_struct wan_driver_data = {
3735 .name = "wan",
3736 .read = wan_read,
3737 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003738 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003739 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003740 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003741};
3742
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003743/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003744 * UWB subdriver
3745 */
3746
3747enum {
3748 /* ACPI GUWB/SUWB bits */
3749 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3750 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3751};
3752
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003753#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3754
Johannes Berg19d337d2009-06-02 13:01:37 +02003755static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003756{
3757 int status;
3758
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003759#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3760 if (dbg_uwbemul)
3761 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003762 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003763#endif
3764
3765 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3766 return -EIO;
3767
3768 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003769 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003770}
3771
Johannes Berg19d337d2009-06-02 13:01:37 +02003772static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003773{
3774 int status;
3775
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003776 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003777 "will attempt to %s UWB\n",
3778 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003779
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003780#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3781 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003782 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003783 return 0;
3784 }
3785#endif
3786
Johannes Berg19d337d2009-06-02 13:01:37 +02003787 if (state == TPACPI_RFK_RADIO_ON)
3788 status = TP_ACPI_UWB_RADIOSSW;
3789 else
3790 status = 0;
3791
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003792 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3793 return -EIO;
3794
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003795 return 0;
3796}
3797
3798/* --------------------------------------------------------------------- */
3799
Johannes Berg19d337d2009-06-02 13:01:37 +02003800static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
3801 .get_status = uwb_get_status,
3802 .set_status = uwb_set_status,
3803};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003804
3805static void uwb_exit(void)
3806{
Johannes Berg19d337d2009-06-02 13:01:37 +02003807 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003808}
3809
3810static int __init uwb_init(struct ibm_init_struct *iibm)
3811{
3812 int res;
3813 int status = 0;
3814
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003815 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3816 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003817
3818 TPACPI_ACPIHANDLE_INIT(hkey);
3819
3820 tp_features.uwb = hkey_handle &&
3821 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3822
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003823 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3824 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003825 str_supported(tp_features.uwb),
3826 status);
3827
3828#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3829 if (dbg_uwbemul) {
3830 tp_features.uwb = 1;
3831 printk(TPACPI_INFO
3832 "uwb switch emulation enabled\n");
3833 } else
3834#endif
3835 if (tp_features.uwb &&
3836 !(status & TP_ACPI_UWB_HWPRESENT)) {
3837 /* no uwb hardware present in system */
3838 tp_features.uwb = 0;
3839 dbg_printk(TPACPI_DBG_INIT,
3840 "uwb hardware not installed\n");
3841 }
3842
3843 if (!tp_features.uwb)
3844 return 1;
3845
3846 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02003847 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003848 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003849 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02003850 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003851 return res;
3852}
3853
3854static struct ibm_struct uwb_driver_data = {
3855 .name = "uwb",
3856 .exit = uwb_exit,
3857 .flags.experimental = 1,
3858};
3859
3860/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003861 * Video subdriver
3862 */
3863
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003864#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3865
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003866enum video_access_mode {
3867 TPACPI_VIDEO_NONE = 0,
3868 TPACPI_VIDEO_570, /* 570 */
3869 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3870 TPACPI_VIDEO_NEW, /* all others */
3871};
3872
3873enum { /* video status flags, based on VIDEO_570 */
3874 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3875 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3876 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3877};
3878
3879enum { /* TPACPI_VIDEO_570 constants */
3880 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3881 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3882 * video_status_flags */
3883 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3884 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3885};
3886
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003887static enum video_access_mode video_supported;
3888static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003889
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003890static int video_autosw_get(void);
3891static int video_autosw_set(int enable);
3892
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003893TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003895static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003897 int ivga;
3898
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003899 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3900
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003901 TPACPI_ACPIHANDLE_INIT(vid);
3902 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003903
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003904 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3905 /* G41, assume IVGA doesn't change */
3906 vid_handle = vid2_handle;
3907
3908 if (!vid_handle)
3909 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003910 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003911 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3912 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003913 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003914 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3915 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003916 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003917 else
3918 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003919 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003921 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3922 str_supported(video_supported != TPACPI_VIDEO_NONE),
3923 video_supported);
3924
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003925 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003928static void video_exit(void)
3929{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003930 dbg_printk(TPACPI_DBG_EXIT,
3931 "restoring original video autoswitch mode\n");
3932 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003933 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003934 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003935}
3936
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003937static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
3939 int status = 0;
3940 int i;
3941
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003942 switch (video_supported) {
3943 case TPACPI_VIDEO_570:
3944 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3945 TP_ACPI_VIDEO_570_PHSCMD))
3946 return -EIO;
3947 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3948 break;
3949 case TPACPI_VIDEO_770:
3950 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3951 return -EIO;
3952 if (i)
3953 status |= TP_ACPI_VIDEO_S_LCD;
3954 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3955 return -EIO;
3956 if (i)
3957 status |= TP_ACPI_VIDEO_S_CRT;
3958 break;
3959 case TPACPI_VIDEO_NEW:
3960 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3961 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3962 return -EIO;
3963 if (i)
3964 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003966 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3967 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3968 return -EIO;
3969 if (i)
3970 status |= TP_ACPI_VIDEO_S_LCD;
3971 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3972 return -EIO;
3973 if (i)
3974 status |= TP_ACPI_VIDEO_S_DVI;
3975 break;
3976 default:
3977 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 return status;
3981}
3982
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003983static int video_outputsw_set(int status)
3984{
3985 int autosw;
3986 int res = 0;
3987
3988 switch (video_supported) {
3989 case TPACPI_VIDEO_570:
3990 res = acpi_evalf(NULL, NULL,
3991 "\\_SB.PHS2", "vdd",
3992 TP_ACPI_VIDEO_570_PHS2CMD,
3993 status | TP_ACPI_VIDEO_570_PHS2SET);
3994 break;
3995 case TPACPI_VIDEO_770:
3996 autosw = video_autosw_get();
3997 if (autosw < 0)
3998 return autosw;
3999
4000 res = video_autosw_set(1);
4001 if (res)
4002 return res;
4003 res = acpi_evalf(vid_handle, NULL,
4004 "ASWT", "vdd", status * 0x100, 0);
4005 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004006 printk(TPACPI_ERR
4007 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004008 return -EIO;
4009 }
4010 break;
4011 case TPACPI_VIDEO_NEW:
4012 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004013 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004014 break;
4015 default:
4016 return -ENOSYS;
4017 }
4018
4019 return (res)? 0 : -EIO;
4020}
4021
4022static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004024 int autosw = 0;
4025
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004026 switch (video_supported) {
4027 case TPACPI_VIDEO_570:
4028 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4029 return -EIO;
4030 break;
4031 case TPACPI_VIDEO_770:
4032 case TPACPI_VIDEO_NEW:
4033 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4034 return -EIO;
4035 break;
4036 default:
4037 return -ENOSYS;
4038 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004039
4040 return autosw & 1;
4041}
4042
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004043static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004044{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004045 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004046 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004047 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004048}
4049
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004050static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004051{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004052 int autosw = video_autosw_get();
4053 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004054
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004055 if (autosw < 0)
4056 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004057
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004058 switch (video_supported) {
4059 case TPACPI_VIDEO_570:
4060 res = video_autosw_set(1);
4061 if (res)
4062 return res;
4063 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4064 break;
4065 case TPACPI_VIDEO_770:
4066 case TPACPI_VIDEO_NEW:
4067 res = video_autosw_set(1);
4068 if (res)
4069 return res;
4070 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4071 break;
4072 default:
4073 return -ENOSYS;
4074 }
4075 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004076 printk(TPACPI_ERR
4077 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004078 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004079 }
4080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004081 return (res)? 0 : -EIO;
4082}
4083
4084static int video_expand_toggle(void)
4085{
4086 switch (video_supported) {
4087 case TPACPI_VIDEO_570:
4088 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4089 0 : -EIO;
4090 case TPACPI_VIDEO_770:
4091 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4092 0 : -EIO;
4093 case TPACPI_VIDEO_NEW:
4094 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4095 0 : -EIO;
4096 default:
4097 return -ENOSYS;
4098 }
4099 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004100}
4101
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004102static int video_read(char *p)
4103{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004104 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004105 int len = 0;
4106
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004107 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004108 len += sprintf(p + len, "status:\t\tnot supported\n");
4109 return len;
4110 }
4111
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004112 status = video_outputsw_get();
4113 if (status < 0)
4114 return status;
4115
4116 autosw = video_autosw_get();
4117 if (autosw < 0)
4118 return autosw;
4119
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004120 len += sprintf(p + len, "status:\t\tsupported\n");
4121 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4122 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004123 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004124 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4125 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4126 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4127 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004128 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004129 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4130 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4131 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4132
4133 return len;
4134}
4135
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004136static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 char *cmd;
4139 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004140 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004142 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004143 return -ENODEV;
4144
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004145 enable = 0;
4146 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 while ((cmd = next_cmd(&buf))) {
4149 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004150 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004152 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004154 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004156 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004157 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004158 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004159 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004160 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004161 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004162 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004164 res = video_autosw_set(1);
4165 if (res)
4166 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004168 res = video_autosw_set(0);
4169 if (res)
4170 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004172 res = video_outputsw_cycle();
4173 if (res)
4174 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004176 res = video_expand_toggle();
4177 if (res)
4178 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 } else
4180 return -EINVAL;
4181 }
4182
4183 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004184 status = video_outputsw_get();
4185 if (status < 0)
4186 return status;
4187 res = video_outputsw_set((status & ~disable) | enable);
4188 if (res)
4189 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 }
4191
4192 return 0;
4193}
4194
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004195static struct ibm_struct video_driver_data = {
4196 .name = "video",
4197 .read = video_read,
4198 .write = video_write,
4199 .exit = video_exit,
4200};
4201
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004202#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4203
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004204/*************************************************************************
4205 * Light (thinklight) subdriver
4206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004208TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4209TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004210
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004211static int light_get_status(void)
4212{
4213 int status = 0;
4214
4215 if (tp_features.light_status) {
4216 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4217 return -EIO;
4218 return (!!status);
4219 }
4220
4221 return -ENXIO;
4222}
4223
4224static int light_set_status(int status)
4225{
4226 int rc;
4227
4228 if (tp_features.light) {
4229 if (cmos_handle) {
4230 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4231 (status)?
4232 TP_CMOS_THINKLIGHT_ON :
4233 TP_CMOS_THINKLIGHT_OFF);
4234 } else {
4235 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4236 (status)? 1 : 0);
4237 }
4238 return (rc)? 0 : -EIO;
4239 }
4240
4241 return -ENXIO;
4242}
4243
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004244static void light_set_status_worker(struct work_struct *work)
4245{
4246 struct tpacpi_led_classdev *data =
4247 container_of(work, struct tpacpi_led_classdev, work);
4248
4249 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004250 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004251}
4252
4253static void light_sysfs_set(struct led_classdev *led_cdev,
4254 enum led_brightness brightness)
4255{
4256 struct tpacpi_led_classdev *data =
4257 container_of(led_cdev,
4258 struct tpacpi_led_classdev,
4259 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004260 data->new_state = (brightness != LED_OFF) ?
4261 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004262 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004263}
4264
4265static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4266{
4267 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4268}
4269
4270static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4271 .led_classdev = {
4272 .name = "tpacpi::thinklight",
4273 .brightness_set = &light_sysfs_set,
4274 .brightness_get = &light_sysfs_get,
4275 }
4276};
4277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004278static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004280 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004281
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004282 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4283
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004284 TPACPI_ACPIHANDLE_INIT(ledb);
4285 TPACPI_ACPIHANDLE_INIT(lght);
4286 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004287 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004288
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004289 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004290 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004291
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004292 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004293 /* light status not supported on
4294 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004295 tp_features.light_status =
4296 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004298 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4299 str_supported(tp_features.light),
4300 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004301
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004302 if (!tp_features.light)
4303 return 1;
4304
4305 rc = led_classdev_register(&tpacpi_pdev->dev,
4306 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004307
4308 if (rc < 0) {
4309 tp_features.light = 0;
4310 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004311 } else {
4312 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004313 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004314
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004315 return rc;
4316}
4317
4318static void light_exit(void)
4319{
4320 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4321 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004322 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004325static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004328 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004330 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004331 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004332 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004333 len += sprintf(p + len, "status:\t\tunknown\n");
4334 len += sprintf(p + len, "commands:\ton, off\n");
4335 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004336 status = light_get_status();
4337 if (status < 0)
4338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340 len += sprintf(p + len, "commands:\ton, off\n");
4341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 return len;
4344}
4345
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004346static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004349 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004351 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004352 return -ENODEV;
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 while ((cmd = next_cmd(&buf))) {
4355 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004356 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004358 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 } else
4360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004363 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004366static struct ibm_struct light_driver_data = {
4367 .name = "light",
4368 .read = light_read,
4369 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004370 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004371};
4372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004373/*************************************************************************
4374 * Dock subdriver
4375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004377#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004378
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004379static void dock_notify(struct ibm_struct *ibm, u32 event);
4380static int dock_read(char *p);
4381static int dock_write(char *buf);
4382
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004383TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004384 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4385 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4386 "\\_SB.PCI.ISA.SLCE", /* 570 */
4387 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4388
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004389/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004390TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004391
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004392static const struct acpi_device_id ibm_pci_device_ids[] = {
4393 {PCI_ROOT_HID_STRING, 0},
4394 {"", 0},
4395};
4396
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004397static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4398 {
4399 .notify = dock_notify,
4400 .handle = &dock_handle,
4401 .type = ACPI_SYSTEM_NOTIFY,
4402 },
4403 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004404 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4405 * We just use it to get notifications of dock hotplug
4406 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004407 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004408 .notify = dock_notify,
4409 .handle = &pci_handle,
4410 .type = ACPI_SYSTEM_NOTIFY,
4411 },
4412};
4413
4414static struct ibm_struct dock_driver_data[2] = {
4415 {
4416 .name = "dock",
4417 .read = dock_read,
4418 .write = dock_write,
4419 .acpi = &ibm_dock_acpidriver[0],
4420 },
4421 {
4422 .name = "dock",
4423 .acpi = &ibm_dock_acpidriver[1],
4424 },
4425};
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427#define dock_docked() (_sta(dock_handle) & 1)
4428
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004429static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004430{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004431 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4432
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004433 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004434
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004435 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4436 str_supported(dock_handle != NULL));
4437
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004438 return (dock_handle)? 0 : 1;
4439}
4440
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004441static int __init dock_init2(struct ibm_init_struct *iibm)
4442{
4443 int dock2_needed;
4444
4445 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4446
4447 if (dock_driver_data[0].flags.acpi_driver_registered &&
4448 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004449 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004450 dock2_needed = (pci_handle != NULL);
4451 vdbg_printk(TPACPI_DBG_INIT,
4452 "dock PCI handler for the TP 570 is %s\n",
4453 str_supported(dock2_needed));
4454 } else {
4455 vdbg_printk(TPACPI_DBG_INIT,
4456 "dock subdriver part 2 not required\n");
4457 dock2_needed = 0;
4458 }
4459
4460 return (dock2_needed)? 0 : 1;
4461}
4462
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004463static void dock_notify(struct ibm_struct *ibm, u32 event)
4464{
4465 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004466 int pci = ibm->acpi->hid && ibm->acpi->device &&
4467 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004468 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004469
4470 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004471 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004472 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004473 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004474 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004475 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004476 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004477 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004478 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004479 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004480 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004481 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004482 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004483 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004484 }
Len Brown14e04fb32007-08-23 15:20:26 -04004485 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004486 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004487 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004488 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004489}
4490
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004491static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 int len = 0;
4494 int docked = dock_docked();
4495
4496 if (!dock_handle)
4497 len += sprintf(p + len, "status:\t\tnot supported\n");
4498 else if (!docked)
4499 len += sprintf(p + len, "status:\t\tundocked\n");
4500 else {
4501 len += sprintf(p + len, "status:\t\tdocked\n");
4502 len += sprintf(p + len, "commands:\tdock, undock\n");
4503 }
4504
4505 return len;
4506}
4507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004508static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
4510 char *cmd;
4511
4512 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004513 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515 while ((cmd = next_cmd(&buf))) {
4516 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004517 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4518 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return -EIO;
4520 } else if (strlencmp(cmd, "dock") == 0) {
4521 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4522 return -EIO;
4523 } else
4524 return -EINVAL;
4525 }
4526
4527 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004530#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004532/*************************************************************************
4533 * Bay subdriver
4534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004536#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004538TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4540 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4541 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4542 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004543TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004544 "_EJ0", /* all others */
4545 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004546TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004547 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4548 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004549TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004550 "_EJ0", /* 770x */
4551 ); /* all others */
4552
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004553static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004555 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4556
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004557 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004558 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004559 TPACPI_ACPIHANDLE_INIT(bay_ej);
4560 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004561 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004562 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004563
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004564 tp_features.bay_status = bay_handle &&
4565 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4566 tp_features.bay_status2 = bay2_handle &&
4567 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004569 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4570 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4571 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4572 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004574 vdbg_printk(TPACPI_DBG_INIT,
4575 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004576 str_supported(tp_features.bay_status),
4577 str_supported(tp_features.bay_eject),
4578 str_supported(tp_features.bay_status2),
4579 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004580
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004581 return (tp_features.bay_status || tp_features.bay_eject ||
4582 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004585static void bay_notify(struct ibm_struct *ibm, u32 event)
4586{
Len Brown14e04fb32007-08-23 15:20:26 -04004587 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004588 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004589 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004590 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004591}
4592
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004593#define bay_occupied(b) (_sta(b##_handle) & 1)
4594
4595static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004598 int occupied = bay_occupied(bay);
4599 int occupied2 = bay_occupied(bay2);
4600 int eject, eject2;
4601
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004602 len += sprintf(p + len, "status:\t\t%s\n",
4603 tp_features.bay_status ?
4604 (occupied ? "occupied" : "unoccupied") :
4605 "not supported");
4606 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004607 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4608 "occupied" : "unoccupied");
4609
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004610 eject = tp_features.bay_eject && occupied;
4611 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004612
4613 if (eject && eject2)
4614 len += sprintf(p + len, "commands:\teject, eject2\n");
4615 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004617 else if (eject2)
4618 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 return len;
4621}
4622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004623static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
4625 char *cmd;
4626
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004627 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004628 return -ENODEV;
4629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004631 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004632 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4633 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004634 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004635 strlencmp(cmd, "eject2") == 0) {
4636 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return -EIO;
4638 } else
4639 return -EINVAL;
4640 }
4641
4642 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004643}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004644
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004645static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4646 .notify = bay_notify,
4647 .handle = &bay_handle,
4648 .type = ACPI_SYSTEM_NOTIFY,
4649};
4650
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004651static struct ibm_struct bay_driver_data = {
4652 .name = "bay",
4653 .read = bay_read,
4654 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004655 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004656};
4657
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004658#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004660/*************************************************************************
4661 * CMOS subdriver
4662 */
4663
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004664/* sysfs cmos_command -------------------------------------------------- */
4665static ssize_t cmos_command_store(struct device *dev,
4666 struct device_attribute *attr,
4667 const char *buf, size_t count)
4668{
4669 unsigned long cmos_cmd;
4670 int res;
4671
4672 if (parse_strtoul(buf, 21, &cmos_cmd))
4673 return -EINVAL;
4674
4675 res = issue_thinkpad_cmos_command(cmos_cmd);
4676 return (res)? res : count;
4677}
4678
4679static struct device_attribute dev_attr_cmos_command =
4680 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4681
4682/* --------------------------------------------------------------------- */
4683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004684static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004685{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004686 int res;
4687
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004688 vdbg_printk(TPACPI_DBG_INIT,
4689 "initializing cmos commands subdriver\n");
4690
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004691 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004692
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004693 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4694 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004695
4696 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4697 if (res)
4698 return res;
4699
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004700 return (cmos_handle)? 0 : 1;
4701}
4702
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004703static void cmos_exit(void)
4704{
4705 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4706}
4707
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004708static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
4710 int len = 0;
4711
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004712 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4713 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (!cmos_handle)
4715 len += sprintf(p + len, "status:\t\tnot supported\n");
4716 else {
4717 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004718 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720
4721 return len;
4722}
4723
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004724static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
4726 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004727 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004730 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4731 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 /* cmos_cmd set */
4733 } else
4734 return -EINVAL;
4735
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004736 res = issue_thinkpad_cmos_command(cmos_cmd);
4737 if (res)
4738 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
4740
4741 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004742}
4743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004744static struct ibm_struct cmos_driver_data = {
4745 .name = "cmos",
4746 .read = cmos_read,
4747 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004748 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004749};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004750
4751/*************************************************************************
4752 * LED subdriver
4753 */
4754
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004755enum led_access_mode {
4756 TPACPI_LED_NONE = 0,
4757 TPACPI_LED_570, /* 570 */
4758 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4759 TPACPI_LED_NEW, /* all others */
4760};
4761
4762enum { /* For TPACPI_LED_OLD */
4763 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4764 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4765 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4766};
4767
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004768static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004769
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004770TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004771 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4772 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004773 "LED", /* all others */
4774 ); /* R30, R31 */
4775
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004776#define TPACPI_LED_NUMLEDS 8
4777static struct tpacpi_led_classdev *tpacpi_leds;
4778static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004779static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004780 /* there's a limit of 19 chars + NULL before 2.6.26 */
4781 "tpacpi::power",
4782 "tpacpi:orange:batt",
4783 "tpacpi:green:batt",
4784 "tpacpi::dock_active",
4785 "tpacpi::bay_active",
4786 "tpacpi::dock_batt",
4787 "tpacpi::unknown_led",
4788 "tpacpi::standby",
4789};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004790#define TPACPI_SAFE_LEDS 0x0081U
4791
4792static inline bool tpacpi_is_led_restricted(const unsigned int led)
4793{
4794#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4795 return false;
4796#else
4797 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4798#endif
4799}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004800
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004801static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004802{
4803 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004804 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004805
4806 switch (led_supported) {
4807 case TPACPI_LED_570:
4808 if (!acpi_evalf(ec_handle,
4809 &status, "GLED", "dd", 1 << led))
4810 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004811 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004812 TPACPI_LED_OFF :
4813 ((status == 1)?
4814 TPACPI_LED_ON :
4815 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004816 tpacpi_led_state_cache[led] = led_s;
4817 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004818 default:
4819 return -ENXIO;
4820 }
4821
4822 /* not reached */
4823}
4824
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004825static int led_set_status(const unsigned int led,
4826 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004827{
4828 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004829 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4830 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004831
4832 int rc = 0;
4833
4834 switch (led_supported) {
4835 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004836 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004837 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004838 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004839 if (unlikely(tpacpi_is_led_restricted(led)))
4840 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004841 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4842 (1 << led), led_sled_arg1[ledstatus]))
4843 rc = -EIO;
4844 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004845 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004846 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004847 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004848 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004849 if (unlikely(tpacpi_is_led_restricted(led)))
4850 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004851 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4852 if (rc >= 0)
4853 rc = ec_write(TPACPI_LED_EC_HLBL,
4854 (ledstatus == TPACPI_LED_BLINK) << led);
4855 if (rc >= 0)
4856 rc = ec_write(TPACPI_LED_EC_HLCL,
4857 (ledstatus != TPACPI_LED_OFF) << led);
4858 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004859 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004860 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004861 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4862 return -EINVAL;
4863 if (unlikely(tpacpi_is_led_restricted(led)))
4864 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004865 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4866 led, led_led_arg1[ledstatus]))
4867 rc = -EIO;
4868 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004869 default:
4870 rc = -ENXIO;
4871 }
4872
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004873 if (!rc)
4874 tpacpi_led_state_cache[led] = ledstatus;
4875
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004876 return rc;
4877}
4878
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004879static void led_set_status_worker(struct work_struct *work)
4880{
4881 struct tpacpi_led_classdev *data =
4882 container_of(work, struct tpacpi_led_classdev, work);
4883
4884 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004885 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004886}
4887
4888static void led_sysfs_set(struct led_classdev *led_cdev,
4889 enum led_brightness brightness)
4890{
4891 struct tpacpi_led_classdev *data = container_of(led_cdev,
4892 struct tpacpi_led_classdev, led_classdev);
4893
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004894 if (brightness == LED_OFF)
4895 data->new_state = TPACPI_LED_OFF;
4896 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4897 data->new_state = TPACPI_LED_ON;
4898 else
4899 data->new_state = TPACPI_LED_BLINK;
4900
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004901 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004902}
4903
4904static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4905 unsigned long *delay_on, unsigned long *delay_off)
4906{
4907 struct tpacpi_led_classdev *data = container_of(led_cdev,
4908 struct tpacpi_led_classdev, led_classdev);
4909
4910 /* Can we choose the flash rate? */
4911 if (*delay_on == 0 && *delay_off == 0) {
4912 /* yes. set them to the hardware blink rate (1 Hz) */
4913 *delay_on = 500; /* ms */
4914 *delay_off = 500; /* ms */
4915 } else if ((*delay_on != 500) || (*delay_off != 500))
4916 return -EINVAL;
4917
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004918 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004919 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004920
4921 return 0;
4922}
4923
4924static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4925{
4926 int rc;
4927
4928 struct tpacpi_led_classdev *data = container_of(led_cdev,
4929 struct tpacpi_led_classdev, led_classdev);
4930
4931 rc = led_get_status(data->led);
4932
4933 if (rc == TPACPI_LED_OFF || rc < 0)
4934 rc = LED_OFF; /* no error handling in led class :( */
4935 else
4936 rc = LED_FULL;
4937
4938 return rc;
4939}
4940
4941static void led_exit(void)
4942{
4943 unsigned int i;
4944
4945 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4946 if (tpacpi_leds[i].led_classdev.name)
4947 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4948 }
4949
4950 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004951}
4952
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004953static int __init tpacpi_init_led(unsigned int led)
4954{
4955 int rc;
4956
4957 tpacpi_leds[led].led = led;
4958
4959 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4960 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4961 if (led_supported == TPACPI_LED_570)
4962 tpacpi_leds[led].led_classdev.brightness_get =
4963 &led_sysfs_get;
4964
4965 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4966
4967 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4968
4969 rc = led_classdev_register(&tpacpi_pdev->dev,
4970 &tpacpi_leds[led].led_classdev);
4971 if (rc < 0)
4972 tpacpi_leds[led].led_classdev.name = NULL;
4973
4974 return rc;
4975}
4976
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004977static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004978{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004979 unsigned int i;
4980 int rc;
4981
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004982 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4983
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004984 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004985
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004986 if (!led_handle)
4987 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004988 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004989 else if (strlencmp(led_path, "SLED") == 0)
4990 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004991 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004992 else if (strlencmp(led_path, "SYSL") == 0)
4993 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004994 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004995 else
4996 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004997 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004998
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004999 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5000 str_supported(led_supported), led_supported);
5001
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005002 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5003 GFP_KERNEL);
5004 if (!tpacpi_leds) {
5005 printk(TPACPI_ERR "Out of memory for LED data\n");
5006 return -ENOMEM;
5007 }
5008
5009 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005010 if (!tpacpi_is_led_restricted(i)) {
5011 rc = tpacpi_init_led(i);
5012 if (rc < 0) {
5013 led_exit();
5014 return rc;
5015 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005016 }
5017 }
5018
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005019#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5020 if (led_supported != TPACPI_LED_NONE)
5021 printk(TPACPI_NOTICE
5022 "warning: userspace override of important "
5023 "firmware LEDs is enabled\n");
5024#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005025 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005026}
5027
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005028#define str_led_status(s) \
5029 ((s) == TPACPI_LED_OFF ? "off" : \
5030 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005031
5032static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 int len = 0;
5035
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005036 if (!led_supported) {
5037 len += sprintf(p + len, "status:\t\tnot supported\n");
5038 return len;
5039 }
5040 len += sprintf(p + len, "status:\t\tsupported\n");
5041
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005042 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005043 /* 570 */
5044 int i, status;
5045 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046 status = led_get_status(i);
5047 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005048 return -EIO;
5049 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005051 }
5052 }
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005055 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
5057 return len;
5058}
5059
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005060static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061{
5062 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005063 int led, rc;
5064 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005065
5066 if (!led_supported)
5067 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005070 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 return -EINVAL;
5072
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005073 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005074 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005076 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005077 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005078 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005079 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005080 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005081 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005082
5083 rc = led_set_status(led, s);
5084 if (rc < 0)
5085 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
5087
5088 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005089}
5090
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005091static struct ibm_struct led_driver_data = {
5092 .name = "led",
5093 .read = led_read,
5094 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005095 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005096};
5097
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005098/*************************************************************************
5099 * Beep subdriver
5100 */
5101
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005102TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005103
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005104static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005105{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005106 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5107
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005108 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005109
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005110 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5111 str_supported(beep_handle != NULL));
5112
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005113 return (beep_handle)? 0 : 1;
5114}
5115
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005116static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
5118 int len = 0;
5119
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005120 if (!beep_handle)
5121 len += sprintf(p + len, "status:\t\tnot supported\n");
5122 else {
5123 len += sprintf(p + len, "status:\t\tsupported\n");
5124 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
5127 return len;
5128}
5129
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005130static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131{
5132 char *cmd;
5133 int beep_cmd;
5134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005135 if (!beep_handle)
5136 return -ENODEV;
5137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005139 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5140 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 /* beep_cmd set */
5142 } else
5143 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 return -EIO;
5146 }
5147
5148 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005149}
5150
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005151static struct ibm_struct beep_driver_data = {
5152 .name = "beep",
5153 .read = beep_read,
5154 .write = beep_write,
5155};
5156
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005157/*************************************************************************
5158 * Thermal subdriver
5159 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005160
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005161enum thermal_access_mode {
5162 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5163 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5164 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5165 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5166 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5167};
5168
5169enum { /* TPACPI_THERMAL_TPEC_* */
5170 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5171 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5172 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5173};
5174
5175#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5176struct ibm_thermal_sensors_struct {
5177 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5178};
5179
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005180static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005181
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005182/* idx is zero-based */
5183static int thermal_get_sensor(int idx, s32 *value)
5184{
5185 int t;
5186 s8 tmp;
5187 char tmpi[5];
5188
5189 t = TP_EC_THERMAL_TMP0;
5190
5191 switch (thermal_read_mode) {
5192#if TPACPI_MAX_THERMAL_SENSORS >= 16
5193 case TPACPI_THERMAL_TPEC_16:
5194 if (idx >= 8 && idx <= 15) {
5195 t = TP_EC_THERMAL_TMP8;
5196 idx -= 8;
5197 }
5198 /* fallthrough */
5199#endif
5200 case TPACPI_THERMAL_TPEC_8:
5201 if (idx <= 7) {
5202 if (!acpi_ec_read(t + idx, &tmp))
5203 return -EIO;
5204 *value = tmp * 1000;
5205 return 0;
5206 }
5207 break;
5208
5209 case TPACPI_THERMAL_ACPI_UPDT:
5210 if (idx <= 7) {
5211 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5212 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5213 return -EIO;
5214 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5215 return -EIO;
5216 *value = (t - 2732) * 100;
5217 return 0;
5218 }
5219 break;
5220
5221 case TPACPI_THERMAL_ACPI_TMP07:
5222 if (idx <= 7) {
5223 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5224 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5225 return -EIO;
5226 if (t > 127 || t < -127)
5227 t = TP_EC_THERMAL_TMP_NA;
5228 *value = t * 1000;
5229 return 0;
5230 }
5231 break;
5232
5233 case TPACPI_THERMAL_NONE:
5234 default:
5235 return -ENOSYS;
5236 }
5237
5238 return -EINVAL;
5239}
5240
5241static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5242{
5243 int res, i;
5244 int n;
5245
5246 n = 8;
5247 i = 0;
5248
5249 if (!s)
5250 return -EINVAL;
5251
5252 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5253 n = 16;
5254
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005255 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005256 res = thermal_get_sensor(i, &s->temp[i]);
5257 if (res)
5258 return res;
5259 }
5260
5261 return n;
5262}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005263
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005264/* sysfs temp##_input -------------------------------------------------- */
5265
5266static ssize_t thermal_temp_input_show(struct device *dev,
5267 struct device_attribute *attr,
5268 char *buf)
5269{
5270 struct sensor_device_attribute *sensor_attr =
5271 to_sensor_dev_attr(attr);
5272 int idx = sensor_attr->index;
5273 s32 value;
5274 int res;
5275
5276 res = thermal_get_sensor(idx, &value);
5277 if (res)
5278 return res;
5279 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5280 return -ENXIO;
5281
5282 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5283}
5284
5285#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005286 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5287 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005288
5289static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5290 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5291 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5292 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5293 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5294 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5295 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5296 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5297 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5298 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5299 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5300 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5301 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5302 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5303 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5304 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5305 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5306};
5307
5308#define THERMAL_ATTRS(X) \
5309 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5310
5311static struct attribute *thermal_temp_input_attr[] = {
5312 THERMAL_ATTRS(8),
5313 THERMAL_ATTRS(9),
5314 THERMAL_ATTRS(10),
5315 THERMAL_ATTRS(11),
5316 THERMAL_ATTRS(12),
5317 THERMAL_ATTRS(13),
5318 THERMAL_ATTRS(14),
5319 THERMAL_ATTRS(15),
5320 THERMAL_ATTRS(0),
5321 THERMAL_ATTRS(1),
5322 THERMAL_ATTRS(2),
5323 THERMAL_ATTRS(3),
5324 THERMAL_ATTRS(4),
5325 THERMAL_ATTRS(5),
5326 THERMAL_ATTRS(6),
5327 THERMAL_ATTRS(7),
5328 NULL
5329};
5330
5331static const struct attribute_group thermal_temp_input16_group = {
5332 .attrs = thermal_temp_input_attr
5333};
5334
5335static const struct attribute_group thermal_temp_input8_group = {
5336 .attrs = &thermal_temp_input_attr[8]
5337};
5338
5339#undef THERMAL_SENSOR_ATTR_TEMP
5340#undef THERMAL_ATTRS
5341
5342/* --------------------------------------------------------------------- */
5343
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005344static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005345{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005346 u8 t, ta1, ta2;
5347 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005348 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005349 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005350
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005351 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5352
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005353 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005355 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005356 /*
5357 * Direct EC access mode: sensors at registers
5358 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5359 * non-implemented, thermal sensors return 0x80 when
5360 * not available
5361 */
5362
5363 ta1 = ta2 = 0;
5364 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005365 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005366 ta1 |= t;
5367 } else {
5368 ta1 = 0;
5369 break;
5370 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005371 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005372 ta2 |= t;
5373 } else {
5374 ta1 = 0;
5375 break;
5376 }
5377 }
5378 if (ta1 == 0) {
5379 /* This is sheer paranoia, but we handle it anyway */
5380 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005381 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005382 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005383 "falling back to ACPI TMPx access "
5384 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005385 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005386 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005387 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005388 "ThinkPad ACPI EC access misbehaving, "
5389 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005390 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005391 }
5392 } else {
5393 thermal_read_mode =
5394 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005395 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005396 }
5397 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005398 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5399 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005400 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005401 } else {
5402 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005404 }
5405 } else {
5406 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005407 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005408 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005410 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5411 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5412 thermal_read_mode);
5413
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005414 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005415 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005416 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005417 &thermal_temp_input16_group);
5418 if (res)
5419 return res;
5420 break;
5421 case TPACPI_THERMAL_TPEC_8:
5422 case TPACPI_THERMAL_ACPI_TMP07:
5423 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005424 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005425 &thermal_temp_input8_group);
5426 if (res)
5427 return res;
5428 break;
5429 case TPACPI_THERMAL_NONE:
5430 default:
5431 return 1;
5432 }
5433
5434 return 0;
5435}
5436
5437static void thermal_exit(void)
5438{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005439 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005440 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005441 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005442 &thermal_temp_input16_group);
5443 break;
5444 case TPACPI_THERMAL_TPEC_8:
5445 case TPACPI_THERMAL_ACPI_TMP07:
5446 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005447 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005448 &thermal_temp_input16_group);
5449 break;
5450 case TPACPI_THERMAL_NONE:
5451 default:
5452 break;
5453 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005454}
5455
5456static int thermal_read(char *p)
5457{
5458 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005459 int n, i;
5460 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005461
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005462 n = thermal_get_sensors(&t);
5463 if (unlikely(n < 0))
5464 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005465
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005466 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005467
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005468 if (n > 0) {
5469 for (i = 0; i < (n - 1); i++)
5470 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5471 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5472 } else
5473 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005474
5475 return len;
5476}
5477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005478static struct ibm_struct thermal_driver_data = {
5479 .name = "thermal",
5480 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005481 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005482};
5483
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005484/*************************************************************************
5485 * EC Dump subdriver
5486 */
5487
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005488static u8 ecdump_regs[256];
5489
5490static int ecdump_read(char *p)
5491{
5492 int len = 0;
5493 int i, j;
5494 u8 v;
5495
5496 len += sprintf(p + len, "EC "
5497 " +00 +01 +02 +03 +04 +05 +06 +07"
5498 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5499 for (i = 0; i < 256; i += 16) {
5500 len += sprintf(p + len, "EC 0x%02x:", i);
5501 for (j = 0; j < 16; j++) {
5502 if (!acpi_ec_read(i + j, &v))
5503 break;
5504 if (v != ecdump_regs[i + j])
5505 len += sprintf(p + len, " *%02x", v);
5506 else
5507 len += sprintf(p + len, " %02x", v);
5508 ecdump_regs[i + j] = v;
5509 }
5510 len += sprintf(p + len, "\n");
5511 if (j != 16)
5512 break;
5513 }
5514
5515 /* These are way too dangerous to advertise openly... */
5516#if 0
5517 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5518 " (<offset> is 00-ff, <value> is 00-ff)\n");
5519 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5520 " (<offset> is 00-ff, <value> is 0-255)\n");
5521#endif
5522 return len;
5523}
5524
5525static int ecdump_write(char *buf)
5526{
5527 char *cmd;
5528 int i, v;
5529
5530 while ((cmd = next_cmd(&buf))) {
5531 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5532 /* i and v set */
5533 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5534 /* i and v set */
5535 } else
5536 return -EINVAL;
5537 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5538 if (!acpi_ec_write(i, v))
5539 return -EIO;
5540 } else
5541 return -EINVAL;
5542 }
5543
5544 return 0;
5545}
5546
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005547static struct ibm_struct ecdump_driver_data = {
5548 .name = "ecdump",
5549 .read = ecdump_read,
5550 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005551 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005552};
5553
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005554/*************************************************************************
5555 * Backlight/brightness subdriver
5556 */
Holger Macht8acb0252006-10-20 14:30:28 -07005557
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005558#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5559
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005560/*
5561 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5562 * CMOS NVRAM byte 0x5E, bits 0-3.
5563 *
5564 * EC HBRV (0x31) has the following layout
5565 * Bit 7: unknown function
5566 * Bit 6: unknown function
5567 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5568 * Bit 4: must be set to zero to avoid problems
5569 * Bit 3-0: backlight brightness level
5570 *
5571 * brightness_get_raw returns status data in the HBRV layout
5572 */
5573
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005574enum {
5575 TP_EC_BACKLIGHT = 0x31,
5576
5577 /* TP_EC_BACKLIGHT bitmasks */
5578 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5579 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5580 TP_EC_BACKLIGHT_MAPSW = 0x20,
5581};
5582
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005583enum tpacpi_brightness_access_mode {
5584 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5585 TPACPI_BRGHT_MODE_EC, /* EC control */
5586 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5587 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5588 TPACPI_BRGHT_MODE_MAX
5589};
5590
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005591static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005592
5593static enum tpacpi_brightness_access_mode brightness_mode =
5594 TPACPI_BRGHT_MODE_MAX;
5595
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005596static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5597
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005598static struct mutex brightness_mutex;
5599
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005600/* NVRAM brightness access,
5601 * call with brightness_mutex held! */
5602static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005603{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005604 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005605
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005606 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5607 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5608 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5609 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005610
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005611 return lnvram;
5612}
5613
5614static void tpacpi_brightness_checkpoint_nvram(void)
5615{
5616 u8 lec = 0;
5617 u8 b_nvram;
5618
5619 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5620 return;
5621
5622 vdbg_printk(TPACPI_DBG_BRGHT,
5623 "trying to checkpoint backlight level to NVRAM...\n");
5624
5625 if (mutex_lock_killable(&brightness_mutex) < 0)
5626 return;
5627
5628 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5629 goto unlock;
5630 lec &= TP_EC_BACKLIGHT_LVLMSK;
5631 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5632
5633 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5634 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5635 /* NVRAM needs update */
5636 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5637 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5638 b_nvram |= lec;
5639 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5640 dbg_printk(TPACPI_DBG_BRGHT,
5641 "updated NVRAM backlight level to %u (0x%02x)\n",
5642 (unsigned int) lec, (unsigned int) b_nvram);
5643 } else
5644 vdbg_printk(TPACPI_DBG_BRGHT,
5645 "NVRAM backlight level already is %u (0x%02x)\n",
5646 (unsigned int) lec, (unsigned int) b_nvram);
5647
5648unlock:
5649 mutex_unlock(&brightness_mutex);
5650}
5651
5652
5653/* call with brightness_mutex held! */
5654static int tpacpi_brightness_get_raw(int *status)
5655{
5656 u8 lec = 0;
5657
5658 switch (brightness_mode) {
5659 case TPACPI_BRGHT_MODE_UCMS_STEP:
5660 *status = tpacpi_brightness_nvram_get();
5661 return 0;
5662 case TPACPI_BRGHT_MODE_EC:
5663 case TPACPI_BRGHT_MODE_ECNVRAM:
5664 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005665 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005666 *status = lec;
5667 return 0;
5668 default:
5669 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005670 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005671}
5672
5673/* call with brightness_mutex held! */
5674/* do NOT call with illegal backlight level value */
5675static int tpacpi_brightness_set_ec(unsigned int value)
5676{
5677 u8 lec = 0;
5678
5679 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5680 return -EIO;
5681
5682 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5683 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5684 (value & TP_EC_BACKLIGHT_LVLMSK))))
5685 return -EIO;
5686
5687 return 0;
5688}
5689
5690/* call with brightness_mutex held! */
5691static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5692{
5693 int cmos_cmd, inc;
5694 unsigned int current_value, i;
5695
5696 current_value = tpacpi_brightness_nvram_get();
5697
5698 if (value == current_value)
5699 return 0;
5700
5701 cmos_cmd = (value > current_value) ?
5702 TP_CMOS_BRIGHTNESS_UP :
5703 TP_CMOS_BRIGHTNESS_DOWN;
5704 inc = (value > current_value) ? 1 : -1;
5705
5706 for (i = current_value; i != value; i += inc)
5707 if (issue_thinkpad_cmos_command(cmos_cmd))
5708 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005709
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005710 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005711}
5712
5713/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005714static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005715{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005716 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005717
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005718 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5719 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005720 return -EINVAL;
5721
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005722 vdbg_printk(TPACPI_DBG_BRGHT,
5723 "set backlight level to %d\n", value);
5724
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005725 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005726 if (res < 0)
5727 return res;
5728
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005729 switch (brightness_mode) {
5730 case TPACPI_BRGHT_MODE_EC:
5731 case TPACPI_BRGHT_MODE_ECNVRAM:
5732 res = tpacpi_brightness_set_ec(value);
5733 break;
5734 case TPACPI_BRGHT_MODE_UCMS_STEP:
5735 res = tpacpi_brightness_set_ucmsstep(value);
5736 break;
5737 default:
5738 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005739 }
5740
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005741 mutex_unlock(&brightness_mutex);
5742 return res;
5743}
5744
5745/* sysfs backlight class ----------------------------------------------- */
5746
5747static int brightness_update_status(struct backlight_device *bd)
5748{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005749 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005750 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5751 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005752 bd->props.brightness : 0;
5753
5754 dbg_printk(TPACPI_DBG_BRGHT,
5755 "backlight: attempt to set level to %d\n",
5756 level);
5757
5758 /* it is the backlight class's job (caller) to handle
5759 * EINTR and other errors properly */
5760 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005761}
Holger Macht8acb0252006-10-20 14:30:28 -07005762
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005763static int brightness_get(struct backlight_device *bd)
5764{
5765 int status, res;
5766
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005767 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005768 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005769 return 0;
5770
5771 res = tpacpi_brightness_get_raw(&status);
5772
5773 mutex_unlock(&brightness_mutex);
5774
5775 if (res < 0)
5776 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005777
5778 return status & TP_EC_BACKLIGHT_LVLMSK;
5779}
5780
Richard Purdie599a52d2007-02-10 23:07:48 +00005781static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005782 .get_brightness = brightness_get,
5783 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005784};
5785
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005786/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005788static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005789{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005790 int b;
5791
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005792 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5793
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005794 mutex_init(&brightness_mutex);
5795
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005796 /*
5797 * We always attempt to detect acpi support, so as to switch
5798 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5799 * going to publish a backlight interface
5800 */
5801 b = tpacpi_check_std_acpi_brightness_support();
5802 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005803
5804 if (acpi_video_backlight_support()) {
5805 if (brightness_enable > 1) {
5806 printk(TPACPI_NOTICE
5807 "Standard ACPI backlight interface "
5808 "available, not loading native one.\n");
5809 return 1;
5810 } else if (brightness_enable == 1) {
5811 printk(TPACPI_NOTICE
5812 "Backlight control force enabled, even if standard "
5813 "ACPI backlight interface is available\n");
5814 }
5815 } else {
5816 if (brightness_enable > 1) {
5817 printk(TPACPI_NOTICE
5818 "Standard ACPI backlight interface not "
5819 "available, thinkpad_acpi native "
5820 "brightness control enabled\n");
5821 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005822 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005823 }
5824
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005825 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005826 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005827 "brightness support disabled by "
5828 "module parameter\n");
5829 return 1;
5830 }
5831
5832 if (b > 16) {
5833 printk(TPACPI_ERR
5834 "Unsupported brightness interface, "
5835 "please contact %s\n", TPACPI_MAIL);
5836 return 1;
5837 }
5838 if (b == 16)
5839 tp_features.bright_16levels = 1;
5840
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005841 /*
5842 * Check for module parameter bogosity, note that we
5843 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5844 * able to detect "unspecified"
5845 */
5846 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005847 return -EINVAL;
5848
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005849 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5850 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5851 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5852 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5853 /*
5854 * IBM models that define HBRV probably have
5855 * EC-based backlight level control
5856 */
5857 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5858 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5859 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5860 else
5861 /* all other IBM ThinkPads */
5862 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5863 } else
5864 /* All Lenovo ThinkPads */
5865 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5866
5867 dbg_printk(TPACPI_DBG_BRGHT,
5868 "selected brightness_mode=%d\n",
5869 brightness_mode);
5870 }
5871
5872 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005873 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005874
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005875 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005876 printk(TPACPI_INFO
5877 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005878
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005879 ibm_backlight_device = backlight_device_register(
5880 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5881 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005882 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005883 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005884 return PTR_ERR(ibm_backlight_device);
5885 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005886 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5887 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005888
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005889 ibm_backlight_device->props.max_brightness =
5890 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005891 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005892 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005893
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005894 return 0;
5895}
5896
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005897static void brightness_suspend(pm_message_t state)
5898{
5899 tpacpi_brightness_checkpoint_nvram();
5900}
5901
5902static void brightness_shutdown(void)
5903{
5904 tpacpi_brightness_checkpoint_nvram();
5905}
5906
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005907static void brightness_exit(void)
5908{
5909 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005910 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005911 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005912 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005913 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005914
5915 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005916}
5917
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005918static int brightness_read(char *p)
5919{
5920 int len = 0;
5921 int level;
5922
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005923 level = brightness_get(NULL);
5924 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005925 len += sprintf(p + len, "level:\t\tunreadable\n");
5926 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005927 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005928 len += sprintf(p + len, "commands:\tup, down\n");
5929 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005930 " (<level> is 0-%d)\n",
5931 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005932 }
5933
5934 return len;
5935}
5936
5937static int brightness_write(char *buf)
5938{
5939 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005940 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005941 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005942 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005943
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005944 level = brightness_get(NULL);
5945 if (level < 0)
5946 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005947
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005948 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005949 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005950 if (level < max_level)
5951 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005952 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005953 if (level > 0)
5954 level--;
5955 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5956 level >= 0 && level <= max_level) {
5957 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005958 } else
5959 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005960 }
5961
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005962 tpacpi_disclose_usertask("procfs brightness",
5963 "set level to %d\n", level);
5964
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005965 /*
5966 * Now we know what the final level should be, so we try to set it.
5967 * Doing it this way makes the syscall restartable in case of EINTR
5968 */
5969 rc = brightness_set(level);
5970 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005971}
5972
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005973static struct ibm_struct brightness_driver_data = {
5974 .name = "brightness",
5975 .read = brightness_read,
5976 .write = brightness_write,
5977 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005978 .suspend = brightness_suspend,
5979 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005980};
5981
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005982/*************************************************************************
5983 * Volume subdriver
5984 */
5985
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005986static int volume_offset = 0x30;
5987
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005988static int volume_read(char *p)
5989{
5990 int len = 0;
5991 u8 level;
5992
5993 if (!acpi_ec_read(volume_offset, &level)) {
5994 len += sprintf(p + len, "level:\t\tunreadable\n");
5995 } else {
5996 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5997 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5998 len += sprintf(p + len, "commands:\tup, down, mute\n");
5999 len += sprintf(p + len, "commands:\tlevel <level>"
6000 " (<level> is 0-15)\n");
6001 }
6002
6003 return len;
6004}
6005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006006static int volume_write(char *buf)
6007{
6008 int cmos_cmd, inc, i;
6009 u8 level, mute;
6010 int new_level, new_mute;
6011 char *cmd;
6012
6013 while ((cmd = next_cmd(&buf))) {
6014 if (!acpi_ec_read(volume_offset, &level))
6015 return -EIO;
6016 new_mute = mute = level & 0x40;
6017 new_level = level = level & 0xf;
6018
6019 if (strlencmp(cmd, "up") == 0) {
6020 if (mute)
6021 new_mute = 0;
6022 else
6023 new_level = level == 15 ? 15 : level + 1;
6024 } else if (strlencmp(cmd, "down") == 0) {
6025 if (mute)
6026 new_mute = 0;
6027 else
6028 new_level = level == 0 ? 0 : level - 1;
6029 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6030 new_level >= 0 && new_level <= 15) {
6031 /* new_level set */
6032 } else if (strlencmp(cmd, "mute") == 0) {
6033 new_mute = 0x40;
6034 } else
6035 return -EINVAL;
6036
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006037 if (new_level != level) {
6038 /* mute doesn't change */
6039
6040 cmos_cmd = (new_level > level) ?
6041 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006042 inc = new_level > level ? 1 : -1;
6043
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006044 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006045 !acpi_ec_write(volume_offset, level)))
6046 return -EIO;
6047
6048 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006049 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006050 !acpi_ec_write(volume_offset, i + inc))
6051 return -EIO;
6052
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006053 if (mute &&
6054 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6055 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006056 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006057 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006058 }
6059
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006060 if (new_mute != mute) {
6061 /* level doesn't change */
6062
6063 cmos_cmd = (new_mute) ?
6064 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006065
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03006066 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006067 !acpi_ec_write(volume_offset, level + new_mute))
6068 return -EIO;
6069 }
6070 }
6071
6072 return 0;
6073}
6074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006075static struct ibm_struct volume_driver_data = {
6076 .name = "volume",
6077 .read = volume_read,
6078 .write = volume_write,
6079};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006080
6081/*************************************************************************
6082 * Fan subdriver
6083 */
6084
6085/*
6086 * FAN ACCESS MODES
6087 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006088 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006089 * ACPI GFAN method: returns fan level
6090 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006091 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006092 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006093 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006094 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006095 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6096 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006097 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6098 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006099 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006100 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006101 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6102 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006103 *
6104 * Fan speed changes of any sort (including those caused by the
6105 * disengaged mode) are usually done slowly by the firmware as the
6106 * maximum ammount of fan duty cycle change per second seems to be
6107 * limited.
6108 *
6109 * Reading is not available if GFAN exists.
6110 * Writing is not available if SFAN exists.
6111 *
6112 * Bits
6113 * 7 automatic mode engaged;
6114 * (default operation mode of the ThinkPad)
6115 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006116 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006117 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006118 * the tachometer while the fan controller ramps up
6119 * the speed (which can take up to a few *minutes*).
6120 * Speeds up fan to 100% duty-cycle, which is far above
6121 * the standard RPM levels. It is not impossible that
6122 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006123 * 5-3 unused in some models. Extra bits for fan level
6124 * in others, but still useless as all values above
6125 * 7 map to the same speed as level 7 in these models.
6126 * 2-0 fan level (0..7 usually)
6127 * 0x00 = stop
6128 * 0x07 = max (set when temperatures critical)
6129 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006130 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006131 *
6132 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6133 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6134 * does so, its initial value is meaningless (0x07).
6135 *
6136 * For firmware bugs, refer to:
6137 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6138 *
6139 * ----
6140 *
6141 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6142 * Main fan tachometer reading (in RPM)
6143 *
6144 * This register is present on all ThinkPads with a new-style EC, and
6145 * it is known not to be present on the A21m/e, and T22, as there is
6146 * something else in offset 0x84 according to the ACPI DSDT. Other
6147 * ThinkPads from this same time period (and earlier) probably lack the
6148 * tachometer as well.
6149 *
Nick Andrew877d0312009-01-26 11:06:57 +01006150 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006151 * was never fixed by IBM to report the EC firmware version string
6152 * probably support the tachometer (like the early X models), so
6153 * detecting it is quite hard. We need more data to know for sure.
6154 *
6155 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6156 * might result.
6157 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006158 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6159 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006160 *
6161 * For firmware bugs, refer to:
6162 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6163 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006164 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006165 * ThinkPad X31, X40, X41. Not available in the X60.
6166 *
6167 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6168 * high speed. ACPI DSDT seems to map these three speeds to levels
6169 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6170 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6171 *
6172 * The speeds are stored on handles
6173 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6174 *
6175 * There are three default speed sets, acessible as handles:
6176 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6177 *
6178 * ACPI DSDT switches which set is in use depending on various
6179 * factors.
6180 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006181 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006182 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6183 * but the ACPI tables just mention level 7.
6184 */
6185
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006186enum { /* Fan control constants */
6187 fan_status_offset = 0x2f, /* EC register 0x2f */
6188 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6189 * 0x84 must be read before 0x85 */
6190
6191 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6192 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6193
6194 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6195};
6196
6197enum fan_status_access_mode {
6198 TPACPI_FAN_NONE = 0, /* No fan status or control */
6199 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6200 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6201};
6202
6203enum fan_control_access_mode {
6204 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6205 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6206 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6207 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6208};
6209
6210enum fan_control_commands {
6211 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6212 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6213 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6214 * and also watchdog cmd */
6215};
6216
6217static int fan_control_allowed;
6218
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006219static enum fan_status_access_mode fan_status_access_mode;
6220static enum fan_control_access_mode fan_control_access_mode;
6221static enum fan_control_commands fan_control_commands;
6222
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006223static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006224static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006225static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006226static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006227
6228static struct mutex fan_mutex;
6229
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006230static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006231static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006232
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006233TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6234TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006235 "\\FSPD", /* 600e/x, 770e, 770x */
6236 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006237TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006238 "JFNS", /* 770x-JL */
6239 ); /* all others */
6240
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006241/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006242 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6243 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6244 * be in auto mode (0x80).
6245 *
6246 * This is corrected by any write to HFSP either by the driver, or
6247 * by the firmware.
6248 *
6249 * We assume 0x07 really means auto mode while this quirk is active,
6250 * as this is far more likely than the ThinkPad being in level 7,
6251 * which is only used by the firmware during thermal emergencies.
6252 */
6253
6254static void fan_quirk1_detect(void)
6255{
6256 /* In some ThinkPads, neither the EC nor the ACPI
6257 * DSDT initialize the HFSP register, and it ends up
6258 * being initially set to 0x07 when it *could* be
6259 * either 0x07 or 0x80.
6260 *
6261 * Enable for TP-1Y (T43), TP-78 (R51e),
6262 * TP-76 (R52), TP-70 (T43, R52), which are known
6263 * to be buggy. */
6264 if (fan_control_initial_status == 0x07) {
6265 switch (thinkpad_id.ec_model) {
6266 case 0x5931: /* TP-1Y */
6267 case 0x3837: /* TP-78 */
6268 case 0x3637: /* TP-76 */
6269 case 0x3037: /* TP-70 */
6270 printk(TPACPI_NOTICE
6271 "fan_init: initial fan status is unknown, "
6272 "assuming it is in auto mode\n");
6273 tp_features.fan_ctrl_status_undef = 1;
6274 ;;
6275 }
6276 }
6277}
6278
6279static void fan_quirk1_handle(u8 *fan_status)
6280{
6281 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6282 if (*fan_status != fan_control_initial_status) {
6283 /* something changed the HFSP regisnter since
6284 * driver init time, so it is not undefined
6285 * anymore */
6286 tp_features.fan_ctrl_status_undef = 0;
6287 } else {
6288 /* Return most likely status. In fact, it
6289 * might be the only possible status */
6290 *fan_status = TP_EC_FAN_AUTO;
6291 }
6292 }
6293}
6294
6295/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006296 * Call with fan_mutex held
6297 */
6298static void fan_update_desired_level(u8 status)
6299{
6300 if ((status &
6301 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6302 if (status > 7)
6303 fan_control_desired_level = 7;
6304 else
6305 fan_control_desired_level = status;
6306 }
6307}
6308
6309static int fan_get_status(u8 *status)
6310{
6311 u8 s;
6312
6313 /* TODO:
6314 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6315
6316 switch (fan_status_access_mode) {
6317 case TPACPI_FAN_RD_ACPI_GFAN:
6318 /* 570, 600e/x, 770e, 770x */
6319
6320 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6321 return -EIO;
6322
6323 if (likely(status))
6324 *status = s & 0x07;
6325
6326 break;
6327
6328 case TPACPI_FAN_RD_TPEC:
6329 /* all except 570, 600e/x, 770e, 770x */
6330 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6331 return -EIO;
6332
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006333 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006334 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006335 fan_quirk1_handle(status);
6336 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006337
6338 break;
6339
6340 default:
6341 return -ENXIO;
6342 }
6343
6344 return 0;
6345}
6346
6347static int fan_get_status_safe(u8 *status)
6348{
6349 int rc;
6350 u8 s;
6351
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006352 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006353 return -ERESTARTSYS;
6354 rc = fan_get_status(&s);
6355 if (!rc)
6356 fan_update_desired_level(s);
6357 mutex_unlock(&fan_mutex);
6358
6359 if (status)
6360 *status = s;
6361
6362 return rc;
6363}
6364
6365static int fan_get_speed(unsigned int *speed)
6366{
6367 u8 hi, lo;
6368
6369 switch (fan_status_access_mode) {
6370 case TPACPI_FAN_RD_TPEC:
6371 /* all except 570, 600e/x, 770e, 770x */
6372 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6373 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6374 return -EIO;
6375
6376 if (likely(speed))
6377 *speed = (hi << 8) | lo;
6378
6379 break;
6380
6381 default:
6382 return -ENXIO;
6383 }
6384
6385 return 0;
6386}
6387
6388static int fan_set_level(int level)
6389{
6390 if (!fan_control_allowed)
6391 return -EPERM;
6392
6393 switch (fan_control_access_mode) {
6394 case TPACPI_FAN_WR_ACPI_SFAN:
6395 if (level >= 0 && level <= 7) {
6396 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6397 return -EIO;
6398 } else
6399 return -EINVAL;
6400 break;
6401
6402 case TPACPI_FAN_WR_ACPI_FANS:
6403 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006404 if (!(level & TP_EC_FAN_AUTO) &&
6405 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006406 ((level < 0) || (level > 7)))
6407 return -EINVAL;
6408
6409 /* safety net should the EC not support AUTO
6410 * or FULLSPEED mode bits and just ignore them */
6411 if (level & TP_EC_FAN_FULLSPEED)
6412 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006413 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006414 level |= 4; /* safety min speed 4 */
6415
6416 if (!acpi_ec_write(fan_status_offset, level))
6417 return -EIO;
6418 else
6419 tp_features.fan_ctrl_status_undef = 0;
6420 break;
6421
6422 default:
6423 return -ENXIO;
6424 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006425
6426 vdbg_printk(TPACPI_DBG_FAN,
6427 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006428 return 0;
6429}
6430
6431static int fan_set_level_safe(int level)
6432{
6433 int rc;
6434
6435 if (!fan_control_allowed)
6436 return -EPERM;
6437
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006438 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006439 return -ERESTARTSYS;
6440
6441 if (level == TPACPI_FAN_LAST_LEVEL)
6442 level = fan_control_desired_level;
6443
6444 rc = fan_set_level(level);
6445 if (!rc)
6446 fan_update_desired_level(level);
6447
6448 mutex_unlock(&fan_mutex);
6449 return rc;
6450}
6451
6452static int fan_set_enable(void)
6453{
6454 u8 s;
6455 int rc;
6456
6457 if (!fan_control_allowed)
6458 return -EPERM;
6459
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006460 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006461 return -ERESTARTSYS;
6462
6463 switch (fan_control_access_mode) {
6464 case TPACPI_FAN_WR_ACPI_FANS:
6465 case TPACPI_FAN_WR_TPEC:
6466 rc = fan_get_status(&s);
6467 if (rc < 0)
6468 break;
6469
6470 /* Don't go out of emergency fan mode */
6471 if (s != 7) {
6472 s &= 0x07;
6473 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6474 }
6475
6476 if (!acpi_ec_write(fan_status_offset, s))
6477 rc = -EIO;
6478 else {
6479 tp_features.fan_ctrl_status_undef = 0;
6480 rc = 0;
6481 }
6482 break;
6483
6484 case TPACPI_FAN_WR_ACPI_SFAN:
6485 rc = fan_get_status(&s);
6486 if (rc < 0)
6487 break;
6488
6489 s &= 0x07;
6490
6491 /* Set fan to at least level 4 */
6492 s |= 4;
6493
6494 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006495 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006496 else
6497 rc = 0;
6498 break;
6499
6500 default:
6501 rc = -ENXIO;
6502 }
6503
6504 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006505
6506 if (!rc)
6507 vdbg_printk(TPACPI_DBG_FAN,
6508 "fan control: set fan control register to 0x%02x\n",
6509 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006510 return rc;
6511}
6512
6513static int fan_set_disable(void)
6514{
6515 int rc;
6516
6517 if (!fan_control_allowed)
6518 return -EPERM;
6519
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006520 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006521 return -ERESTARTSYS;
6522
6523 rc = 0;
6524 switch (fan_control_access_mode) {
6525 case TPACPI_FAN_WR_ACPI_FANS:
6526 case TPACPI_FAN_WR_TPEC:
6527 if (!acpi_ec_write(fan_status_offset, 0x00))
6528 rc = -EIO;
6529 else {
6530 fan_control_desired_level = 0;
6531 tp_features.fan_ctrl_status_undef = 0;
6532 }
6533 break;
6534
6535 case TPACPI_FAN_WR_ACPI_SFAN:
6536 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6537 rc = -EIO;
6538 else
6539 fan_control_desired_level = 0;
6540 break;
6541
6542 default:
6543 rc = -ENXIO;
6544 }
6545
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006546 if (!rc)
6547 vdbg_printk(TPACPI_DBG_FAN,
6548 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006549
6550 mutex_unlock(&fan_mutex);
6551 return rc;
6552}
6553
6554static int fan_set_speed(int speed)
6555{
6556 int rc;
6557
6558 if (!fan_control_allowed)
6559 return -EPERM;
6560
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006561 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006562 return -ERESTARTSYS;
6563
6564 rc = 0;
6565 switch (fan_control_access_mode) {
6566 case TPACPI_FAN_WR_ACPI_FANS:
6567 if (speed >= 0 && speed <= 65535) {
6568 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6569 speed, speed, speed))
6570 rc = -EIO;
6571 } else
6572 rc = -EINVAL;
6573 break;
6574
6575 default:
6576 rc = -ENXIO;
6577 }
6578
6579 mutex_unlock(&fan_mutex);
6580 return rc;
6581}
6582
6583static void fan_watchdog_reset(void)
6584{
6585 static int fan_watchdog_active;
6586
6587 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6588 return;
6589
6590 if (fan_watchdog_active)
6591 cancel_delayed_work(&fan_watchdog_task);
6592
6593 if (fan_watchdog_maxinterval > 0 &&
6594 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6595 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006596 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006597 msecs_to_jiffies(fan_watchdog_maxinterval
6598 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006599 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006600 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006601 "watchdog will not trigger\n");
6602 }
6603 } else
6604 fan_watchdog_active = 0;
6605}
6606
6607static void fan_watchdog_fire(struct work_struct *ignored)
6608{
6609 int rc;
6610
6611 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6612 return;
6613
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006614 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006615 rc = fan_set_enable();
6616 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006617 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006618 "will try again later...\n", -rc);
6619 /* reschedule for later */
6620 fan_watchdog_reset();
6621 }
6622}
6623
6624/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006625 * SYSFS fan layout: hwmon compatible (device)
6626 *
6627 * pwm*_enable:
6628 * 0: "disengaged" mode
6629 * 1: manual mode
6630 * 2: native EC "auto" mode (recommended, hardware default)
6631 *
6632 * pwm*: set speed in manual mode, ignored otherwise.
6633 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6634 * interpolation.
6635 *
6636 * fan*_input: tachometer reading, RPM
6637 *
6638 *
6639 * SYSFS fan layout: extensions
6640 *
6641 * fan_watchdog (driver):
6642 * fan watchdog interval in seconds, 0 disables (default), max 120
6643 */
6644
6645/* sysfs fan pwm1_enable ----------------------------------------------- */
6646static ssize_t fan_pwm1_enable_show(struct device *dev,
6647 struct device_attribute *attr,
6648 char *buf)
6649{
6650 int res, mode;
6651 u8 status;
6652
6653 res = fan_get_status_safe(&status);
6654 if (res)
6655 return res;
6656
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006657 if (status & TP_EC_FAN_FULLSPEED) {
6658 mode = 0;
6659 } else if (status & TP_EC_FAN_AUTO) {
6660 mode = 2;
6661 } else
6662 mode = 1;
6663
6664 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6665}
6666
6667static ssize_t fan_pwm1_enable_store(struct device *dev,
6668 struct device_attribute *attr,
6669 const char *buf, size_t count)
6670{
6671 unsigned long t;
6672 int res, level;
6673
6674 if (parse_strtoul(buf, 2, &t))
6675 return -EINVAL;
6676
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006677 tpacpi_disclose_usertask("hwmon pwm1_enable",
6678 "set fan mode to %lu\n", t);
6679
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006680 switch (t) {
6681 case 0:
6682 level = TP_EC_FAN_FULLSPEED;
6683 break;
6684 case 1:
6685 level = TPACPI_FAN_LAST_LEVEL;
6686 break;
6687 case 2:
6688 level = TP_EC_FAN_AUTO;
6689 break;
6690 case 3:
6691 /* reserved for software-controlled auto mode */
6692 return -ENOSYS;
6693 default:
6694 return -EINVAL;
6695 }
6696
6697 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006698 if (res == -ENXIO)
6699 return -EINVAL;
6700 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006701 return res;
6702
6703 fan_watchdog_reset();
6704
6705 return count;
6706}
6707
6708static struct device_attribute dev_attr_fan_pwm1_enable =
6709 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6710 fan_pwm1_enable_show, fan_pwm1_enable_store);
6711
6712/* sysfs fan pwm1 ------------------------------------------------------ */
6713static ssize_t fan_pwm1_show(struct device *dev,
6714 struct device_attribute *attr,
6715 char *buf)
6716{
6717 int res;
6718 u8 status;
6719
6720 res = fan_get_status_safe(&status);
6721 if (res)
6722 return res;
6723
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006724 if ((status &
6725 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6726 status = fan_control_desired_level;
6727
6728 if (status > 7)
6729 status = 7;
6730
6731 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6732}
6733
6734static ssize_t fan_pwm1_store(struct device *dev,
6735 struct device_attribute *attr,
6736 const char *buf, size_t count)
6737{
6738 unsigned long s;
6739 int rc;
6740 u8 status, newlevel;
6741
6742 if (parse_strtoul(buf, 255, &s))
6743 return -EINVAL;
6744
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006745 tpacpi_disclose_usertask("hwmon pwm1",
6746 "set fan speed to %lu\n", s);
6747
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006748 /* scale down from 0-255 to 0-7 */
6749 newlevel = (s >> 5) & 0x07;
6750
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006751 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006752 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006753
6754 rc = fan_get_status(&status);
6755 if (!rc && (status &
6756 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6757 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006758 if (rc == -ENXIO)
6759 rc = -EINVAL;
6760 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006761 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006762 fan_watchdog_reset();
6763 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006764 }
6765
6766 mutex_unlock(&fan_mutex);
6767 return (rc)? rc : count;
6768}
6769
6770static struct device_attribute dev_attr_fan_pwm1 =
6771 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6772 fan_pwm1_show, fan_pwm1_store);
6773
6774/* sysfs fan fan1_input ------------------------------------------------ */
6775static ssize_t fan_fan1_input_show(struct device *dev,
6776 struct device_attribute *attr,
6777 char *buf)
6778{
6779 int res;
6780 unsigned int speed;
6781
6782 res = fan_get_speed(&speed);
6783 if (res < 0)
6784 return res;
6785
6786 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6787}
6788
6789static struct device_attribute dev_attr_fan_fan1_input =
6790 __ATTR(fan1_input, S_IRUGO,
6791 fan_fan1_input_show, NULL);
6792
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006793/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006794static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6795 char *buf)
6796{
6797 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6798}
6799
6800static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6801 const char *buf, size_t count)
6802{
6803 unsigned long t;
6804
6805 if (parse_strtoul(buf, 120, &t))
6806 return -EINVAL;
6807
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006808 if (!fan_control_allowed)
6809 return -EPERM;
6810
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006811 fan_watchdog_maxinterval = t;
6812 fan_watchdog_reset();
6813
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006814 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6815
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006816 return count;
6817}
6818
6819static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6820 fan_fan_watchdog_show, fan_fan_watchdog_store);
6821
6822/* --------------------------------------------------------------------- */
6823static struct attribute *fan_attributes[] = {
6824 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6825 &dev_attr_fan_fan1_input.attr,
6826 NULL
6827};
6828
6829static const struct attribute_group fan_attr_group = {
6830 .attrs = fan_attributes,
6831};
6832
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006833static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006834{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006835 int rc;
6836
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6838 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006839
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006840 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006841 fan_status_access_mode = TPACPI_FAN_NONE;
6842 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006843 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006844 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006845 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006846 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006847
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006848 TPACPI_ACPIHANDLE_INIT(fans);
6849 TPACPI_ACPIHANDLE_INIT(gfan);
6850 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006851
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006852 if (gfan_handle) {
6853 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006854 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006855 } else {
6856 /* all other ThinkPads: note that even old-style
6857 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006858 if (likely(acpi_ec_read(fan_status_offset,
6859 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006860 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006861 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006862 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006863 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006864 "ThinkPad ACPI EC access misbehaving, "
6865 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006866 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006867 }
6868 }
6869
6870 if (sfan_handle) {
6871 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006872 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006873 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006874 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006875 } else {
6876 if (!gfan_handle) {
6877 /* gfan without sfan means no fan control */
6878 /* all other models implement TP EC 0x2f control */
6879
6880 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006881 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006882 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006883 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006884 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006885 TPACPI_FAN_CMD_SPEED |
6886 TPACPI_FAN_CMD_LEVEL |
6887 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006888 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006889 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006890 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006891 TPACPI_FAN_CMD_LEVEL |
6892 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006893 }
6894 }
6895 }
6896
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006897 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6898 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006899 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6900 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6901 fan_status_access_mode, fan_control_access_mode);
6902
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006903 /* fan control master switch */
6904 if (!fan_control_allowed) {
6905 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6906 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006907 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006908 "fan control features disabled by parameter\n");
6909 }
6910
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006911 /* update fan_control_desired_level */
6912 if (fan_status_access_mode != TPACPI_FAN_NONE)
6913 fan_get_status_safe(NULL);
6914
6915 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6916 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006917 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006918 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006919 if (rc < 0)
6920 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006921
6922 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6923 &driver_attr_fan_watchdog);
6924 if (rc < 0) {
6925 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6926 &fan_attr_group);
6927 return rc;
6928 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006929 return 0;
6930 } else
6931 return 1;
6932}
6933
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006934static void fan_exit(void)
6935{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00006936 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006937 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006938
6939 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006940 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006941 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6942 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006943
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006944 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006945 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006946}
6947
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006948static void fan_suspend(pm_message_t state)
6949{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006950 int rc;
6951
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006952 if (!fan_control_allowed)
6953 return;
6954
6955 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006956 fan_control_resume_level = 0;
6957 rc = fan_get_status_safe(&fan_control_resume_level);
6958 if (rc < 0)
6959 printk(TPACPI_NOTICE
6960 "failed to read fan level for later "
6961 "restore during resume: %d\n", rc);
6962
6963 /* if it is undefined, don't attempt to restore it.
6964 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006965 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006966 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006967}
6968
6969static void fan_resume(void)
6970{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006971 u8 current_level = 7;
6972 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006973 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006974
6975 /* DSDT *always* updates status on resume */
6976 tp_features.fan_ctrl_status_undef = 0;
6977
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006978 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006979 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006980 (fan_get_status_safe(&current_level) < 0))
6981 return;
6982
6983 switch (fan_control_access_mode) {
6984 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006985 /* never decrease fan level */
6986 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006987 break;
6988 case TPACPI_FAN_WR_ACPI_FANS:
6989 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006990 /* never decrease fan level, scale is:
6991 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6992 *
6993 * We expect the firmware to set either 7 or AUTO, but we
6994 * handle FULLSPEED out of paranoia.
6995 *
6996 * So, we can safely only restore FULLSPEED or 7, anything
6997 * else could slow the fan. Restoring AUTO is useless, at
6998 * best that's exactly what the DSDT already set (it is the
6999 * slower it uses).
7000 *
7001 * Always keep in mind that the DSDT *will* have set the
7002 * fans to what the vendor supposes is the best level. We
7003 * muck with it only to speed the fan up.
7004 */
7005 if (fan_control_resume_level != 7 &&
7006 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7007 return;
7008 else
7009 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7010 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007011 break;
7012 default:
7013 return;
7014 }
7015 if (do_set) {
7016 printk(TPACPI_NOTICE
7017 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007018 fan_control_resume_level);
7019 rc = fan_set_level_safe(fan_control_resume_level);
7020 if (rc < 0)
7021 printk(TPACPI_NOTICE
7022 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007023 }
7024}
7025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007026static int fan_read(char *p)
7027{
7028 int len = 0;
7029 int rc;
7030 u8 status;
7031 unsigned int speed = 0;
7032
7033 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007034 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007035 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007036 rc = fan_get_status_safe(&status);
7037 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007038 return rc;
7039
7040 len += sprintf(p + len, "status:\t\t%s\n"
7041 "level:\t\t%d\n",
7042 (status != 0) ? "enabled" : "disabled", status);
7043 break;
7044
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007045 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007046 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007047 rc = fan_get_status_safe(&status);
7048 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007049 return rc;
7050
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007051 len += sprintf(p + len, "status:\t\t%s\n",
7052 (status != 0) ? "enabled" : "disabled");
7053
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007054 rc = fan_get_speed(&speed);
7055 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007056 return rc;
7057
7058 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7059
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007060 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007061 /* Disengaged mode takes precedence */
7062 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007063 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007064 len += sprintf(p + len, "level:\t\tauto\n");
7065 else
7066 len += sprintf(p + len, "level:\t\t%d\n", status);
7067 break;
7068
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007069 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007070 default:
7071 len += sprintf(p + len, "status:\t\tnot supported\n");
7072 }
7073
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007074 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007075 len += sprintf(p + len, "commands:\tlevel <level>");
7076
7077 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007078 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007079 len += sprintf(p + len, " (<level> is 0-7)\n");
7080 break;
7081
7082 default:
7083 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007084 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007085 break;
7086 }
7087 }
7088
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007089 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007090 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007091 "commands:\twatchdog <timeout> (<timeout> "
7092 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007093
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007094 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007095 len += sprintf(p + len, "commands:\tspeed <speed>"
7096 " (<speed> is 0-65535)\n");
7097
7098 return len;
7099}
7100
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007101static int fan_write_cmd_level(const char *cmd, int *rc)
7102{
7103 int level;
7104
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007105 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007106 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007107 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007108 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007109 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02007110 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007111 return 0;
7112
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007113 *rc = fan_set_level_safe(level);
7114 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007115 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007116 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007117 else if (!*rc)
7118 tpacpi_disclose_usertask("procfs fan",
7119 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007120
7121 return 1;
7122}
7123
7124static int fan_write_cmd_enable(const char *cmd, int *rc)
7125{
7126 if (strlencmp(cmd, "enable") != 0)
7127 return 0;
7128
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007129 *rc = fan_set_enable();
7130 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007131 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007132 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007133 else if (!*rc)
7134 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007135
7136 return 1;
7137}
7138
7139static int fan_write_cmd_disable(const char *cmd, int *rc)
7140{
7141 if (strlencmp(cmd, "disable") != 0)
7142 return 0;
7143
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007144 *rc = fan_set_disable();
7145 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007146 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007147 "access mode %d", fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007148 else if (!*rc)
7149 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007150
7151 return 1;
7152}
7153
7154static int fan_write_cmd_speed(const char *cmd, int *rc)
7155{
7156 int speed;
7157
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02007158 /* TODO:
7159 * Support speed <low> <medium> <high> ? */
7160
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007161 if (sscanf(cmd, "speed %d", &speed) != 1)
7162 return 0;
7163
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007164 *rc = fan_set_speed(speed);
7165 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007166 printk(TPACPI_ERR "speed 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 speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007171
7172 return 1;
7173}
7174
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007175static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7176{
7177 int interval;
7178
7179 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7180 return 0;
7181
7182 if (interval < 0 || interval > 120)
7183 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007184 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007185 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007186 tpacpi_disclose_usertask("procfs fan",
7187 "set watchdog timer to %d\n",
7188 interval);
7189 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007190
7191 return 1;
7192}
7193
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007194static int fan_write(char *buf)
7195{
7196 char *cmd;
7197 int rc = 0;
7198
7199 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007200 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007201 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007202 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007203 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007204 fan_write_cmd_disable(cmd, &rc) ||
7205 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007206 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007207 fan_write_cmd_speed(cmd, &rc))
7208 )
7209 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007210 else if (!rc)
7211 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007212 }
7213
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007214 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007215}
7216
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007217static struct ibm_struct fan_driver_data = {
7218 .name = "fan",
7219 .read = fan_read,
7220 .write = fan_write,
7221 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007222 .suspend = fan_suspend,
7223 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007224};
7225
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007226/****************************************************************************
7227 ****************************************************************************
7228 *
7229 * Infrastructure
7230 *
7231 ****************************************************************************
7232 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007234/* sysfs name ---------------------------------------------------------- */
7235static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7236 struct device_attribute *attr,
7237 char *buf)
7238{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007239 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007240}
7241
7242static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7243 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7244
7245/* --------------------------------------------------------------------- */
7246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007247/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007248static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007249
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007250/*
7251 * Module and infrastructure proble, init and exit handling
7252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007254static int force_load;
7255
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007256#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007257static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007258{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007259 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007260
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007261 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007262}
7263#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7264
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007265static void ibm_exit(struct ibm_struct *ibm)
7266{
7267 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7268
7269 list_del_init(&ibm->all_drivers);
7270
7271 if (ibm->flags.acpi_notify_installed) {
7272 dbg_printk(TPACPI_DBG_EXIT,
7273 "%s: acpi_remove_notify_handler\n", ibm->name);
7274 BUG_ON(!ibm->acpi);
7275 acpi_remove_notify_handler(*ibm->acpi->handle,
7276 ibm->acpi->type,
7277 dispatch_acpi_notify);
7278 ibm->flags.acpi_notify_installed = 0;
7279 ibm->flags.acpi_notify_installed = 0;
7280 }
7281
7282 if (ibm->flags.proc_created) {
7283 dbg_printk(TPACPI_DBG_EXIT,
7284 "%s: remove_proc_entry\n", ibm->name);
7285 remove_proc_entry(ibm->name, proc_dir);
7286 ibm->flags.proc_created = 0;
7287 }
7288
7289 if (ibm->flags.acpi_driver_registered) {
7290 dbg_printk(TPACPI_DBG_EXIT,
7291 "%s: acpi_bus_unregister_driver\n", ibm->name);
7292 BUG_ON(!ibm->acpi);
7293 acpi_bus_unregister_driver(ibm->acpi->driver);
7294 kfree(ibm->acpi->driver);
7295 ibm->acpi->driver = NULL;
7296 ibm->flags.acpi_driver_registered = 0;
7297 }
7298
7299 if (ibm->flags.init_called && ibm->exit) {
7300 ibm->exit();
7301 ibm->flags.init_called = 0;
7302 }
7303
7304 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7305}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007306
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007307static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308{
7309 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007310 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 struct proc_dir_entry *entry;
7312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007313 BUG_ON(ibm == NULL);
7314
7315 INIT_LIST_HEAD(&ibm->all_drivers);
7316
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007317 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 return 0;
7319
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007320 dbg_printk(TPACPI_DBG_INIT,
7321 "probing for %s\n", ibm->name);
7322
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007323 if (iibm->init) {
7324 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007325 if (ret > 0)
7326 return 0; /* probe failed */
7327 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007329
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007330 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 }
7332
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007333 if (ibm->acpi) {
7334 if (ibm->acpi->hid) {
7335 ret = register_tpacpi_subdriver(ibm);
7336 if (ret)
7337 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007338 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007339
7340 if (ibm->acpi->notify) {
7341 ret = setup_acpi_notify(ibm);
7342 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007343 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007344 ibm->name);
7345 ret = 0;
7346 goto err_out;
7347 }
7348 if (ret < 0)
7349 goto err_out;
7350 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007351 }
7352
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007353 dbg_printk(TPACPI_DBG_INIT,
7354 "%s installed\n", ibm->name);
7355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007356 if (ibm->read) {
7357 entry = create_proc_entry(ibm->name,
7358 S_IFREG | S_IRUGO | S_IWUSR,
7359 proc_dir);
7360 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007361 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007362 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007363 ret = -ENODEV;
7364 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007365 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007366 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007367 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007368 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007369 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007370 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007373 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7374
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007376
7377err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007378 dbg_printk(TPACPI_DBG_INIT,
7379 "%s: at error exit path with result %d\n",
7380 ibm->name, ret);
7381
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007382 ibm_exit(ibm);
7383 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384}
7385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007386/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007388/* returns 0 - probe ok, or < 0 - probe error.
7389 * Probe ok doesn't mean thinkpad found.
7390 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7391static int __must_check __init get_thinkpad_model_data(
7392 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007393{
Jeff Garzik18552562007-10-03 15:15:40 -04007394 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007395 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007396 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007397
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007398 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007399 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007400
7401 memset(tp, 0, sizeof(*tp));
7402
7403 if (dmi_name_in_vendors("IBM"))
7404 tp->vendor = PCI_VENDOR_ID_IBM;
7405 else if (dmi_name_in_vendors("LENOVO"))
7406 tp->vendor = PCI_VENDOR_ID_LENOVO;
7407 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007408 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007409
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007410 s = dmi_get_system_info(DMI_BIOS_VERSION);
7411 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7412 if (s && !tp->bios_version_str)
7413 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007414 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007415 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007416 tp->bios_model = tp->bios_version_str[0]
7417 | (tp->bios_version_str[1] << 8);
7418
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007419 /*
7420 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7421 * X32 or newer, all Z series; Some models must have an
7422 * up-to-date BIOS or they will not be detected.
7423 *
7424 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7425 */
7426 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007427 if (sscanf(dev->name,
7428 "IBM ThinkPad Embedded Controller -[%17c",
7429 ec_fw_string) == 1) {
7430 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7431 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007432
7433 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007434 if (!tp->ec_version_str)
7435 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007436 tp->ec_model = ec_fw_string[0]
7437 | (ec_fw_string[1] << 8);
7438 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007439 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007440 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007441
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007442 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7443 if (s && !strnicmp(s, "ThinkPad", 8)) {
7444 tp->model_str = kstrdup(s, GFP_KERNEL);
7445 if (!tp->model_str)
7446 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007447 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007448
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007449 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7450 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7451 if (s && !tp->nummodel_str)
7452 return -ENOMEM;
7453
7454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455}
7456
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007457static int __init probe_for_thinkpad(void)
7458{
7459 int is_thinkpad;
7460
7461 if (acpi_disabled)
7462 return -ENODEV;
7463
7464 /*
7465 * Non-ancient models have better DMI tagging, but very old models
7466 * don't.
7467 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007468 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007469
7470 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007471 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007472 if (!ec_handle) {
7473 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007474 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007475 "Not yet supported ThinkPad detected!\n");
7476 return -ENODEV;
7477 }
7478
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007479 /*
7480 * Risks a regression on very old machines, but reduces potential
7481 * false positives a damn great deal
7482 */
7483 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007484 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007485
7486 if (!is_thinkpad && !force_load)
7487 return -ENODEV;
7488
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007489 return 0;
7490}
7491
7492
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007493/* Module init, exit, parameters */
7494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007495static struct ibm_init_struct ibms_init[] __initdata = {
7496 {
7497 .init = thinkpad_acpi_driver_init,
7498 .data = &thinkpad_acpi_driver_data,
7499 },
7500 {
7501 .init = hotkey_init,
7502 .data = &hotkey_driver_data,
7503 },
7504 {
7505 .init = bluetooth_init,
7506 .data = &bluetooth_driver_data,
7507 },
7508 {
7509 .init = wan_init,
7510 .data = &wan_driver_data,
7511 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007512 {
7513 .init = uwb_init,
7514 .data = &uwb_driver_data,
7515 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007516#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007517 {
7518 .init = video_init,
7519 .data = &video_driver_data,
7520 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007521#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007522 {
7523 .init = light_init,
7524 .data = &light_driver_data,
7525 },
7526#ifdef CONFIG_THINKPAD_ACPI_DOCK
7527 {
7528 .init = dock_init,
7529 .data = &dock_driver_data[0],
7530 },
7531 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007532 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007533 .data = &dock_driver_data[1],
7534 },
7535#endif
7536#ifdef CONFIG_THINKPAD_ACPI_BAY
7537 {
7538 .init = bay_init,
7539 .data = &bay_driver_data,
7540 },
7541#endif
7542 {
7543 .init = cmos_init,
7544 .data = &cmos_driver_data,
7545 },
7546 {
7547 .init = led_init,
7548 .data = &led_driver_data,
7549 },
7550 {
7551 .init = beep_init,
7552 .data = &beep_driver_data,
7553 },
7554 {
7555 .init = thermal_init,
7556 .data = &thermal_driver_data,
7557 },
7558 {
7559 .data = &ecdump_driver_data,
7560 },
7561 {
7562 .init = brightness_init,
7563 .data = &brightness_driver_data,
7564 },
7565 {
7566 .data = &volume_driver_data,
7567 },
7568 {
7569 .init = fan_init,
7570 .data = &fan_driver_data,
7571 },
7572};
7573
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007574static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7575{
7576 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007577 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007578
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007579 if (!kp || !kp->name || !val)
7580 return -EINVAL;
7581
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007582 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7583 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007584 WARN_ON(ibm == NULL);
7585
7586 if (!ibm || !ibm->name)
7587 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007588
7589 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7590 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007591 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007592 strcpy(ibms_init[i].param, val);
7593 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007594 return 0;
7595 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007596 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007597
7598 return -EINVAL;
7599}
7600
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007601module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007602MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007603 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007604
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007605module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007606MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007607
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007608module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007609MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007610 "Attempts to load the driver even on a "
7611 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007612
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007613module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007614MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007615 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007616
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007617module_param_named(brightness_mode, brightness_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007618MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007619 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00007620 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007621
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007622module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007623MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007624 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007625
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007626module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007627MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007628 "used for backwards compatibility with userspace, "
7629 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007630
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007631#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007632 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007633 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007634 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007635
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007636TPACPI_PARAM(hotkey);
7637TPACPI_PARAM(bluetooth);
7638TPACPI_PARAM(video);
7639TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007640#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007641TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007642#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007643#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007644TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007645#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007646TPACPI_PARAM(cmos);
7647TPACPI_PARAM(led);
7648TPACPI_PARAM(beep);
7649TPACPI_PARAM(ecdump);
7650TPACPI_PARAM(brightness);
7651TPACPI_PARAM(volume);
7652TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007653
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007654#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7655module_param(dbg_wlswemul, uint, 0);
7656MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7657module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7658MODULE_PARM_DESC(wlsw_state,
7659 "Initial state of the emulated WLSW switch");
7660
7661module_param(dbg_bluetoothemul, uint, 0);
7662MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7663module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7664MODULE_PARM_DESC(bluetooth_state,
7665 "Initial state of the emulated bluetooth switch");
7666
7667module_param(dbg_wwanemul, uint, 0);
7668MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7669module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7670MODULE_PARM_DESC(wwan_state,
7671 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007672
7673module_param(dbg_uwbemul, uint, 0);
7674MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7675module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7676MODULE_PARM_DESC(uwb_state,
7677 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007678#endif
7679
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007680static void thinkpad_acpi_module_exit(void)
7681{
7682 struct ibm_struct *ibm, *itmp;
7683
7684 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7685
7686 list_for_each_entry_safe_reverse(ibm, itmp,
7687 &tpacpi_all_drivers,
7688 all_drivers) {
7689 ibm_exit(ibm);
7690 }
7691
7692 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7693
7694 if (tpacpi_inputdev) {
7695 if (tp_features.input_device_registered)
7696 input_unregister_device(tpacpi_inputdev);
7697 else
7698 input_free_device(tpacpi_inputdev);
7699 }
7700
7701 if (tpacpi_hwmon)
7702 hwmon_device_unregister(tpacpi_hwmon);
7703
7704 if (tp_features.sensors_pdev_attrs_registered)
7705 device_remove_file(&tpacpi_sensors_pdev->dev,
7706 &dev_attr_thinkpad_acpi_pdev_name);
7707 if (tpacpi_sensors_pdev)
7708 platform_device_unregister(tpacpi_sensors_pdev);
7709 if (tpacpi_pdev)
7710 platform_device_unregister(tpacpi_pdev);
7711
7712 if (tp_features.sensors_pdrv_attrs_registered)
7713 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7714 if (tp_features.platform_drv_attrs_registered)
7715 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7716
7717 if (tp_features.sensors_pdrv_registered)
7718 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7719
7720 if (tp_features.platform_drv_registered)
7721 platform_driver_unregister(&tpacpi_pdriver);
7722
7723 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007724 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007725
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007726 if (tpacpi_wq)
7727 destroy_workqueue(tpacpi_wq);
7728
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007729 kfree(thinkpad_id.bios_version_str);
7730 kfree(thinkpad_id.ec_version_str);
7731 kfree(thinkpad_id.model_str);
7732}
7733
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007734
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007735static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736{
7737 int ret, i;
7738
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007739 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7740
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007741 /* Parameter checking */
7742 if (hotkey_report_mode > 2)
7743 return -EINVAL;
7744
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007745 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007746
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007747 ret = get_thinkpad_model_data(&thinkpad_id);
7748 if (ret) {
7749 printk(TPACPI_ERR
7750 "unable to get DMI data: %d\n", ret);
7751 thinkpad_acpi_module_exit();
7752 return ret;
7753 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007754 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007755 if (ret) {
7756 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007757 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007760 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007761
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007762 TPACPI_ACPIHANDLE_INIT(ecrd);
7763 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007765 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7766 if (!tpacpi_wq) {
7767 thinkpad_acpi_module_exit();
7768 return -ENOMEM;
7769 }
7770
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007771 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007773 printk(TPACPI_ERR
7774 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007775 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 return -ENODEV;
7777 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007778
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007779 ret = platform_driver_register(&tpacpi_pdriver);
7780 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007781 printk(TPACPI_ERR
7782 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007783 thinkpad_acpi_module_exit();
7784 return ret;
7785 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007786 tp_features.platform_drv_registered = 1;
7787
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007788 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7789 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007790 printk(TPACPI_ERR
7791 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007792 thinkpad_acpi_module_exit();
7793 return ret;
7794 }
7795 tp_features.sensors_pdrv_registered = 1;
7796
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007797 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007798 if (!ret) {
7799 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007800 ret = tpacpi_create_driver_attributes(
7801 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007802 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007803 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007804 printk(TPACPI_ERR
7805 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007806 thinkpad_acpi_module_exit();
7807 return ret;
7808 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007809 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007810
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007811
7812 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007813 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007814 NULL, 0);
7815 if (IS_ERR(tpacpi_pdev)) {
7816 ret = PTR_ERR(tpacpi_pdev);
7817 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007818 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007819 thinkpad_acpi_module_exit();
7820 return ret;
7821 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007822 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007823 TPACPI_HWMON_DRVR_NAME,
7824 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007825 if (IS_ERR(tpacpi_sensors_pdev)) {
7826 ret = PTR_ERR(tpacpi_sensors_pdev);
7827 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007828 printk(TPACPI_ERR
7829 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007830 thinkpad_acpi_module_exit();
7831 return ret;
7832 }
7833 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7834 &dev_attr_thinkpad_acpi_pdev_name);
7835 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007836 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007837 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007838 thinkpad_acpi_module_exit();
7839 return ret;
7840 }
7841 tp_features.sensors_pdev_attrs_registered = 1;
7842 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007843 if (IS_ERR(tpacpi_hwmon)) {
7844 ret = PTR_ERR(tpacpi_hwmon);
7845 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007846 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007847 thinkpad_acpi_module_exit();
7848 return ret;
7849 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007850 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007851 tpacpi_inputdev = input_allocate_device();
7852 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007853 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007854 thinkpad_acpi_module_exit();
7855 return -ENOMEM;
7856 } else {
7857 /* Prepare input device, but don't register */
7858 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007859 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007860 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007861 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7862 thinkpad_id.vendor :
7863 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007864 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7865 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7866 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007867 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7868 ret = ibm_init(&ibms_init[i]);
7869 if (ret >= 0 && *ibms_init[i].param)
7870 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007872 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 return ret;
7874 }
7875 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007876 ret = input_register_device(tpacpi_inputdev);
7877 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007878 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007879 thinkpad_acpi_module_exit();
7880 return ret;
7881 } else {
7882 tp_features.input_device_registered = 1;
7883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007885 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 return 0;
7887}
7888
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007889MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7890
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007891/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007892 * This will autoload the driver in almost every ThinkPad
7893 * in widespread use.
7894 *
7895 * Only _VERY_ old models, like the 240, 240x and 570 lack
7896 * the HKEY event interface.
7897 */
7898MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7899
7900/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007901 * DMI matching for module autoloading
7902 *
7903 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7904 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7905 *
7906 * Only models listed in thinkwiki will be supported, so add yours
7907 * if it is not there yet.
7908 */
7909#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007910 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007911
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007912/* Ancient thinkpad BIOSes have to be identified by
7913 * BIOS type or model number, and there are far less
7914 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00007915IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007916
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00007917MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7918MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007919MODULE_DESCRIPTION(TPACPI_DESC);
7920MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007921MODULE_LICENSE("GPL");
7922
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007923module_init(thinkpad_acpi_module_init);
7924module_exit(thinkpad_acpi_module_exit);