blob: 0a4796a246ea13cb34ce214081431e77e63162a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhaa2fbce2009-01-11 03:01:10 -020024#define TPACPI_VERSION "0.22"
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +000025#define TPACPI_SYSFS_VERSION 0x020300
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000057#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020058#include <linux/kthread.h>
59#include <linux/freezer.h>
60#include <linux/delay.h>
61
62#include <linux/nvram.h>
63#include <linux/proc_fs.h>
64#include <linux/sysfs.h>
65#include <linux/backlight.h>
66#include <linux/fb.h>
67#include <linux/platform_device.h>
68#include <linux/hwmon.h>
69#include <linux/hwmon-sysfs.h>
70#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030071#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030072#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020073#include <asm/uaccess.h>
74
75#include <linux/dmi.h>
76#include <linux/jiffies.h>
77#include <linux/workqueue.h>
78
79#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020080
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
148/****************************************************************************
149 * Main driver
150 */
151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200152#define TPACPI_NAME "thinkpad"
153#define TPACPI_DESC "ThinkPad ACPI Extras"
154#define TPACPI_FILE TPACPI_NAME "_acpi"
155#define TPACPI_URL "http://ibm-acpi.sf.net/"
156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200157
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200158#define TPACPI_PROC_DIR "ibm"
159#define TPACPI_ACPI_EVENT_PREFIX "ibm"
160#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300161#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300165#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200167#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200168
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300169/* rfkill switches */
170enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200173 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300174};
175
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000176/* printk headers */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200177#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000178#define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181#define TPACPI_ERR KERN_ERR TPACPI_LOG
182#define TPACPI_WARN KERN_WARNING TPACPI_LOG
183#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184#define TPACPI_INFO KERN_INFO TPACPI_LOG
185#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200186
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000187/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200188#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000189#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200190#define TPACPI_DBG_INIT 0x0001
191#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000192#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000193#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200194
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200195#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
196#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
197#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200198
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200199
200/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200201 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200202 */
203
204struct ibm_struct;
205
206struct tp_acpi_drv_struct {
207 const struct acpi_device_id *hid;
208 struct acpi_driver *driver;
209
210 void (*notify) (struct ibm_struct *, u32);
211 acpi_handle *handle;
212 u32 type;
213 struct acpi_device *device;
214};
215
216struct ibm_struct {
217 char *name;
218
219 int (*read) (char *);
220 int (*write) (char *);
221 void (*exit) (void);
222 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200223 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200224 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200225
226 struct list_head all_drivers;
227
228 struct tp_acpi_drv_struct *acpi;
229
230 struct {
231 u8 acpi_driver_registered:1;
232 u8 acpi_notify_installed:1;
233 u8 proc_created:1;
234 u8 init_called:1;
235 u8 experimental:1;
236 } flags;
237};
238
239struct ibm_init_struct {
240 char param[32];
241
242 int (*init) (struct ibm_init_struct *);
243 struct ibm_struct *data;
244};
245
246static struct {
247#ifdef CONFIG_THINKPAD_ACPI_BAY
248 u32 bay_status:1;
249 u32 bay_eject:1;
250 u32 bay_status2:1;
251 u32 bay_eject2:1;
252#endif
253 u32 bluetooth:1;
254 u32 hotkey:1;
255 u32 hotkey_mask:1;
256 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200257 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200258 u32 light:1;
259 u32 light_status:1;
260 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300261 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200262 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200263 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264 u32 fan_ctrl_status_undef:1;
265 u32 input_device_registered:1;
266 u32 platform_drv_registered:1;
267 u32 platform_drv_attrs_registered:1;
268 u32 sensors_pdrv_registered:1;
269 u32 sensors_pdrv_attrs_registered:1;
270 u32 sensors_pdev_attrs_registered:1;
271 u32 hotkey_poll_active:1;
272} tp_features;
273
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300274static struct {
275 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300276 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300277} tp_warned;
278
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200279struct thinkpad_id_data {
280 unsigned int vendor; /* ThinkPad vendor:
281 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
282
283 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
284 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
285
286 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
287 u16 ec_model;
288
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300289 char *model_str; /* ThinkPad T43 */
290 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200291};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200292static struct thinkpad_id_data thinkpad_id;
293
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300294static enum {
295 TPACPI_LIFE_INIT = 0,
296 TPACPI_LIFE_RUNNING,
297 TPACPI_LIFE_EXITING,
298} tpacpi_lifecycle;
299
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200300static int experimental;
301static u32 dbg_level;
302
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300303static struct workqueue_struct *tpacpi_wq;
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;
309 enum led_brightness new_brightness;
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 Holschuhd64c81c42008-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 Holschuh0e74dc22008-07-21 09:15:51 -03001001static int __init tpacpi_new_rfkill(const unsigned int id,
1002 struct rfkill **rfk,
1003 const enum rfkill_type rfktype,
1004 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001005 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001006 int (*toggle_radio)(void *, enum rfkill_state),
1007 int (*get_state)(void *, enum rfkill_state *))
1008{
1009 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001010 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1011
1012 res = get_state(NULL, &initial_state);
1013 if (res < 0) {
1014 printk(TPACPI_ERR
1015 "failed to read initial state for %s, error %d; "
1016 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001017 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001018 /* try to set the initial state as the default for the rfkill
1019 * type, since we ask the firmware to preserve it across S5 in
1020 * NVRAM */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00001021 if (rfkill_set_default(rfktype,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001022 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1023 RFKILL_STATE_UNBLOCKED :
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00001024 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1025 vdbg_printk(TPACPI_DBG_RFKILL,
1026 "Default state for %s cannot be changed\n",
1027 name);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001028 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001029
1030 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1031 if (!*rfk) {
1032 printk(TPACPI_ERR
1033 "failed to allocate memory for rfkill class\n");
1034 return -ENOMEM;
1035 }
1036
1037 (*rfk)->name = name;
1038 (*rfk)->get_state = get_state;
1039 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001040 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001041
1042 res = rfkill_register(*rfk);
1043 if (res < 0) {
1044 printk(TPACPI_ERR
1045 "failed to register %s rfkill switch: %d\n",
1046 name, res);
1047 rfkill_free(*rfk);
1048 *rfk = NULL;
1049 return res;
1050 }
1051
1052 return 0;
1053}
1054
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001055static void printk_deprecated_attribute(const char * const what,
1056 const char * const details)
1057{
1058 tpacpi_log_usertask("deprecated sysfs attribute");
1059 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1060 "will be removed. %s\n",
1061 what, details);
1062}
1063
1064static void printk_deprecated_rfkill_attribute(const char * const what)
1065{
1066 printk_deprecated_attribute(what,
1067 "Please switch to generic rfkill before year 2010");
1068}
1069
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001070/*************************************************************************
1071 * thinkpad-acpi driver attributes
1072 */
1073
1074/* interface_version --------------------------------------------------- */
1075static ssize_t tpacpi_driver_interface_version_show(
1076 struct device_driver *drv,
1077 char *buf)
1078{
1079 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1080}
1081
1082static DRIVER_ATTR(interface_version, S_IRUGO,
1083 tpacpi_driver_interface_version_show, NULL);
1084
1085/* debug_level --------------------------------------------------------- */
1086static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1087 char *buf)
1088{
1089 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1090}
1091
1092static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1093 const char *buf, size_t count)
1094{
1095 unsigned long t;
1096
1097 if (parse_strtoul(buf, 0xffff, &t))
1098 return -EINVAL;
1099
1100 dbg_level = t;
1101
1102 return count;
1103}
1104
1105static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1106 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1107
1108/* version ------------------------------------------------------------- */
1109static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1110 char *buf)
1111{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001112 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1113 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001114}
1115
1116static DRIVER_ATTR(version, S_IRUGO,
1117 tpacpi_driver_version_show, NULL);
1118
1119/* --------------------------------------------------------------------- */
1120
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001121#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1122
1123static void tpacpi_send_radiosw_update(void);
1124
1125/* wlsw_emulstate ------------------------------------------------------ */
1126static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1127 char *buf)
1128{
1129 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1130}
1131
1132static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1133 const char *buf, size_t count)
1134{
1135 unsigned long t;
1136
1137 if (parse_strtoul(buf, 1, &t))
1138 return -EINVAL;
1139
1140 if (tpacpi_wlsw_emulstate != t) {
1141 tpacpi_wlsw_emulstate = !!t;
1142 tpacpi_send_radiosw_update();
1143 } else
1144 tpacpi_wlsw_emulstate = !!t;
1145
1146 return count;
1147}
1148
1149static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1150 tpacpi_driver_wlsw_emulstate_show,
1151 tpacpi_driver_wlsw_emulstate_store);
1152
1153/* bluetooth_emulstate ------------------------------------------------- */
1154static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1155 struct device_driver *drv,
1156 char *buf)
1157{
1158 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1159}
1160
1161static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1162 struct device_driver *drv,
1163 const char *buf, size_t count)
1164{
1165 unsigned long t;
1166
1167 if (parse_strtoul(buf, 1, &t))
1168 return -EINVAL;
1169
1170 tpacpi_bluetooth_emulstate = !!t;
1171
1172 return count;
1173}
1174
1175static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1176 tpacpi_driver_bluetooth_emulstate_show,
1177 tpacpi_driver_bluetooth_emulstate_store);
1178
1179/* wwan_emulstate ------------------------------------------------- */
1180static ssize_t tpacpi_driver_wwan_emulstate_show(
1181 struct device_driver *drv,
1182 char *buf)
1183{
1184 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1185}
1186
1187static ssize_t tpacpi_driver_wwan_emulstate_store(
1188 struct device_driver *drv,
1189 const char *buf, size_t count)
1190{
1191 unsigned long t;
1192
1193 if (parse_strtoul(buf, 1, &t))
1194 return -EINVAL;
1195
1196 tpacpi_wwan_emulstate = !!t;
1197
1198 return count;
1199}
1200
1201static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1202 tpacpi_driver_wwan_emulstate_show,
1203 tpacpi_driver_wwan_emulstate_store);
1204
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001205/* uwb_emulstate ------------------------------------------------- */
1206static ssize_t tpacpi_driver_uwb_emulstate_show(
1207 struct device_driver *drv,
1208 char *buf)
1209{
1210 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1211}
1212
1213static ssize_t tpacpi_driver_uwb_emulstate_store(
1214 struct device_driver *drv,
1215 const char *buf, size_t count)
1216{
1217 unsigned long t;
1218
1219 if (parse_strtoul(buf, 1, &t))
1220 return -EINVAL;
1221
1222 tpacpi_uwb_emulstate = !!t;
1223
1224 return count;
1225}
1226
1227static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1228 tpacpi_driver_uwb_emulstate_show,
1229 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001230#endif
1231
1232/* --------------------------------------------------------------------- */
1233
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001234static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001235 &driver_attr_debug_level, &driver_attr_version,
1236 &driver_attr_interface_version,
1237};
1238
1239static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1240{
1241 int i, res;
1242
1243 i = 0;
1244 res = 0;
1245 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1246 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1247 i++;
1248 }
1249
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001250#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1251 if (!res && dbg_wlswemul)
1252 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1253 if (!res && dbg_bluetoothemul)
1254 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1255 if (!res && dbg_wwanemul)
1256 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001257 if (!res && dbg_uwbemul)
1258 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001259#endif
1260
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001261 return res;
1262}
1263
1264static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1265{
1266 int i;
1267
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001268 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001269 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001270
1271#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1272 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1273 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1274 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001275 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001276#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001277}
1278
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001279/****************************************************************************
1280 ****************************************************************************
1281 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001282 * Subdrivers
1283 *
1284 ****************************************************************************
1285 ****************************************************************************/
1286
1287/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001288 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001289 */
1290
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001291static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001293 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1294 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001296 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001297 (thinkpad_id.bios_version_str) ?
1298 thinkpad_id.bios_version_str : "unknown",
1299 (thinkpad_id.ec_version_str) ?
1300 thinkpad_id.ec_version_str : "unknown");
1301
1302 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001303 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001304 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1305 "IBM" : ((thinkpad_id.vendor ==
1306 PCI_VENDOR_ID_LENOVO) ?
1307 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001308 thinkpad_id.model_str,
1309 (thinkpad_id.nummodel_str) ?
1310 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return 0;
1313}
1314
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001315static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 int len = 0;
1318
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001319 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1320 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 return len;
1323}
1324
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001325static struct ibm_struct thinkpad_acpi_driver_data = {
1326 .name = "driver",
1327 .read = thinkpad_acpi_driver_read,
1328};
1329
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001330/*************************************************************************
1331 * Hotkey subdriver
1332 */
1333
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001334enum { /* hot key scan codes (derived from ACPI DSDT) */
1335 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1336 TP_ACPI_HOTKEYSCAN_FNF2,
1337 TP_ACPI_HOTKEYSCAN_FNF3,
1338 TP_ACPI_HOTKEYSCAN_FNF4,
1339 TP_ACPI_HOTKEYSCAN_FNF5,
1340 TP_ACPI_HOTKEYSCAN_FNF6,
1341 TP_ACPI_HOTKEYSCAN_FNF7,
1342 TP_ACPI_HOTKEYSCAN_FNF8,
1343 TP_ACPI_HOTKEYSCAN_FNF9,
1344 TP_ACPI_HOTKEYSCAN_FNF10,
1345 TP_ACPI_HOTKEYSCAN_FNF11,
1346 TP_ACPI_HOTKEYSCAN_FNF12,
1347 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1348 TP_ACPI_HOTKEYSCAN_FNINSERT,
1349 TP_ACPI_HOTKEYSCAN_FNDELETE,
1350 TP_ACPI_HOTKEYSCAN_FNHOME,
1351 TP_ACPI_HOTKEYSCAN_FNEND,
1352 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1353 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1354 TP_ACPI_HOTKEYSCAN_FNSPACE,
1355 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1356 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1357 TP_ACPI_HOTKEYSCAN_MUTE,
1358 TP_ACPI_HOTKEYSCAN_THINKPAD,
1359};
1360
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001361enum { /* Keys available through NVRAM polling */
1362 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1363 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1364};
1365
1366enum { /* Positions of some of the keys in hotkey masks */
1367 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1368 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1369 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1370 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1371 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1372 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1373 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1374 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1375 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1376 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1377 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1378};
1379
1380enum { /* NVRAM to ACPI HKEY group map */
1381 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1382 TP_ACPI_HKEY_ZOOM_MASK |
1383 TP_ACPI_HKEY_DISPSWTCH_MASK |
1384 TP_ACPI_HKEY_HIBERNATE_MASK,
1385 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1386 TP_ACPI_HKEY_BRGHTDWN_MASK,
1387 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1388 TP_ACPI_HKEY_VOLDWN_MASK |
1389 TP_ACPI_HKEY_MUTE_MASK,
1390};
1391
1392#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1393struct tp_nvram_state {
1394 u16 thinkpad_toggle:1;
1395 u16 zoom_toggle:1;
1396 u16 display_toggle:1;
1397 u16 thinklight_toggle:1;
1398 u16 hibernate_toggle:1;
1399 u16 displayexp_toggle:1;
1400 u16 display_state:1;
1401 u16 brightness_toggle:1;
1402 u16 volume_toggle:1;
1403 u16 mute:1;
1404
1405 u8 brightness_level;
1406 u8 volume_level;
1407};
1408
1409static struct task_struct *tpacpi_hotkey_task;
1410static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1411static int hotkey_poll_freq = 10; /* Hz */
1412static struct mutex hotkey_thread_mutex;
1413static struct mutex hotkey_thread_data_mutex;
1414static unsigned int hotkey_config_change;
1415
1416#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1417
1418#define hotkey_source_mask 0U
1419
1420#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1421
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001422static struct mutex hotkey_mutex;
1423
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001424static enum { /* Reasons for waking up */
1425 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1426 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1427 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1428} hotkey_wakeup_reason;
1429
1430static int hotkey_autosleep_ack;
1431
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001432static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001433static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001434static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001435static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001436
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001437static unsigned int hotkey_report_mode;
1438
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001439static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001440
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001441static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001442
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001443#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1444#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001445 do { \
1446 mutex_lock(&hotkey_thread_data_mutex); \
1447 hotkey_config_change++; \
1448 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001449#define HOTKEY_CONFIG_CRITICAL_END \
1450 mutex_unlock(&hotkey_thread_data_mutex);
1451#else
1452#define HOTKEY_CONFIG_CRITICAL_START
1453#define HOTKEY_CONFIG_CRITICAL_END
1454#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1455
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001456/* HKEY.MHKG() return bits */
1457#define TP_HOTKEY_TABLET_MASK (1 << 3)
1458
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001459static int hotkey_get_wlsw(int *status)
1460{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001461#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1462 if (dbg_wlswemul) {
1463 *status = !!tpacpi_wlsw_emulstate;
1464 return 0;
1465 }
1466#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001467 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1468 return -EIO;
1469 return 0;
1470}
1471
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001472static int hotkey_get_tablet_mode(int *status)
1473{
1474 int s;
1475
1476 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1477 return -EIO;
1478
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001479 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1480 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001481}
1482
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001483/*
1484 * Call with hotkey_mutex held
1485 */
1486static int hotkey_mask_get(void)
1487{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001488 u32 m = 0;
1489
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001490 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001491 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001492 return -EIO;
1493 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001494 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001495
1496 return 0;
1497}
1498
1499/*
1500 * Call with hotkey_mutex held
1501 */
1502static int hotkey_mask_set(u32 mask)
1503{
1504 int i;
1505 int rc = 0;
1506
1507 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001508 if (!tp_warned.hotkey_mask_ff &&
1509 (mask == 0xffff || mask == 0xffffff ||
1510 mask == 0xffffffff)) {
1511 tp_warned.hotkey_mask_ff = 1;
1512 printk(TPACPI_NOTICE
1513 "setting the hotkey mask to 0x%08x is likely "
1514 "not the best way to go about it\n", mask);
1515 printk(TPACPI_NOTICE
1516 "please consider using the driver defaults, "
1517 "and refer to up-to-date thinkpad-acpi "
1518 "documentation\n");
1519 }
1520
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001521 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001522 for (i = 0; i < 32; i++) {
1523 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001524 /* enable in firmware mask only keys not in NVRAM
1525 * mode, but enable the key in the cached hotkey_mask
1526 * regardless of mode, or the key will end up
1527 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001528 if (!acpi_evalf(hkey_handle,
1529 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001530 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001531 rc = -EIO;
1532 break;
1533 } else {
1534 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1535 }
1536 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001537 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001538
1539 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001540 if (!hotkey_mask_get() && !rc &&
1541 (hotkey_mask & ~hotkey_source_mask) !=
1542 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001543 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001544 "requested hot key mask 0x%08x, but "
1545 "firmware forced it to 0x%08x\n",
1546 mask, hotkey_mask);
1547 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001548 } else {
1549#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1550 HOTKEY_CONFIG_CRITICAL_START
1551 hotkey_mask = mask & hotkey_source_mask;
1552 HOTKEY_CONFIG_CRITICAL_END
1553 hotkey_mask_get();
1554 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001555 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001556 "requested hot key mask 0x%08x, "
1557 "forced to 0x%08x (NVRAM poll mask is "
1558 "0x%08x): no firmware mask support\n",
1559 mask, hotkey_mask, hotkey_source_mask);
1560 }
1561#else
1562 hotkey_mask_get();
1563 rc = -ENXIO;
1564#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001565 }
1566
1567 return rc;
1568}
1569
1570static int hotkey_status_get(int *status)
1571{
1572 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1573 return -EIO;
1574
1575 return 0;
1576}
1577
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001578static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001579{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001580 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001581 return -EIO;
1582
1583 return 0;
1584}
1585
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001586static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001587{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001588 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001589
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001590 if (tp_features.hotkey_tablet &&
1591 !hotkey_get_tablet_mode(&state)) {
1592 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001593
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001594 input_report_switch(tpacpi_inputdev,
1595 SW_TABLET_MODE, !!state);
1596 input_sync(tpacpi_inputdev);
1597
1598 mutex_unlock(&tpacpi_inputdev_send_mutex);
1599 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001600}
1601
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001602static void tpacpi_input_send_key(unsigned int scancode)
1603{
1604 unsigned int keycode;
1605
1606 keycode = hotkey_keycode_map[scancode];
1607
1608 if (keycode != KEY_RESERVED) {
1609 mutex_lock(&tpacpi_inputdev_send_mutex);
1610
1611 input_report_key(tpacpi_inputdev, keycode, 1);
1612 if (keycode == KEY_UNKNOWN)
1613 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1614 scancode);
1615 input_sync(tpacpi_inputdev);
1616
1617 input_report_key(tpacpi_inputdev, keycode, 0);
1618 if (keycode == KEY_UNKNOWN)
1619 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1620 scancode);
1621 input_sync(tpacpi_inputdev);
1622
1623 mutex_unlock(&tpacpi_inputdev_send_mutex);
1624 }
1625}
1626
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001627#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1628static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1629
1630static void tpacpi_hotkey_send_key(unsigned int scancode)
1631{
1632 tpacpi_input_send_key(scancode);
1633 if (hotkey_report_mode < 2) {
1634 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1635 0x80, 0x1001 + scancode);
1636 }
1637}
1638
1639static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1640{
1641 u8 d;
1642
1643 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1644 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1645 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1646 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1647 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1648 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1649 }
1650 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1651 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1652 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1653 }
1654 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1655 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1656 n->displayexp_toggle =
1657 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1658 }
1659 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1660 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1661 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1662 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1663 n->brightness_toggle =
1664 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1665 }
1666 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1667 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1668 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1669 >> TP_NVRAM_POS_LEVEL_VOLUME;
1670 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1671 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1672 }
1673}
1674
1675#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001676 do { \
1677 if ((mask & (1 << __scancode)) && \
1678 oldn->__member != newn->__member) \
1679 tpacpi_hotkey_send_key(__scancode); \
1680 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001681
1682#define TPACPI_MAY_SEND_KEY(__scancode) \
1683 do { if (mask & (1 << __scancode)) \
1684 tpacpi_hotkey_send_key(__scancode); } while (0)
1685
1686static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001687 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001688 u32 mask)
1689{
1690 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1694
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1696
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1698
1699 /* handle volume */
1700 if (oldn->volume_toggle != newn->volume_toggle) {
1701 if (oldn->mute != newn->mute) {
1702 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1703 }
1704 if (oldn->volume_level > newn->volume_level) {
1705 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1706 } else if (oldn->volume_level < newn->volume_level) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1708 } else if (oldn->mute == newn->mute) {
1709 /* repeated key presses that didn't change state */
1710 if (newn->mute) {
1711 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1712 } else if (newn->volume_level != 0) {
1713 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1714 } else {
1715 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1716 }
1717 }
1718 }
1719
1720 /* handle brightness */
1721 if (oldn->brightness_toggle != newn->brightness_toggle) {
1722 if (oldn->brightness_level < newn->brightness_level) {
1723 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1724 } else if (oldn->brightness_level > newn->brightness_level) {
1725 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1726 } else {
1727 /* repeated key presses that didn't change state */
1728 if (newn->brightness_level != 0) {
1729 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1730 } else {
1731 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1732 }
1733 }
1734 }
1735}
1736
1737#undef TPACPI_COMPARE_KEY
1738#undef TPACPI_MAY_SEND_KEY
1739
1740static int hotkey_kthread(void *data)
1741{
1742 struct tp_nvram_state s[2];
1743 u32 mask;
1744 unsigned int si, so;
1745 unsigned long t;
1746 unsigned int change_detector, must_reset;
1747
1748 mutex_lock(&hotkey_thread_mutex);
1749
1750 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1751 goto exit;
1752
1753 set_freezable();
1754
1755 so = 0;
1756 si = 1;
1757 t = 0;
1758
1759 /* Initial state for compares */
1760 mutex_lock(&hotkey_thread_data_mutex);
1761 change_detector = hotkey_config_change;
1762 mask = hotkey_source_mask & hotkey_mask;
1763 mutex_unlock(&hotkey_thread_data_mutex);
1764 hotkey_read_nvram(&s[so], mask);
1765
1766 while (!kthread_should_stop() && hotkey_poll_freq) {
1767 if (t == 0)
1768 t = 1000/hotkey_poll_freq;
1769 t = msleep_interruptible(t);
1770 if (unlikely(kthread_should_stop()))
1771 break;
1772 must_reset = try_to_freeze();
1773 if (t > 0 && !must_reset)
1774 continue;
1775
1776 mutex_lock(&hotkey_thread_data_mutex);
1777 if (must_reset || hotkey_config_change != change_detector) {
1778 /* forget old state on thaw or config change */
1779 si = so;
1780 t = 0;
1781 change_detector = hotkey_config_change;
1782 }
1783 mask = hotkey_source_mask & hotkey_mask;
1784 mutex_unlock(&hotkey_thread_data_mutex);
1785
1786 if (likely(mask)) {
1787 hotkey_read_nvram(&s[si], mask);
1788 if (likely(si != so)) {
1789 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001790 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001791 }
1792 }
1793
1794 so = si;
1795 si ^= 1;
1796 }
1797
1798exit:
1799 mutex_unlock(&hotkey_thread_mutex);
1800 return 0;
1801}
1802
1803static void hotkey_poll_stop_sync(void)
1804{
1805 if (tpacpi_hotkey_task) {
1806 if (frozen(tpacpi_hotkey_task) ||
1807 freezing(tpacpi_hotkey_task))
1808 thaw_process(tpacpi_hotkey_task);
1809
1810 kthread_stop(tpacpi_hotkey_task);
1811 tpacpi_hotkey_task = NULL;
1812 mutex_lock(&hotkey_thread_mutex);
1813 /* at this point, the thread did exit */
1814 mutex_unlock(&hotkey_thread_mutex);
1815 }
1816}
1817
1818/* call with hotkey_mutex held */
1819static void hotkey_poll_setup(int may_warn)
1820{
1821 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1822 hotkey_poll_freq > 0 &&
1823 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1824 if (!tpacpi_hotkey_task) {
1825 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001826 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001827 if (IS_ERR(tpacpi_hotkey_task)) {
1828 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001829 printk(TPACPI_ERR
1830 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001831 "for hotkey polling\n");
1832 }
1833 }
1834 } else {
1835 hotkey_poll_stop_sync();
1836 if (may_warn &&
1837 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001838 printk(TPACPI_NOTICE
1839 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001840 "which is currently disabled\n",
1841 hotkey_source_mask);
1842 }
1843 }
1844}
1845
1846static void hotkey_poll_setup_safe(int may_warn)
1847{
1848 mutex_lock(&hotkey_mutex);
1849 hotkey_poll_setup(may_warn);
1850 mutex_unlock(&hotkey_mutex);
1851}
1852
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001853#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1854
1855static void hotkey_poll_setup_safe(int __unused)
1856{
1857}
1858
1859#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1860
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001861static int hotkey_inputdev_open(struct input_dev *dev)
1862{
1863 switch (tpacpi_lifecycle) {
1864 case TPACPI_LIFE_INIT:
1865 /*
1866 * hotkey_init will call hotkey_poll_setup_safe
1867 * at the appropriate moment
1868 */
1869 return 0;
1870 case TPACPI_LIFE_EXITING:
1871 return -EBUSY;
1872 case TPACPI_LIFE_RUNNING:
1873 hotkey_poll_setup_safe(0);
1874 return 0;
1875 }
1876
1877 /* Should only happen if tpacpi_lifecycle is corrupt */
1878 BUG();
1879 return -EBUSY;
1880}
1881
1882static void hotkey_inputdev_close(struct input_dev *dev)
1883{
1884 /* disable hotkey polling when possible */
1885 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1886 hotkey_poll_setup_safe(0);
1887}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001888
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001889/* sysfs hotkey enable ------------------------------------------------- */
1890static ssize_t hotkey_enable_show(struct device *dev,
1891 struct device_attribute *attr,
1892 char *buf)
1893{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001894 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001895
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001896 printk_deprecated_attribute("hotkey_enable",
1897 "Hotkey reporting is always enabled");
1898
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001899 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001900 if (res)
1901 return res;
1902
1903 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1904}
1905
1906static ssize_t hotkey_enable_store(struct device *dev,
1907 struct device_attribute *attr,
1908 const char *buf, size_t count)
1909{
1910 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001911
1912 printk_deprecated_attribute("hotkey_enable",
1913 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001914
1915 if (parse_strtoul(buf, 1, &t))
1916 return -EINVAL;
1917
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001918 if (t == 0)
1919 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001920
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001921 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001922}
1923
1924static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001925 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001926 hotkey_enable_show, hotkey_enable_store);
1927
1928/* sysfs hotkey mask --------------------------------------------------- */
1929static ssize_t hotkey_mask_show(struct device *dev,
1930 struct device_attribute *attr,
1931 char *buf)
1932{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001933 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001934
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001935 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001936 return -ERESTARTSYS;
1937 res = hotkey_mask_get();
1938 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001939
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001940 return (res)?
1941 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001942}
1943
1944static ssize_t hotkey_mask_store(struct device *dev,
1945 struct device_attribute *attr,
1946 const char *buf, size_t count)
1947{
1948 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001949 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001950
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001951 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001952 return -EINVAL;
1953
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001954 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001955 return -ERESTARTSYS;
1956
1957 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001958
1959#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1960 hotkey_poll_setup(1);
1961#endif
1962
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001963 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001964
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00001965 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
1966
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001967 return (res) ? res : count;
1968}
1969
1970static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001971 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001972 hotkey_mask_show, hotkey_mask_store);
1973
1974/* sysfs hotkey bios_enabled ------------------------------------------- */
1975static ssize_t hotkey_bios_enabled_show(struct device *dev,
1976 struct device_attribute *attr,
1977 char *buf)
1978{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00001979 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001980}
1981
1982static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001983 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001984
1985/* sysfs hotkey bios_mask ---------------------------------------------- */
1986static ssize_t hotkey_bios_mask_show(struct device *dev,
1987 struct device_attribute *attr,
1988 char *buf)
1989{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001990 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001991}
1992
1993static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001994 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001995
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001996/* sysfs hotkey all_mask ----------------------------------------------- */
1997static ssize_t hotkey_all_mask_show(struct device *dev,
1998 struct device_attribute *attr,
1999 char *buf)
2000{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002001 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2002 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002003}
2004
2005static struct device_attribute dev_attr_hotkey_all_mask =
2006 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2007
2008/* sysfs hotkey recommended_mask --------------------------------------- */
2009static ssize_t hotkey_recommended_mask_show(struct device *dev,
2010 struct device_attribute *attr,
2011 char *buf)
2012{
2013 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002014 (hotkey_all_mask | hotkey_source_mask)
2015 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002016}
2017
2018static struct device_attribute dev_attr_hotkey_recommended_mask =
2019 __ATTR(hotkey_recommended_mask, S_IRUGO,
2020 hotkey_recommended_mask_show, NULL);
2021
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002022#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2023
2024/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2025static ssize_t hotkey_source_mask_show(struct device *dev,
2026 struct device_attribute *attr,
2027 char *buf)
2028{
2029 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2030}
2031
2032static ssize_t hotkey_source_mask_store(struct device *dev,
2033 struct device_attribute *attr,
2034 const char *buf, size_t count)
2035{
2036 unsigned long t;
2037
2038 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2039 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2040 return -EINVAL;
2041
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002042 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043 return -ERESTARTSYS;
2044
2045 HOTKEY_CONFIG_CRITICAL_START
2046 hotkey_source_mask = t;
2047 HOTKEY_CONFIG_CRITICAL_END
2048
2049 hotkey_poll_setup(1);
2050
2051 mutex_unlock(&hotkey_mutex);
2052
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002053 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2054
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002055 return count;
2056}
2057
2058static struct device_attribute dev_attr_hotkey_source_mask =
2059 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2060 hotkey_source_mask_show, hotkey_source_mask_store);
2061
2062/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2063static ssize_t hotkey_poll_freq_show(struct device *dev,
2064 struct device_attribute *attr,
2065 char *buf)
2066{
2067 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2068}
2069
2070static ssize_t hotkey_poll_freq_store(struct device *dev,
2071 struct device_attribute *attr,
2072 const char *buf, size_t count)
2073{
2074 unsigned long t;
2075
2076 if (parse_strtoul(buf, 25, &t))
2077 return -EINVAL;
2078
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002079 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002080 return -ERESTARTSYS;
2081
2082 hotkey_poll_freq = t;
2083
2084 hotkey_poll_setup(1);
2085 mutex_unlock(&hotkey_mutex);
2086
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002087 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2088
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002089 return count;
2090}
2091
2092static struct device_attribute dev_attr_hotkey_poll_freq =
2093 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2094 hotkey_poll_freq_show, hotkey_poll_freq_store);
2095
2096#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2097
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002098/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002099static ssize_t hotkey_radio_sw_show(struct device *dev,
2100 struct device_attribute *attr,
2101 char *buf)
2102{
2103 int res, s;
2104 res = hotkey_get_wlsw(&s);
2105 if (res < 0)
2106 return res;
2107
2108 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2109}
2110
2111static struct device_attribute dev_attr_hotkey_radio_sw =
2112 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2113
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002114static void hotkey_radio_sw_notify_change(void)
2115{
2116 if (tp_features.hotkey_wlsw)
2117 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2118 "hotkey_radio_sw");
2119}
2120
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002121/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2122static ssize_t hotkey_tablet_mode_show(struct device *dev,
2123 struct device_attribute *attr,
2124 char *buf)
2125{
2126 int res, s;
2127 res = hotkey_get_tablet_mode(&s);
2128 if (res < 0)
2129 return res;
2130
2131 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2132}
2133
2134static struct device_attribute dev_attr_hotkey_tablet_mode =
2135 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2136
2137static void hotkey_tablet_mode_notify_change(void)
2138{
2139 if (tp_features.hotkey_tablet)
2140 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2141 "hotkey_tablet_mode");
2142}
2143
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002144/* sysfs hotkey report_mode -------------------------------------------- */
2145static ssize_t hotkey_report_mode_show(struct device *dev,
2146 struct device_attribute *attr,
2147 char *buf)
2148{
2149 return snprintf(buf, PAGE_SIZE, "%d\n",
2150 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2151}
2152
2153static struct device_attribute dev_attr_hotkey_report_mode =
2154 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2155
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002156/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002157static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2158 struct device_attribute *attr,
2159 char *buf)
2160{
2161 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2162}
2163
2164static struct device_attribute dev_attr_hotkey_wakeup_reason =
2165 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2166
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002167static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002168{
2169 if (tp_features.hotkey_mask)
2170 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2171 "wakeup_reason");
2172}
2173
2174/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002175static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2176 struct device_attribute *attr,
2177 char *buf)
2178{
2179 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2180}
2181
2182static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2183 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2184 hotkey_wakeup_hotunplug_complete_show, NULL);
2185
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002186static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002187{
2188 if (tp_features.hotkey_mask)
2189 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2190 "wakeup_hotunplug_complete");
2191}
2192
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002193/* --------------------------------------------------------------------- */
2194
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002195static struct attribute *hotkey_attributes[] __initdata = {
2196 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002197 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002198 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002199#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2200 &dev_attr_hotkey_mask.attr,
2201 &dev_attr_hotkey_all_mask.attr,
2202 &dev_attr_hotkey_recommended_mask.attr,
2203 &dev_attr_hotkey_source_mask.attr,
2204 &dev_attr_hotkey_poll_freq.attr,
2205#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002206};
2207
2208static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002209 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002210#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2211 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002212 &dev_attr_hotkey_all_mask.attr,
2213 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002214#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002215 &dev_attr_hotkey_wakeup_reason.attr,
2216 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002217};
2218
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002219static void bluetooth_update_rfk(void);
2220static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002221static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002222static void tpacpi_send_radiosw_update(void)
2223{
2224 int wlsw;
2225
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002226 /* Sync these BEFORE sending any rfkill events */
2227 if (tp_features.bluetooth)
2228 bluetooth_update_rfk();
2229 if (tp_features.wan)
2230 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002231 if (tp_features.uwb)
2232 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002233
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002234 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2235 mutex_lock(&tpacpi_inputdev_send_mutex);
2236
2237 input_report_switch(tpacpi_inputdev,
2238 SW_RFKILL_ALL, !!wlsw);
2239 input_sync(tpacpi_inputdev);
2240
2241 mutex_unlock(&tpacpi_inputdev_send_mutex);
2242 }
2243 hotkey_radio_sw_notify_change();
2244}
2245
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002246static void hotkey_exit(void)
2247{
2248#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2249 hotkey_poll_stop_sync();
2250#endif
2251
2252 if (hotkey_dev_attributes)
2253 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2254
2255 kfree(hotkey_keycode_map);
2256
2257 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002258 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002259 "restoring original hot key mask\n");
2260 /* no short-circuit boolean operator below! */
2261 if ((hotkey_mask_set(hotkey_orig_mask) |
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002262 hotkey_status_set(false)) != 0)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002263 printk(TPACPI_ERR
2264 "failed to restore hot key mask "
2265 "to BIOS defaults\n");
2266 }
2267}
2268
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002269static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002270{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002271 /* Requirements for changing the default keymaps:
2272 *
2273 * 1. Many of the keys are mapped to KEY_RESERVED for very
2274 * good reasons. Do not change them unless you have deep
2275 * knowledge on the IBM and Lenovo ThinkPad firmware for
2276 * the various ThinkPad models. The driver behaves
2277 * differently for KEY_RESERVED: such keys have their
2278 * hot key mask *unset* in mask_recommended, and also
2279 * in the initial hot key mask programmed into the
2280 * firmware at driver load time, which means the firm-
2281 * ware may react very differently if you change them to
2282 * something else;
2283 *
2284 * 2. You must be subscribed to the linux-thinkpad and
2285 * ibm-acpi-devel mailing lists, and you should read the
2286 * list archives since 2007 if you want to change the
2287 * keymaps. This requirement exists so that you will
2288 * know the past history of problems with the thinkpad-
2289 * acpi driver keymaps, and also that you will be
2290 * listening to any bug reports;
2291 *
2292 * 3. Do not send thinkpad-acpi specific patches directly to
2293 * for merging, *ever*. Send them to the linux-acpi
2294 * mailinglist for comments. Merging is to be done only
2295 * through acpi-test and the ACPI maintainer.
2296 *
2297 * If the above is too much to ask, don't change the keymap.
2298 * Ask the thinkpad-acpi maintainer to do it, instead.
2299 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002300 static u16 ibm_keycode_map[] __initdata = {
2301 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2302 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2303 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2304 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002305
2306 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002307 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2308 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2309 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002310
2311 /* brightness: firmware always reacts to them, unless
2312 * X.org did some tricks in the radeon BIOS scratch
2313 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002314 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002315 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002316
2317 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002318 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002319
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002320 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2321 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002322
2323 /* Volume: firmware always react to it and reprograms
2324 * the built-in *extra* mixer. Never map it to control
2325 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002326 KEY_RESERVED, /* 0x14: VOLUME UP */
2327 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2328 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002329
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002330 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002331
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002332 /* (assignments unknown, please report if found) */
2333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2335 };
2336 static u16 lenovo_keycode_map[] __initdata = {
2337 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2338 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2339 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2340 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002341
2342 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002343 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2344 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2345 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002346
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002347 /* These either have to go through ACPI video, or
2348 * act like in the IBM ThinkPads, so don't ever
2349 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002350 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002351 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002352
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002353 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002354
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002355 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2356 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002357
2358 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2359 * react to it and reprograms the built-in *extra* mixer.
2360 * Never map it to control another mixer by default.
2361 *
2362 * T60?, T61, R60?, R61: firmware and EC tries to send
2363 * these over the regular keyboard, so these are no-ops,
2364 * but there are still weird bugs re. MUTE, so do not
2365 * change unless you get test reports from all Lenovo
2366 * models. May cause the BIOS to interfere with the
2367 * HDA mixer.
2368 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002369 KEY_RESERVED, /* 0x14: VOLUME UP */
2370 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2371 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002372
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002373 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002374
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002375 /* (assignments unknown, please report if found) */
2376 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2377 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2378 };
2379
2380#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2381#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2382#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2383
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002384 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002385 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002386 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002387
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002388 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2389 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002390
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002391 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002392 BUG_ON(tpacpi_inputdev->open != NULL ||
2393 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002394
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002395 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002396 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002397
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002398#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2399 mutex_init(&hotkey_thread_mutex);
2400 mutex_init(&hotkey_thread_data_mutex);
2401#endif
2402
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002403 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002404 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002405
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002406 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2407 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002408 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002409
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002410 if (!tp_features.hotkey)
2411 return 1;
2412
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03002413 tpacpi_disable_brightness_delay();
2414
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002415 hotkey_dev_attributes = create_attr_set(13, NULL);
2416 if (!hotkey_dev_attributes)
2417 return -ENOMEM;
2418 res = add_many_to_attr_set(hotkey_dev_attributes,
2419 hotkey_attributes,
2420 ARRAY_SIZE(hotkey_attributes));
2421 if (res)
2422 goto err_exit;
2423
2424 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2425 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2426 for HKEY interface version 0x100 */
2427 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2428 if ((hkeyv >> 8) != 1) {
2429 printk(TPACPI_ERR "unknown version of the "
2430 "HKEY interface: 0x%x\n", hkeyv);
2431 printk(TPACPI_ERR "please report this to %s\n",
2432 TPACPI_MAIL);
2433 } else {
2434 /*
2435 * MHKV 0x100 in A31, R40, R40e,
2436 * T4x, X31, and later
2437 */
2438 tp_features.hotkey_mask = 1;
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002439 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2440 "firmware HKEY interface version: 0x%x\n",
2441 hkeyv);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002442 }
2443 }
2444
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2446 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002447 str_supported(tp_features.hotkey_mask));
2448
2449 if (tp_features.hotkey_mask) {
2450 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2451 "MHKA", "qd")) {
2452 printk(TPACPI_ERR
2453 "missing MHKA handler, "
2454 "please report this to %s\n",
2455 TPACPI_MAIL);
2456 /* FN+F12, FN+F4, FN+F3 */
2457 hotkey_all_mask = 0x080cU;
2458 }
2459 }
2460
2461 /* hotkey_source_mask *must* be zero for
2462 * the first hotkey_mask_get */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002463 if (tp_features.hotkey_mask) {
2464 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002465 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002466 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002467
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002468 hotkey_orig_mask = hotkey_mask;
2469 res = add_many_to_attr_set(
2470 hotkey_dev_attributes,
2471 hotkey_mask_attributes,
2472 ARRAY_SIZE(hotkey_mask_attributes));
2473 if (res)
2474 goto err_exit;
2475 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002476
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002477#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002478 if (tp_features.hotkey_mask) {
2479 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2480 & ~hotkey_all_mask;
2481 } else {
2482 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2483 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002485 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002486 "hotkey source mask 0x%08x, polling freq %d\n",
2487 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488#endif
2489
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002490#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2491 if (dbg_wlswemul) {
2492 tp_features.hotkey_wlsw = 1;
2493 printk(TPACPI_INFO
2494 "radio switch emulation enabled\n");
2495 } else
2496#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002497 /* Not all thinkpads have a hardware radio switch */
2498 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2499 tp_features.hotkey_wlsw = 1;
2500 printk(TPACPI_INFO
2501 "radio switch found; radios are %s\n",
2502 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002503 }
2504 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002505 res = add_to_attr_set(hotkey_dev_attributes,
2506 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002507
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002508 /* For X41t, X60t, X61t Tablets... */
2509 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2510 tp_features.hotkey_tablet = 1;
2511 printk(TPACPI_INFO
2512 "possible tablet mode switch found; "
2513 "ThinkPad in %s mode\n",
2514 (status & TP_HOTKEY_TABLET_MASK)?
2515 "tablet" : "laptop");
2516 res = add_to_attr_set(hotkey_dev_attributes,
2517 &dev_attr_hotkey_tablet_mode.attr);
2518 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002519
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002520 if (!res)
2521 res = register_attr_set_with_sysfs(
2522 hotkey_dev_attributes,
2523 &tpacpi_pdev->dev.kobj);
2524 if (res)
2525 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002526
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002527 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002528
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002529 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2530 GFP_KERNEL);
2531 if (!hotkey_keycode_map) {
2532 printk(TPACPI_ERR
2533 "failed to allocate memory for key map\n");
2534 res = -ENOMEM;
2535 goto err_exit;
2536 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002537
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002538 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002539 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002540 "using Lenovo default hot key map\n");
2541 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2542 TPACPI_HOTKEY_MAP_SIZE);
2543 } else {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002544 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002545 "using IBM default hot key map\n");
2546 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2547 TPACPI_HOTKEY_MAP_SIZE);
2548 }
2549
2550 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2551 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2552 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2553 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2554 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2555 tpacpi_inputdev->keycode = hotkey_keycode_map;
2556 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2557 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2558 set_bit(hotkey_keycode_map[i],
2559 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002560 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002561 if (i < sizeof(hotkey_reserved_mask)*8)
2562 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002563 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002564 }
2565
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002566 if (tp_features.hotkey_wlsw) {
2567 set_bit(EV_SW, tpacpi_inputdev->evbit);
2568 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2569 }
2570 if (tp_features.hotkey_tablet) {
2571 set_bit(EV_SW, tpacpi_inputdev->evbit);
2572 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002573 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002574
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002575 /* Do not issue duplicate brightness change events to
2576 * userspace */
2577 if (!tp_features.bright_acpimode)
2578 /* update bright_acpimode... */
2579 tpacpi_check_std_acpi_brightness_support();
2580
2581 if (tp_features.bright_acpimode) {
2582 printk(TPACPI_INFO
2583 "This ThinkPad has standard ACPI backlight "
2584 "brightness control, supported by the ACPI "
2585 "video driver\n");
2586 printk(TPACPI_NOTICE
2587 "Disabling thinkpad-acpi brightness events "
2588 "by default...\n");
2589
2590 /* The hotkey_reserved_mask change below is not
2591 * necessary while the keys are at KEY_RESERVED in the
2592 * default map, but better safe than sorry, leave it
2593 * here as a marker of what we have to do, especially
2594 * when we finally become able to set this at runtime
2595 * on response to X.org requests */
2596 hotkey_reserved_mask |=
2597 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2598 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002599 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002600
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002601 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2602 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002603 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002604 if (res) {
2605 hotkey_exit();
2606 return res;
2607 }
2608 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2609 & ~hotkey_reserved_mask)
2610 | hotkey_orig_mask);
2611 if (res < 0 && res != -ENXIO) {
2612 hotkey_exit();
2613 return res;
2614 }
2615
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002616 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2617 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002618 (hotkey_report_mode < 2) ?
2619 "enabled" : "disabled");
2620
2621 tpacpi_inputdev->open = &hotkey_inputdev_open;
2622 tpacpi_inputdev->close = &hotkey_inputdev_close;
2623
2624 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002625 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002626 tpacpi_input_send_tabletsw();
2627
2628 return 0;
2629
2630err_exit:
2631 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2632 hotkey_dev_attributes = NULL;
2633
2634 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002635}
2636
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002637static bool hotkey_notify_hotkey(const u32 hkey,
2638 bool *send_acpi_ev,
2639 bool *ignore_acpi_ev)
2640{
2641 /* 0x1000-0x1FFF: key presses */
2642 unsigned int scancode = hkey & 0xfff;
2643 *send_acpi_ev = true;
2644 *ignore_acpi_ev = false;
2645
2646 if (scancode > 0 && scancode < 0x21) {
2647 scancode--;
2648 if (!(hotkey_source_mask & (1 << scancode))) {
2649 tpacpi_input_send_key(scancode);
2650 *send_acpi_ev = false;
2651 } else {
2652 *ignore_acpi_ev = true;
2653 }
2654 return true;
2655 }
2656 return false;
2657}
2658
2659static bool hotkey_notify_wakeup(const u32 hkey,
2660 bool *send_acpi_ev,
2661 bool *ignore_acpi_ev)
2662{
2663 /* 0x2000-0x2FFF: Wakeup reason */
2664 *send_acpi_ev = true;
2665 *ignore_acpi_ev = false;
2666
2667 switch (hkey) {
2668 case 0x2304: /* suspend, undock */
2669 case 0x2404: /* hibernation, undock */
2670 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2671 *ignore_acpi_ev = true;
2672 break;
2673
2674 case 0x2305: /* suspend, bay eject */
2675 case 0x2405: /* hibernation, bay eject */
2676 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2677 *ignore_acpi_ev = true;
2678 break;
2679
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002680 case 0x2313: /* Battery on critical low level (S3) */
2681 case 0x2413: /* Battery on critical low level (S4) */
2682 printk(TPACPI_ALERT
2683 "EMERGENCY WAKEUP: battery almost empty\n");
2684 /* how to auto-heal: */
2685 /* 2313: woke up from S3, go to S4/S5 */
2686 /* 2413: woke up from S4, go to S5 */
2687 break;
2688
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002689 default:
2690 return false;
2691 }
2692
2693 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2694 printk(TPACPI_INFO
2695 "woke up due to a hot-unplug "
2696 "request...\n");
2697 hotkey_wakeup_reason_notify_change();
2698 }
2699 return true;
2700}
2701
2702static bool hotkey_notify_usrevent(const u32 hkey,
2703 bool *send_acpi_ev,
2704 bool *ignore_acpi_ev)
2705{
2706 /* 0x5000-0x5FFF: human interface helpers */
2707 *send_acpi_ev = true;
2708 *ignore_acpi_ev = false;
2709
2710 switch (hkey) {
2711 case 0x5010: /* Lenovo new BIOS: brightness changed */
2712 case 0x500b: /* X61t: tablet pen inserted into bay */
2713 case 0x500c: /* X61t: tablet pen removed from bay */
2714 return true;
2715
2716 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2717 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2718 tpacpi_input_send_tabletsw();
2719 hotkey_tablet_mode_notify_change();
2720 *send_acpi_ev = false;
2721 return true;
2722
2723 case 0x5001:
2724 case 0x5002:
2725 /* LID switch events. Do not propagate */
2726 *ignore_acpi_ev = true;
2727 return true;
2728
2729 default:
2730 return false;
2731 }
2732}
2733
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002734static bool hotkey_notify_thermal(const u32 hkey,
2735 bool *send_acpi_ev,
2736 bool *ignore_acpi_ev)
2737{
2738 /* 0x6000-0x6FFF: thermal alarms */
2739 *send_acpi_ev = true;
2740 *ignore_acpi_ev = false;
2741
2742 switch (hkey) {
2743 case 0x6011:
2744 printk(TPACPI_CRIT
2745 "THERMAL ALARM: battery is too hot!\n");
2746 /* recommended action: warn user through gui */
2747 return true;
2748 case 0x6012:
2749 printk(TPACPI_ALERT
2750 "THERMAL EMERGENCY: battery is extremely hot!\n");
2751 /* recommended action: immediate sleep/hibernate */
2752 return true;
2753 case 0x6021:
2754 printk(TPACPI_CRIT
2755 "THERMAL ALARM: "
2756 "a sensor reports something is too hot!\n");
2757 /* recommended action: warn user through gui, that */
2758 /* some internal component is too hot */
2759 return true;
2760 case 0x6022:
2761 printk(TPACPI_ALERT
2762 "THERMAL EMERGENCY: "
2763 "a sensor reports something is extremely hot!\n");
2764 /* recommended action: immediate sleep/hibernate */
2765 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002766 case 0x6030:
2767 printk(TPACPI_INFO
2768 "EC reports that Thermal Table has changed\n");
2769 /* recommended action: do nothing, we don't have
2770 * Lenovo ATM information */
2771 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002772 default:
2773 printk(TPACPI_ALERT
2774 "THERMAL ALERT: unknown thermal alarm received\n");
2775 return false;
2776 }
2777}
2778
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002779static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2780{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002781 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002782 bool send_acpi_ev;
2783 bool ignore_acpi_ev;
2784 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002785
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002786 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002787 printk(TPACPI_ERR
2788 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002789 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002790 acpi_bus_generate_netlink_event(
2791 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002792 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002793 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002794 return;
2795 }
2796
2797 while (1) {
2798 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002799 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002800 return;
2801 }
2802
2803 if (hkey == 0) {
2804 /* queue empty */
2805 return;
2806 }
2807
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002808 send_acpi_ev = true;
2809 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002810
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002811 switch (hkey >> 12) {
2812 case 1:
2813 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002814 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2815 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002816 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002817 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002818 /* 0x2000-0x2FFF: Wakeup reason */
2819 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2820 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002821 break;
2822 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002823 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002824 if (hkey == 0x3003) {
2825 hotkey_autosleep_ack = 1;
2826 printk(TPACPI_INFO
2827 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002828 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002829 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002830 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002831 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002832 }
2833 break;
2834 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002835 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002836 if (hkey == 0x4003) {
2837 hotkey_autosleep_ack = 1;
2838 printk(TPACPI_INFO
2839 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002840 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002841 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002842 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002843 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002844 }
2845 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002846 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002847 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002848 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2849 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002850 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002851 case 6:
2852 /* 0x6000-0x6FFF: thermal alarms */
2853 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2854 &ignore_acpi_ev);
2855 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002856 case 7:
2857 /* 0x7000-0x7FFF: misc */
2858 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002859 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002860 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002861 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002862 break;
2863 }
2864 /* fallthrough to default */
2865 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002866 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002867 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002868 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002869 printk(TPACPI_NOTICE
2870 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002871 printk(TPACPI_NOTICE
2872 "please report the conditions when this "
2873 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002874 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002875
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002876 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002877 if (!ignore_acpi_ev &&
2878 (send_acpi_ev || hotkey_report_mode < 2)) {
2879 acpi_bus_generate_proc_event(ibm->acpi->device,
2880 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002881 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002882
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002883 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002884 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002885 acpi_bus_generate_netlink_event(
2886 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002887 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002888 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002889 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002890 }
2891}
2892
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002893static void hotkey_suspend(pm_message_t state)
2894{
2895 /* Do these on suspend, we get the events on early resume! */
2896 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2897 hotkey_autosleep_ack = 0;
2898}
2899
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002900static void hotkey_resume(void)
2901{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03002902 tpacpi_disable_brightness_delay();
2903
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002904 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002905 printk(TPACPI_ERR
2906 "error while trying to read hot key mask "
2907 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002908 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002909 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002910 hotkey_wakeup_reason_notify_change();
2911 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002912 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002913}
2914
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002915/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002916static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002918 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 int len = 0;
2920
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002921 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002922 len += sprintf(p + len, "status:\t\tnot supported\n");
2923 return len;
2924 }
2925
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002926 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002927 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002928 res = hotkey_status_get(&status);
2929 if (!res)
2930 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002931 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002932 if (res)
2933 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002936 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002937 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 len += sprintf(p + len,
2939 "commands:\tenable, disable, reset, <mask>\n");
2940 } else {
2941 len += sprintf(p + len, "mask:\t\tnot supported\n");
2942 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2943 }
2944
2945 return len;
2946}
2947
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002948static void hotkey_enabledisable_warn(void)
2949{
2950 tpacpi_log_usertask("procfs hotkey enable/disable");
2951 WARN(1, TPACPI_WARN
2952 "hotkey enable/disable functionality has been "
2953 "removed from the driver. Hotkeys are always enabled.\n");
2954}
2955
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002956static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002958 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002959 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002962 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return -ENODEV;
2964
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002965 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002966 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002967
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002968 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002969
2970 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 while ((cmd = next_cmd(&buf))) {
2972 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002973 hotkey_enabledisable_warn();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002975 hotkey_enabledisable_warn();
2976 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002978 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2980 /* mask set */
2981 } else if (sscanf(cmd, "%x", &mask) == 1) {
2982 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002983 } else {
2984 res = -EINVAL;
2985 goto errexit;
2986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002988
2989 if (!res)
2990 tpacpi_disclose_usertask("procfs hotkey",
2991 "set mask to 0x%08x\n", mask);
2992
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002993 if (!res && mask != hotkey_mask)
2994 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002996errexit:
2997 mutex_unlock(&hotkey_mutex);
2998 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003001static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003002 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003003 {"", 0},
3004};
3005
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003006static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003007 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003008 .notify = hotkey_notify,
3009 .handle = &hkey_handle,
3010 .type = ACPI_DEVICE_NOTIFY,
3011};
3012
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003013static struct ibm_struct hotkey_driver_data = {
3014 .name = "hotkey",
3015 .read = hotkey_read,
3016 .write = hotkey_write,
3017 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003018 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003019 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003020 .acpi = &ibm_hotkey_acpidriver,
3021};
3022
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003023/*************************************************************************
3024 * Bluetooth subdriver
3025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003027enum {
3028 /* ACPI GBDC/SBDC bits */
3029 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3030 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003031 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3032 off / last state */
3033};
3034
3035enum {
3036 /* ACPI \BLTH commands */
3037 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3038 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3039 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3040 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3041 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003042};
3043
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003044#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3045
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003046static struct rfkill *tpacpi_bluetooth_rfkill;
3047
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003048static void bluetooth_suspend(pm_message_t state)
3049{
3050 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003051 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3052 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3053 vdbg_printk(TPACPI_DBG_RFKILL,
3054 "bluetooth power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003055}
3056
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003057static int bluetooth_get_radiosw(void)
3058{
3059 int status;
3060
3061 if (!tp_features.bluetooth)
3062 return -ENODEV;
3063
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003064 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3065 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003066 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003067
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003068#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3069 if (dbg_bluetoothemul)
3070 return (tpacpi_bluetooth_emulstate) ?
3071 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3072#endif
3073
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003074 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3075 return -EIO;
3076
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003077 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3078 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003079}
3080
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003081static void bluetooth_update_rfk(void)
3082{
3083 int status;
3084
3085 if (!tpacpi_bluetooth_rfkill)
3086 return;
3087
3088 status = bluetooth_get_radiosw();
3089 if (status < 0)
3090 return;
3091 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003092
3093 vdbg_printk(TPACPI_DBG_RFKILL,
3094 "forced rfkill state to %d\n",
3095 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003096}
3097
3098static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003099{
3100 int status;
3101
3102 if (!tp_features.bluetooth)
3103 return -ENODEV;
3104
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003105 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3106 * reason to risk weird behaviour. */
3107 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3108 && radio_on)
3109 return -EPERM;
3110
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003111 vdbg_printk(TPACPI_DBG_RFKILL,
3112 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3113
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003114#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3115 if (dbg_bluetoothemul) {
3116 tpacpi_bluetooth_emulstate = !!radio_on;
3117 if (update_rfk)
3118 bluetooth_update_rfk();
3119 return 0;
3120 }
3121#endif
3122
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003123 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003124 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003125 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003126 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003127 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003128 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3129 return -EIO;
3130
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003131 if (update_rfk)
3132 bluetooth_update_rfk();
3133
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003134 return 0;
3135}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003136
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003137/* sysfs bluetooth enable ---------------------------------------------- */
3138static ssize_t bluetooth_enable_show(struct device *dev,
3139 struct device_attribute *attr,
3140 char *buf)
3141{
3142 int status;
3143
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003144 printk_deprecated_rfkill_attribute("bluetooth_enable");
3145
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003146 status = bluetooth_get_radiosw();
3147 if (status < 0)
3148 return status;
3149
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003150 return snprintf(buf, PAGE_SIZE, "%d\n",
3151 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003152}
3153
3154static ssize_t bluetooth_enable_store(struct device *dev,
3155 struct device_attribute *attr,
3156 const char *buf, size_t count)
3157{
3158 unsigned long t;
3159 int res;
3160
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003161 printk_deprecated_rfkill_attribute("bluetooth_enable");
3162
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003163 if (parse_strtoul(buf, 1, &t))
3164 return -EINVAL;
3165
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003166 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3167
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003168 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003169
3170 return (res) ? res : count;
3171}
3172
3173static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003174 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003175 bluetooth_enable_show, bluetooth_enable_store);
3176
3177/* --------------------------------------------------------------------- */
3178
3179static struct attribute *bluetooth_attributes[] = {
3180 &dev_attr_bluetooth_enable.attr,
3181 NULL
3182};
3183
3184static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003185 .attrs = bluetooth_attributes,
3186};
3187
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003188static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3189{
3190 int bts = bluetooth_get_radiosw();
3191
3192 if (bts < 0)
3193 return bts;
3194
3195 *state = bts;
3196 return 0;
3197}
3198
3199static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3200{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003201 dbg_printk(TPACPI_DBG_RFKILL,
3202 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003203 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3204}
3205
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003206static void bluetooth_shutdown(void)
3207{
3208 /* Order firmware to save current state to NVRAM */
3209 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3210 TP_ACPI_BLTH_SAVE_STATE))
3211 printk(TPACPI_NOTICE
3212 "failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003213 else
3214 vdbg_printk(TPACPI_DBG_RFKILL,
3215 "bluestooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003216}
3217
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003218static void bluetooth_exit(void)
3219{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003220 bluetooth_shutdown();
3221
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003222 if (tpacpi_bluetooth_rfkill)
3223 rfkill_unregister(tpacpi_bluetooth_rfkill);
3224
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003225 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3226 &bluetooth_attr_group);
3227}
3228
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003229static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003231 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003232 int status = 0;
3233
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3235 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003236
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003237 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003238
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003239 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3240 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003241 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003242 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003244 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3245 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003246 str_supported(tp_features.bluetooth),
3247 status);
3248
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003249#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3250 if (dbg_bluetoothemul) {
3251 tp_features.bluetooth = 1;
3252 printk(TPACPI_INFO
3253 "bluetooth switch emulation enabled\n");
3254 } else
3255#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003256 if (tp_features.bluetooth &&
3257 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3258 /* no bluetooth hardware present in system */
3259 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003260 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003261 "bluetooth hardware not installed\n");
3262 }
3263
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003264 if (!tp_features.bluetooth)
3265 return 1;
3266
3267 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003268 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003269 if (res)
3270 return res;
3271
3272 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3273 &tpacpi_bluetooth_rfkill,
3274 RFKILL_TYPE_BLUETOOTH,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003275 TPACPI_RFK_BLUETOOTH_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003276 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003277 tpacpi_bluetooth_rfk_set,
3278 tpacpi_bluetooth_rfk_get);
3279 if (res) {
3280 bluetooth_exit();
3281 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003282 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003283
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003287/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003288static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003291 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003293 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003296 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003297 (status == RFKILL_STATE_UNBLOCKED) ?
3298 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 len += sprintf(p + len, "commands:\tenable, disable\n");
3300 }
3301
3302 return len;
3303}
3304
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003305static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 char *cmd;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003308 int state = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003310 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003311 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 while ((cmd = next_cmd(&buf))) {
3314 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003315 state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003317 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 } else
3319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003322 if (state != -1) {
3323 tpacpi_disclose_usertask("procfs bluetooth",
3324 "attempt to %s\n",
3325 state ? "enable" : "disable");
3326 bluetooth_set_radiosw(state, 1);
3327 }
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return 0;
3330}
3331
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003332static struct ibm_struct bluetooth_driver_data = {
3333 .name = "bluetooth",
3334 .read = bluetooth_read,
3335 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003336 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003337 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003338 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003339};
3340
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003341/*************************************************************************
3342 * Wan subdriver
3343 */
3344
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003345enum {
3346 /* ACPI GWAN/SWAN bits */
3347 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3348 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003349 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3350 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003351};
3352
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003353#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3354
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003355static struct rfkill *tpacpi_wan_rfkill;
3356
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003357static void wan_suspend(pm_message_t state)
3358{
3359 /* Try to make sure radio will resume powered off */
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003360 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3361 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3362 vdbg_printk(TPACPI_DBG_RFKILL,
3363 "WWAN power down on resume request failed\n");
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003364}
3365
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003366static int wan_get_radiosw(void)
3367{
3368 int status;
3369
3370 if (!tp_features.wan)
3371 return -ENODEV;
3372
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003373 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3374 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003375 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003376
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003377#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3378 if (dbg_wwanemul)
3379 return (tpacpi_wwan_emulstate) ?
3380 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3381#endif
3382
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003383 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3384 return -EIO;
3385
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003386 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3387 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003388}
3389
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003390static void wan_update_rfk(void)
3391{
3392 int status;
3393
3394 if (!tpacpi_wan_rfkill)
3395 return;
3396
3397 status = wan_get_radiosw();
3398 if (status < 0)
3399 return;
3400 rfkill_force_state(tpacpi_wan_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003401
3402 vdbg_printk(TPACPI_DBG_RFKILL,
3403 "forced rfkill state to %d\n",
3404 status);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003405}
3406
3407static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003408{
3409 int status;
3410
3411 if (!tp_features.wan)
3412 return -ENODEV;
3413
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003414 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3415 * reason to risk weird behaviour. */
3416 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3417 && radio_on)
3418 return -EPERM;
3419
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003420 vdbg_printk(TPACPI_DBG_RFKILL,
3421 "will %s WWAN\n", radio_on ? "enable" : "disable");
3422
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003423#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3424 if (dbg_wwanemul) {
3425 tpacpi_wwan_emulstate = !!radio_on;
3426 if (update_rfk)
3427 wan_update_rfk();
3428 return 0;
3429 }
3430#endif
3431
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003432 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003433 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003434 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003435 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003436 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003437 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3438 return -EIO;
3439
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003440 if (update_rfk)
3441 wan_update_rfk();
3442
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003443 return 0;
3444}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003445
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003446/* sysfs wan enable ---------------------------------------------------- */
3447static ssize_t wan_enable_show(struct device *dev,
3448 struct device_attribute *attr,
3449 char *buf)
3450{
3451 int status;
3452
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003453 printk_deprecated_rfkill_attribute("wwan_enable");
3454
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003455 status = wan_get_radiosw();
3456 if (status < 0)
3457 return status;
3458
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003459 return snprintf(buf, PAGE_SIZE, "%d\n",
3460 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003461}
3462
3463static ssize_t wan_enable_store(struct device *dev,
3464 struct device_attribute *attr,
3465 const char *buf, size_t count)
3466{
3467 unsigned long t;
3468 int res;
3469
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00003470 printk_deprecated_rfkill_attribute("wwan_enable");
3471
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003472 if (parse_strtoul(buf, 1, &t))
3473 return -EINVAL;
3474
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003475 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3476
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003477 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003478
3479 return (res) ? res : count;
3480}
3481
3482static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003483 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003484 wan_enable_show, wan_enable_store);
3485
3486/* --------------------------------------------------------------------- */
3487
3488static struct attribute *wan_attributes[] = {
3489 &dev_attr_wan_enable.attr,
3490 NULL
3491};
3492
3493static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003494 .attrs = wan_attributes,
3495};
3496
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003497static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3498{
3499 int wans = wan_get_radiosw();
3500
3501 if (wans < 0)
3502 return wans;
3503
3504 *state = wans;
3505 return 0;
3506}
3507
3508static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3509{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003510 dbg_printk(TPACPI_DBG_RFKILL,
3511 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003512 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3513}
3514
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003515static void wan_shutdown(void)
3516{
3517 /* Order firmware to save current state to NVRAM */
3518 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3519 TP_ACPI_WGSV_SAVE_STATE))
3520 printk(TPACPI_NOTICE
3521 "failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003522 else
3523 vdbg_printk(TPACPI_DBG_RFKILL,
3524 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003525}
3526
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003527static void wan_exit(void)
3528{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003529 wan_shutdown();
3530
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003531 if (tpacpi_wan_rfkill)
3532 rfkill_unregister(tpacpi_wan_rfkill);
3533
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003534 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3535 &wan_attr_group);
3536}
3537
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003538static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003539{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003540 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003541 int status = 0;
3542
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003543 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3544 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003545
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003546 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003547
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003548 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003549 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003550
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003551 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3552 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003553 str_supported(tp_features.wan),
3554 status);
3555
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003556#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3557 if (dbg_wwanemul) {
3558 tp_features.wan = 1;
3559 printk(TPACPI_INFO
3560 "wwan switch emulation enabled\n");
3561 } else
3562#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003563 if (tp_features.wan &&
3564 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3565 /* no wan hardware present in system */
3566 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003567 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003568 "wan hardware not installed\n");
3569 }
3570
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003571 if (!tp_features.wan)
3572 return 1;
3573
3574 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003575 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003576 if (res)
3577 return res;
3578
3579 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3580 &tpacpi_wan_rfkill,
3581 RFKILL_TYPE_WWAN,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003582 TPACPI_RFK_WWAN_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003583 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003584 tpacpi_wan_rfk_set,
3585 tpacpi_wan_rfk_get);
3586 if (res) {
3587 wan_exit();
3588 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003589 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003590
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003591 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003592}
3593
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003594/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003595static int wan_read(char *p)
3596{
3597 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003598 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003599
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003600 tpacpi_disclose_usertask("procfs wan", "read");
3601
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003602 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003603 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003604 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003605 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003606 (status == RFKILL_STATE_UNBLOCKED) ?
3607 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003608 len += sprintf(p + len, "commands:\tenable, disable\n");
3609 }
3610
3611 return len;
3612}
3613
3614static int wan_write(char *buf)
3615{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003616 char *cmd;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003617 int state = -1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003618
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003619 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003620 return -ENODEV;
3621
3622 while ((cmd = next_cmd(&buf))) {
3623 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003624 state = 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003625 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003626 state = 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003627 } else
3628 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003629 }
3630
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003631 if (state != -1) {
3632 tpacpi_disclose_usertask("procfs wan",
3633 "attempt to %s\n",
3634 state ? "enable" : "disable");
3635 wan_set_radiosw(state, 1);
3636 }
3637
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003638 return 0;
3639}
3640
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003641static struct ibm_struct wan_driver_data = {
3642 .name = "wan",
3643 .read = wan_read,
3644 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003645 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003646 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003647 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003648};
3649
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003650/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003651 * UWB subdriver
3652 */
3653
3654enum {
3655 /* ACPI GUWB/SUWB bits */
3656 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3657 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3658};
3659
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003660#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3661
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003662static struct rfkill *tpacpi_uwb_rfkill;
3663
3664static int uwb_get_radiosw(void)
3665{
3666 int status;
3667
3668 if (!tp_features.uwb)
3669 return -ENODEV;
3670
3671 /* WLSW overrides UWB in firmware/hardware, reflect that */
3672 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3673 return RFKILL_STATE_HARD_BLOCKED;
3674
3675#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3676 if (dbg_uwbemul)
3677 return (tpacpi_uwb_emulstate) ?
3678 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3679#endif
3680
3681 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3682 return -EIO;
3683
3684 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3685 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3686}
3687
3688static void uwb_update_rfk(void)
3689{
3690 int status;
3691
3692 if (!tpacpi_uwb_rfkill)
3693 return;
3694
3695 status = uwb_get_radiosw();
3696 if (status < 0)
3697 return;
3698 rfkill_force_state(tpacpi_uwb_rfkill, status);
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003699
3700 vdbg_printk(TPACPI_DBG_RFKILL,
3701 "forced rfkill state to %d\n",
3702 status);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003703}
3704
3705static int uwb_set_radiosw(int radio_on, int update_rfk)
3706{
3707 int status;
3708
3709 if (!tp_features.uwb)
3710 return -ENODEV;
3711
3712 /* WLSW overrides UWB in firmware/hardware, but there is no
3713 * reason to risk weird behaviour. */
3714 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3715 && radio_on)
3716 return -EPERM;
3717
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003718 vdbg_printk(TPACPI_DBG_RFKILL,
3719 "will %s UWB\n", radio_on ? "enable" : "disable");
3720
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003721#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3722 if (dbg_uwbemul) {
3723 tpacpi_uwb_emulstate = !!radio_on;
3724 if (update_rfk)
3725 uwb_update_rfk();
3726 return 0;
3727 }
3728#endif
3729
3730 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3731 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3732 return -EIO;
3733
3734 if (update_rfk)
3735 uwb_update_rfk();
3736
3737 return 0;
3738}
3739
3740/* --------------------------------------------------------------------- */
3741
3742static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3743{
3744 int uwbs = uwb_get_radiosw();
3745
3746 if (uwbs < 0)
3747 return uwbs;
3748
3749 *state = uwbs;
3750 return 0;
3751}
3752
3753static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3754{
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003755 dbg_printk(TPACPI_DBG_RFKILL,
3756 "request to change radio state to %d\n", state);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003757 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3758}
3759
3760static void uwb_exit(void)
3761{
3762 if (tpacpi_uwb_rfkill)
3763 rfkill_unregister(tpacpi_uwb_rfkill);
3764}
3765
3766static int __init uwb_init(struct ibm_init_struct *iibm)
3767{
3768 int res;
3769 int status = 0;
3770
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003771 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3772 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003773
3774 TPACPI_ACPIHANDLE_INIT(hkey);
3775
3776 tp_features.uwb = hkey_handle &&
3777 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3778
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003779 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3780 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003781 str_supported(tp_features.uwb),
3782 status);
3783
3784#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3785 if (dbg_uwbemul) {
3786 tp_features.uwb = 1;
3787 printk(TPACPI_INFO
3788 "uwb switch emulation enabled\n");
3789 } else
3790#endif
3791 if (tp_features.uwb &&
3792 !(status & TP_ACPI_UWB_HWPRESENT)) {
3793 /* no uwb hardware present in system */
3794 tp_features.uwb = 0;
3795 dbg_printk(TPACPI_DBG_INIT,
3796 "uwb hardware not installed\n");
3797 }
3798
3799 if (!tp_features.uwb)
3800 return 1;
3801
3802 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3803 &tpacpi_uwb_rfkill,
3804 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003805 TPACPI_RFK_UWB_SW_NAME,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003806 false,
3807 tpacpi_uwb_rfk_set,
3808 tpacpi_uwb_rfk_get);
3809
3810 return res;
3811}
3812
3813static struct ibm_struct uwb_driver_data = {
3814 .name = "uwb",
3815 .exit = uwb_exit,
3816 .flags.experimental = 1,
3817};
3818
3819/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003820 * Video subdriver
3821 */
3822
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003823#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3824
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003825enum video_access_mode {
3826 TPACPI_VIDEO_NONE = 0,
3827 TPACPI_VIDEO_570, /* 570 */
3828 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3829 TPACPI_VIDEO_NEW, /* all others */
3830};
3831
3832enum { /* video status flags, based on VIDEO_570 */
3833 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3834 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3835 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3836};
3837
3838enum { /* TPACPI_VIDEO_570 constants */
3839 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3840 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3841 * video_status_flags */
3842 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3843 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3844};
3845
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003846static enum video_access_mode video_supported;
3847static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003848
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003849static int video_autosw_get(void);
3850static int video_autosw_set(int enable);
3851
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003852TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003853
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003854static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003856 int ivga;
3857
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003858 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3859
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003860 TPACPI_ACPIHANDLE_INIT(vid);
3861 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003862
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3864 /* G41, assume IVGA doesn't change */
3865 vid_handle = vid2_handle;
3866
3867 if (!vid_handle)
3868 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003869 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003870 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3871 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003872 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003873 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3874 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003875 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003876 else
3877 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003878 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003880 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3881 str_supported(video_supported != TPACPI_VIDEO_NONE),
3882 video_supported);
3883
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003884 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003887static void video_exit(void)
3888{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003889 dbg_printk(TPACPI_DBG_EXIT,
3890 "restoring original video autoswitch mode\n");
3891 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003892 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003893 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894}
3895
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003896static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 int status = 0;
3899 int i;
3900
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003901 switch (video_supported) {
3902 case TPACPI_VIDEO_570:
3903 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3904 TP_ACPI_VIDEO_570_PHSCMD))
3905 return -EIO;
3906 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3907 break;
3908 case TPACPI_VIDEO_770:
3909 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3910 return -EIO;
3911 if (i)
3912 status |= TP_ACPI_VIDEO_S_LCD;
3913 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3914 return -EIO;
3915 if (i)
3916 status |= TP_ACPI_VIDEO_S_CRT;
3917 break;
3918 case TPACPI_VIDEO_NEW:
3919 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3920 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3921 return -EIO;
3922 if (i)
3923 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003925 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3926 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3927 return -EIO;
3928 if (i)
3929 status |= TP_ACPI_VIDEO_S_LCD;
3930 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3931 return -EIO;
3932 if (i)
3933 status |= TP_ACPI_VIDEO_S_DVI;
3934 break;
3935 default:
3936 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 return status;
3940}
3941
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003942static int video_outputsw_set(int status)
3943{
3944 int autosw;
3945 int res = 0;
3946
3947 switch (video_supported) {
3948 case TPACPI_VIDEO_570:
3949 res = acpi_evalf(NULL, NULL,
3950 "\\_SB.PHS2", "vdd",
3951 TP_ACPI_VIDEO_570_PHS2CMD,
3952 status | TP_ACPI_VIDEO_570_PHS2SET);
3953 break;
3954 case TPACPI_VIDEO_770:
3955 autosw = video_autosw_get();
3956 if (autosw < 0)
3957 return autosw;
3958
3959 res = video_autosw_set(1);
3960 if (res)
3961 return res;
3962 res = acpi_evalf(vid_handle, NULL,
3963 "ASWT", "vdd", status * 0x100, 0);
3964 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003965 printk(TPACPI_ERR
3966 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003967 return -EIO;
3968 }
3969 break;
3970 case TPACPI_VIDEO_NEW:
3971 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003972 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003973 break;
3974 default:
3975 return -ENOSYS;
3976 }
3977
3978 return (res)? 0 : -EIO;
3979}
3980
3981static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003983 int autosw = 0;
3984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003985 switch (video_supported) {
3986 case TPACPI_VIDEO_570:
3987 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3988 return -EIO;
3989 break;
3990 case TPACPI_VIDEO_770:
3991 case TPACPI_VIDEO_NEW:
3992 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3993 return -EIO;
3994 break;
3995 default:
3996 return -ENOSYS;
3997 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003998
3999 return autosw & 1;
4000}
4001
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004002static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004003{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004004 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004006 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004007}
4008
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004009static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004010{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004011 int autosw = video_autosw_get();
4012 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004013
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004014 if (autosw < 0)
4015 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004016
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004017 switch (video_supported) {
4018 case TPACPI_VIDEO_570:
4019 res = video_autosw_set(1);
4020 if (res)
4021 return res;
4022 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4023 break;
4024 case TPACPI_VIDEO_770:
4025 case TPACPI_VIDEO_NEW:
4026 res = video_autosw_set(1);
4027 if (res)
4028 return res;
4029 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4030 break;
4031 default:
4032 return -ENOSYS;
4033 }
4034 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004035 printk(TPACPI_ERR
4036 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004037 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004038 }
4039
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004040 return (res)? 0 : -EIO;
4041}
4042
4043static int video_expand_toggle(void)
4044{
4045 switch (video_supported) {
4046 case TPACPI_VIDEO_570:
4047 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4048 0 : -EIO;
4049 case TPACPI_VIDEO_770:
4050 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4051 0 : -EIO;
4052 case TPACPI_VIDEO_NEW:
4053 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4054 0 : -EIO;
4055 default:
4056 return -ENOSYS;
4057 }
4058 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004059}
4060
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004061static int video_read(char *p)
4062{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004063 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004064 int len = 0;
4065
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004066 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004067 len += sprintf(p + len, "status:\t\tnot supported\n");
4068 return len;
4069 }
4070
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004071 status = video_outputsw_get();
4072 if (status < 0)
4073 return status;
4074
4075 autosw = video_autosw_get();
4076 if (autosw < 0)
4077 return autosw;
4078
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004079 len += sprintf(p + len, "status:\t\tsupported\n");
4080 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4081 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004082 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004083 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4084 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4085 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4086 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004087 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004088 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4089 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4090 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4091
4092 return len;
4093}
4094
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004095static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 char *cmd;
4098 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004099 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004101 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004102 return -ENODEV;
4103
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004104 enable = 0;
4105 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 while ((cmd = next_cmd(&buf))) {
4108 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004109 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004111 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004113 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004115 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004116 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004117 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004118 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004119 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004120 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004121 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004123 res = video_autosw_set(1);
4124 if (res)
4125 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004127 res = video_autosw_set(0);
4128 if (res)
4129 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004131 res = video_outputsw_cycle();
4132 if (res)
4133 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004135 res = video_expand_toggle();
4136 if (res)
4137 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 } else
4139 return -EINVAL;
4140 }
4141
4142 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004143 status = video_outputsw_get();
4144 if (status < 0)
4145 return status;
4146 res = video_outputsw_set((status & ~disable) | enable);
4147 if (res)
4148 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
4150
4151 return 0;
4152}
4153
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004154static struct ibm_struct video_driver_data = {
4155 .name = "video",
4156 .read = video_read,
4157 .write = video_write,
4158 .exit = video_exit,
4159};
4160
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004161#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4162
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004163/*************************************************************************
4164 * Light (thinklight) subdriver
4165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004167TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4168TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004169
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004170static int light_get_status(void)
4171{
4172 int status = 0;
4173
4174 if (tp_features.light_status) {
4175 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4176 return -EIO;
4177 return (!!status);
4178 }
4179
4180 return -ENXIO;
4181}
4182
4183static int light_set_status(int status)
4184{
4185 int rc;
4186
4187 if (tp_features.light) {
4188 if (cmos_handle) {
4189 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4190 (status)?
4191 TP_CMOS_THINKLIGHT_ON :
4192 TP_CMOS_THINKLIGHT_OFF);
4193 } else {
4194 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4195 (status)? 1 : 0);
4196 }
4197 return (rc)? 0 : -EIO;
4198 }
4199
4200 return -ENXIO;
4201}
4202
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004203static void light_set_status_worker(struct work_struct *work)
4204{
4205 struct tpacpi_led_classdev *data =
4206 container_of(work, struct tpacpi_led_classdev, work);
4207
4208 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4209 light_set_status((data->new_brightness != LED_OFF));
4210}
4211
4212static void light_sysfs_set(struct led_classdev *led_cdev,
4213 enum led_brightness brightness)
4214{
4215 struct tpacpi_led_classdev *data =
4216 container_of(led_cdev,
4217 struct tpacpi_led_classdev,
4218 led_classdev);
4219 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004220 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004221}
4222
4223static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4224{
4225 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4226}
4227
4228static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4229 .led_classdev = {
4230 .name = "tpacpi::thinklight",
4231 .brightness_set = &light_sysfs_set,
4232 .brightness_get = &light_sysfs_get,
4233 }
4234};
4235
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004236static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004238 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004239
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004240 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4241
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004242 TPACPI_ACPIHANDLE_INIT(ledb);
4243 TPACPI_ACPIHANDLE_INIT(lght);
4244 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004245 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004246
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004247 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004248 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004249
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004250 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004251 /* light status not supported on
4252 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004253 tp_features.light_status =
4254 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004256 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4257 str_supported(tp_features.light),
4258 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004259
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004260 if (!tp_features.light)
4261 return 1;
4262
4263 rc = led_classdev_register(&tpacpi_pdev->dev,
4264 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004265
4266 if (rc < 0) {
4267 tp_features.light = 0;
4268 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004269 } else {
4270 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004271 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004272
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004273 return rc;
4274}
4275
4276static void light_exit(void)
4277{
4278 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4279 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004280 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004283static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004286 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004288 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004289 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004290 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004291 len += sprintf(p + len, "status:\t\tunknown\n");
4292 len += sprintf(p + len, "commands:\ton, off\n");
4293 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004294 status = light_get_status();
4295 if (status < 0)
4296 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004298 len += sprintf(p + len, "commands:\ton, off\n");
4299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 return len;
4302}
4303
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004304static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004307 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004308
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004309 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004310 return -ENODEV;
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 while ((cmd = next_cmd(&buf))) {
4313 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004314 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004316 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 } else
4318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
4320
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004321 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004324static struct ibm_struct light_driver_data = {
4325 .name = "light",
4326 .read = light_read,
4327 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004328 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004329};
4330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004331/*************************************************************************
4332 * Dock subdriver
4333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004335#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004336
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004337static void dock_notify(struct ibm_struct *ibm, u32 event);
4338static int dock_read(char *p);
4339static int dock_write(char *buf);
4340
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004341TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004342 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4343 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4344 "\\_SB.PCI.ISA.SLCE", /* 570 */
4345 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4346
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004347/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004348TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004349
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004350static const struct acpi_device_id ibm_pci_device_ids[] = {
4351 {PCI_ROOT_HID_STRING, 0},
4352 {"", 0},
4353};
4354
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004355static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4356 {
4357 .notify = dock_notify,
4358 .handle = &dock_handle,
4359 .type = ACPI_SYSTEM_NOTIFY,
4360 },
4361 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004362 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4363 * We just use it to get notifications of dock hotplug
4364 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004365 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004366 .notify = dock_notify,
4367 .handle = &pci_handle,
4368 .type = ACPI_SYSTEM_NOTIFY,
4369 },
4370};
4371
4372static struct ibm_struct dock_driver_data[2] = {
4373 {
4374 .name = "dock",
4375 .read = dock_read,
4376 .write = dock_write,
4377 .acpi = &ibm_dock_acpidriver[0],
4378 },
4379 {
4380 .name = "dock",
4381 .acpi = &ibm_dock_acpidriver[1],
4382 },
4383};
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385#define dock_docked() (_sta(dock_handle) & 1)
4386
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004387static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004388{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004389 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4390
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004391 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004392
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004393 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4394 str_supported(dock_handle != NULL));
4395
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004396 return (dock_handle)? 0 : 1;
4397}
4398
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004399static int __init dock_init2(struct ibm_init_struct *iibm)
4400{
4401 int dock2_needed;
4402
4403 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4404
4405 if (dock_driver_data[0].flags.acpi_driver_registered &&
4406 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004407 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004408 dock2_needed = (pci_handle != NULL);
4409 vdbg_printk(TPACPI_DBG_INIT,
4410 "dock PCI handler for the TP 570 is %s\n",
4411 str_supported(dock2_needed));
4412 } else {
4413 vdbg_printk(TPACPI_DBG_INIT,
4414 "dock subdriver part 2 not required\n");
4415 dock2_needed = 0;
4416 }
4417
4418 return (dock2_needed)? 0 : 1;
4419}
4420
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004421static void dock_notify(struct ibm_struct *ibm, u32 event)
4422{
4423 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004424 int pci = ibm->acpi->hid && ibm->acpi->device &&
4425 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004426 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004427
4428 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004429 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004430 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004431 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004432 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004433 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004434 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004435 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004437 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004438 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004439 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004440 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004441 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004442 }
Len Brown14e04fb32007-08-23 15:20:26 -04004443 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004444 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004445 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004446 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447}
4448
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004449static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 int len = 0;
4452 int docked = dock_docked();
4453
4454 if (!dock_handle)
4455 len += sprintf(p + len, "status:\t\tnot supported\n");
4456 else if (!docked)
4457 len += sprintf(p + len, "status:\t\tundocked\n");
4458 else {
4459 len += sprintf(p + len, "status:\t\tdocked\n");
4460 len += sprintf(p + len, "commands:\tdock, undock\n");
4461 }
4462
4463 return len;
4464}
4465
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004466static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
4468 char *cmd;
4469
4470 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004471 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
4473 while ((cmd = next_cmd(&buf))) {
4474 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004475 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4476 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 return -EIO;
4478 } else if (strlencmp(cmd, "dock") == 0) {
4479 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4480 return -EIO;
4481 } else
4482 return -EINVAL;
4483 }
4484
4485 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004488#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004490/*************************************************************************
4491 * Bay subdriver
4492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004494#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004495
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004496TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4498 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4499 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4500 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004501TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004502 "_EJ0", /* all others */
4503 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004504TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004505 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4506 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004507TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004508 "_EJ0", /* 770x */
4509 ); /* all others */
4510
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004511static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004513 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4514
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004515 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004516 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004517 TPACPI_ACPIHANDLE_INIT(bay_ej);
4518 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004519 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004520 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004521
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004522 tp_features.bay_status = bay_handle &&
4523 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4524 tp_features.bay_status2 = bay2_handle &&
4525 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004526
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004527 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4528 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4529 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4530 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004532 vdbg_printk(TPACPI_DBG_INIT,
4533 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004534 str_supported(tp_features.bay_status),
4535 str_supported(tp_features.bay_eject),
4536 str_supported(tp_features.bay_status2),
4537 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004538
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004539 return (tp_features.bay_status || tp_features.bay_eject ||
4540 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004543static void bay_notify(struct ibm_struct *ibm, u32 event)
4544{
Len Brown14e04fb32007-08-23 15:20:26 -04004545 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004546 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004547 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004548 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004549}
4550
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004551#define bay_occupied(b) (_sta(b##_handle) & 1)
4552
4553static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004556 int occupied = bay_occupied(bay);
4557 int occupied2 = bay_occupied(bay2);
4558 int eject, eject2;
4559
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004560 len += sprintf(p + len, "status:\t\t%s\n",
4561 tp_features.bay_status ?
4562 (occupied ? "occupied" : "unoccupied") :
4563 "not supported");
4564 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4566 "occupied" : "unoccupied");
4567
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004568 eject = tp_features.bay_eject && occupied;
4569 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570
4571 if (eject && eject2)
4572 len += sprintf(p + len, "commands:\teject, eject2\n");
4573 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575 else if (eject2)
4576 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
4578 return len;
4579}
4580
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004581static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
4583 char *cmd;
4584
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004585 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004586 return -ENODEV;
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004589 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004590 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4591 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004592 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004593 strlencmp(cmd, "eject2") == 0) {
4594 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return -EIO;
4596 } else
4597 return -EINVAL;
4598 }
4599
4600 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004601}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004602
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004603static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4604 .notify = bay_notify,
4605 .handle = &bay_handle,
4606 .type = ACPI_SYSTEM_NOTIFY,
4607};
4608
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004609static struct ibm_struct bay_driver_data = {
4610 .name = "bay",
4611 .read = bay_read,
4612 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004613 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004614};
4615
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004616#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004618/*************************************************************************
4619 * CMOS subdriver
4620 */
4621
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004622/* sysfs cmos_command -------------------------------------------------- */
4623static ssize_t cmos_command_store(struct device *dev,
4624 struct device_attribute *attr,
4625 const char *buf, size_t count)
4626{
4627 unsigned long cmos_cmd;
4628 int res;
4629
4630 if (parse_strtoul(buf, 21, &cmos_cmd))
4631 return -EINVAL;
4632
4633 res = issue_thinkpad_cmos_command(cmos_cmd);
4634 return (res)? res : count;
4635}
4636
4637static struct device_attribute dev_attr_cmos_command =
4638 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4639
4640/* --------------------------------------------------------------------- */
4641
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004642static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004643{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004644 int res;
4645
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004646 vdbg_printk(TPACPI_DBG_INIT,
4647 "initializing cmos commands subdriver\n");
4648
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004649 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004650
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004651 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4652 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004653
4654 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4655 if (res)
4656 return res;
4657
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004658 return (cmos_handle)? 0 : 1;
4659}
4660
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004661static void cmos_exit(void)
4662{
4663 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4664}
4665
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004666static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
4668 int len = 0;
4669
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004670 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4671 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 if (!cmos_handle)
4673 len += sprintf(p + len, "status:\t\tnot supported\n");
4674 else {
4675 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004676 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 }
4678
4679 return len;
4680}
4681
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004685 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004688 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4689 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 /* cmos_cmd set */
4691 } else
4692 return -EINVAL;
4693
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004694 res = issue_thinkpad_cmos_command(cmos_cmd);
4695 if (res)
4696 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 }
4698
4699 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004700}
4701
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004702static struct ibm_struct cmos_driver_data = {
4703 .name = "cmos",
4704 .read = cmos_read,
4705 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004706 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004707};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004708
4709/*************************************************************************
4710 * LED subdriver
4711 */
4712
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004713enum led_access_mode {
4714 TPACPI_LED_NONE = 0,
4715 TPACPI_LED_570, /* 570 */
4716 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4717 TPACPI_LED_NEW, /* all others */
4718};
4719
4720enum { /* For TPACPI_LED_OLD */
4721 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4722 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4723 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4724};
4725
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004726enum led_status_t {
4727 TPACPI_LED_OFF = 0,
4728 TPACPI_LED_ON,
4729 TPACPI_LED_BLINK,
4730};
4731
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004732static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004733
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004734TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004735 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4736 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004737 "LED", /* all others */
4738 ); /* R30, R31 */
4739
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004740#define TPACPI_LED_NUMLEDS 8
4741static struct tpacpi_led_classdev *tpacpi_leds;
4742static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004743static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004744 /* there's a limit of 19 chars + NULL before 2.6.26 */
4745 "tpacpi::power",
4746 "tpacpi:orange:batt",
4747 "tpacpi:green:batt",
4748 "tpacpi::dock_active",
4749 "tpacpi::bay_active",
4750 "tpacpi::dock_batt",
4751 "tpacpi::unknown_led",
4752 "tpacpi::standby",
4753};
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004754#define TPACPI_SAFE_LEDS 0x0081U
4755
4756static inline bool tpacpi_is_led_restricted(const unsigned int led)
4757{
4758#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4759 return false;
4760#else
4761 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4762#endif
4763}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004764
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004765static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004766{
4767 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004768 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004769
4770 switch (led_supported) {
4771 case TPACPI_LED_570:
4772 if (!acpi_evalf(ec_handle,
4773 &status, "GLED", "dd", 1 << led))
4774 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004775 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004776 TPACPI_LED_OFF :
4777 ((status == 1)?
4778 TPACPI_LED_ON :
4779 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004780 tpacpi_led_state_cache[led] = led_s;
4781 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004782 default:
4783 return -ENXIO;
4784 }
4785
4786 /* not reached */
4787}
4788
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004789static int led_set_status(const unsigned int led,
4790 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004791{
4792 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004793 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4794 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004795
4796 int rc = 0;
4797
4798 switch (led_supported) {
4799 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004800 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004801 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004802 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004803 if (unlikely(tpacpi_is_led_restricted(led)))
4804 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004805 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4806 (1 << led), led_sled_arg1[ledstatus]))
4807 rc = -EIO;
4808 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004809 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004810 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004811 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004812 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004813 if (unlikely(tpacpi_is_led_restricted(led)))
4814 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004815 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4816 if (rc >= 0)
4817 rc = ec_write(TPACPI_LED_EC_HLBL,
4818 (ledstatus == TPACPI_LED_BLINK) << led);
4819 if (rc >= 0)
4820 rc = ec_write(TPACPI_LED_EC_HLCL,
4821 (ledstatus != TPACPI_LED_OFF) << led);
4822 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004823 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004824 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004825 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4826 return -EINVAL;
4827 if (unlikely(tpacpi_is_led_restricted(led)))
4828 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004829 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4830 led, led_led_arg1[ledstatus]))
4831 rc = -EIO;
4832 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004833 default:
4834 rc = -ENXIO;
4835 }
4836
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004837 if (!rc)
4838 tpacpi_led_state_cache[led] = ledstatus;
4839
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 return rc;
4841}
4842
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004843static void led_sysfs_set_status(unsigned int led,
4844 enum led_brightness brightness)
4845{
4846 led_set_status(led,
4847 (brightness == LED_OFF) ?
4848 TPACPI_LED_OFF :
4849 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4850 TPACPI_LED_BLINK : TPACPI_LED_ON);
4851}
4852
4853static void led_set_status_worker(struct work_struct *work)
4854{
4855 struct tpacpi_led_classdev *data =
4856 container_of(work, struct tpacpi_led_classdev, work);
4857
4858 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4859 led_sysfs_set_status(data->led, data->new_brightness);
4860}
4861
4862static void led_sysfs_set(struct led_classdev *led_cdev,
4863 enum led_brightness brightness)
4864{
4865 struct tpacpi_led_classdev *data = container_of(led_cdev,
4866 struct tpacpi_led_classdev, led_classdev);
4867
4868 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004869 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004870}
4871
4872static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4873 unsigned long *delay_on, unsigned long *delay_off)
4874{
4875 struct tpacpi_led_classdev *data = container_of(led_cdev,
4876 struct tpacpi_led_classdev, led_classdev);
4877
4878 /* Can we choose the flash rate? */
4879 if (*delay_on == 0 && *delay_off == 0) {
4880 /* yes. set them to the hardware blink rate (1 Hz) */
4881 *delay_on = 500; /* ms */
4882 *delay_off = 500; /* ms */
4883 } else if ((*delay_on != 500) || (*delay_off != 500))
4884 return -EINVAL;
4885
4886 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004887 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004888
4889 return 0;
4890}
4891
4892static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4893{
4894 int rc;
4895
4896 struct tpacpi_led_classdev *data = container_of(led_cdev,
4897 struct tpacpi_led_classdev, led_classdev);
4898
4899 rc = led_get_status(data->led);
4900
4901 if (rc == TPACPI_LED_OFF || rc < 0)
4902 rc = LED_OFF; /* no error handling in led class :( */
4903 else
4904 rc = LED_FULL;
4905
4906 return rc;
4907}
4908
4909static void led_exit(void)
4910{
4911 unsigned int i;
4912
4913 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4914 if (tpacpi_leds[i].led_classdev.name)
4915 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4916 }
4917
4918 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004919}
4920
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004921static int __init tpacpi_init_led(unsigned int led)
4922{
4923 int rc;
4924
4925 tpacpi_leds[led].led = led;
4926
4927 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4928 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4929 if (led_supported == TPACPI_LED_570)
4930 tpacpi_leds[led].led_classdev.brightness_get =
4931 &led_sysfs_get;
4932
4933 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4934
4935 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4936
4937 rc = led_classdev_register(&tpacpi_pdev->dev,
4938 &tpacpi_leds[led].led_classdev);
4939 if (rc < 0)
4940 tpacpi_leds[led].led_classdev.name = NULL;
4941
4942 return rc;
4943}
4944
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004945static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004946{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004947 unsigned int i;
4948 int rc;
4949
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004950 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4951
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004952 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004953
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004954 if (!led_handle)
4955 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004956 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004957 else if (strlencmp(led_path, "SLED") == 0)
4958 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004959 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004960 else if (strlencmp(led_path, "SYSL") == 0)
4961 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004962 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963 else
4964 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004965 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004967 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4968 str_supported(led_supported), led_supported);
4969
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004970 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4971 GFP_KERNEL);
4972 if (!tpacpi_leds) {
4973 printk(TPACPI_ERR "Out of memory for LED data\n");
4974 return -ENOMEM;
4975 }
4976
4977 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004978 if (!tpacpi_is_led_restricted(i)) {
4979 rc = tpacpi_init_led(i);
4980 if (rc < 0) {
4981 led_exit();
4982 return rc;
4983 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004984 }
4985 }
4986
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00004987#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4988 if (led_supported != TPACPI_LED_NONE)
4989 printk(TPACPI_NOTICE
4990 "warning: userspace override of important "
4991 "firmware LEDs is enabled\n");
4992#endif
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004993 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004994}
4995
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004996#define str_led_status(s) \
4997 ((s) == TPACPI_LED_OFF ? "off" : \
4998 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004999
5000static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
5002 int len = 0;
5003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005004 if (!led_supported) {
5005 len += sprintf(p + len, "status:\t\tnot supported\n");
5006 return len;
5007 }
5008 len += sprintf(p + len, "status:\t\tsupported\n");
5009
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005010 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005011 /* 570 */
5012 int i, status;
5013 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005014 status = led_get_status(i);
5015 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005016 return -EIO;
5017 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005018 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005019 }
5020 }
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005023 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 return len;
5026}
5027
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005028static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029{
5030 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005031 int led, rc;
5032 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005033
5034 if (!led_supported)
5035 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
5037 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005038 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 return -EINVAL;
5040
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005041 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005042 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005044 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005045 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005047 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005048 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005049 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005050
5051 rc = led_set_status(led, s);
5052 if (rc < 0)
5053 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
5055
5056 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005057}
5058
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005059static struct ibm_struct led_driver_data = {
5060 .name = "led",
5061 .read = led_read,
5062 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005063 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005064};
5065
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005066/*************************************************************************
5067 * Beep subdriver
5068 */
5069
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005070TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005071
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005072static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005073{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005074 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5075
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005076 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005077
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005078 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5079 str_supported(beep_handle != NULL));
5080
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005081 return (beep_handle)? 0 : 1;
5082}
5083
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005084static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
5086 int len = 0;
5087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005088 if (!beep_handle)
5089 len += sprintf(p + len, "status:\t\tnot supported\n");
5090 else {
5091 len += sprintf(p + len, "status:\t\tsupported\n");
5092 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
5095 return len;
5096}
5097
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005098static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099{
5100 char *cmd;
5101 int beep_cmd;
5102
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005103 if (!beep_handle)
5104 return -ENODEV;
5105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005107 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5108 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 /* beep_cmd set */
5110 } else
5111 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005112 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return -EIO;
5114 }
5115
5116 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005117}
5118
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005119static struct ibm_struct beep_driver_data = {
5120 .name = "beep",
5121 .read = beep_read,
5122 .write = beep_write,
5123};
5124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005125/*************************************************************************
5126 * Thermal subdriver
5127 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005128
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005129enum thermal_access_mode {
5130 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5131 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5132 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5133 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5134 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5135};
5136
5137enum { /* TPACPI_THERMAL_TPEC_* */
5138 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5139 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5140 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5141};
5142
5143#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5144struct ibm_thermal_sensors_struct {
5145 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5146};
5147
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005148static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005149
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005150/* idx is zero-based */
5151static int thermal_get_sensor(int idx, s32 *value)
5152{
5153 int t;
5154 s8 tmp;
5155 char tmpi[5];
5156
5157 t = TP_EC_THERMAL_TMP0;
5158
5159 switch (thermal_read_mode) {
5160#if TPACPI_MAX_THERMAL_SENSORS >= 16
5161 case TPACPI_THERMAL_TPEC_16:
5162 if (idx >= 8 && idx <= 15) {
5163 t = TP_EC_THERMAL_TMP8;
5164 idx -= 8;
5165 }
5166 /* fallthrough */
5167#endif
5168 case TPACPI_THERMAL_TPEC_8:
5169 if (idx <= 7) {
5170 if (!acpi_ec_read(t + idx, &tmp))
5171 return -EIO;
5172 *value = tmp * 1000;
5173 return 0;
5174 }
5175 break;
5176
5177 case TPACPI_THERMAL_ACPI_UPDT:
5178 if (idx <= 7) {
5179 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5180 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5181 return -EIO;
5182 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5183 return -EIO;
5184 *value = (t - 2732) * 100;
5185 return 0;
5186 }
5187 break;
5188
5189 case TPACPI_THERMAL_ACPI_TMP07:
5190 if (idx <= 7) {
5191 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5192 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5193 return -EIO;
5194 if (t > 127 || t < -127)
5195 t = TP_EC_THERMAL_TMP_NA;
5196 *value = t * 1000;
5197 return 0;
5198 }
5199 break;
5200
5201 case TPACPI_THERMAL_NONE:
5202 default:
5203 return -ENOSYS;
5204 }
5205
5206 return -EINVAL;
5207}
5208
5209static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5210{
5211 int res, i;
5212 int n;
5213
5214 n = 8;
5215 i = 0;
5216
5217 if (!s)
5218 return -EINVAL;
5219
5220 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5221 n = 16;
5222
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005223 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005224 res = thermal_get_sensor(i, &s->temp[i]);
5225 if (res)
5226 return res;
5227 }
5228
5229 return n;
5230}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005231
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005232/* sysfs temp##_input -------------------------------------------------- */
5233
5234static ssize_t thermal_temp_input_show(struct device *dev,
5235 struct device_attribute *attr,
5236 char *buf)
5237{
5238 struct sensor_device_attribute *sensor_attr =
5239 to_sensor_dev_attr(attr);
5240 int idx = sensor_attr->index;
5241 s32 value;
5242 int res;
5243
5244 res = thermal_get_sensor(idx, &value);
5245 if (res)
5246 return res;
5247 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5248 return -ENXIO;
5249
5250 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5251}
5252
5253#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005254 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5255 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005256
5257static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5258 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5259 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5260 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5261 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5262 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5263 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5264 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5265 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5266 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5267 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5268 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5269 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5270 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5271 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5272 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5273 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5274};
5275
5276#define THERMAL_ATTRS(X) \
5277 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5278
5279static struct attribute *thermal_temp_input_attr[] = {
5280 THERMAL_ATTRS(8),
5281 THERMAL_ATTRS(9),
5282 THERMAL_ATTRS(10),
5283 THERMAL_ATTRS(11),
5284 THERMAL_ATTRS(12),
5285 THERMAL_ATTRS(13),
5286 THERMAL_ATTRS(14),
5287 THERMAL_ATTRS(15),
5288 THERMAL_ATTRS(0),
5289 THERMAL_ATTRS(1),
5290 THERMAL_ATTRS(2),
5291 THERMAL_ATTRS(3),
5292 THERMAL_ATTRS(4),
5293 THERMAL_ATTRS(5),
5294 THERMAL_ATTRS(6),
5295 THERMAL_ATTRS(7),
5296 NULL
5297};
5298
5299static const struct attribute_group thermal_temp_input16_group = {
5300 .attrs = thermal_temp_input_attr
5301};
5302
5303static const struct attribute_group thermal_temp_input8_group = {
5304 .attrs = &thermal_temp_input_attr[8]
5305};
5306
5307#undef THERMAL_SENSOR_ATTR_TEMP
5308#undef THERMAL_ATTRS
5309
5310/* --------------------------------------------------------------------- */
5311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005312static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005313{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005314 u8 t, ta1, ta2;
5315 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005316 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005317 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005318
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005319 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5320
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005321 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005322
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005323 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005324 /*
5325 * Direct EC access mode: sensors at registers
5326 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5327 * non-implemented, thermal sensors return 0x80 when
5328 * not available
5329 */
5330
5331 ta1 = ta2 = 0;
5332 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005333 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005334 ta1 |= t;
5335 } else {
5336 ta1 = 0;
5337 break;
5338 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005339 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005340 ta2 |= t;
5341 } else {
5342 ta1 = 0;
5343 break;
5344 }
5345 }
5346 if (ta1 == 0) {
5347 /* This is sheer paranoia, but we handle it anyway */
5348 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005349 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005350 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005351 "falling back to ACPI TMPx access "
5352 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005353 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005354 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005355 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005356 "ThinkPad ACPI EC access misbehaving, "
5357 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005358 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005359 }
5360 } else {
5361 thermal_read_mode =
5362 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005363 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005364 }
5365 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005366 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5367 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005368 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005369 } else {
5370 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005371 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005372 }
5373 } else {
5374 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005375 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005376 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005377
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005378 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5379 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5380 thermal_read_mode);
5381
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005382 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005383 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005384 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005385 &thermal_temp_input16_group);
5386 if (res)
5387 return res;
5388 break;
5389 case TPACPI_THERMAL_TPEC_8:
5390 case TPACPI_THERMAL_ACPI_TMP07:
5391 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005392 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005393 &thermal_temp_input8_group);
5394 if (res)
5395 return res;
5396 break;
5397 case TPACPI_THERMAL_NONE:
5398 default:
5399 return 1;
5400 }
5401
5402 return 0;
5403}
5404
5405static void thermal_exit(void)
5406{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005407 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005408 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005409 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005410 &thermal_temp_input16_group);
5411 break;
5412 case TPACPI_THERMAL_TPEC_8:
5413 case TPACPI_THERMAL_ACPI_TMP07:
5414 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005415 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005416 &thermal_temp_input16_group);
5417 break;
5418 case TPACPI_THERMAL_NONE:
5419 default:
5420 break;
5421 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005422}
5423
5424static int thermal_read(char *p)
5425{
5426 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005427 int n, i;
5428 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005429
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005430 n = thermal_get_sensors(&t);
5431 if (unlikely(n < 0))
5432 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005433
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005434 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005435
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005436 if (n > 0) {
5437 for (i = 0; i < (n - 1); i++)
5438 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5439 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5440 } else
5441 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005442
5443 return len;
5444}
5445
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005446static struct ibm_struct thermal_driver_data = {
5447 .name = "thermal",
5448 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005449 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005450};
5451
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005452/*************************************************************************
5453 * EC Dump subdriver
5454 */
5455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005456static u8 ecdump_regs[256];
5457
5458static int ecdump_read(char *p)
5459{
5460 int len = 0;
5461 int i, j;
5462 u8 v;
5463
5464 len += sprintf(p + len, "EC "
5465 " +00 +01 +02 +03 +04 +05 +06 +07"
5466 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5467 for (i = 0; i < 256; i += 16) {
5468 len += sprintf(p + len, "EC 0x%02x:", i);
5469 for (j = 0; j < 16; j++) {
5470 if (!acpi_ec_read(i + j, &v))
5471 break;
5472 if (v != ecdump_regs[i + j])
5473 len += sprintf(p + len, " *%02x", v);
5474 else
5475 len += sprintf(p + len, " %02x", v);
5476 ecdump_regs[i + j] = v;
5477 }
5478 len += sprintf(p + len, "\n");
5479 if (j != 16)
5480 break;
5481 }
5482
5483 /* These are way too dangerous to advertise openly... */
5484#if 0
5485 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5486 " (<offset> is 00-ff, <value> is 00-ff)\n");
5487 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5488 " (<offset> is 00-ff, <value> is 0-255)\n");
5489#endif
5490 return len;
5491}
5492
5493static int ecdump_write(char *buf)
5494{
5495 char *cmd;
5496 int i, v;
5497
5498 while ((cmd = next_cmd(&buf))) {
5499 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5500 /* i and v set */
5501 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5502 /* i and v set */
5503 } else
5504 return -EINVAL;
5505 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5506 if (!acpi_ec_write(i, v))
5507 return -EIO;
5508 } else
5509 return -EINVAL;
5510 }
5511
5512 return 0;
5513}
5514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005515static struct ibm_struct ecdump_driver_data = {
5516 .name = "ecdump",
5517 .read = ecdump_read,
5518 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005519 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005520};
5521
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005522/*************************************************************************
5523 * Backlight/brightness subdriver
5524 */
Holger Macht8acb0252006-10-20 14:30:28 -07005525
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005526#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5527
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005528enum {
5529 TP_EC_BACKLIGHT = 0x31,
5530
5531 /* TP_EC_BACKLIGHT bitmasks */
5532 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5533 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5534 TP_EC_BACKLIGHT_MAPSW = 0x20,
5535};
5536
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005537static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005538static int brightness_mode;
5539static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5540
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005541static struct mutex brightness_mutex;
5542
5543/*
5544 * ThinkPads can read brightness from two places: EC 0x31, or
5545 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005546 *
5547 * EC 0x31 has the following layout
5548 * Bit 7: unknown function
5549 * Bit 6: unknown function
5550 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5551 * Bit 4: must be set to zero to avoid problems
5552 * Bit 3-0: backlight brightness level
5553 *
5554 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005555 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005556static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005557{
5558 u8 lec = 0, lcmos = 0, level = 0;
5559
5560 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005561 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005562 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005563 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005564 };
5565 if (brightness_mode & 2) {
5566 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5567 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5568 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5569 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5570 level = lcmos;
5571 }
5572
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005573 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005574 *status = lec; /* Prefer EC, CMOS is just a backing store */
5575 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005576 if (lec == lcmos)
5577 tp_warned.bright_cmos_ec_unsync = 0;
5578 else {
5579 if (!tp_warned.bright_cmos_ec_unsync) {
5580 printk(TPACPI_ERR
5581 "CMOS NVRAM (%u) and EC (%u) do not "
5582 "agree on display brightness level\n",
5583 (unsigned int) lcmos,
5584 (unsigned int) lec);
5585 tp_warned.bright_cmos_ec_unsync = 1;
5586 }
5587 return -EIO;
5588 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005589 } else {
5590 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005591 }
5592
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005593 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005594}
5595
5596/* May return EINTR which can always be mapped to ERESTARTSYS */
5597static int brightness_set(int value)
5598{
5599 int cmos_cmd, inc, i, res;
5600 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005601 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005602
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005603 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5604 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005605 return -EINVAL;
5606
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005607 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005608 if (res < 0)
5609 return res;
5610
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005611 res = brightness_get_raw(&current_value);
5612 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005613 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005614
5615 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5616 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005617
5618 cmos_cmd = value > current_value ?
5619 TP_CMOS_BRIGHTNESS_UP :
5620 TP_CMOS_BRIGHTNESS_DOWN;
5621 inc = (value > current_value)? 1 : -1;
5622
5623 res = 0;
5624 for (i = current_value; i != value; i += inc) {
5625 if ((brightness_mode & 2) &&
5626 issue_thinkpad_cmos_command(cmos_cmd)) {
5627 res = -EIO;
5628 goto errout;
5629 }
5630 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005631 !acpi_ec_write(TP_EC_BACKLIGHT,
5632 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005633 res = -EIO;
5634 goto errout;;
5635 }
5636 }
5637
5638errout:
5639 mutex_unlock(&brightness_mutex);
5640 return res;
5641}
5642
5643/* sysfs backlight class ----------------------------------------------- */
5644
5645static int brightness_update_status(struct backlight_device *bd)
5646{
5647 /* it is the backlight class's job (caller) to handle
5648 * EINTR and other errors properly */
5649 return brightness_set(
5650 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5651 bd->props.power == FB_BLANK_UNBLANK) ?
5652 bd->props.brightness : 0);
5653}
Holger Macht8acb0252006-10-20 14:30:28 -07005654
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005655static int brightness_get(struct backlight_device *bd)
5656{
5657 int status, res;
5658
5659 res = brightness_get_raw(&status);
5660 if (res < 0)
5661 return 0; /* FIXME: teach backlight about error handling */
5662
5663 return status & TP_EC_BACKLIGHT_LVLMSK;
5664}
5665
Richard Purdie599a52d2007-02-10 23:07:48 +00005666static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005667 .get_brightness = brightness_get,
5668 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005669};
5670
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005671/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005672
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005673static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005674{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005675 int b;
5676
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005677 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5678
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005679 mutex_init(&brightness_mutex);
5680
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005681 /*
5682 * We always attempt to detect acpi support, so as to switch
5683 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5684 * going to publish a backlight interface
5685 */
5686 b = tpacpi_check_std_acpi_brightness_support();
5687 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005688
5689 if (acpi_video_backlight_support()) {
5690 if (brightness_enable > 1) {
5691 printk(TPACPI_NOTICE
5692 "Standard ACPI backlight interface "
5693 "available, not loading native one.\n");
5694 return 1;
5695 } else if (brightness_enable == 1) {
5696 printk(TPACPI_NOTICE
5697 "Backlight control force enabled, even if standard "
5698 "ACPI backlight interface is available\n");
5699 }
5700 } else {
5701 if (brightness_enable > 1) {
5702 printk(TPACPI_NOTICE
5703 "Standard ACPI backlight interface not "
5704 "available, thinkpad_acpi native "
5705 "brightness control enabled\n");
5706 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005707 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005708 }
5709
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005710 if (!brightness_enable) {
5711 dbg_printk(TPACPI_DBG_INIT,
5712 "brightness support disabled by "
5713 "module parameter\n");
5714 return 1;
5715 }
5716
5717 if (b > 16) {
5718 printk(TPACPI_ERR
5719 "Unsupported brightness interface, "
5720 "please contact %s\n", TPACPI_MAIL);
5721 return 1;
5722 }
5723 if (b == 16)
5724 tp_features.bright_16levels = 1;
5725
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005726 if (!brightness_mode) {
5727 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5728 brightness_mode = 2;
5729 else
5730 brightness_mode = 3;
5731
5732 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5733 brightness_mode);
5734 }
5735
5736 if (brightness_mode > 3)
5737 return -EINVAL;
5738
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005739 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005740 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005741
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005742 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005743 printk(TPACPI_INFO
5744 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005745
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005746 ibm_backlight_device = backlight_device_register(
5747 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5748 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005749 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005750 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005751 return PTR_ERR(ibm_backlight_device);
5752 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005753 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005754
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005755 ibm_backlight_device->props.max_brightness =
5756 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005757 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005758 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005759
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005760 return 0;
5761}
5762
5763static void brightness_exit(void)
5764{
5765 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005766 vdbg_printk(TPACPI_DBG_EXIT,
5767 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005768 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005769 }
5770}
5771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005772static int brightness_read(char *p)
5773{
5774 int len = 0;
5775 int level;
5776
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005777 level = brightness_get(NULL);
5778 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005779 len += sprintf(p + len, "level:\t\tunreadable\n");
5780 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005781 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005782 len += sprintf(p + len, "commands:\tup, down\n");
5783 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005784 " (<level> is 0-%d)\n",
5785 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005786 }
5787
5788 return len;
5789}
5790
5791static int brightness_write(char *buf)
5792{
5793 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005794 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005795 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005796 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005797
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005798 level = brightness_get(NULL);
5799 if (level < 0)
5800 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005801
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005802 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005803 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005804 if (level < max_level)
5805 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005806 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005807 if (level > 0)
5808 level--;
5809 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5810 level >= 0 && level <= max_level) {
5811 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005812 } else
5813 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005814 }
5815
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005816 /*
5817 * Now we know what the final level should be, so we try to set it.
5818 * Doing it this way makes the syscall restartable in case of EINTR
5819 */
5820 rc = brightness_set(level);
5821 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005822}
5823
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005824static struct ibm_struct brightness_driver_data = {
5825 .name = "brightness",
5826 .read = brightness_read,
5827 .write = brightness_write,
5828 .exit = brightness_exit,
5829};
5830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831/*************************************************************************
5832 * Volume subdriver
5833 */
5834
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005835static int volume_offset = 0x30;
5836
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005837static int volume_read(char *p)
5838{
5839 int len = 0;
5840 u8 level;
5841
5842 if (!acpi_ec_read(volume_offset, &level)) {
5843 len += sprintf(p + len, "level:\t\tunreadable\n");
5844 } else {
5845 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5846 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5847 len += sprintf(p + len, "commands:\tup, down, mute\n");
5848 len += sprintf(p + len, "commands:\tlevel <level>"
5849 " (<level> is 0-15)\n");
5850 }
5851
5852 return len;
5853}
5854
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005855static int volume_write(char *buf)
5856{
5857 int cmos_cmd, inc, i;
5858 u8 level, mute;
5859 int new_level, new_mute;
5860 char *cmd;
5861
5862 while ((cmd = next_cmd(&buf))) {
5863 if (!acpi_ec_read(volume_offset, &level))
5864 return -EIO;
5865 new_mute = mute = level & 0x40;
5866 new_level = level = level & 0xf;
5867
5868 if (strlencmp(cmd, "up") == 0) {
5869 if (mute)
5870 new_mute = 0;
5871 else
5872 new_level = level == 15 ? 15 : level + 1;
5873 } else if (strlencmp(cmd, "down") == 0) {
5874 if (mute)
5875 new_mute = 0;
5876 else
5877 new_level = level == 0 ? 0 : level - 1;
5878 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5879 new_level >= 0 && new_level <= 15) {
5880 /* new_level set */
5881 } else if (strlencmp(cmd, "mute") == 0) {
5882 new_mute = 0x40;
5883 } else
5884 return -EINVAL;
5885
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005886 if (new_level != level) {
5887 /* mute doesn't change */
5888
5889 cmos_cmd = (new_level > level) ?
5890 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005891 inc = new_level > level ? 1 : -1;
5892
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005893 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005894 !acpi_ec_write(volume_offset, level)))
5895 return -EIO;
5896
5897 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005898 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005899 !acpi_ec_write(volume_offset, i + inc))
5900 return -EIO;
5901
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005902 if (mute &&
5903 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5904 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005905 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005906 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005907 }
5908
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005909 if (new_mute != mute) {
5910 /* level doesn't change */
5911
5912 cmos_cmd = (new_mute) ?
5913 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005914
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005915 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005916 !acpi_ec_write(volume_offset, level + new_mute))
5917 return -EIO;
5918 }
5919 }
5920
5921 return 0;
5922}
5923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005924static struct ibm_struct volume_driver_data = {
5925 .name = "volume",
5926 .read = volume_read,
5927 .write = volume_write,
5928};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005929
5930/*************************************************************************
5931 * Fan subdriver
5932 */
5933
5934/*
5935 * FAN ACCESS MODES
5936 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005937 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005938 * ACPI GFAN method: returns fan level
5939 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005940 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005941 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005942 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005943 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005944 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5945 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005946 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5947 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005948 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005949 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005950 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5951 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005952 *
5953 * Fan speed changes of any sort (including those caused by the
5954 * disengaged mode) are usually done slowly by the firmware as the
5955 * maximum ammount of fan duty cycle change per second seems to be
5956 * limited.
5957 *
5958 * Reading is not available if GFAN exists.
5959 * Writing is not available if SFAN exists.
5960 *
5961 * Bits
5962 * 7 automatic mode engaged;
5963 * (default operation mode of the ThinkPad)
5964 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005965 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005966 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005967 * the tachometer while the fan controller ramps up
5968 * the speed (which can take up to a few *minutes*).
5969 * Speeds up fan to 100% duty-cycle, which is far above
5970 * the standard RPM levels. It is not impossible that
5971 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005972 * 5-3 unused in some models. Extra bits for fan level
5973 * in others, but still useless as all values above
5974 * 7 map to the same speed as level 7 in these models.
5975 * 2-0 fan level (0..7 usually)
5976 * 0x00 = stop
5977 * 0x07 = max (set when temperatures critical)
5978 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005979 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005980 *
5981 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5982 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5983 * does so, its initial value is meaningless (0x07).
5984 *
5985 * For firmware bugs, refer to:
5986 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5987 *
5988 * ----
5989 *
5990 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5991 * Main fan tachometer reading (in RPM)
5992 *
5993 * This register is present on all ThinkPads with a new-style EC, and
5994 * it is known not to be present on the A21m/e, and T22, as there is
5995 * something else in offset 0x84 according to the ACPI DSDT. Other
5996 * ThinkPads from this same time period (and earlier) probably lack the
5997 * tachometer as well.
5998 *
5999 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
6000 * was never fixed by IBM to report the EC firmware version string
6001 * probably support the tachometer (like the early X models), so
6002 * detecting it is quite hard. We need more data to know for sure.
6003 *
6004 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6005 * might result.
6006 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03006007 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6008 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006009 *
6010 * For firmware bugs, refer to:
6011 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6012 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006013 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006014 * ThinkPad X31, X40, X41. Not available in the X60.
6015 *
6016 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6017 * high speed. ACPI DSDT seems to map these three speeds to levels
6018 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6019 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6020 *
6021 * The speeds are stored on handles
6022 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6023 *
6024 * There are three default speed sets, acessible as handles:
6025 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6026 *
6027 * ACPI DSDT switches which set is in use depending on various
6028 * factors.
6029 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006030 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006031 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6032 * but the ACPI tables just mention level 7.
6033 */
6034
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006035enum { /* Fan control constants */
6036 fan_status_offset = 0x2f, /* EC register 0x2f */
6037 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6038 * 0x84 must be read before 0x85 */
6039
6040 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6041 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6042
6043 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6044};
6045
6046enum fan_status_access_mode {
6047 TPACPI_FAN_NONE = 0, /* No fan status or control */
6048 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6049 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6050};
6051
6052enum fan_control_access_mode {
6053 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6054 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6055 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6056 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6057};
6058
6059enum fan_control_commands {
6060 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6061 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6062 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6063 * and also watchdog cmd */
6064};
6065
6066static int fan_control_allowed;
6067
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006068static enum fan_status_access_mode fan_status_access_mode;
6069static enum fan_control_access_mode fan_control_access_mode;
6070static enum fan_control_commands fan_control_commands;
6071
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006072static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006073static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006074static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006075static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006076
6077static struct mutex fan_mutex;
6078
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006079static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05006080static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006081
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006082TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6083TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006084 "\\FSPD", /* 600e/x, 770e, 770x */
6085 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006086TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006087 "JFNS", /* 770x-JL */
6088 ); /* all others */
6089
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006090/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006091 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6092 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6093 * be in auto mode (0x80).
6094 *
6095 * This is corrected by any write to HFSP either by the driver, or
6096 * by the firmware.
6097 *
6098 * We assume 0x07 really means auto mode while this quirk is active,
6099 * as this is far more likely than the ThinkPad being in level 7,
6100 * which is only used by the firmware during thermal emergencies.
6101 */
6102
6103static void fan_quirk1_detect(void)
6104{
6105 /* In some ThinkPads, neither the EC nor the ACPI
6106 * DSDT initialize the HFSP register, and it ends up
6107 * being initially set to 0x07 when it *could* be
6108 * either 0x07 or 0x80.
6109 *
6110 * Enable for TP-1Y (T43), TP-78 (R51e),
6111 * TP-76 (R52), TP-70 (T43, R52), which are known
6112 * to be buggy. */
6113 if (fan_control_initial_status == 0x07) {
6114 switch (thinkpad_id.ec_model) {
6115 case 0x5931: /* TP-1Y */
6116 case 0x3837: /* TP-78 */
6117 case 0x3637: /* TP-76 */
6118 case 0x3037: /* TP-70 */
6119 printk(TPACPI_NOTICE
6120 "fan_init: initial fan status is unknown, "
6121 "assuming it is in auto mode\n");
6122 tp_features.fan_ctrl_status_undef = 1;
6123 ;;
6124 }
6125 }
6126}
6127
6128static void fan_quirk1_handle(u8 *fan_status)
6129{
6130 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6131 if (*fan_status != fan_control_initial_status) {
6132 /* something changed the HFSP regisnter since
6133 * driver init time, so it is not undefined
6134 * anymore */
6135 tp_features.fan_ctrl_status_undef = 0;
6136 } else {
6137 /* Return most likely status. In fact, it
6138 * might be the only possible status */
6139 *fan_status = TP_EC_FAN_AUTO;
6140 }
6141 }
6142}
6143
6144/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006145 * Call with fan_mutex held
6146 */
6147static void fan_update_desired_level(u8 status)
6148{
6149 if ((status &
6150 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6151 if (status > 7)
6152 fan_control_desired_level = 7;
6153 else
6154 fan_control_desired_level = status;
6155 }
6156}
6157
6158static int fan_get_status(u8 *status)
6159{
6160 u8 s;
6161
6162 /* TODO:
6163 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6164
6165 switch (fan_status_access_mode) {
6166 case TPACPI_FAN_RD_ACPI_GFAN:
6167 /* 570, 600e/x, 770e, 770x */
6168
6169 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6170 return -EIO;
6171
6172 if (likely(status))
6173 *status = s & 0x07;
6174
6175 break;
6176
6177 case TPACPI_FAN_RD_TPEC:
6178 /* all except 570, 600e/x, 770e, 770x */
6179 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6180 return -EIO;
6181
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006182 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006183 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006184 fan_quirk1_handle(status);
6185 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006186
6187 break;
6188
6189 default:
6190 return -ENXIO;
6191 }
6192
6193 return 0;
6194}
6195
6196static int fan_get_status_safe(u8 *status)
6197{
6198 int rc;
6199 u8 s;
6200
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006201 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006202 return -ERESTARTSYS;
6203 rc = fan_get_status(&s);
6204 if (!rc)
6205 fan_update_desired_level(s);
6206 mutex_unlock(&fan_mutex);
6207
6208 if (status)
6209 *status = s;
6210
6211 return rc;
6212}
6213
6214static int fan_get_speed(unsigned int *speed)
6215{
6216 u8 hi, lo;
6217
6218 switch (fan_status_access_mode) {
6219 case TPACPI_FAN_RD_TPEC:
6220 /* all except 570, 600e/x, 770e, 770x */
6221 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6222 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6223 return -EIO;
6224
6225 if (likely(speed))
6226 *speed = (hi << 8) | lo;
6227
6228 break;
6229
6230 default:
6231 return -ENXIO;
6232 }
6233
6234 return 0;
6235}
6236
6237static int fan_set_level(int level)
6238{
6239 if (!fan_control_allowed)
6240 return -EPERM;
6241
6242 switch (fan_control_access_mode) {
6243 case TPACPI_FAN_WR_ACPI_SFAN:
6244 if (level >= 0 && level <= 7) {
6245 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6246 return -EIO;
6247 } else
6248 return -EINVAL;
6249 break;
6250
6251 case TPACPI_FAN_WR_ACPI_FANS:
6252 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006253 if (!(level & TP_EC_FAN_AUTO) &&
6254 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006255 ((level < 0) || (level > 7)))
6256 return -EINVAL;
6257
6258 /* safety net should the EC not support AUTO
6259 * or FULLSPEED mode bits and just ignore them */
6260 if (level & TP_EC_FAN_FULLSPEED)
6261 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006262 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006263 level |= 4; /* safety min speed 4 */
6264
6265 if (!acpi_ec_write(fan_status_offset, level))
6266 return -EIO;
6267 else
6268 tp_features.fan_ctrl_status_undef = 0;
6269 break;
6270
6271 default:
6272 return -ENXIO;
6273 }
6274 return 0;
6275}
6276
6277static int fan_set_level_safe(int level)
6278{
6279 int rc;
6280
6281 if (!fan_control_allowed)
6282 return -EPERM;
6283
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006284 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006285 return -ERESTARTSYS;
6286
6287 if (level == TPACPI_FAN_LAST_LEVEL)
6288 level = fan_control_desired_level;
6289
6290 rc = fan_set_level(level);
6291 if (!rc)
6292 fan_update_desired_level(level);
6293
6294 mutex_unlock(&fan_mutex);
6295 return rc;
6296}
6297
6298static int fan_set_enable(void)
6299{
6300 u8 s;
6301 int rc;
6302
6303 if (!fan_control_allowed)
6304 return -EPERM;
6305
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006306 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006307 return -ERESTARTSYS;
6308
6309 switch (fan_control_access_mode) {
6310 case TPACPI_FAN_WR_ACPI_FANS:
6311 case TPACPI_FAN_WR_TPEC:
6312 rc = fan_get_status(&s);
6313 if (rc < 0)
6314 break;
6315
6316 /* Don't go out of emergency fan mode */
6317 if (s != 7) {
6318 s &= 0x07;
6319 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6320 }
6321
6322 if (!acpi_ec_write(fan_status_offset, s))
6323 rc = -EIO;
6324 else {
6325 tp_features.fan_ctrl_status_undef = 0;
6326 rc = 0;
6327 }
6328 break;
6329
6330 case TPACPI_FAN_WR_ACPI_SFAN:
6331 rc = fan_get_status(&s);
6332 if (rc < 0)
6333 break;
6334
6335 s &= 0x07;
6336
6337 /* Set fan to at least level 4 */
6338 s |= 4;
6339
6340 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006341 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006342 else
6343 rc = 0;
6344 break;
6345
6346 default:
6347 rc = -ENXIO;
6348 }
6349
6350 mutex_unlock(&fan_mutex);
6351 return rc;
6352}
6353
6354static int fan_set_disable(void)
6355{
6356 int rc;
6357
6358 if (!fan_control_allowed)
6359 return -EPERM;
6360
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006361 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006362 return -ERESTARTSYS;
6363
6364 rc = 0;
6365 switch (fan_control_access_mode) {
6366 case TPACPI_FAN_WR_ACPI_FANS:
6367 case TPACPI_FAN_WR_TPEC:
6368 if (!acpi_ec_write(fan_status_offset, 0x00))
6369 rc = -EIO;
6370 else {
6371 fan_control_desired_level = 0;
6372 tp_features.fan_ctrl_status_undef = 0;
6373 }
6374 break;
6375
6376 case TPACPI_FAN_WR_ACPI_SFAN:
6377 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6378 rc = -EIO;
6379 else
6380 fan_control_desired_level = 0;
6381 break;
6382
6383 default:
6384 rc = -ENXIO;
6385 }
6386
6387
6388 mutex_unlock(&fan_mutex);
6389 return rc;
6390}
6391
6392static int fan_set_speed(int speed)
6393{
6394 int rc;
6395
6396 if (!fan_control_allowed)
6397 return -EPERM;
6398
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006399 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006400 return -ERESTARTSYS;
6401
6402 rc = 0;
6403 switch (fan_control_access_mode) {
6404 case TPACPI_FAN_WR_ACPI_FANS:
6405 if (speed >= 0 && speed <= 65535) {
6406 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6407 speed, speed, speed))
6408 rc = -EIO;
6409 } else
6410 rc = -EINVAL;
6411 break;
6412
6413 default:
6414 rc = -ENXIO;
6415 }
6416
6417 mutex_unlock(&fan_mutex);
6418 return rc;
6419}
6420
6421static void fan_watchdog_reset(void)
6422{
6423 static int fan_watchdog_active;
6424
6425 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6426 return;
6427
6428 if (fan_watchdog_active)
6429 cancel_delayed_work(&fan_watchdog_task);
6430
6431 if (fan_watchdog_maxinterval > 0 &&
6432 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6433 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006434 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006435 msecs_to_jiffies(fan_watchdog_maxinterval
6436 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006437 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006438 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006439 "watchdog will not trigger\n");
6440 }
6441 } else
6442 fan_watchdog_active = 0;
6443}
6444
6445static void fan_watchdog_fire(struct work_struct *ignored)
6446{
6447 int rc;
6448
6449 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6450 return;
6451
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006452 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006453 rc = fan_set_enable();
6454 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006455 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006456 "will try again later...\n", -rc);
6457 /* reschedule for later */
6458 fan_watchdog_reset();
6459 }
6460}
6461
6462/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006463 * SYSFS fan layout: hwmon compatible (device)
6464 *
6465 * pwm*_enable:
6466 * 0: "disengaged" mode
6467 * 1: manual mode
6468 * 2: native EC "auto" mode (recommended, hardware default)
6469 *
6470 * pwm*: set speed in manual mode, ignored otherwise.
6471 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6472 * interpolation.
6473 *
6474 * fan*_input: tachometer reading, RPM
6475 *
6476 *
6477 * SYSFS fan layout: extensions
6478 *
6479 * fan_watchdog (driver):
6480 * fan watchdog interval in seconds, 0 disables (default), max 120
6481 */
6482
6483/* sysfs fan pwm1_enable ----------------------------------------------- */
6484static ssize_t fan_pwm1_enable_show(struct device *dev,
6485 struct device_attribute *attr,
6486 char *buf)
6487{
6488 int res, mode;
6489 u8 status;
6490
6491 res = fan_get_status_safe(&status);
6492 if (res)
6493 return res;
6494
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006495 if (status & TP_EC_FAN_FULLSPEED) {
6496 mode = 0;
6497 } else if (status & TP_EC_FAN_AUTO) {
6498 mode = 2;
6499 } else
6500 mode = 1;
6501
6502 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6503}
6504
6505static ssize_t fan_pwm1_enable_store(struct device *dev,
6506 struct device_attribute *attr,
6507 const char *buf, size_t count)
6508{
6509 unsigned long t;
6510 int res, level;
6511
6512 if (parse_strtoul(buf, 2, &t))
6513 return -EINVAL;
6514
6515 switch (t) {
6516 case 0:
6517 level = TP_EC_FAN_FULLSPEED;
6518 break;
6519 case 1:
6520 level = TPACPI_FAN_LAST_LEVEL;
6521 break;
6522 case 2:
6523 level = TP_EC_FAN_AUTO;
6524 break;
6525 case 3:
6526 /* reserved for software-controlled auto mode */
6527 return -ENOSYS;
6528 default:
6529 return -EINVAL;
6530 }
6531
6532 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006533 if (res == -ENXIO)
6534 return -EINVAL;
6535 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006536 return res;
6537
6538 fan_watchdog_reset();
6539
6540 return count;
6541}
6542
6543static struct device_attribute dev_attr_fan_pwm1_enable =
6544 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6545 fan_pwm1_enable_show, fan_pwm1_enable_store);
6546
6547/* sysfs fan pwm1 ------------------------------------------------------ */
6548static ssize_t fan_pwm1_show(struct device *dev,
6549 struct device_attribute *attr,
6550 char *buf)
6551{
6552 int res;
6553 u8 status;
6554
6555 res = fan_get_status_safe(&status);
6556 if (res)
6557 return res;
6558
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006559 if ((status &
6560 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6561 status = fan_control_desired_level;
6562
6563 if (status > 7)
6564 status = 7;
6565
6566 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6567}
6568
6569static ssize_t fan_pwm1_store(struct device *dev,
6570 struct device_attribute *attr,
6571 const char *buf, size_t count)
6572{
6573 unsigned long s;
6574 int rc;
6575 u8 status, newlevel;
6576
6577 if (parse_strtoul(buf, 255, &s))
6578 return -EINVAL;
6579
6580 /* scale down from 0-255 to 0-7 */
6581 newlevel = (s >> 5) & 0x07;
6582
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006583 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006584 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006585
6586 rc = fan_get_status(&status);
6587 if (!rc && (status &
6588 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6589 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006590 if (rc == -ENXIO)
6591 rc = -EINVAL;
6592 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006593 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006594 fan_watchdog_reset();
6595 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006596 }
6597
6598 mutex_unlock(&fan_mutex);
6599 return (rc)? rc : count;
6600}
6601
6602static struct device_attribute dev_attr_fan_pwm1 =
6603 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6604 fan_pwm1_show, fan_pwm1_store);
6605
6606/* sysfs fan fan1_input ------------------------------------------------ */
6607static ssize_t fan_fan1_input_show(struct device *dev,
6608 struct device_attribute *attr,
6609 char *buf)
6610{
6611 int res;
6612 unsigned int speed;
6613
6614 res = fan_get_speed(&speed);
6615 if (res < 0)
6616 return res;
6617
6618 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6619}
6620
6621static struct device_attribute dev_attr_fan_fan1_input =
6622 __ATTR(fan1_input, S_IRUGO,
6623 fan_fan1_input_show, NULL);
6624
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006625/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006626static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6627 char *buf)
6628{
6629 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6630}
6631
6632static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6633 const char *buf, size_t count)
6634{
6635 unsigned long t;
6636
6637 if (parse_strtoul(buf, 120, &t))
6638 return -EINVAL;
6639
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006640 if (!fan_control_allowed)
6641 return -EPERM;
6642
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006643 fan_watchdog_maxinterval = t;
6644 fan_watchdog_reset();
6645
6646 return count;
6647}
6648
6649static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6650 fan_fan_watchdog_show, fan_fan_watchdog_store);
6651
6652/* --------------------------------------------------------------------- */
6653static struct attribute *fan_attributes[] = {
6654 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6655 &dev_attr_fan_fan1_input.attr,
6656 NULL
6657};
6658
6659static const struct attribute_group fan_attr_group = {
6660 .attrs = fan_attributes,
6661};
6662
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006663static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006664{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006665 int rc;
6666
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006667 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6668
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006669 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006670 fan_status_access_mode = TPACPI_FAN_NONE;
6671 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006672 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006673 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006674 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006675 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006676
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006677 TPACPI_ACPIHANDLE_INIT(fans);
6678 TPACPI_ACPIHANDLE_INIT(gfan);
6679 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006680
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006681 if (gfan_handle) {
6682 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006683 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006684 } else {
6685 /* all other ThinkPads: note that even old-style
6686 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006687 if (likely(acpi_ec_read(fan_status_offset,
6688 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006689 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006690 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006691 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006692 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006693 "ThinkPad ACPI EC access misbehaving, "
6694 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006695 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006696 }
6697 }
6698
6699 if (sfan_handle) {
6700 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006701 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006702 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006703 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006704 } else {
6705 if (!gfan_handle) {
6706 /* gfan without sfan means no fan control */
6707 /* all other models implement TP EC 0x2f control */
6708
6709 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006710 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006711 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006712 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006713 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006714 TPACPI_FAN_CMD_SPEED |
6715 TPACPI_FAN_CMD_LEVEL |
6716 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006717 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006718 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006719 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006720 TPACPI_FAN_CMD_LEVEL |
6721 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006722 }
6723 }
6724 }
6725
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006726 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6727 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6728 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6729 fan_status_access_mode, fan_control_access_mode);
6730
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006731 /* fan control master switch */
6732 if (!fan_control_allowed) {
6733 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6734 fan_control_commands = 0;
6735 dbg_printk(TPACPI_DBG_INIT,
6736 "fan control features disabled by parameter\n");
6737 }
6738
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006739 /* update fan_control_desired_level */
6740 if (fan_status_access_mode != TPACPI_FAN_NONE)
6741 fan_get_status_safe(NULL);
6742
6743 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6744 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006745 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006746 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006747 if (rc < 0)
6748 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006749
6750 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6751 &driver_attr_fan_watchdog);
6752 if (rc < 0) {
6753 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6754 &fan_attr_group);
6755 return rc;
6756 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006757 return 0;
6758 } else
6759 return 1;
6760}
6761
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006762static void fan_exit(void)
6763{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006764 vdbg_printk(TPACPI_DBG_EXIT,
6765 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006766
6767 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006768 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006769 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6770 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006772 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006773 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006774}
6775
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006776static void fan_suspend(pm_message_t state)
6777{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006778 int rc;
6779
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006780 if (!fan_control_allowed)
6781 return;
6782
6783 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006784 fan_control_resume_level = 0;
6785 rc = fan_get_status_safe(&fan_control_resume_level);
6786 if (rc < 0)
6787 printk(TPACPI_NOTICE
6788 "failed to read fan level for later "
6789 "restore during resume: %d\n", rc);
6790
6791 /* if it is undefined, don't attempt to restore it.
6792 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006793 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006794 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006795}
6796
6797static void fan_resume(void)
6798{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006799 u8 current_level = 7;
6800 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006801 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006802
6803 /* DSDT *always* updates status on resume */
6804 tp_features.fan_ctrl_status_undef = 0;
6805
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006806 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006807 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006808 (fan_get_status_safe(&current_level) < 0))
6809 return;
6810
6811 switch (fan_control_access_mode) {
6812 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006813 /* never decrease fan level */
6814 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006815 break;
6816 case TPACPI_FAN_WR_ACPI_FANS:
6817 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006818 /* never decrease fan level, scale is:
6819 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6820 *
6821 * We expect the firmware to set either 7 or AUTO, but we
6822 * handle FULLSPEED out of paranoia.
6823 *
6824 * So, we can safely only restore FULLSPEED or 7, anything
6825 * else could slow the fan. Restoring AUTO is useless, at
6826 * best that's exactly what the DSDT already set (it is the
6827 * slower it uses).
6828 *
6829 * Always keep in mind that the DSDT *will* have set the
6830 * fans to what the vendor supposes is the best level. We
6831 * muck with it only to speed the fan up.
6832 */
6833 if (fan_control_resume_level != 7 &&
6834 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6835 return;
6836 else
6837 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6838 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006839 break;
6840 default:
6841 return;
6842 }
6843 if (do_set) {
6844 printk(TPACPI_NOTICE
6845 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006846 fan_control_resume_level);
6847 rc = fan_set_level_safe(fan_control_resume_level);
6848 if (rc < 0)
6849 printk(TPACPI_NOTICE
6850 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006851 }
6852}
6853
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006854static int fan_read(char *p)
6855{
6856 int len = 0;
6857 int rc;
6858 u8 status;
6859 unsigned int speed = 0;
6860
6861 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006862 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006863 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006864 rc = fan_get_status_safe(&status);
6865 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006866 return rc;
6867
6868 len += sprintf(p + len, "status:\t\t%s\n"
6869 "level:\t\t%d\n",
6870 (status != 0) ? "enabled" : "disabled", status);
6871 break;
6872
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006873 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006874 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006875 rc = fan_get_status_safe(&status);
6876 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006877 return rc;
6878
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006879 len += sprintf(p + len, "status:\t\t%s\n",
6880 (status != 0) ? "enabled" : "disabled");
6881
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006882 rc = fan_get_speed(&speed);
6883 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006884 return rc;
6885
6886 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6887
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006888 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006889 /* Disengaged mode takes precedence */
6890 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006891 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006892 len += sprintf(p + len, "level:\t\tauto\n");
6893 else
6894 len += sprintf(p + len, "level:\t\t%d\n", status);
6895 break;
6896
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006897 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006898 default:
6899 len += sprintf(p + len, "status:\t\tnot supported\n");
6900 }
6901
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006902 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006903 len += sprintf(p + len, "commands:\tlevel <level>");
6904
6905 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006906 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006907 len += sprintf(p + len, " (<level> is 0-7)\n");
6908 break;
6909
6910 default:
6911 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006912 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006913 break;
6914 }
6915 }
6916
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006917 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006918 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006919 "commands:\twatchdog <timeout> (<timeout> "
6920 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006921
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006922 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006923 len += sprintf(p + len, "commands:\tspeed <speed>"
6924 " (<speed> is 0-65535)\n");
6925
6926 return len;
6927}
6928
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006929static int fan_write_cmd_level(const char *cmd, int *rc)
6930{
6931 int level;
6932
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006933 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006934 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006935 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006936 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006937 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006938 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006939 return 0;
6940
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006941 *rc = fan_set_level_safe(level);
6942 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006943 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006944 "access mode %d", fan_control_access_mode);
6945
6946 return 1;
6947}
6948
6949static int fan_write_cmd_enable(const char *cmd, int *rc)
6950{
6951 if (strlencmp(cmd, "enable") != 0)
6952 return 0;
6953
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006954 *rc = fan_set_enable();
6955 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006956 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006957 "access mode %d", fan_control_access_mode);
6958
6959 return 1;
6960}
6961
6962static int fan_write_cmd_disable(const char *cmd, int *rc)
6963{
6964 if (strlencmp(cmd, "disable") != 0)
6965 return 0;
6966
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006967 *rc = fan_set_disable();
6968 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006969 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006970 "access mode %d", fan_control_access_mode);
6971
6972 return 1;
6973}
6974
6975static int fan_write_cmd_speed(const char *cmd, int *rc)
6976{
6977 int speed;
6978
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006979 /* TODO:
6980 * Support speed <low> <medium> <high> ? */
6981
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006982 if (sscanf(cmd, "speed %d", &speed) != 1)
6983 return 0;
6984
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006985 *rc = fan_set_speed(speed);
6986 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006987 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006988 "access mode %d", fan_control_access_mode);
6989
6990 return 1;
6991}
6992
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006993static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6994{
6995 int interval;
6996
6997 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6998 return 0;
6999
7000 if (interval < 0 || interval > 120)
7001 *rc = -EINVAL;
7002 else
7003 fan_watchdog_maxinterval = interval;
7004
7005 return 1;
7006}
7007
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007008static int fan_write(char *buf)
7009{
7010 char *cmd;
7011 int rc = 0;
7012
7013 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007014 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007015 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007016 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007017 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007018 fan_write_cmd_disable(cmd, &rc) ||
7019 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007020 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007021 fan_write_cmd_speed(cmd, &rc))
7022 )
7023 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007024 else if (!rc)
7025 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007026 }
7027
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02007028 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007029}
7030
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007031static struct ibm_struct fan_driver_data = {
7032 .name = "fan",
7033 .read = fan_read,
7034 .write = fan_write,
7035 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03007036 .suspend = fan_suspend,
7037 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007038};
7039
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007040/****************************************************************************
7041 ****************************************************************************
7042 *
7043 * Infrastructure
7044 *
7045 ****************************************************************************
7046 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007048/* sysfs name ---------------------------------------------------------- */
7049static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7050 struct device_attribute *attr,
7051 char *buf)
7052{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007053 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007054}
7055
7056static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7057 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7058
7059/* --------------------------------------------------------------------- */
7060
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007061/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03007062static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007063
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007064/*
7065 * Module and infrastructure proble, init and exit handling
7066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007068static int force_load;
7069
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007070#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007071static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007072{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007073 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007075 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007076}
7077#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7078
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007079static void ibm_exit(struct ibm_struct *ibm)
7080{
7081 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7082
7083 list_del_init(&ibm->all_drivers);
7084
7085 if (ibm->flags.acpi_notify_installed) {
7086 dbg_printk(TPACPI_DBG_EXIT,
7087 "%s: acpi_remove_notify_handler\n", ibm->name);
7088 BUG_ON(!ibm->acpi);
7089 acpi_remove_notify_handler(*ibm->acpi->handle,
7090 ibm->acpi->type,
7091 dispatch_acpi_notify);
7092 ibm->flags.acpi_notify_installed = 0;
7093 ibm->flags.acpi_notify_installed = 0;
7094 }
7095
7096 if (ibm->flags.proc_created) {
7097 dbg_printk(TPACPI_DBG_EXIT,
7098 "%s: remove_proc_entry\n", ibm->name);
7099 remove_proc_entry(ibm->name, proc_dir);
7100 ibm->flags.proc_created = 0;
7101 }
7102
7103 if (ibm->flags.acpi_driver_registered) {
7104 dbg_printk(TPACPI_DBG_EXIT,
7105 "%s: acpi_bus_unregister_driver\n", ibm->name);
7106 BUG_ON(!ibm->acpi);
7107 acpi_bus_unregister_driver(ibm->acpi->driver);
7108 kfree(ibm->acpi->driver);
7109 ibm->acpi->driver = NULL;
7110 ibm->flags.acpi_driver_registered = 0;
7111 }
7112
7113 if (ibm->flags.init_called && ibm->exit) {
7114 ibm->exit();
7115 ibm->flags.init_called = 0;
7116 }
7117
7118 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7119}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007120
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007121static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122{
7123 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007124 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 struct proc_dir_entry *entry;
7126
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007127 BUG_ON(ibm == NULL);
7128
7129 INIT_LIST_HEAD(&ibm->all_drivers);
7130
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007131 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 return 0;
7133
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007134 dbg_printk(TPACPI_DBG_INIT,
7135 "probing for %s\n", ibm->name);
7136
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007137 if (iibm->init) {
7138 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007139 if (ret > 0)
7140 return 0; /* probe failed */
7141 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007143
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007144 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 }
7146
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007147 if (ibm->acpi) {
7148 if (ibm->acpi->hid) {
7149 ret = register_tpacpi_subdriver(ibm);
7150 if (ret)
7151 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007152 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007153
7154 if (ibm->acpi->notify) {
7155 ret = setup_acpi_notify(ibm);
7156 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007157 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007158 ibm->name);
7159 ret = 0;
7160 goto err_out;
7161 }
7162 if (ret < 0)
7163 goto err_out;
7164 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007165 }
7166
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007167 dbg_printk(TPACPI_DBG_INIT,
7168 "%s installed\n", ibm->name);
7169
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007170 if (ibm->read) {
7171 entry = create_proc_entry(ibm->name,
7172 S_IFREG | S_IRUGO | S_IWUSR,
7173 proc_dir);
7174 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007175 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007176 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007177 ret = -ENODEV;
7178 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007179 }
7180 entry->owner = THIS_MODULE;
7181 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007182 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007183 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007184 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007185 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007188 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7189
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007191
7192err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007193 dbg_printk(TPACPI_DBG_INIT,
7194 "%s: at error exit path with result %d\n",
7195 ibm->name, ret);
7196
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007197 ibm_exit(ibm);
7198 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199}
7200
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007201/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007203/* returns 0 - probe ok, or < 0 - probe error.
7204 * Probe ok doesn't mean thinkpad found.
7205 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7206static int __must_check __init get_thinkpad_model_data(
7207 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007208{
Jeff Garzik18552562007-10-03 15:15:40 -04007209 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007210 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007211 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007212
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007213 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007214 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007215
7216 memset(tp, 0, sizeof(*tp));
7217
7218 if (dmi_name_in_vendors("IBM"))
7219 tp->vendor = PCI_VENDOR_ID_IBM;
7220 else if (dmi_name_in_vendors("LENOVO"))
7221 tp->vendor = PCI_VENDOR_ID_LENOVO;
7222 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007223 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007224
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007225 s = dmi_get_system_info(DMI_BIOS_VERSION);
7226 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7227 if (s && !tp->bios_version_str)
7228 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007229 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007230 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007231 tp->bios_model = tp->bios_version_str[0]
7232 | (tp->bios_version_str[1] << 8);
7233
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007234 /*
7235 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7236 * X32 or newer, all Z series; Some models must have an
7237 * up-to-date BIOS or they will not be detected.
7238 *
7239 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7240 */
7241 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007242 if (sscanf(dev->name,
7243 "IBM ThinkPad Embedded Controller -[%17c",
7244 ec_fw_string) == 1) {
7245 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7246 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007247
7248 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007249 if (!tp->ec_version_str)
7250 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007251 tp->ec_model = ec_fw_string[0]
7252 | (ec_fw_string[1] << 8);
7253 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007254 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007255 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007256
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007257 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7258 if (s && !strnicmp(s, "ThinkPad", 8)) {
7259 tp->model_str = kstrdup(s, GFP_KERNEL);
7260 if (!tp->model_str)
7261 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007262 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007263
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007264 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7265 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7266 if (s && !tp->nummodel_str)
7267 return -ENOMEM;
7268
7269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270}
7271
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007272static int __init probe_for_thinkpad(void)
7273{
7274 int is_thinkpad;
7275
7276 if (acpi_disabled)
7277 return -ENODEV;
7278
7279 /*
7280 * Non-ancient models have better DMI tagging, but very old models
7281 * don't.
7282 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007283 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007284
7285 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007286 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007287 if (!ec_handle) {
7288 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007289 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007290 "Not yet supported ThinkPad detected!\n");
7291 return -ENODEV;
7292 }
7293
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007294 /*
7295 * Risks a regression on very old machines, but reduces potential
7296 * false positives a damn great deal
7297 */
7298 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007299 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007300
7301 if (!is_thinkpad && !force_load)
7302 return -ENODEV;
7303
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007304 return 0;
7305}
7306
7307
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007308/* Module init, exit, parameters */
7309
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007310static struct ibm_init_struct ibms_init[] __initdata = {
7311 {
7312 .init = thinkpad_acpi_driver_init,
7313 .data = &thinkpad_acpi_driver_data,
7314 },
7315 {
7316 .init = hotkey_init,
7317 .data = &hotkey_driver_data,
7318 },
7319 {
7320 .init = bluetooth_init,
7321 .data = &bluetooth_driver_data,
7322 },
7323 {
7324 .init = wan_init,
7325 .data = &wan_driver_data,
7326 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007327 {
7328 .init = uwb_init,
7329 .data = &uwb_driver_data,
7330 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007331#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007332 {
7333 .init = video_init,
7334 .data = &video_driver_data,
7335 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007336#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007337 {
7338 .init = light_init,
7339 .data = &light_driver_data,
7340 },
7341#ifdef CONFIG_THINKPAD_ACPI_DOCK
7342 {
7343 .init = dock_init,
7344 .data = &dock_driver_data[0],
7345 },
7346 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007347 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007348 .data = &dock_driver_data[1],
7349 },
7350#endif
7351#ifdef CONFIG_THINKPAD_ACPI_BAY
7352 {
7353 .init = bay_init,
7354 .data = &bay_driver_data,
7355 },
7356#endif
7357 {
7358 .init = cmos_init,
7359 .data = &cmos_driver_data,
7360 },
7361 {
7362 .init = led_init,
7363 .data = &led_driver_data,
7364 },
7365 {
7366 .init = beep_init,
7367 .data = &beep_driver_data,
7368 },
7369 {
7370 .init = thermal_init,
7371 .data = &thermal_driver_data,
7372 },
7373 {
7374 .data = &ecdump_driver_data,
7375 },
7376 {
7377 .init = brightness_init,
7378 .data = &brightness_driver_data,
7379 },
7380 {
7381 .data = &volume_driver_data,
7382 },
7383 {
7384 .init = fan_init,
7385 .data = &fan_driver_data,
7386 },
7387};
7388
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007389static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7390{
7391 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007392 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007393
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007394 if (!kp || !kp->name || !val)
7395 return -EINVAL;
7396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007397 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7398 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007399 WARN_ON(ibm == NULL);
7400
7401 if (!ibm || !ibm->name)
7402 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007403
7404 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7405 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007406 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007407 strcpy(ibms_init[i].param, val);
7408 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007409 return 0;
7410 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007411 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007412
7413 return -EINVAL;
7414}
7415
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007416module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007417MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007418 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007419
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007420module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007421MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007422
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007423module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007424MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007425 "Attempts to load the driver even on a "
7426 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007427
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007428module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007429MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007430 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007431
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007432module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007433MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007434 "Selects brightness control strategy: "
7435 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007436
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007437module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007438MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007439 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007440
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007441module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007442MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007443 "used for backwards compatibility with userspace, "
7444 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007445
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007446#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007447 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007448 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007449 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007450
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007451TPACPI_PARAM(hotkey);
7452TPACPI_PARAM(bluetooth);
7453TPACPI_PARAM(video);
7454TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007455#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007456TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007457#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007458#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007459TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007460#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007461TPACPI_PARAM(cmos);
7462TPACPI_PARAM(led);
7463TPACPI_PARAM(beep);
7464TPACPI_PARAM(ecdump);
7465TPACPI_PARAM(brightness);
7466TPACPI_PARAM(volume);
7467TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007468
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007469#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7470module_param(dbg_wlswemul, uint, 0);
7471MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7472module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7473MODULE_PARM_DESC(wlsw_state,
7474 "Initial state of the emulated WLSW switch");
7475
7476module_param(dbg_bluetoothemul, uint, 0);
7477MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7478module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7479MODULE_PARM_DESC(bluetooth_state,
7480 "Initial state of the emulated bluetooth switch");
7481
7482module_param(dbg_wwanemul, uint, 0);
7483MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7484module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7485MODULE_PARM_DESC(wwan_state,
7486 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007487
7488module_param(dbg_uwbemul, uint, 0);
7489MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7490module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7491MODULE_PARM_DESC(uwb_state,
7492 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007493#endif
7494
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007495static void thinkpad_acpi_module_exit(void)
7496{
7497 struct ibm_struct *ibm, *itmp;
7498
7499 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7500
7501 list_for_each_entry_safe_reverse(ibm, itmp,
7502 &tpacpi_all_drivers,
7503 all_drivers) {
7504 ibm_exit(ibm);
7505 }
7506
7507 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7508
7509 if (tpacpi_inputdev) {
7510 if (tp_features.input_device_registered)
7511 input_unregister_device(tpacpi_inputdev);
7512 else
7513 input_free_device(tpacpi_inputdev);
7514 }
7515
7516 if (tpacpi_hwmon)
7517 hwmon_device_unregister(tpacpi_hwmon);
7518
7519 if (tp_features.sensors_pdev_attrs_registered)
7520 device_remove_file(&tpacpi_sensors_pdev->dev,
7521 &dev_attr_thinkpad_acpi_pdev_name);
7522 if (tpacpi_sensors_pdev)
7523 platform_device_unregister(tpacpi_sensors_pdev);
7524 if (tpacpi_pdev)
7525 platform_device_unregister(tpacpi_pdev);
7526
7527 if (tp_features.sensors_pdrv_attrs_registered)
7528 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7529 if (tp_features.platform_drv_attrs_registered)
7530 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7531
7532 if (tp_features.sensors_pdrv_registered)
7533 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7534
7535 if (tp_features.platform_drv_registered)
7536 platform_driver_unregister(&tpacpi_pdriver);
7537
7538 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007539 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007540
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007541 if (tpacpi_wq)
7542 destroy_workqueue(tpacpi_wq);
7543
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007544 kfree(thinkpad_id.bios_version_str);
7545 kfree(thinkpad_id.ec_version_str);
7546 kfree(thinkpad_id.model_str);
7547}
7548
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007549
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007550static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551{
7552 int ret, i;
7553
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007554 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7555
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007556 /* Parameter checking */
7557 if (hotkey_report_mode > 2)
7558 return -EINVAL;
7559
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007560 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007561
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007562 ret = get_thinkpad_model_data(&thinkpad_id);
7563 if (ret) {
7564 printk(TPACPI_ERR
7565 "unable to get DMI data: %d\n", ret);
7566 thinkpad_acpi_module_exit();
7567 return ret;
7568 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007569 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007570 if (ret) {
7571 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007572 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007575 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007576
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007577 TPACPI_ACPIHANDLE_INIT(ecrd);
7578 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007580 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7581 if (!tpacpi_wq) {
7582 thinkpad_acpi_module_exit();
7583 return -ENOMEM;
7584 }
7585
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007586 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007588 printk(TPACPI_ERR
7589 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007590 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 return -ENODEV;
7592 }
7593 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007594
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007595 ret = platform_driver_register(&tpacpi_pdriver);
7596 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007597 printk(TPACPI_ERR
7598 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007599 thinkpad_acpi_module_exit();
7600 return ret;
7601 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007602 tp_features.platform_drv_registered = 1;
7603
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007604 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7605 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007606 printk(TPACPI_ERR
7607 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007608 thinkpad_acpi_module_exit();
7609 return ret;
7610 }
7611 tp_features.sensors_pdrv_registered = 1;
7612
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007613 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007614 if (!ret) {
7615 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007616 ret = tpacpi_create_driver_attributes(
7617 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007618 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007619 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007620 printk(TPACPI_ERR
7621 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007622 thinkpad_acpi_module_exit();
7623 return ret;
7624 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007625 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007626
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007627
7628 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007629 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007630 NULL, 0);
7631 if (IS_ERR(tpacpi_pdev)) {
7632 ret = PTR_ERR(tpacpi_pdev);
7633 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007634 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007635 thinkpad_acpi_module_exit();
7636 return ret;
7637 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007638 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007639 TPACPI_HWMON_DRVR_NAME,
7640 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007641 if (IS_ERR(tpacpi_sensors_pdev)) {
7642 ret = PTR_ERR(tpacpi_sensors_pdev);
7643 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007644 printk(TPACPI_ERR
7645 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007646 thinkpad_acpi_module_exit();
7647 return ret;
7648 }
7649 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7650 &dev_attr_thinkpad_acpi_pdev_name);
7651 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007652 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007653 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007654 thinkpad_acpi_module_exit();
7655 return ret;
7656 }
7657 tp_features.sensors_pdev_attrs_registered = 1;
7658 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007659 if (IS_ERR(tpacpi_hwmon)) {
7660 ret = PTR_ERR(tpacpi_hwmon);
7661 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007662 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007663 thinkpad_acpi_module_exit();
7664 return ret;
7665 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007666 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007667 tpacpi_inputdev = input_allocate_device();
7668 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007669 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007670 thinkpad_acpi_module_exit();
7671 return -ENOMEM;
7672 } else {
7673 /* Prepare input device, but don't register */
7674 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007675 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007676 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007677 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7678 thinkpad_id.vendor :
7679 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007680 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7681 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7682 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007683 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7684 ret = ibm_init(&ibms_init[i]);
7685 if (ret >= 0 && *ibms_init[i].param)
7686 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007688 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 return ret;
7690 }
7691 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007692 ret = input_register_device(tpacpi_inputdev);
7693 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007694 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007695 thinkpad_acpi_module_exit();
7696 return ret;
7697 } else {
7698 tp_features.input_device_registered = 1;
7699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007701 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 return 0;
7703}
7704
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007705MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7706
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007707/*
7708 * DMI matching for module autoloading
7709 *
7710 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7711 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7712 *
7713 * Only models listed in thinkwiki will be supported, so add yours
7714 * if it is not there yet.
7715 */
7716#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007717 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007718
7719/* Non-ancient thinkpads */
7720MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7721MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7722
7723/* Ancient thinkpad BIOSes have to be identified by
7724 * BIOS type or model number, and there are far less
7725 * BIOS types than model numbers... */
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007726IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7727IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7728IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007729
7730MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007731MODULE_DESCRIPTION(TPACPI_DESC);
7732MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007733MODULE_LICENSE("GPL");
7734
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007735module_init(thinkpad_acpi_module_init);
7736module_exit(thinkpad_acpi_module_exit);