blob: 3367df9d4f992a60990aff2ed7638b66653a956b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhaa2fbce2009-01-11 03:01:10 -020024#define TPACPI_VERSION "0.22"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030071#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020072#include <asm/uaccess.h>
73
74#include <linux/dmi.h>
75#include <linux/jiffies.h>
76#include <linux/workqueue.h>
77
78#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020079
80#include <linux/pci_ids.h>
81
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020082
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030089#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200118/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200119#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200125/* ACPI \WGSV commands */
126enum {
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
131};
132
133/* TP_ACPI_WGSV_GET_STATE bits */
134enum {
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
145};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
147/****************************************************************************
148 * Main driver
149 */
150
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200151#define TPACPI_NAME "thinkpad"
152#define TPACPI_DESC "ThinkPad ACPI Extras"
153#define TPACPI_FILE TPACPI_NAME "_acpi"
154#define TPACPI_URL "http://ibm-acpi.sf.net/"
155#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200156
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200157#define TPACPI_PROC_DIR "ibm"
158#define TPACPI_ACPI_EVENT_PREFIX "ibm"
159#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300160#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200161#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300163#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300164#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300165
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200166#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200167
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300168/* rfkill switches */
169enum {
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200172 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300173};
174
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200175/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200176#define TPACPI_LOG TPACPI_FILE ": "
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -0200177#define TPACPI_ALERT KERN_ALERT TPACPI_LOG
178#define TPACPI_CRIT KERN_CRIT TPACPI_LOG
179#define TPACPI_ERR KERN_ERR TPACPI_LOG
180#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
181#define TPACPI_INFO KERN_INFO TPACPI_LOG
182#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200183
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200184#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200185#define TPACPI_DBG_INIT 0x0001
186#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200187
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200188#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
189#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
190#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200191
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200192
193/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200194 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200195 */
196
197struct ibm_struct;
198
199struct tp_acpi_drv_struct {
200 const struct acpi_device_id *hid;
201 struct acpi_driver *driver;
202
203 void (*notify) (struct ibm_struct *, u32);
204 acpi_handle *handle;
205 u32 type;
206 struct acpi_device *device;
207};
208
209struct ibm_struct {
210 char *name;
211
212 int (*read) (char *);
213 int (*write) (char *);
214 void (*exit) (void);
215 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200216 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200217 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200218
219 struct list_head all_drivers;
220
221 struct tp_acpi_drv_struct *acpi;
222
223 struct {
224 u8 acpi_driver_registered:1;
225 u8 acpi_notify_installed:1;
226 u8 proc_created:1;
227 u8 init_called:1;
228 u8 experimental:1;
229 } flags;
230};
231
232struct ibm_init_struct {
233 char param[32];
234
235 int (*init) (struct ibm_init_struct *);
236 struct ibm_struct *data;
237};
238
239static struct {
240#ifdef CONFIG_THINKPAD_ACPI_BAY
241 u32 bay_status:1;
242 u32 bay_eject:1;
243 u32 bay_status2:1;
244 u32 bay_eject2:1;
245#endif
246 u32 bluetooth:1;
247 u32 hotkey:1;
248 u32 hotkey_mask:1;
249 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200250 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251 u32 light:1;
252 u32 light_status:1;
253 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300254 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200256 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257 u32 fan_ctrl_status_undef:1;
258 u32 input_device_registered:1;
259 u32 platform_drv_registered:1;
260 u32 platform_drv_attrs_registered:1;
261 u32 sensors_pdrv_registered:1;
262 u32 sensors_pdrv_attrs_registered:1;
263 u32 sensors_pdev_attrs_registered:1;
264 u32 hotkey_poll_active:1;
265} tp_features;
266
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300267static struct {
268 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300269 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300270} tp_warned;
271
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200272struct thinkpad_id_data {
273 unsigned int vendor; /* ThinkPad vendor:
274 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
275
276 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
277 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
278
279 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
280 u16 ec_model;
281
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300282 char *model_str; /* ThinkPad T43 */
283 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200284};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200285static struct thinkpad_id_data thinkpad_id;
286
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300287static enum {
288 TPACPI_LIFE_INIT = 0,
289 TPACPI_LIFE_RUNNING,
290 TPACPI_LIFE_EXITING,
291} tpacpi_lifecycle;
292
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200293static int experimental;
294static u32 dbg_level;
295
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300296static struct workqueue_struct *tpacpi_wq;
297
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300298/* Special LED class that can defer work */
299struct tpacpi_led_classdev {
300 struct led_classdev led_classdev;
301 struct work_struct work;
302 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300303 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300304};
305
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200306#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
307static int dbg_wlswemul;
308static int tpacpi_wlsw_emulstate;
309static int dbg_bluetoothemul;
310static int tpacpi_bluetooth_emulstate;
311static int dbg_wwanemul;
312static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200313static int dbg_uwbemul;
314static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200315#endif
316
317
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000318/*************************************************************************
319 * Debugging helpers
320 */
321
322#define dbg_printk(a_dbg_level, format, arg...) \
323 do { if (dbg_level & (a_dbg_level)) \
324 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
325 } while (0)
326
327#ifdef CONFIG_THINKPAD_ACPI_DEBUG
328#define vdbg_printk dbg_printk
329static const char *str_supported(int is_supported);
330#else
331#define vdbg_printk(a_dbg_level, format, arg...) \
332 do { } while (0)
333#endif
334
335
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300336/****************************************************************************
337 ****************************************************************************
338 *
339 * ACPI Helpers and device model
340 *
341 ****************************************************************************
342 ****************************************************************************/
343
344/*************************************************************************
345 * ACPI basic handles
346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300348static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200350#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 static acpi_handle object##_handle; \
352 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400353 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 static char *object##_paths[] = { paths }
355
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200356TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400357 "\\_SB.PCI.ISA.EC", /* 570 */
358 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
359 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
360 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
361 "\\_SB.PCI0.ICH3.EC0", /* R31 */
362 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300363 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200365TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
366TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200368TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
369 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400370 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
371 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300372 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400373
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200374TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400375 "^HKEY", /* R30, R31 */
376 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400378
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200379TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
380 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
381 "\\_SB.PCI0.VID0", /* 770e */
382 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
383 "\\_SB.PCI0.AGP.VID", /* all others */
384 ); /* R30, R31 */
385
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400386
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387/*************************************************************************
388 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200389 */
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static int acpi_evalf(acpi_handle handle,
392 void *res, char *method, char *fmt, ...)
393{
394 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200396 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400397 struct acpi_buffer result, *resultp;
398 union acpi_object out_obj;
399 acpi_status status;
400 va_list ap;
401 char res_type;
402 int success;
403 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200406 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408 }
409
410 if (*fmt == 'q') {
411 quiet = 1;
412 fmt++;
413 } else
414 quiet = 0;
415
416 res_type = *(fmt++);
417
418 params.count = 0;
419 params.pointer = &in_objs[0];
420
421 va_start(ap, fmt);
422 while (*fmt) {
423 char c = *(fmt++);
424 switch (c) {
425 case 'd': /* int */
426 in_objs[params.count].integer.value = va_arg(ap, int);
427 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
428 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400429 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200431 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 "with invalid format character '%c'\n", c);
433 return 0;
434 }
435 }
436 va_end(ap);
437
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400438 if (res_type != 'v') {
439 result.length = sizeof(out_obj);
440 result.pointer = &out_obj;
441 resultp = &result;
442 } else
443 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400445 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400448 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (res)
450 *(int *)res = out_obj.integer.value;
451 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
452 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 success = status == AE_OK;
455 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400456 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200458 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 "with invalid format character '%c'\n", res_type);
460 return 0;
461 }
462
463 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200464 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 method, fmt0, status);
466
467 return success;
468}
469
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200470static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300471{
472 int v;
473
474 if (ecrd_handle) {
475 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
476 return 0;
477 *p = v;
478 } else {
479 if (ec_read(i, p) < 0)
480 return 0;
481 }
482
483 return 1;
484}
485
486static int acpi_ec_write(int i, u8 v)
487{
488 if (ecwr_handle) {
489 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
490 return 0;
491 } else {
492 if (ec_write(i, v) < 0)
493 return 0;
494 }
495
496 return 1;
497}
498
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200499#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300500static int _sta(acpi_handle handle)
501{
502 int status;
503
504 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
505 status = 0;
506
507 return status;
508}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200509#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300511static int issue_thinkpad_cmos_command(int cmos_cmd)
512{
513 if (!cmos_handle)
514 return -ENXIO;
515
516 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
517 return -EIO;
518
519 return 0;
520}
521
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300522/*************************************************************************
523 * ACPI device model
524 */
525
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200526#define TPACPI_ACPIHANDLE_INIT(object) \
527 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200528 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
529
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300530static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300531 acpi_handle *handle, acpi_handle parent,
532 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533{
534 int i;
535 acpi_status status;
536
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300537 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
538 name);
539
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540 for (i = 0; i < num_paths; i++) {
541 status = acpi_get_handle(parent, paths[i], handle);
542 if (ACPI_SUCCESS(status)) {
543 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300544 dbg_printk(TPACPI_DBG_INIT,
545 "Found ACPI handle %s for %s\n",
546 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 return;
548 }
549 }
550
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300551 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
552 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300553 *handle = NULL;
554}
555
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300556static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300557{
558 struct ibm_struct *ibm = data;
559
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300560 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
561 return;
562
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300563 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564 return;
565
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300566 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300567}
568
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300569static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570{
571 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300572 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300573
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300574 BUG_ON(!ibm->acpi);
575
576 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577 return 0;
578
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300579 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300580 "setting up ACPI notify for %s\n", ibm->name);
581
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300582 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
583 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200584 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300585 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586 return -ENODEV;
587 }
588
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700589 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300590 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200591 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300592 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 status = acpi_install_notify_handler(*ibm->acpi->handle,
595 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300596 if (ACPI_FAILURE(status)) {
597 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200598 printk(TPACPI_NOTICE
599 "another device driver is already "
600 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300601 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200602 printk(TPACPI_ERR
603 "acpi_install_notify_handler(%s) failed: %d\n",
604 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300605 }
606 return -ENODEV;
607 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300608 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300609 return 0;
610}
611
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300612static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300613{
614 return 0;
615}
616
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300617static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300618{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300619 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300620
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300621 dbg_printk(TPACPI_DBG_INIT,
622 "registering %s as an ACPI driver\n", ibm->name);
623
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300624 BUG_ON(!ibm->acpi);
625
626 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
627 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300628 printk(TPACPI_ERR
629 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300630 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300631 }
632
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200633 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300634 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200635
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300636 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300637
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300638 rc = acpi_bus_register_driver(ibm->acpi->driver);
639 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200640 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200641 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300642 kfree(ibm->acpi->driver);
643 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300644 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300645 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300646
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300647 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648}
649
650
651/****************************************************************************
652 ****************************************************************************
653 *
654 * Procfs Helpers
655 *
656 ****************************************************************************
657 ****************************************************************************/
658
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300659static int dispatch_procfs_read(char *page, char **start, off_t off,
660 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661{
662 struct ibm_struct *ibm = data;
663 int len;
664
665 if (!ibm || !ibm->read)
666 return -EINVAL;
667
668 len = ibm->read(page);
669 if (len < 0)
670 return len;
671
672 if (len <= off + count)
673 *eof = 1;
674 *start = page + off;
675 len -= off;
676 if (len > count)
677 len = count;
678 if (len < 0)
679 len = 0;
680
681 return len;
682}
683
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300684static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200685 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300686 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687{
688 struct ibm_struct *ibm = data;
689 char *kernbuf;
690 int ret;
691
692 if (!ibm || !ibm->write)
693 return -EINVAL;
694
695 kernbuf = kmalloc(count + 2, GFP_KERNEL);
696 if (!kernbuf)
697 return -ENOMEM;
698
699 if (copy_from_user(kernbuf, userbuf, count)) {
700 kfree(kernbuf);
701 return -EFAULT;
702 }
703
704 kernbuf[count] = 0;
705 strcat(kernbuf, ",");
706 ret = ibm->write(kernbuf);
707 if (ret == 0)
708 ret = count;
709
710 kfree(kernbuf);
711
712 return ret;
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static char *next_cmd(char **cmds)
716{
717 char *start = *cmds;
718 char *end;
719
720 while ((end = strchr(start, ',')) && end == start)
721 start = end + 1;
722
723 if (!end)
724 return NULL;
725
726 *end = 0;
727 *cmds = end + 1;
728 return start;
729}
730
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300731
732/****************************************************************************
733 ****************************************************************************
734 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300735 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300736 *
737 ****************************************************************************
738 ****************************************************************************/
739
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300740static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300741static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700742static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300743static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300744static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200745static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300746
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200747static int tpacpi_suspend_handler(struct platform_device *pdev,
748 pm_message_t state)
749{
750 struct ibm_struct *ibm, *itmp;
751
752 list_for_each_entry_safe(ibm, itmp,
753 &tpacpi_all_drivers,
754 all_drivers) {
755 if (ibm->suspend)
756 (ibm->suspend)(state);
757 }
758
759 return 0;
760}
761
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300762static int tpacpi_resume_handler(struct platform_device *pdev)
763{
764 struct ibm_struct *ibm, *itmp;
765
766 list_for_each_entry_safe(ibm, itmp,
767 &tpacpi_all_drivers,
768 all_drivers) {
769 if (ibm->resume)
770 (ibm->resume)();
771 }
772
773 return 0;
774}
775
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200776static void tpacpi_shutdown_handler(struct platform_device *pdev)
777{
778 struct ibm_struct *ibm, *itmp;
779
780 list_for_each_entry_safe(ibm, itmp,
781 &tpacpi_all_drivers,
782 all_drivers) {
783 if (ibm->shutdown)
784 (ibm->shutdown)();
785 }
786}
787
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300788static struct platform_driver tpacpi_pdriver = {
789 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200790 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300791 .owner = THIS_MODULE,
792 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200793 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300794 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200795 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300796};
797
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300798static struct platform_driver tpacpi_hwmon_pdriver = {
799 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200800 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300801 .owner = THIS_MODULE,
802 },
803};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300804
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300805/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300806 * sysfs support helpers
807 */
808
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200809struct attribute_set {
810 unsigned int members, max_members;
811 struct attribute_group group;
812};
813
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300814struct attribute_set_obj {
815 struct attribute_set s;
816 struct attribute *a;
817} __attribute__((packed));
818
819static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200820 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300821{
822 struct attribute_set_obj *sobj;
823
824 if (max_members == 0)
825 return NULL;
826
827 /* Allocates space for implicit NULL at the end too */
828 sobj = kzalloc(sizeof(struct attribute_set_obj) +
829 max_members * sizeof(struct attribute *),
830 GFP_KERNEL);
831 if (!sobj)
832 return NULL;
833 sobj->s.max_members = max_members;
834 sobj->s.group.attrs = &sobj->a;
835 sobj->s.group.name = name;
836
837 return &sobj->s;
838}
839
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200840#define destroy_attr_set(_set) \
841 kfree(_set);
842
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300843/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200844static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300845{
846 if (!s || !attr)
847 return -EINVAL;
848
849 if (s->members >= s->max_members)
850 return -ENOMEM;
851
852 s->group.attrs[s->members] = attr;
853 s->members++;
854
855 return 0;
856}
857
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200858static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300859 struct attribute **attr,
860 unsigned int count)
861{
862 int i, res;
863
864 for (i = 0; i < count; i++) {
865 res = add_to_attr_set(s, attr[i]);
866 if (res)
867 return res;
868 }
869
870 return 0;
871}
872
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200873static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300874{
875 sysfs_remove_group(kobj, &s->group);
876 destroy_attr_set(s);
877}
878
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200879#define register_attr_set_with_sysfs(_attr_set, _kobj) \
880 sysfs_create_group(_kobj, &_attr_set->group)
881
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300882static int parse_strtoul(const char *buf,
883 unsigned long max, unsigned long *value)
884{
885 char *endp;
886
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300887 while (*buf && isspace(*buf))
888 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300889 *value = simple_strtoul(buf, &endp, 0);
890 while (*endp && isspace(*endp))
891 endp++;
892 if (*endp || *value > max)
893 return -EINVAL;
894
895 return 0;
896}
897
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300898static void tpacpi_disable_brightness_delay(void)
899{
900 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
901 printk(TPACPI_NOTICE
902 "ACPI backlight control delay disabled\n");
903}
904
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300905static int __init tpacpi_query_bcl_levels(acpi_handle handle)
906{
907 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
908 union acpi_object *obj;
909 int rc;
910
911 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
912 obj = (union acpi_object *)buffer.pointer;
913 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
914 printk(TPACPI_ERR "Unknown _BCL data, "
915 "please report this to %s\n", TPACPI_MAIL);
916 rc = 0;
917 } else {
918 rc = obj->package.count;
919 }
920 } else {
921 return 0;
922 }
923
924 kfree(buffer.pointer);
925 return rc;
926}
927
928static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
929 u32 lvl, void *context, void **rv)
930{
931 char name[ACPI_PATH_SEGMENT_LENGTH];
932 struct acpi_buffer buffer = { sizeof(name), &name };
933
934 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
935 !strncmp("_BCL", name, sizeof(name) - 1)) {
936 BUG_ON(!rv || !*rv);
937 **(int **)rv = tpacpi_query_bcl_levels(handle);
938 return AE_CTRL_TERMINATE;
939 } else {
940 return AE_OK;
941 }
942}
943
944/*
945 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
946 */
947static int __init tpacpi_check_std_acpi_brightness_support(void)
948{
949 int status;
950 int bcl_levels = 0;
951 void *bcl_ptr = &bcl_levels;
952
953 if (!vid_handle) {
954 TPACPI_ACPIHANDLE_INIT(vid);
955 }
956 if (!vid_handle)
957 return 0;
958
959 /*
960 * Search for a _BCL method, and execute it. This is safe on all
961 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
962 * BIOS in ACPI backlight control mode. We do NOT have to care
963 * about calling the _BCL method in an enabled video device, any
964 * will do for our purposes.
965 */
966
967 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
968 tpacpi_acpi_walk_find_bcl, NULL,
969 &bcl_ptr);
970
971 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
972 tp_features.bright_acpimode = 1;
973 return (bcl_levels - 2);
974 }
975
976 return 0;
977}
978
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300979static int __init tpacpi_new_rfkill(const unsigned int id,
980 struct rfkill **rfk,
981 const enum rfkill_type rfktype,
982 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200983 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300984 int (*toggle_radio)(void *, enum rfkill_state),
985 int (*get_state)(void *, enum rfkill_state *))
986{
987 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200988 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
989
990 res = get_state(NULL, &initial_state);
991 if (res < 0) {
992 printk(TPACPI_ERR
993 "failed to read initial state for %s, error %d; "
994 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200995 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200996 /* try to set the initial state as the default for the rfkill
997 * type, since we ask the firmware to preserve it across S5 in
998 * NVRAM */
999 rfkill_set_default(rfktype,
1000 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1001 RFKILL_STATE_UNBLOCKED :
1002 RFKILL_STATE_SOFT_BLOCKED);
1003 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001004
1005 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1006 if (!*rfk) {
1007 printk(TPACPI_ERR
1008 "failed to allocate memory for rfkill class\n");
1009 return -ENOMEM;
1010 }
1011
1012 (*rfk)->name = name;
1013 (*rfk)->get_state = get_state;
1014 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001015 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001016
1017 res = rfkill_register(*rfk);
1018 if (res < 0) {
1019 printk(TPACPI_ERR
1020 "failed to register %s rfkill switch: %d\n",
1021 name, res);
1022 rfkill_free(*rfk);
1023 *rfk = NULL;
1024 return res;
1025 }
1026
1027 return 0;
1028}
1029
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001030/*************************************************************************
1031 * thinkpad-acpi driver attributes
1032 */
1033
1034/* interface_version --------------------------------------------------- */
1035static ssize_t tpacpi_driver_interface_version_show(
1036 struct device_driver *drv,
1037 char *buf)
1038{
1039 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1040}
1041
1042static DRIVER_ATTR(interface_version, S_IRUGO,
1043 tpacpi_driver_interface_version_show, NULL);
1044
1045/* debug_level --------------------------------------------------------- */
1046static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1047 char *buf)
1048{
1049 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1050}
1051
1052static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1053 const char *buf, size_t count)
1054{
1055 unsigned long t;
1056
1057 if (parse_strtoul(buf, 0xffff, &t))
1058 return -EINVAL;
1059
1060 dbg_level = t;
1061
1062 return count;
1063}
1064
1065static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1066 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1067
1068/* version ------------------------------------------------------------- */
1069static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1070 char *buf)
1071{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001072 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1073 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001074}
1075
1076static DRIVER_ATTR(version, S_IRUGO,
1077 tpacpi_driver_version_show, NULL);
1078
1079/* --------------------------------------------------------------------- */
1080
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001081#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1082
1083static void tpacpi_send_radiosw_update(void);
1084
1085/* wlsw_emulstate ------------------------------------------------------ */
1086static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1087 char *buf)
1088{
1089 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1090}
1091
1092static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1093 const char *buf, size_t count)
1094{
1095 unsigned long t;
1096
1097 if (parse_strtoul(buf, 1, &t))
1098 return -EINVAL;
1099
1100 if (tpacpi_wlsw_emulstate != t) {
1101 tpacpi_wlsw_emulstate = !!t;
1102 tpacpi_send_radiosw_update();
1103 } else
1104 tpacpi_wlsw_emulstate = !!t;
1105
1106 return count;
1107}
1108
1109static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1110 tpacpi_driver_wlsw_emulstate_show,
1111 tpacpi_driver_wlsw_emulstate_store);
1112
1113/* bluetooth_emulstate ------------------------------------------------- */
1114static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1115 struct device_driver *drv,
1116 char *buf)
1117{
1118 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1119}
1120
1121static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1122 struct device_driver *drv,
1123 const char *buf, size_t count)
1124{
1125 unsigned long t;
1126
1127 if (parse_strtoul(buf, 1, &t))
1128 return -EINVAL;
1129
1130 tpacpi_bluetooth_emulstate = !!t;
1131
1132 return count;
1133}
1134
1135static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1136 tpacpi_driver_bluetooth_emulstate_show,
1137 tpacpi_driver_bluetooth_emulstate_store);
1138
1139/* wwan_emulstate ------------------------------------------------- */
1140static ssize_t tpacpi_driver_wwan_emulstate_show(
1141 struct device_driver *drv,
1142 char *buf)
1143{
1144 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1145}
1146
1147static ssize_t tpacpi_driver_wwan_emulstate_store(
1148 struct device_driver *drv,
1149 const char *buf, size_t count)
1150{
1151 unsigned long t;
1152
1153 if (parse_strtoul(buf, 1, &t))
1154 return -EINVAL;
1155
1156 tpacpi_wwan_emulstate = !!t;
1157
1158 return count;
1159}
1160
1161static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1162 tpacpi_driver_wwan_emulstate_show,
1163 tpacpi_driver_wwan_emulstate_store);
1164
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001165/* uwb_emulstate ------------------------------------------------- */
1166static ssize_t tpacpi_driver_uwb_emulstate_show(
1167 struct device_driver *drv,
1168 char *buf)
1169{
1170 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1171}
1172
1173static ssize_t tpacpi_driver_uwb_emulstate_store(
1174 struct device_driver *drv,
1175 const char *buf, size_t count)
1176{
1177 unsigned long t;
1178
1179 if (parse_strtoul(buf, 1, &t))
1180 return -EINVAL;
1181
1182 tpacpi_uwb_emulstate = !!t;
1183
1184 return count;
1185}
1186
1187static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1188 tpacpi_driver_uwb_emulstate_show,
1189 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001190#endif
1191
1192/* --------------------------------------------------------------------- */
1193
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001194static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001195 &driver_attr_debug_level, &driver_attr_version,
1196 &driver_attr_interface_version,
1197};
1198
1199static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1200{
1201 int i, res;
1202
1203 i = 0;
1204 res = 0;
1205 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1206 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1207 i++;
1208 }
1209
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001210#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1211 if (!res && dbg_wlswemul)
1212 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1213 if (!res && dbg_bluetoothemul)
1214 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1215 if (!res && dbg_wwanemul)
1216 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001217 if (!res && dbg_uwbemul)
1218 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001219#endif
1220
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001221 return res;
1222}
1223
1224static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1225{
1226 int i;
1227
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001228 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001229 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001230
1231#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1232 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1233 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1234 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001235 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001236#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001237}
1238
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001239/****************************************************************************
1240 ****************************************************************************
1241 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001242 * Subdrivers
1243 *
1244 ****************************************************************************
1245 ****************************************************************************/
1246
1247/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001248 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001249 */
1250
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001251static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001253 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1254 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001256 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001257 (thinkpad_id.bios_version_str) ?
1258 thinkpad_id.bios_version_str : "unknown",
1259 (thinkpad_id.ec_version_str) ?
1260 thinkpad_id.ec_version_str : "unknown");
1261
1262 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001263 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001264 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1265 "IBM" : ((thinkpad_id.vendor ==
1266 PCI_VENDOR_ID_LENOVO) ?
1267 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001268 thinkpad_id.model_str,
1269 (thinkpad_id.nummodel_str) ?
1270 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 0;
1273}
1274
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001275static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int len = 0;
1278
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001279 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1280 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 return len;
1283}
1284
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001285static struct ibm_struct thinkpad_acpi_driver_data = {
1286 .name = "driver",
1287 .read = thinkpad_acpi_driver_read,
1288};
1289
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001290/*************************************************************************
1291 * Hotkey subdriver
1292 */
1293
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001294enum { /* hot key scan codes (derived from ACPI DSDT) */
1295 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1296 TP_ACPI_HOTKEYSCAN_FNF2,
1297 TP_ACPI_HOTKEYSCAN_FNF3,
1298 TP_ACPI_HOTKEYSCAN_FNF4,
1299 TP_ACPI_HOTKEYSCAN_FNF5,
1300 TP_ACPI_HOTKEYSCAN_FNF6,
1301 TP_ACPI_HOTKEYSCAN_FNF7,
1302 TP_ACPI_HOTKEYSCAN_FNF8,
1303 TP_ACPI_HOTKEYSCAN_FNF9,
1304 TP_ACPI_HOTKEYSCAN_FNF10,
1305 TP_ACPI_HOTKEYSCAN_FNF11,
1306 TP_ACPI_HOTKEYSCAN_FNF12,
1307 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1308 TP_ACPI_HOTKEYSCAN_FNINSERT,
1309 TP_ACPI_HOTKEYSCAN_FNDELETE,
1310 TP_ACPI_HOTKEYSCAN_FNHOME,
1311 TP_ACPI_HOTKEYSCAN_FNEND,
1312 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1313 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1314 TP_ACPI_HOTKEYSCAN_FNSPACE,
1315 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1316 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1317 TP_ACPI_HOTKEYSCAN_MUTE,
1318 TP_ACPI_HOTKEYSCAN_THINKPAD,
1319};
1320
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001321enum { /* Keys available through NVRAM polling */
1322 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1323 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1324};
1325
1326enum { /* Positions of some of the keys in hotkey masks */
1327 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1328 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1329 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1330 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1331 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1332 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1333 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1334 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1335 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1336 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1337 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1338};
1339
1340enum { /* NVRAM to ACPI HKEY group map */
1341 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1342 TP_ACPI_HKEY_ZOOM_MASK |
1343 TP_ACPI_HKEY_DISPSWTCH_MASK |
1344 TP_ACPI_HKEY_HIBERNATE_MASK,
1345 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1346 TP_ACPI_HKEY_BRGHTDWN_MASK,
1347 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1348 TP_ACPI_HKEY_VOLDWN_MASK |
1349 TP_ACPI_HKEY_MUTE_MASK,
1350};
1351
1352#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1353struct tp_nvram_state {
1354 u16 thinkpad_toggle:1;
1355 u16 zoom_toggle:1;
1356 u16 display_toggle:1;
1357 u16 thinklight_toggle:1;
1358 u16 hibernate_toggle:1;
1359 u16 displayexp_toggle:1;
1360 u16 display_state:1;
1361 u16 brightness_toggle:1;
1362 u16 volume_toggle:1;
1363 u16 mute:1;
1364
1365 u8 brightness_level;
1366 u8 volume_level;
1367};
1368
1369static struct task_struct *tpacpi_hotkey_task;
1370static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1371static int hotkey_poll_freq = 10; /* Hz */
1372static struct mutex hotkey_thread_mutex;
1373static struct mutex hotkey_thread_data_mutex;
1374static unsigned int hotkey_config_change;
1375
1376#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1377
1378#define hotkey_source_mask 0U
1379
1380#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1381
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001382static struct mutex hotkey_mutex;
1383
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001384static enum { /* Reasons for waking up */
1385 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1386 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1387 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1388} hotkey_wakeup_reason;
1389
1390static int hotkey_autosleep_ack;
1391
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001393static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001394static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001395static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001396static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001398static unsigned int hotkey_report_mode;
1399
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001400static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001401
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001402static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001403
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001404#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1405#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001406 do { \
1407 mutex_lock(&hotkey_thread_data_mutex); \
1408 hotkey_config_change++; \
1409 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001410#define HOTKEY_CONFIG_CRITICAL_END \
1411 mutex_unlock(&hotkey_thread_data_mutex);
1412#else
1413#define HOTKEY_CONFIG_CRITICAL_START
1414#define HOTKEY_CONFIG_CRITICAL_END
1415#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1416
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001417/* HKEY.MHKG() return bits */
1418#define TP_HOTKEY_TABLET_MASK (1 << 3)
1419
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001420static int hotkey_get_wlsw(int *status)
1421{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001422#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1423 if (dbg_wlswemul) {
1424 *status = !!tpacpi_wlsw_emulstate;
1425 return 0;
1426 }
1427#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001428 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1429 return -EIO;
1430 return 0;
1431}
1432
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001433static int hotkey_get_tablet_mode(int *status)
1434{
1435 int s;
1436
1437 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1438 return -EIO;
1439
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001440 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1441 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001442}
1443
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001444/*
1445 * Call with hotkey_mutex held
1446 */
1447static int hotkey_mask_get(void)
1448{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001449 u32 m = 0;
1450
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001451 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001452 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001453 return -EIO;
1454 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001455 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001456
1457 return 0;
1458}
1459
1460/*
1461 * Call with hotkey_mutex held
1462 */
1463static int hotkey_mask_set(u32 mask)
1464{
1465 int i;
1466 int rc = 0;
1467
1468 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001469 if (!tp_warned.hotkey_mask_ff &&
1470 (mask == 0xffff || mask == 0xffffff ||
1471 mask == 0xffffffff)) {
1472 tp_warned.hotkey_mask_ff = 1;
1473 printk(TPACPI_NOTICE
1474 "setting the hotkey mask to 0x%08x is likely "
1475 "not the best way to go about it\n", mask);
1476 printk(TPACPI_NOTICE
1477 "please consider using the driver defaults, "
1478 "and refer to up-to-date thinkpad-acpi "
1479 "documentation\n");
1480 }
1481
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001482 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001483 for (i = 0; i < 32; i++) {
1484 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001485 /* enable in firmware mask only keys not in NVRAM
1486 * mode, but enable the key in the cached hotkey_mask
1487 * regardless of mode, or the key will end up
1488 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001489 if (!acpi_evalf(hkey_handle,
1490 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001491 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001492 rc = -EIO;
1493 break;
1494 } else {
1495 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1496 }
1497 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001498 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001499
1500 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001501 if (!hotkey_mask_get() && !rc &&
1502 (hotkey_mask & ~hotkey_source_mask) !=
1503 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001504 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001505 "requested hot key mask 0x%08x, but "
1506 "firmware forced it to 0x%08x\n",
1507 mask, hotkey_mask);
1508 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001509 } else {
1510#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1511 HOTKEY_CONFIG_CRITICAL_START
1512 hotkey_mask = mask & hotkey_source_mask;
1513 HOTKEY_CONFIG_CRITICAL_END
1514 hotkey_mask_get();
1515 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001516 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001517 "requested hot key mask 0x%08x, "
1518 "forced to 0x%08x (NVRAM poll mask is "
1519 "0x%08x): no firmware mask support\n",
1520 mask, hotkey_mask, hotkey_source_mask);
1521 }
1522#else
1523 hotkey_mask_get();
1524 rc = -ENXIO;
1525#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001526 }
1527
1528 return rc;
1529}
1530
1531static int hotkey_status_get(int *status)
1532{
1533 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1534 return -EIO;
1535
1536 return 0;
1537}
1538
1539static int hotkey_status_set(int status)
1540{
1541 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1542 return -EIO;
1543
1544 return 0;
1545}
1546
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001547static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001548{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001549 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001550
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001551 if (tp_features.hotkey_tablet &&
1552 !hotkey_get_tablet_mode(&state)) {
1553 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001554
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001555 input_report_switch(tpacpi_inputdev,
1556 SW_TABLET_MODE, !!state);
1557 input_sync(tpacpi_inputdev);
1558
1559 mutex_unlock(&tpacpi_inputdev_send_mutex);
1560 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001561}
1562
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001563static void tpacpi_input_send_key(unsigned int scancode)
1564{
1565 unsigned int keycode;
1566
1567 keycode = hotkey_keycode_map[scancode];
1568
1569 if (keycode != KEY_RESERVED) {
1570 mutex_lock(&tpacpi_inputdev_send_mutex);
1571
1572 input_report_key(tpacpi_inputdev, keycode, 1);
1573 if (keycode == KEY_UNKNOWN)
1574 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1575 scancode);
1576 input_sync(tpacpi_inputdev);
1577
1578 input_report_key(tpacpi_inputdev, keycode, 0);
1579 if (keycode == KEY_UNKNOWN)
1580 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1581 scancode);
1582 input_sync(tpacpi_inputdev);
1583
1584 mutex_unlock(&tpacpi_inputdev_send_mutex);
1585 }
1586}
1587
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001588#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1589static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1590
1591static void tpacpi_hotkey_send_key(unsigned int scancode)
1592{
1593 tpacpi_input_send_key(scancode);
1594 if (hotkey_report_mode < 2) {
1595 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1596 0x80, 0x1001 + scancode);
1597 }
1598}
1599
1600static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1601{
1602 u8 d;
1603
1604 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1605 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1606 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1607 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1608 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1609 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1610 }
1611 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1612 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1613 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1614 }
1615 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1616 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1617 n->displayexp_toggle =
1618 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1619 }
1620 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1621 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1622 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1623 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1624 n->brightness_toggle =
1625 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1626 }
1627 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1628 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1629 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1630 >> TP_NVRAM_POS_LEVEL_VOLUME;
1631 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1632 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1633 }
1634}
1635
1636#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001637 do { \
1638 if ((mask & (1 << __scancode)) && \
1639 oldn->__member != newn->__member) \
1640 tpacpi_hotkey_send_key(__scancode); \
1641 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001642
1643#define TPACPI_MAY_SEND_KEY(__scancode) \
1644 do { if (mask & (1 << __scancode)) \
1645 tpacpi_hotkey_send_key(__scancode); } while (0)
1646
1647static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001648 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001649 u32 mask)
1650{
1651 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1652 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1653 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1654 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1655
1656 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1657
1658 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1659
1660 /* handle volume */
1661 if (oldn->volume_toggle != newn->volume_toggle) {
1662 if (oldn->mute != newn->mute) {
1663 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1664 }
1665 if (oldn->volume_level > newn->volume_level) {
1666 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1667 } else if (oldn->volume_level < newn->volume_level) {
1668 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1669 } else if (oldn->mute == newn->mute) {
1670 /* repeated key presses that didn't change state */
1671 if (newn->mute) {
1672 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1673 } else if (newn->volume_level != 0) {
1674 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1675 } else {
1676 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1677 }
1678 }
1679 }
1680
1681 /* handle brightness */
1682 if (oldn->brightness_toggle != newn->brightness_toggle) {
1683 if (oldn->brightness_level < newn->brightness_level) {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1685 } else if (oldn->brightness_level > newn->brightness_level) {
1686 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1687 } else {
1688 /* repeated key presses that didn't change state */
1689 if (newn->brightness_level != 0) {
1690 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1691 } else {
1692 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1693 }
1694 }
1695 }
1696}
1697
1698#undef TPACPI_COMPARE_KEY
1699#undef TPACPI_MAY_SEND_KEY
1700
1701static int hotkey_kthread(void *data)
1702{
1703 struct tp_nvram_state s[2];
1704 u32 mask;
1705 unsigned int si, so;
1706 unsigned long t;
1707 unsigned int change_detector, must_reset;
1708
1709 mutex_lock(&hotkey_thread_mutex);
1710
1711 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1712 goto exit;
1713
1714 set_freezable();
1715
1716 so = 0;
1717 si = 1;
1718 t = 0;
1719
1720 /* Initial state for compares */
1721 mutex_lock(&hotkey_thread_data_mutex);
1722 change_detector = hotkey_config_change;
1723 mask = hotkey_source_mask & hotkey_mask;
1724 mutex_unlock(&hotkey_thread_data_mutex);
1725 hotkey_read_nvram(&s[so], mask);
1726
1727 while (!kthread_should_stop() && hotkey_poll_freq) {
1728 if (t == 0)
1729 t = 1000/hotkey_poll_freq;
1730 t = msleep_interruptible(t);
1731 if (unlikely(kthread_should_stop()))
1732 break;
1733 must_reset = try_to_freeze();
1734 if (t > 0 && !must_reset)
1735 continue;
1736
1737 mutex_lock(&hotkey_thread_data_mutex);
1738 if (must_reset || hotkey_config_change != change_detector) {
1739 /* forget old state on thaw or config change */
1740 si = so;
1741 t = 0;
1742 change_detector = hotkey_config_change;
1743 }
1744 mask = hotkey_source_mask & hotkey_mask;
1745 mutex_unlock(&hotkey_thread_data_mutex);
1746
1747 if (likely(mask)) {
1748 hotkey_read_nvram(&s[si], mask);
1749 if (likely(si != so)) {
1750 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001751 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001752 }
1753 }
1754
1755 so = si;
1756 si ^= 1;
1757 }
1758
1759exit:
1760 mutex_unlock(&hotkey_thread_mutex);
1761 return 0;
1762}
1763
1764static void hotkey_poll_stop_sync(void)
1765{
1766 if (tpacpi_hotkey_task) {
1767 if (frozen(tpacpi_hotkey_task) ||
1768 freezing(tpacpi_hotkey_task))
1769 thaw_process(tpacpi_hotkey_task);
1770
1771 kthread_stop(tpacpi_hotkey_task);
1772 tpacpi_hotkey_task = NULL;
1773 mutex_lock(&hotkey_thread_mutex);
1774 /* at this point, the thread did exit */
1775 mutex_unlock(&hotkey_thread_mutex);
1776 }
1777}
1778
1779/* call with hotkey_mutex held */
1780static void hotkey_poll_setup(int may_warn)
1781{
1782 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1783 hotkey_poll_freq > 0 &&
1784 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1785 if (!tpacpi_hotkey_task) {
1786 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001787 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001788 if (IS_ERR(tpacpi_hotkey_task)) {
1789 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001790 printk(TPACPI_ERR
1791 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001792 "for hotkey polling\n");
1793 }
1794 }
1795 } else {
1796 hotkey_poll_stop_sync();
1797 if (may_warn &&
1798 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001799 printk(TPACPI_NOTICE
1800 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001801 "which is currently disabled\n",
1802 hotkey_source_mask);
1803 }
1804 }
1805}
1806
1807static void hotkey_poll_setup_safe(int may_warn)
1808{
1809 mutex_lock(&hotkey_mutex);
1810 hotkey_poll_setup(may_warn);
1811 mutex_unlock(&hotkey_mutex);
1812}
1813
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001814#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1815
1816static void hotkey_poll_setup_safe(int __unused)
1817{
1818}
1819
1820#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1821
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001822static int hotkey_inputdev_open(struct input_dev *dev)
1823{
1824 switch (tpacpi_lifecycle) {
1825 case TPACPI_LIFE_INIT:
1826 /*
1827 * hotkey_init will call hotkey_poll_setup_safe
1828 * at the appropriate moment
1829 */
1830 return 0;
1831 case TPACPI_LIFE_EXITING:
1832 return -EBUSY;
1833 case TPACPI_LIFE_RUNNING:
1834 hotkey_poll_setup_safe(0);
1835 return 0;
1836 }
1837
1838 /* Should only happen if tpacpi_lifecycle is corrupt */
1839 BUG();
1840 return -EBUSY;
1841}
1842
1843static void hotkey_inputdev_close(struct input_dev *dev)
1844{
1845 /* disable hotkey polling when possible */
1846 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1847 hotkey_poll_setup_safe(0);
1848}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001849
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001850/* sysfs hotkey enable ------------------------------------------------- */
1851static ssize_t hotkey_enable_show(struct device *dev,
1852 struct device_attribute *attr,
1853 char *buf)
1854{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001855 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001856
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001857 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001858 if (res)
1859 return res;
1860
1861 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1862}
1863
1864static ssize_t hotkey_enable_store(struct device *dev,
1865 struct device_attribute *attr,
1866 const char *buf, size_t count)
1867{
1868 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001869 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001870
1871 if (parse_strtoul(buf, 1, &t))
1872 return -EINVAL;
1873
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001874 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001875
1876 return (res) ? res : count;
1877}
1878
1879static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001880 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001881 hotkey_enable_show, hotkey_enable_store);
1882
1883/* sysfs hotkey mask --------------------------------------------------- */
1884static ssize_t hotkey_mask_show(struct device *dev,
1885 struct device_attribute *attr,
1886 char *buf)
1887{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001888 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001889
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001890 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001891 return -ERESTARTSYS;
1892 res = hotkey_mask_get();
1893 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001894
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001895 return (res)?
1896 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001897}
1898
1899static ssize_t hotkey_mask_store(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t count)
1902{
1903 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001904 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001905
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001906 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001907 return -EINVAL;
1908
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001909 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001910 return -ERESTARTSYS;
1911
1912 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001913
1914#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 hotkey_poll_setup(1);
1916#endif
1917
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001918 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001919
1920 return (res) ? res : count;
1921}
1922
1923static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001925 hotkey_mask_show, hotkey_mask_store);
1926
1927/* sysfs hotkey bios_enabled ------------------------------------------- */
1928static ssize_t hotkey_bios_enabled_show(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1931{
1932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1933}
1934
1935static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001937
1938/* sysfs hotkey bios_mask ---------------------------------------------- */
1939static ssize_t hotkey_bios_mask_show(struct device *dev,
1940 struct device_attribute *attr,
1941 char *buf)
1942{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001944}
1945
1946static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001948
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001949/* sysfs hotkey all_mask ----------------------------------------------- */
1950static ssize_t hotkey_all_mask_show(struct device *dev,
1951 struct device_attribute *attr,
1952 char *buf)
1953{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001954 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1955 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001956}
1957
1958static struct device_attribute dev_attr_hotkey_all_mask =
1959 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1960
1961/* sysfs hotkey recommended_mask --------------------------------------- */
1962static ssize_t hotkey_recommended_mask_show(struct device *dev,
1963 struct device_attribute *attr,
1964 char *buf)
1965{
1966 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001967 (hotkey_all_mask | hotkey_source_mask)
1968 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001969}
1970
1971static struct device_attribute dev_attr_hotkey_recommended_mask =
1972 __ATTR(hotkey_recommended_mask, S_IRUGO,
1973 hotkey_recommended_mask_show, NULL);
1974
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001975#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1976
1977/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1978static ssize_t hotkey_source_mask_show(struct device *dev,
1979 struct device_attribute *attr,
1980 char *buf)
1981{
1982 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1983}
1984
1985static ssize_t hotkey_source_mask_store(struct device *dev,
1986 struct device_attribute *attr,
1987 const char *buf, size_t count)
1988{
1989 unsigned long t;
1990
1991 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1992 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1993 return -EINVAL;
1994
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02001995 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001996 return -ERESTARTSYS;
1997
1998 HOTKEY_CONFIG_CRITICAL_START
1999 hotkey_source_mask = t;
2000 HOTKEY_CONFIG_CRITICAL_END
2001
2002 hotkey_poll_setup(1);
2003
2004 mutex_unlock(&hotkey_mutex);
2005
2006 return count;
2007}
2008
2009static struct device_attribute dev_attr_hotkey_source_mask =
2010 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2011 hotkey_source_mask_show, hotkey_source_mask_store);
2012
2013/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2014static ssize_t hotkey_poll_freq_show(struct device *dev,
2015 struct device_attribute *attr,
2016 char *buf)
2017{
2018 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2019}
2020
2021static ssize_t hotkey_poll_freq_store(struct device *dev,
2022 struct device_attribute *attr,
2023 const char *buf, size_t count)
2024{
2025 unsigned long t;
2026
2027 if (parse_strtoul(buf, 25, &t))
2028 return -EINVAL;
2029
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002030 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002031 return -ERESTARTSYS;
2032
2033 hotkey_poll_freq = t;
2034
2035 hotkey_poll_setup(1);
2036 mutex_unlock(&hotkey_mutex);
2037
2038 return count;
2039}
2040
2041static struct device_attribute dev_attr_hotkey_poll_freq =
2042 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2043 hotkey_poll_freq_show, hotkey_poll_freq_store);
2044
2045#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2046
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002047/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002048static ssize_t hotkey_radio_sw_show(struct device *dev,
2049 struct device_attribute *attr,
2050 char *buf)
2051{
2052 int res, s;
2053 res = hotkey_get_wlsw(&s);
2054 if (res < 0)
2055 return res;
2056
2057 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2058}
2059
2060static struct device_attribute dev_attr_hotkey_radio_sw =
2061 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2062
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002063static void hotkey_radio_sw_notify_change(void)
2064{
2065 if (tp_features.hotkey_wlsw)
2066 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2067 "hotkey_radio_sw");
2068}
2069
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002070/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2071static ssize_t hotkey_tablet_mode_show(struct device *dev,
2072 struct device_attribute *attr,
2073 char *buf)
2074{
2075 int res, s;
2076 res = hotkey_get_tablet_mode(&s);
2077 if (res < 0)
2078 return res;
2079
2080 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2081}
2082
2083static struct device_attribute dev_attr_hotkey_tablet_mode =
2084 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2085
2086static void hotkey_tablet_mode_notify_change(void)
2087{
2088 if (tp_features.hotkey_tablet)
2089 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2090 "hotkey_tablet_mode");
2091}
2092
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002093/* sysfs hotkey report_mode -------------------------------------------- */
2094static ssize_t hotkey_report_mode_show(struct device *dev,
2095 struct device_attribute *attr,
2096 char *buf)
2097{
2098 return snprintf(buf, PAGE_SIZE, "%d\n",
2099 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2100}
2101
2102static struct device_attribute dev_attr_hotkey_report_mode =
2103 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2104
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002105/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002106static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2109{
2110 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2111}
2112
2113static struct device_attribute dev_attr_hotkey_wakeup_reason =
2114 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2115
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002116static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002117{
2118 if (tp_features.hotkey_mask)
2119 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2120 "wakeup_reason");
2121}
2122
2123/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002124static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2125 struct device_attribute *attr,
2126 char *buf)
2127{
2128 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2129}
2130
2131static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2132 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2133 hotkey_wakeup_hotunplug_complete_show, NULL);
2134
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002135static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002136{
2137 if (tp_features.hotkey_mask)
2138 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2139 "wakeup_hotunplug_complete");
2140}
2141
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002142/* --------------------------------------------------------------------- */
2143
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002144static struct attribute *hotkey_attributes[] __initdata = {
2145 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002146 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002147 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002148#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2149 &dev_attr_hotkey_mask.attr,
2150 &dev_attr_hotkey_all_mask.attr,
2151 &dev_attr_hotkey_recommended_mask.attr,
2152 &dev_attr_hotkey_source_mask.attr,
2153 &dev_attr_hotkey_poll_freq.attr,
2154#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002155};
2156
2157static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002158 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002159#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2160 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002161 &dev_attr_hotkey_all_mask.attr,
2162 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002163#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002164 &dev_attr_hotkey_wakeup_reason.attr,
2165 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002166};
2167
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002168static void bluetooth_update_rfk(void);
2169static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002170static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002171static void tpacpi_send_radiosw_update(void)
2172{
2173 int wlsw;
2174
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002175 /* Sync these BEFORE sending any rfkill events */
2176 if (tp_features.bluetooth)
2177 bluetooth_update_rfk();
2178 if (tp_features.wan)
2179 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002180 if (tp_features.uwb)
2181 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002182
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002183 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2184 mutex_lock(&tpacpi_inputdev_send_mutex);
2185
2186 input_report_switch(tpacpi_inputdev,
2187 SW_RFKILL_ALL, !!wlsw);
2188 input_sync(tpacpi_inputdev);
2189
2190 mutex_unlock(&tpacpi_inputdev_send_mutex);
2191 }
2192 hotkey_radio_sw_notify_change();
2193}
2194
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002195static void hotkey_exit(void)
2196{
2197#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2198 hotkey_poll_stop_sync();
2199#endif
2200
2201 if (hotkey_dev_attributes)
2202 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2203
2204 kfree(hotkey_keycode_map);
2205
2206 if (tp_features.hotkey) {
2207 dbg_printk(TPACPI_DBG_EXIT,
2208 "restoring original hot key mask\n");
2209 /* no short-circuit boolean operator below! */
2210 if ((hotkey_mask_set(hotkey_orig_mask) |
2211 hotkey_status_set(hotkey_orig_status)) != 0)
2212 printk(TPACPI_ERR
2213 "failed to restore hot key mask "
2214 "to BIOS defaults\n");
2215 }
2216}
2217
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002218static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002219{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002220 /* Requirements for changing the default keymaps:
2221 *
2222 * 1. Many of the keys are mapped to KEY_RESERVED for very
2223 * good reasons. Do not change them unless you have deep
2224 * knowledge on the IBM and Lenovo ThinkPad firmware for
2225 * the various ThinkPad models. The driver behaves
2226 * differently for KEY_RESERVED: such keys have their
2227 * hot key mask *unset* in mask_recommended, and also
2228 * in the initial hot key mask programmed into the
2229 * firmware at driver load time, which means the firm-
2230 * ware may react very differently if you change them to
2231 * something else;
2232 *
2233 * 2. You must be subscribed to the linux-thinkpad and
2234 * ibm-acpi-devel mailing lists, and you should read the
2235 * list archives since 2007 if you want to change the
2236 * keymaps. This requirement exists so that you will
2237 * know the past history of problems with the thinkpad-
2238 * acpi driver keymaps, and also that you will be
2239 * listening to any bug reports;
2240 *
2241 * 3. Do not send thinkpad-acpi specific patches directly to
2242 * for merging, *ever*. Send them to the linux-acpi
2243 * mailinglist for comments. Merging is to be done only
2244 * through acpi-test and the ACPI maintainer.
2245 *
2246 * If the above is too much to ask, don't change the keymap.
2247 * Ask the thinkpad-acpi maintainer to do it, instead.
2248 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002249 static u16 ibm_keycode_map[] __initdata = {
2250 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2251 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2252 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2253 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002254
2255 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002256 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2257 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2258 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002259
2260 /* brightness: firmware always reacts to them, unless
2261 * X.org did some tricks in the radeon BIOS scratch
2262 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002263 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002264 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002265
2266 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002267 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002268
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002269 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2270 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002271
2272 /* Volume: firmware always react to it and reprograms
2273 * the built-in *extra* mixer. Never map it to control
2274 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002275 KEY_RESERVED, /* 0x14: VOLUME UP */
2276 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2277 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002278
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002279 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002280
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002281 /* (assignments unknown, please report if found) */
2282 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2283 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2284 };
2285 static u16 lenovo_keycode_map[] __initdata = {
2286 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2287 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2288 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2289 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002290
2291 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002292 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2293 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2294 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002295
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002296 /* These either have to go through ACPI video, or
2297 * act like in the IBM ThinkPads, so don't ever
2298 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002299 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002300 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002301
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002302 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002303
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002304 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2305 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002306
2307 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2308 * react to it and reprograms the built-in *extra* mixer.
2309 * Never map it to control another mixer by default.
2310 *
2311 * T60?, T61, R60?, R61: firmware and EC tries to send
2312 * these over the regular keyboard, so these are no-ops,
2313 * but there are still weird bugs re. MUTE, so do not
2314 * change unless you get test reports from all Lenovo
2315 * models. May cause the BIOS to interfere with the
2316 * HDA mixer.
2317 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002318 KEY_RESERVED, /* 0x14: VOLUME UP */
2319 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2320 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002321
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002322 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002323
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002324 /* (assignments unknown, please report if found) */
2325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2327 };
2328
2329#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2330#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2331#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2332
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002333 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002334 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002335 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002336
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002337 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2338
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002339 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002340 BUG_ON(tpacpi_inputdev->open != NULL ||
2341 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002342
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002343 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002344 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002345
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002346#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2347 mutex_init(&hotkey_thread_mutex);
2348 mutex_init(&hotkey_thread_data_mutex);
2349#endif
2350
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002351 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002352 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002353
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002354 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002355 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002356
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002357 if (!tp_features.hotkey)
2358 return 1;
2359
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002360 tpacpi_disable_brightness_delay();
2361
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002362 hotkey_dev_attributes = create_attr_set(13, NULL);
2363 if (!hotkey_dev_attributes)
2364 return -ENOMEM;
2365 res = add_many_to_attr_set(hotkey_dev_attributes,
2366 hotkey_attributes,
2367 ARRAY_SIZE(hotkey_attributes));
2368 if (res)
2369 goto err_exit;
2370
2371 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2372 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2373 for HKEY interface version 0x100 */
2374 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2375 if ((hkeyv >> 8) != 1) {
2376 printk(TPACPI_ERR "unknown version of the "
2377 "HKEY interface: 0x%x\n", hkeyv);
2378 printk(TPACPI_ERR "please report this to %s\n",
2379 TPACPI_MAIL);
2380 } else {
2381 /*
2382 * MHKV 0x100 in A31, R40, R40e,
2383 * T4x, X31, and later
2384 */
2385 tp_features.hotkey_mask = 1;
2386 }
2387 }
2388
2389 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2390 str_supported(tp_features.hotkey_mask));
2391
2392 if (tp_features.hotkey_mask) {
2393 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2394 "MHKA", "qd")) {
2395 printk(TPACPI_ERR
2396 "missing MHKA handler, "
2397 "please report this to %s\n",
2398 TPACPI_MAIL);
2399 /* FN+F12, FN+F4, FN+F3 */
2400 hotkey_all_mask = 0x080cU;
2401 }
2402 }
2403
2404 /* hotkey_source_mask *must* be zero for
2405 * the first hotkey_mask_get */
2406 res = hotkey_status_get(&hotkey_orig_status);
2407 if (res)
2408 goto err_exit;
2409
2410 if (tp_features.hotkey_mask) {
2411 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002412 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002413 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002414
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002415 hotkey_orig_mask = hotkey_mask;
2416 res = add_many_to_attr_set(
2417 hotkey_dev_attributes,
2418 hotkey_mask_attributes,
2419 ARRAY_SIZE(hotkey_mask_attributes));
2420 if (res)
2421 goto err_exit;
2422 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002423
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002424#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002425 if (tp_features.hotkey_mask) {
2426 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2427 & ~hotkey_all_mask;
2428 } else {
2429 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2430 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002431
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002432 vdbg_printk(TPACPI_DBG_INIT,
2433 "hotkey source mask 0x%08x, polling freq %d\n",
2434 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002435#endif
2436
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002437#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2438 if (dbg_wlswemul) {
2439 tp_features.hotkey_wlsw = 1;
2440 printk(TPACPI_INFO
2441 "radio switch emulation enabled\n");
2442 } else
2443#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002444 /* Not all thinkpads have a hardware radio switch */
2445 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2446 tp_features.hotkey_wlsw = 1;
2447 printk(TPACPI_INFO
2448 "radio switch found; radios are %s\n",
2449 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002450 }
2451 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002452 res = add_to_attr_set(hotkey_dev_attributes,
2453 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002454
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002455 /* For X41t, X60t, X61t Tablets... */
2456 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2457 tp_features.hotkey_tablet = 1;
2458 printk(TPACPI_INFO
2459 "possible tablet mode switch found; "
2460 "ThinkPad in %s mode\n",
2461 (status & TP_HOTKEY_TABLET_MASK)?
2462 "tablet" : "laptop");
2463 res = add_to_attr_set(hotkey_dev_attributes,
2464 &dev_attr_hotkey_tablet_mode.attr);
2465 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002466
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002467 if (!res)
2468 res = register_attr_set_with_sysfs(
2469 hotkey_dev_attributes,
2470 &tpacpi_pdev->dev.kobj);
2471 if (res)
2472 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002473
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002474 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002475
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002476 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2477 GFP_KERNEL);
2478 if (!hotkey_keycode_map) {
2479 printk(TPACPI_ERR
2480 "failed to allocate memory for key map\n");
2481 res = -ENOMEM;
2482 goto err_exit;
2483 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002484
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002485 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2486 dbg_printk(TPACPI_DBG_INIT,
2487 "using Lenovo default hot key map\n");
2488 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2489 TPACPI_HOTKEY_MAP_SIZE);
2490 } else {
2491 dbg_printk(TPACPI_DBG_INIT,
2492 "using IBM default hot key map\n");
2493 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2494 TPACPI_HOTKEY_MAP_SIZE);
2495 }
2496
2497 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2498 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2499 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2500 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2501 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2502 tpacpi_inputdev->keycode = hotkey_keycode_map;
2503 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2504 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2505 set_bit(hotkey_keycode_map[i],
2506 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002507 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002508 if (i < sizeof(hotkey_reserved_mask)*8)
2509 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002510 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002511 }
2512
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002513 if (tp_features.hotkey_wlsw) {
2514 set_bit(EV_SW, tpacpi_inputdev->evbit);
2515 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2516 }
2517 if (tp_features.hotkey_tablet) {
2518 set_bit(EV_SW, tpacpi_inputdev->evbit);
2519 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002520 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002521
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002522 /* Do not issue duplicate brightness change events to
2523 * userspace */
2524 if (!tp_features.bright_acpimode)
2525 /* update bright_acpimode... */
2526 tpacpi_check_std_acpi_brightness_support();
2527
2528 if (tp_features.bright_acpimode) {
2529 printk(TPACPI_INFO
2530 "This ThinkPad has standard ACPI backlight "
2531 "brightness control, supported by the ACPI "
2532 "video driver\n");
2533 printk(TPACPI_NOTICE
2534 "Disabling thinkpad-acpi brightness events "
2535 "by default...\n");
2536
2537 /* The hotkey_reserved_mask change below is not
2538 * necessary while the keys are at KEY_RESERVED in the
2539 * default map, but better safe than sorry, leave it
2540 * here as a marker of what we have to do, especially
2541 * when we finally become able to set this at runtime
2542 * on response to X.org requests */
2543 hotkey_reserved_mask |=
2544 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2545 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002546 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002547
2548 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2549 res = hotkey_status_set(1);
2550 if (res) {
2551 hotkey_exit();
2552 return res;
2553 }
2554 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2555 & ~hotkey_reserved_mask)
2556 | hotkey_orig_mask);
2557 if (res < 0 && res != -ENXIO) {
2558 hotkey_exit();
2559 return res;
2560 }
2561
2562 dbg_printk(TPACPI_DBG_INIT,
2563 "legacy hot key reporting over procfs %s\n",
2564 (hotkey_report_mode < 2) ?
2565 "enabled" : "disabled");
2566
2567 tpacpi_inputdev->open = &hotkey_inputdev_open;
2568 tpacpi_inputdev->close = &hotkey_inputdev_close;
2569
2570 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002571 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002572 tpacpi_input_send_tabletsw();
2573
2574 return 0;
2575
2576err_exit:
2577 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2578 hotkey_dev_attributes = NULL;
2579
2580 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002581}
2582
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002583static bool hotkey_notify_hotkey(const u32 hkey,
2584 bool *send_acpi_ev,
2585 bool *ignore_acpi_ev)
2586{
2587 /* 0x1000-0x1FFF: key presses */
2588 unsigned int scancode = hkey & 0xfff;
2589 *send_acpi_ev = true;
2590 *ignore_acpi_ev = false;
2591
2592 if (scancode > 0 && scancode < 0x21) {
2593 scancode--;
2594 if (!(hotkey_source_mask & (1 << scancode))) {
2595 tpacpi_input_send_key(scancode);
2596 *send_acpi_ev = false;
2597 } else {
2598 *ignore_acpi_ev = true;
2599 }
2600 return true;
2601 }
2602 return false;
2603}
2604
2605static bool hotkey_notify_wakeup(const u32 hkey,
2606 bool *send_acpi_ev,
2607 bool *ignore_acpi_ev)
2608{
2609 /* 0x2000-0x2FFF: Wakeup reason */
2610 *send_acpi_ev = true;
2611 *ignore_acpi_ev = false;
2612
2613 switch (hkey) {
2614 case 0x2304: /* suspend, undock */
2615 case 0x2404: /* hibernation, undock */
2616 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2617 *ignore_acpi_ev = true;
2618 break;
2619
2620 case 0x2305: /* suspend, bay eject */
2621 case 0x2405: /* hibernation, bay eject */
2622 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2623 *ignore_acpi_ev = true;
2624 break;
2625
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002626 case 0x2313: /* Battery on critical low level (S3) */
2627 case 0x2413: /* Battery on critical low level (S4) */
2628 printk(TPACPI_ALERT
2629 "EMERGENCY WAKEUP: battery almost empty\n");
2630 /* how to auto-heal: */
2631 /* 2313: woke up from S3, go to S4/S5 */
2632 /* 2413: woke up from S4, go to S5 */
2633 break;
2634
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002635 default:
2636 return false;
2637 }
2638
2639 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2640 printk(TPACPI_INFO
2641 "woke up due to a hot-unplug "
2642 "request...\n");
2643 hotkey_wakeup_reason_notify_change();
2644 }
2645 return true;
2646}
2647
2648static bool hotkey_notify_usrevent(const u32 hkey,
2649 bool *send_acpi_ev,
2650 bool *ignore_acpi_ev)
2651{
2652 /* 0x5000-0x5FFF: human interface helpers */
2653 *send_acpi_ev = true;
2654 *ignore_acpi_ev = false;
2655
2656 switch (hkey) {
2657 case 0x5010: /* Lenovo new BIOS: brightness changed */
2658 case 0x500b: /* X61t: tablet pen inserted into bay */
2659 case 0x500c: /* X61t: tablet pen removed from bay */
2660 return true;
2661
2662 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2663 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2664 tpacpi_input_send_tabletsw();
2665 hotkey_tablet_mode_notify_change();
2666 *send_acpi_ev = false;
2667 return true;
2668
2669 case 0x5001:
2670 case 0x5002:
2671 /* LID switch events. Do not propagate */
2672 *ignore_acpi_ev = true;
2673 return true;
2674
2675 default:
2676 return false;
2677 }
2678}
2679
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002680static bool hotkey_notify_thermal(const u32 hkey,
2681 bool *send_acpi_ev,
2682 bool *ignore_acpi_ev)
2683{
2684 /* 0x6000-0x6FFF: thermal alarms */
2685 *send_acpi_ev = true;
2686 *ignore_acpi_ev = false;
2687
2688 switch (hkey) {
2689 case 0x6011:
2690 printk(TPACPI_CRIT
2691 "THERMAL ALARM: battery is too hot!\n");
2692 /* recommended action: warn user through gui */
2693 return true;
2694 case 0x6012:
2695 printk(TPACPI_ALERT
2696 "THERMAL EMERGENCY: battery is extremely hot!\n");
2697 /* recommended action: immediate sleep/hibernate */
2698 return true;
2699 case 0x6021:
2700 printk(TPACPI_CRIT
2701 "THERMAL ALARM: "
2702 "a sensor reports something is too hot!\n");
2703 /* recommended action: warn user through gui, that */
2704 /* some internal component is too hot */
2705 return true;
2706 case 0x6022:
2707 printk(TPACPI_ALERT
2708 "THERMAL EMERGENCY: "
2709 "a sensor reports something is extremely hot!\n");
2710 /* recommended action: immediate sleep/hibernate */
2711 return true;
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02002712 case 0x6030:
2713 printk(TPACPI_INFO
2714 "EC reports that Thermal Table has changed\n");
2715 /* recommended action: do nothing, we don't have
2716 * Lenovo ATM information */
2717 return true;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002718 default:
2719 printk(TPACPI_ALERT
2720 "THERMAL ALERT: unknown thermal alarm received\n");
2721 return false;
2722 }
2723}
2724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002725static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2726{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002727 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002728 bool send_acpi_ev;
2729 bool ignore_acpi_ev;
2730 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002731
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002732 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002733 printk(TPACPI_ERR
2734 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002735 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002736 acpi_bus_generate_netlink_event(
2737 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002738 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002739 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002740 return;
2741 }
2742
2743 while (1) {
2744 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002745 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002746 return;
2747 }
2748
2749 if (hkey == 0) {
2750 /* queue empty */
2751 return;
2752 }
2753
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002754 send_acpi_ev = true;
2755 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002756
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002757 switch (hkey >> 12) {
2758 case 1:
2759 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002760 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2761 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002762 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002763 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002764 /* 0x2000-0x2FFF: Wakeup reason */
2765 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2766 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002767 break;
2768 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002769 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002770 if (hkey == 0x3003) {
2771 hotkey_autosleep_ack = 1;
2772 printk(TPACPI_INFO
2773 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002774 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002775 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002776 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002777 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002778 }
2779 break;
2780 case 4:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002781 /* 0x4000-0x4FFF: dock-related wakeups */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002782 if (hkey == 0x4003) {
2783 hotkey_autosleep_ack = 1;
2784 printk(TPACPI_INFO
2785 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002786 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002787 known_ev = true;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002788 } else {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002789 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002790 }
2791 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002792 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002793 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002794 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2795 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002796 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02002797 case 6:
2798 /* 0x6000-0x6FFF: thermal alarms */
2799 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2800 &ignore_acpi_ev);
2801 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002802 case 7:
2803 /* 0x7000-0x7FFF: misc */
2804 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002805 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002806 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002807 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002808 break;
2809 }
2810 /* fallthrough to default */
2811 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002812 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002813 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02002814 if (!known_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002815 printk(TPACPI_NOTICE
2816 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhcb429352009-01-11 03:01:07 -02002817 printk(TPACPI_NOTICE
2818 "please report the conditions when this "
2819 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002820 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002821
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002822 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002823 if (!ignore_acpi_ev &&
2824 (send_acpi_ev || hotkey_report_mode < 2)) {
2825 acpi_bus_generate_proc_event(ibm->acpi->device,
2826 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002827 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002828
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002829 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002830 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002831 acpi_bus_generate_netlink_event(
2832 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002833 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002834 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002835 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002836 }
2837}
2838
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002839static void hotkey_suspend(pm_message_t state)
2840{
2841 /* Do these on suspend, we get the events on early resume! */
2842 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2843 hotkey_autosleep_ack = 0;
2844}
2845
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002846static void hotkey_resume(void)
2847{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002848 tpacpi_disable_brightness_delay();
2849
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002850 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002851 printk(TPACPI_ERR
2852 "error while trying to read hot key mask "
2853 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002854 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002855 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002856 hotkey_wakeup_reason_notify_change();
2857 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002858 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002859}
2860
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002861/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002862static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002864 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 int len = 0;
2866
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002867 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002868 len += sprintf(p + len, "status:\t\tnot supported\n");
2869 return len;
2870 }
2871
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002872 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002873 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002874 res = hotkey_status_get(&status);
2875 if (!res)
2876 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002877 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002878 if (res)
2879 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002882 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002883 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 len += sprintf(p + len,
2885 "commands:\tenable, disable, reset, <mask>\n");
2886 } else {
2887 len += sprintf(p + len, "mask:\t\tnot supported\n");
2888 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2889 }
2890
2891 return len;
2892}
2893
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002894static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002896 int res, status;
2897 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002900 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return -ENODEV;
2902
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002903 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002904 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002905
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002906 status = -1;
2907 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002908
2909 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 while ((cmd = next_cmd(&buf))) {
2911 if (strlencmp(cmd, "enable") == 0) {
2912 status = 1;
2913 } else if (strlencmp(cmd, "disable") == 0) {
2914 status = 0;
2915 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002916 status = hotkey_orig_status;
2917 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2919 /* mask set */
2920 } else if (sscanf(cmd, "%x", &mask) == 1) {
2921 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002922 } else {
2923 res = -EINVAL;
2924 goto errexit;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002927 if (status != -1)
2928 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002930 if (!res && mask != hotkey_mask)
2931 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002933errexit:
2934 mutex_unlock(&hotkey_mutex);
2935 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002938static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002939 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002940 {"", 0},
2941};
2942
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002943static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002944 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002945 .notify = hotkey_notify,
2946 .handle = &hkey_handle,
2947 .type = ACPI_DEVICE_NOTIFY,
2948};
2949
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002950static struct ibm_struct hotkey_driver_data = {
2951 .name = "hotkey",
2952 .read = hotkey_read,
2953 .write = hotkey_write,
2954 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002955 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002956 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002957 .acpi = &ibm_hotkey_acpidriver,
2958};
2959
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002960/*************************************************************************
2961 * Bluetooth subdriver
2962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002964enum {
2965 /* ACPI GBDC/SBDC bits */
2966 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2967 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002968 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2969 off / last state */
2970};
2971
2972enum {
2973 /* ACPI \BLTH commands */
2974 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2975 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2976 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2977 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2978 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002979};
2980
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002981static struct rfkill *tpacpi_bluetooth_rfkill;
2982
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002983static void bluetooth_suspend(pm_message_t state)
2984{
2985 /* Try to make sure radio will resume powered off */
2986 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2987 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2988}
2989
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002990static int bluetooth_get_radiosw(void)
2991{
2992 int status;
2993
2994 if (!tp_features.bluetooth)
2995 return -ENODEV;
2996
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002997 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2998 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002999 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003000
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003001#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3002 if (dbg_bluetoothemul)
3003 return (tpacpi_bluetooth_emulstate) ?
3004 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3005#endif
3006
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003007 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3008 return -EIO;
3009
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003010 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3011 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003012}
3013
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003014static void bluetooth_update_rfk(void)
3015{
3016 int status;
3017
3018 if (!tpacpi_bluetooth_rfkill)
3019 return;
3020
3021 status = bluetooth_get_radiosw();
3022 if (status < 0)
3023 return;
3024 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3025}
3026
3027static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003028{
3029 int status;
3030
3031 if (!tp_features.bluetooth)
3032 return -ENODEV;
3033
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003034 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3035 * reason to risk weird behaviour. */
3036 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3037 && radio_on)
3038 return -EPERM;
3039
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003040#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3041 if (dbg_bluetoothemul) {
3042 tpacpi_bluetooth_emulstate = !!radio_on;
3043 if (update_rfk)
3044 bluetooth_update_rfk();
3045 return 0;
3046 }
3047#endif
3048
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003049 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003050 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003051 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003052 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003053 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003054 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3055 return -EIO;
3056
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003057 if (update_rfk)
3058 bluetooth_update_rfk();
3059
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003060 return 0;
3061}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003062
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003063/* sysfs bluetooth enable ---------------------------------------------- */
3064static ssize_t bluetooth_enable_show(struct device *dev,
3065 struct device_attribute *attr,
3066 char *buf)
3067{
3068 int status;
3069
3070 status = bluetooth_get_radiosw();
3071 if (status < 0)
3072 return status;
3073
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003074 return snprintf(buf, PAGE_SIZE, "%d\n",
3075 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003076}
3077
3078static ssize_t bluetooth_enable_store(struct device *dev,
3079 struct device_attribute *attr,
3080 const char *buf, size_t count)
3081{
3082 unsigned long t;
3083 int res;
3084
3085 if (parse_strtoul(buf, 1, &t))
3086 return -EINVAL;
3087
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003088 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003089
3090 return (res) ? res : count;
3091}
3092
3093static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003094 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003095 bluetooth_enable_show, bluetooth_enable_store);
3096
3097/* --------------------------------------------------------------------- */
3098
3099static struct attribute *bluetooth_attributes[] = {
3100 &dev_attr_bluetooth_enable.attr,
3101 NULL
3102};
3103
3104static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003105 .attrs = bluetooth_attributes,
3106};
3107
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003108static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3109{
3110 int bts = bluetooth_get_radiosw();
3111
3112 if (bts < 0)
3113 return bts;
3114
3115 *state = bts;
3116 return 0;
3117}
3118
3119static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3120{
3121 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3122}
3123
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003124static void bluetooth_shutdown(void)
3125{
3126 /* Order firmware to save current state to NVRAM */
3127 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3128 TP_ACPI_BLTH_SAVE_STATE))
3129 printk(TPACPI_NOTICE
3130 "failed to save bluetooth state to NVRAM\n");
3131}
3132
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003133static void bluetooth_exit(void)
3134{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003135 bluetooth_shutdown();
3136
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003137 if (tpacpi_bluetooth_rfkill)
3138 rfkill_unregister(tpacpi_bluetooth_rfkill);
3139
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003140 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3141 &bluetooth_attr_group);
3142}
3143
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003144static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003146 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003147 int status = 0;
3148
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003149 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3150
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003151 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003152
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003153 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3154 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003155 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003156 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003158 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3159 str_supported(tp_features.bluetooth),
3160 status);
3161
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003162#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3163 if (dbg_bluetoothemul) {
3164 tp_features.bluetooth = 1;
3165 printk(TPACPI_INFO
3166 "bluetooth switch emulation enabled\n");
3167 } else
3168#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003169 if (tp_features.bluetooth &&
3170 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3171 /* no bluetooth hardware present in system */
3172 tp_features.bluetooth = 0;
3173 dbg_printk(TPACPI_DBG_INIT,
3174 "bluetooth hardware not installed\n");
3175 }
3176
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003177 if (!tp_features.bluetooth)
3178 return 1;
3179
3180 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003181 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003182 if (res)
3183 return res;
3184
3185 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3186 &tpacpi_bluetooth_rfkill,
3187 RFKILL_TYPE_BLUETOOTH,
3188 "tpacpi_bluetooth_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003189 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003190 tpacpi_bluetooth_rfk_set,
3191 tpacpi_bluetooth_rfk_get);
3192 if (res) {
3193 bluetooth_exit();
3194 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003195 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003196
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003200/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003201static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
3203 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003204 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003206 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003209 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003210 (status == RFKILL_STATE_UNBLOCKED) ?
3211 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 len += sprintf(p + len, "commands:\tenable, disable\n");
3213 }
3214
3215 return len;
3216}
3217
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003218static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003222 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003223 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 while ((cmd = next_cmd(&buf))) {
3226 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003227 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003229 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 } else
3231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 0;
3235}
3236
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003237static struct ibm_struct bluetooth_driver_data = {
3238 .name = "bluetooth",
3239 .read = bluetooth_read,
3240 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003241 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003242 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003243 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003244};
3245
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003246/*************************************************************************
3247 * Wan subdriver
3248 */
3249
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003250enum {
3251 /* ACPI GWAN/SWAN bits */
3252 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3253 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003254 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3255 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003256};
3257
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003258static struct rfkill *tpacpi_wan_rfkill;
3259
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003260static void wan_suspend(pm_message_t state)
3261{
3262 /* Try to make sure radio will resume powered off */
3263 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3264 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3265}
3266
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003267static int wan_get_radiosw(void)
3268{
3269 int status;
3270
3271 if (!tp_features.wan)
3272 return -ENODEV;
3273
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003274 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3275 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003276 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003277
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003278#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3279 if (dbg_wwanemul)
3280 return (tpacpi_wwan_emulstate) ?
3281 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3282#endif
3283
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003284 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3285 return -EIO;
3286
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003287 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3288 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003289}
3290
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003291static void wan_update_rfk(void)
3292{
3293 int status;
3294
3295 if (!tpacpi_wan_rfkill)
3296 return;
3297
3298 status = wan_get_radiosw();
3299 if (status < 0)
3300 return;
3301 rfkill_force_state(tpacpi_wan_rfkill, status);
3302}
3303
3304static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003305{
3306 int status;
3307
3308 if (!tp_features.wan)
3309 return -ENODEV;
3310
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003311 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3312 * reason to risk weird behaviour. */
3313 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3314 && radio_on)
3315 return -EPERM;
3316
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003317#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3318 if (dbg_wwanemul) {
3319 tpacpi_wwan_emulstate = !!radio_on;
3320 if (update_rfk)
3321 wan_update_rfk();
3322 return 0;
3323 }
3324#endif
3325
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003326 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003327 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003328 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003329 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003330 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003331 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3332 return -EIO;
3333
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003334 if (update_rfk)
3335 wan_update_rfk();
3336
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003337 return 0;
3338}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003339
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003340/* sysfs wan enable ---------------------------------------------------- */
3341static ssize_t wan_enable_show(struct device *dev,
3342 struct device_attribute *attr,
3343 char *buf)
3344{
3345 int status;
3346
3347 status = wan_get_radiosw();
3348 if (status < 0)
3349 return status;
3350
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003351 return snprintf(buf, PAGE_SIZE, "%d\n",
3352 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003353}
3354
3355static ssize_t wan_enable_store(struct device *dev,
3356 struct device_attribute *attr,
3357 const char *buf, size_t count)
3358{
3359 unsigned long t;
3360 int res;
3361
3362 if (parse_strtoul(buf, 1, &t))
3363 return -EINVAL;
3364
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003365 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003366
3367 return (res) ? res : count;
3368}
3369
3370static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003371 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003372 wan_enable_show, wan_enable_store);
3373
3374/* --------------------------------------------------------------------- */
3375
3376static struct attribute *wan_attributes[] = {
3377 &dev_attr_wan_enable.attr,
3378 NULL
3379};
3380
3381static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003382 .attrs = wan_attributes,
3383};
3384
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003385static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3386{
3387 int wans = wan_get_radiosw();
3388
3389 if (wans < 0)
3390 return wans;
3391
3392 *state = wans;
3393 return 0;
3394}
3395
3396static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3397{
3398 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3399}
3400
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003401static void wan_shutdown(void)
3402{
3403 /* Order firmware to save current state to NVRAM */
3404 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3405 TP_ACPI_WGSV_SAVE_STATE))
3406 printk(TPACPI_NOTICE
3407 "failed to save WWAN state to NVRAM\n");
3408}
3409
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003410static void wan_exit(void)
3411{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003412 wan_shutdown();
3413
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003414 if (tpacpi_wan_rfkill)
3415 rfkill_unregister(tpacpi_wan_rfkill);
3416
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003417 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3418 &wan_attr_group);
3419}
3420
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003421static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003422{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003423 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003424 int status = 0;
3425
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003426 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3427
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003428 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003429
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003430 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003431 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003432
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003433 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3434 str_supported(tp_features.wan),
3435 status);
3436
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003437#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3438 if (dbg_wwanemul) {
3439 tp_features.wan = 1;
3440 printk(TPACPI_INFO
3441 "wwan switch emulation enabled\n");
3442 } else
3443#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003444 if (tp_features.wan &&
3445 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3446 /* no wan hardware present in system */
3447 tp_features.wan = 0;
3448 dbg_printk(TPACPI_DBG_INIT,
3449 "wan hardware not installed\n");
3450 }
3451
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003452 if (!tp_features.wan)
3453 return 1;
3454
3455 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003456 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003457 if (res)
3458 return res;
3459
3460 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3461 &tpacpi_wan_rfkill,
3462 RFKILL_TYPE_WWAN,
3463 "tpacpi_wwan_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003464 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003465 tpacpi_wan_rfk_set,
3466 tpacpi_wan_rfk_get);
3467 if (res) {
3468 wan_exit();
3469 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003470 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003471
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003472 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003473}
3474
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003475/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003476static int wan_read(char *p)
3477{
3478 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003479 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003480
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003481 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003482 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003483 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003484 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003485 (status == RFKILL_STATE_UNBLOCKED) ?
3486 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003487 len += sprintf(p + len, "commands:\tenable, disable\n");
3488 }
3489
3490 return len;
3491}
3492
3493static int wan_write(char *buf)
3494{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003495 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003496
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003497 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003498 return -ENODEV;
3499
3500 while ((cmd = next_cmd(&buf))) {
3501 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003502 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003503 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003504 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003505 } else
3506 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003507 }
3508
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003509 return 0;
3510}
3511
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003512static struct ibm_struct wan_driver_data = {
3513 .name = "wan",
3514 .read = wan_read,
3515 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003516 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003517 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003518 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003519};
3520
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003521/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003522 * UWB subdriver
3523 */
3524
3525enum {
3526 /* ACPI GUWB/SUWB bits */
3527 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3528 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3529};
3530
3531static struct rfkill *tpacpi_uwb_rfkill;
3532
3533static int uwb_get_radiosw(void)
3534{
3535 int status;
3536
3537 if (!tp_features.uwb)
3538 return -ENODEV;
3539
3540 /* WLSW overrides UWB in firmware/hardware, reflect that */
3541 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3542 return RFKILL_STATE_HARD_BLOCKED;
3543
3544#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3545 if (dbg_uwbemul)
3546 return (tpacpi_uwb_emulstate) ?
3547 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3548#endif
3549
3550 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3551 return -EIO;
3552
3553 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3554 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3555}
3556
3557static void uwb_update_rfk(void)
3558{
3559 int status;
3560
3561 if (!tpacpi_uwb_rfkill)
3562 return;
3563
3564 status = uwb_get_radiosw();
3565 if (status < 0)
3566 return;
3567 rfkill_force_state(tpacpi_uwb_rfkill, status);
3568}
3569
3570static int uwb_set_radiosw(int radio_on, int update_rfk)
3571{
3572 int status;
3573
3574 if (!tp_features.uwb)
3575 return -ENODEV;
3576
3577 /* WLSW overrides UWB in firmware/hardware, but there is no
3578 * reason to risk weird behaviour. */
3579 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3580 && radio_on)
3581 return -EPERM;
3582
3583#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3584 if (dbg_uwbemul) {
3585 tpacpi_uwb_emulstate = !!radio_on;
3586 if (update_rfk)
3587 uwb_update_rfk();
3588 return 0;
3589 }
3590#endif
3591
3592 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3593 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3594 return -EIO;
3595
3596 if (update_rfk)
3597 uwb_update_rfk();
3598
3599 return 0;
3600}
3601
3602/* --------------------------------------------------------------------- */
3603
3604static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3605{
3606 int uwbs = uwb_get_radiosw();
3607
3608 if (uwbs < 0)
3609 return uwbs;
3610
3611 *state = uwbs;
3612 return 0;
3613}
3614
3615static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3616{
3617 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3618}
3619
3620static void uwb_exit(void)
3621{
3622 if (tpacpi_uwb_rfkill)
3623 rfkill_unregister(tpacpi_uwb_rfkill);
3624}
3625
3626static int __init uwb_init(struct ibm_init_struct *iibm)
3627{
3628 int res;
3629 int status = 0;
3630
3631 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3632
3633 TPACPI_ACPIHANDLE_INIT(hkey);
3634
3635 tp_features.uwb = hkey_handle &&
3636 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3637
3638 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3639 str_supported(tp_features.uwb),
3640 status);
3641
3642#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3643 if (dbg_uwbemul) {
3644 tp_features.uwb = 1;
3645 printk(TPACPI_INFO
3646 "uwb switch emulation enabled\n");
3647 } else
3648#endif
3649 if (tp_features.uwb &&
3650 !(status & TP_ACPI_UWB_HWPRESENT)) {
3651 /* no uwb hardware present in system */
3652 tp_features.uwb = 0;
3653 dbg_printk(TPACPI_DBG_INIT,
3654 "uwb hardware not installed\n");
3655 }
3656
3657 if (!tp_features.uwb)
3658 return 1;
3659
3660 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3661 &tpacpi_uwb_rfkill,
3662 RFKILL_TYPE_UWB,
3663 "tpacpi_uwb_sw",
3664 false,
3665 tpacpi_uwb_rfk_set,
3666 tpacpi_uwb_rfk_get);
3667
3668 return res;
3669}
3670
3671static struct ibm_struct uwb_driver_data = {
3672 .name = "uwb",
3673 .exit = uwb_exit,
3674 .flags.experimental = 1,
3675};
3676
3677/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003678 * Video subdriver
3679 */
3680
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003681#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3682
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003683enum video_access_mode {
3684 TPACPI_VIDEO_NONE = 0,
3685 TPACPI_VIDEO_570, /* 570 */
3686 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3687 TPACPI_VIDEO_NEW, /* all others */
3688};
3689
3690enum { /* video status flags, based on VIDEO_570 */
3691 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3692 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3693 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3694};
3695
3696enum { /* TPACPI_VIDEO_570 constants */
3697 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3698 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3699 * video_status_flags */
3700 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3701 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3702};
3703
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003704static enum video_access_mode video_supported;
3705static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003706
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003707static int video_autosw_get(void);
3708static int video_autosw_set(int enable);
3709
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003710TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003711
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003712static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003714 int ivga;
3715
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003716 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3717
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003718 TPACPI_ACPIHANDLE_INIT(vid);
3719 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003721 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3722 /* G41, assume IVGA doesn't change */
3723 vid_handle = vid2_handle;
3724
3725 if (!vid_handle)
3726 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003727 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003728 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3729 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003730 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003731 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3732 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003733 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003734 else
3735 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003736 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003738 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3739 str_supported(video_supported != TPACPI_VIDEO_NONE),
3740 video_supported);
3741
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003742 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003745static void video_exit(void)
3746{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003747 dbg_printk(TPACPI_DBG_EXIT,
3748 "restoring original video autoswitch mode\n");
3749 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003750 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003751 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003752}
3753
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003754static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 int status = 0;
3757 int i;
3758
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003759 switch (video_supported) {
3760 case TPACPI_VIDEO_570:
3761 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3762 TP_ACPI_VIDEO_570_PHSCMD))
3763 return -EIO;
3764 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3765 break;
3766 case TPACPI_VIDEO_770:
3767 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3768 return -EIO;
3769 if (i)
3770 status |= TP_ACPI_VIDEO_S_LCD;
3771 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3772 return -EIO;
3773 if (i)
3774 status |= TP_ACPI_VIDEO_S_CRT;
3775 break;
3776 case TPACPI_VIDEO_NEW:
3777 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3778 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3779 return -EIO;
3780 if (i)
3781 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003783 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3784 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3785 return -EIO;
3786 if (i)
3787 status |= TP_ACPI_VIDEO_S_LCD;
3788 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3789 return -EIO;
3790 if (i)
3791 status |= TP_ACPI_VIDEO_S_DVI;
3792 break;
3793 default:
3794 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 return status;
3798}
3799
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003800static int video_outputsw_set(int status)
3801{
3802 int autosw;
3803 int res = 0;
3804
3805 switch (video_supported) {
3806 case TPACPI_VIDEO_570:
3807 res = acpi_evalf(NULL, NULL,
3808 "\\_SB.PHS2", "vdd",
3809 TP_ACPI_VIDEO_570_PHS2CMD,
3810 status | TP_ACPI_VIDEO_570_PHS2SET);
3811 break;
3812 case TPACPI_VIDEO_770:
3813 autosw = video_autosw_get();
3814 if (autosw < 0)
3815 return autosw;
3816
3817 res = video_autosw_set(1);
3818 if (res)
3819 return res;
3820 res = acpi_evalf(vid_handle, NULL,
3821 "ASWT", "vdd", status * 0x100, 0);
3822 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003823 printk(TPACPI_ERR
3824 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003825 return -EIO;
3826 }
3827 break;
3828 case TPACPI_VIDEO_NEW:
3829 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003830 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003831 break;
3832 default:
3833 return -ENOSYS;
3834 }
3835
3836 return (res)? 0 : -EIO;
3837}
3838
3839static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003841 int autosw = 0;
3842
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003843 switch (video_supported) {
3844 case TPACPI_VIDEO_570:
3845 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3846 return -EIO;
3847 break;
3848 case TPACPI_VIDEO_770:
3849 case TPACPI_VIDEO_NEW:
3850 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3851 return -EIO;
3852 break;
3853 default:
3854 return -ENOSYS;
3855 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003856
3857 return autosw & 1;
3858}
3859
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003860static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003861{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003862 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003864 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003865}
3866
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003867static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003868{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003869 int autosw = video_autosw_get();
3870 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003871
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003872 if (autosw < 0)
3873 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003874
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003875 switch (video_supported) {
3876 case TPACPI_VIDEO_570:
3877 res = video_autosw_set(1);
3878 if (res)
3879 return res;
3880 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3881 break;
3882 case TPACPI_VIDEO_770:
3883 case TPACPI_VIDEO_NEW:
3884 res = video_autosw_set(1);
3885 if (res)
3886 return res;
3887 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3888 break;
3889 default:
3890 return -ENOSYS;
3891 }
3892 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003893 printk(TPACPI_ERR
3894 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003895 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003896 }
3897
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003898 return (res)? 0 : -EIO;
3899}
3900
3901static int video_expand_toggle(void)
3902{
3903 switch (video_supported) {
3904 case TPACPI_VIDEO_570:
3905 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3906 0 : -EIO;
3907 case TPACPI_VIDEO_770:
3908 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3909 0 : -EIO;
3910 case TPACPI_VIDEO_NEW:
3911 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3912 0 : -EIO;
3913 default:
3914 return -ENOSYS;
3915 }
3916 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003917}
3918
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003919static int video_read(char *p)
3920{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003921 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003922 int len = 0;
3923
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003924 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003925 len += sprintf(p + len, "status:\t\tnot supported\n");
3926 return len;
3927 }
3928
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003929 status = video_outputsw_get();
3930 if (status < 0)
3931 return status;
3932
3933 autosw = video_autosw_get();
3934 if (autosw < 0)
3935 return autosw;
3936
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003937 len += sprintf(p + len, "status:\t\tsupported\n");
3938 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3939 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003940 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003941 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3942 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3943 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3944 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003945 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003946 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3947 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3948 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3949
3950 return len;
3951}
3952
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003953static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 char *cmd;
3956 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003957 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003959 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960 return -ENODEV;
3961
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003962 enable = 0;
3963 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
3965 while ((cmd = next_cmd(&buf))) {
3966 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003967 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003969 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003971 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003973 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003974 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003975 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003976 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003977 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003978 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003979 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003981 res = video_autosw_set(1);
3982 if (res)
3983 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003985 res = video_autosw_set(0);
3986 if (res)
3987 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003989 res = video_outputsw_cycle();
3990 if (res)
3991 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003993 res = video_expand_toggle();
3994 if (res)
3995 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 } else
3997 return -EINVAL;
3998 }
3999
4000 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004001 status = video_outputsw_get();
4002 if (status < 0)
4003 return status;
4004 res = video_outputsw_set((status & ~disable) | enable);
4005 if (res)
4006 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 }
4008
4009 return 0;
4010}
4011
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004012static struct ibm_struct video_driver_data = {
4013 .name = "video",
4014 .read = video_read,
4015 .write = video_write,
4016 .exit = video_exit,
4017};
4018
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004019#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4020
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004021/*************************************************************************
4022 * Light (thinklight) subdriver
4023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004025TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4026TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004027
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004028static int light_get_status(void)
4029{
4030 int status = 0;
4031
4032 if (tp_features.light_status) {
4033 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4034 return -EIO;
4035 return (!!status);
4036 }
4037
4038 return -ENXIO;
4039}
4040
4041static int light_set_status(int status)
4042{
4043 int rc;
4044
4045 if (tp_features.light) {
4046 if (cmos_handle) {
4047 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4048 (status)?
4049 TP_CMOS_THINKLIGHT_ON :
4050 TP_CMOS_THINKLIGHT_OFF);
4051 } else {
4052 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4053 (status)? 1 : 0);
4054 }
4055 return (rc)? 0 : -EIO;
4056 }
4057
4058 return -ENXIO;
4059}
4060
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004061static void light_set_status_worker(struct work_struct *work)
4062{
4063 struct tpacpi_led_classdev *data =
4064 container_of(work, struct tpacpi_led_classdev, work);
4065
4066 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4067 light_set_status((data->new_brightness != LED_OFF));
4068}
4069
4070static void light_sysfs_set(struct led_classdev *led_cdev,
4071 enum led_brightness brightness)
4072{
4073 struct tpacpi_led_classdev *data =
4074 container_of(led_cdev,
4075 struct tpacpi_led_classdev,
4076 led_classdev);
4077 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004078 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004079}
4080
4081static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4082{
4083 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4084}
4085
4086static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4087 .led_classdev = {
4088 .name = "tpacpi::thinklight",
4089 .brightness_set = &light_sysfs_set,
4090 .brightness_get = &light_sysfs_get,
4091 }
4092};
4093
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004094static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004096 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004097
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004098 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4099
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004100 TPACPI_ACPIHANDLE_INIT(ledb);
4101 TPACPI_ACPIHANDLE_INIT(lght);
4102 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004103 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004104
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004105 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004106 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004107
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004108 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004109 /* light status not supported on
4110 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004111 tp_features.light_status =
4112 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004114 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4115 str_supported(tp_features.light),
4116 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004117
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004118 if (!tp_features.light)
4119 return 1;
4120
4121 rc = led_classdev_register(&tpacpi_pdev->dev,
4122 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004123
4124 if (rc < 0) {
4125 tp_features.light = 0;
4126 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004127 } else {
4128 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004129 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004130
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004131 return rc;
4132}
4133
4134static void light_exit(void)
4135{
4136 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4137 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004138 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004141static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004144 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004146 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004147 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004148 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004149 len += sprintf(p + len, "status:\t\tunknown\n");
4150 len += sprintf(p + len, "commands:\ton, off\n");
4151 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004152 status = light_get_status();
4153 if (status < 0)
4154 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004156 len += sprintf(p + len, "commands:\ton, off\n");
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
4159 return len;
4160}
4161
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004162static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004165 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004166
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004167 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004168 return -ENODEV;
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 while ((cmd = next_cmd(&buf))) {
4171 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004172 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004174 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 } else
4176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 }
4178
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004179 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004182static struct ibm_struct light_driver_data = {
4183 .name = "light",
4184 .read = light_read,
4185 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004186 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004187};
4188
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004189/*************************************************************************
4190 * Dock subdriver
4191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004193#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004194
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004195static void dock_notify(struct ibm_struct *ibm, u32 event);
4196static int dock_read(char *p);
4197static int dock_write(char *buf);
4198
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004199TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004200 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4201 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4202 "\\_SB.PCI.ISA.SLCE", /* 570 */
4203 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4204
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004205/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004206TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004207
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004208static const struct acpi_device_id ibm_pci_device_ids[] = {
4209 {PCI_ROOT_HID_STRING, 0},
4210 {"", 0},
4211};
4212
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004213static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4214 {
4215 .notify = dock_notify,
4216 .handle = &dock_handle,
4217 .type = ACPI_SYSTEM_NOTIFY,
4218 },
4219 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004220 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4221 * We just use it to get notifications of dock hotplug
4222 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004223 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004224 .notify = dock_notify,
4225 .handle = &pci_handle,
4226 .type = ACPI_SYSTEM_NOTIFY,
4227 },
4228};
4229
4230static struct ibm_struct dock_driver_data[2] = {
4231 {
4232 .name = "dock",
4233 .read = dock_read,
4234 .write = dock_write,
4235 .acpi = &ibm_dock_acpidriver[0],
4236 },
4237 {
4238 .name = "dock",
4239 .acpi = &ibm_dock_acpidriver[1],
4240 },
4241};
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243#define dock_docked() (_sta(dock_handle) & 1)
4244
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004245static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004246{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004247 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4248
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004249 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004250
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004251 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4252 str_supported(dock_handle != NULL));
4253
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004254 return (dock_handle)? 0 : 1;
4255}
4256
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004257static int __init dock_init2(struct ibm_init_struct *iibm)
4258{
4259 int dock2_needed;
4260
4261 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4262
4263 if (dock_driver_data[0].flags.acpi_driver_registered &&
4264 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004265 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004266 dock2_needed = (pci_handle != NULL);
4267 vdbg_printk(TPACPI_DBG_INIT,
4268 "dock PCI handler for the TP 570 is %s\n",
4269 str_supported(dock2_needed));
4270 } else {
4271 vdbg_printk(TPACPI_DBG_INIT,
4272 "dock subdriver part 2 not required\n");
4273 dock2_needed = 0;
4274 }
4275
4276 return (dock2_needed)? 0 : 1;
4277}
4278
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004279static void dock_notify(struct ibm_struct *ibm, u32 event)
4280{
4281 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004282 int pci = ibm->acpi->hid && ibm->acpi->device &&
4283 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004284 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004285
4286 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004287 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004288 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004289 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004290 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004291 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004292 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004293 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004294 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004295 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004296 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004297 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004298 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004299 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004300 }
Len Brown14e04fb32007-08-23 15:20:26 -04004301 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004302 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004303 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004304 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004305}
4306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004307static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
4309 int len = 0;
4310 int docked = dock_docked();
4311
4312 if (!dock_handle)
4313 len += sprintf(p + len, "status:\t\tnot supported\n");
4314 else if (!docked)
4315 len += sprintf(p + len, "status:\t\tundocked\n");
4316 else {
4317 len += sprintf(p + len, "status:\t\tdocked\n");
4318 len += sprintf(p + len, "commands:\tdock, undock\n");
4319 }
4320
4321 return len;
4322}
4323
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004324static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
4326 char *cmd;
4327
4328 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 while ((cmd = next_cmd(&buf))) {
4332 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004333 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4334 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return -EIO;
4336 } else if (strlencmp(cmd, "dock") == 0) {
4337 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4338 return -EIO;
4339 } else
4340 return -EINVAL;
4341 }
4342
4343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004346#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004348/*************************************************************************
4349 * Bay subdriver
4350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004352#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004353
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004354TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004355 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4356 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4357 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4358 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004359TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004360 "_EJ0", /* all others */
4361 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004362TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004363 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4364 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004365TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004366 "_EJ0", /* 770x */
4367 ); /* all others */
4368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004369static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004371 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4372
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004373 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004374 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004375 TPACPI_ACPIHANDLE_INIT(bay_ej);
4376 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004377 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004378 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004379
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004380 tp_features.bay_status = bay_handle &&
4381 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4382 tp_features.bay_status2 = bay2_handle &&
4383 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004384
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004385 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4386 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4387 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4388 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004390 vdbg_printk(TPACPI_DBG_INIT,
4391 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004392 str_supported(tp_features.bay_status),
4393 str_supported(tp_features.bay_eject),
4394 str_supported(tp_features.bay_status2),
4395 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004396
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004397 return (tp_features.bay_status || tp_features.bay_eject ||
4398 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004401static void bay_notify(struct ibm_struct *ibm, u32 event)
4402{
Len Brown14e04fb32007-08-23 15:20:26 -04004403 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004404 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004405 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004406 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004407}
4408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004409#define bay_occupied(b) (_sta(b##_handle) & 1)
4410
4411static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
4413 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004414 int occupied = bay_occupied(bay);
4415 int occupied2 = bay_occupied(bay2);
4416 int eject, eject2;
4417
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004418 len += sprintf(p + len, "status:\t\t%s\n",
4419 tp_features.bay_status ?
4420 (occupied ? "occupied" : "unoccupied") :
4421 "not supported");
4422 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004423 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4424 "occupied" : "unoccupied");
4425
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004426 eject = tp_features.bay_eject && occupied;
4427 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428
4429 if (eject && eject2)
4430 len += sprintf(p + len, "commands:\teject, eject2\n");
4431 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004433 else if (eject2)
4434 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 return len;
4437}
4438
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004439static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
4441 char *cmd;
4442
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004443 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004444 return -ENODEV;
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004447 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004448 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4449 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004450 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004451 strlencmp(cmd, "eject2") == 0) {
4452 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return -EIO;
4454 } else
4455 return -EINVAL;
4456 }
4457
4458 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004459}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004460
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004461static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4462 .notify = bay_notify,
4463 .handle = &bay_handle,
4464 .type = ACPI_SYSTEM_NOTIFY,
4465};
4466
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004467static struct ibm_struct bay_driver_data = {
4468 .name = "bay",
4469 .read = bay_read,
4470 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004471 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004472};
4473
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004474#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004476/*************************************************************************
4477 * CMOS subdriver
4478 */
4479
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004480/* sysfs cmos_command -------------------------------------------------- */
4481static ssize_t cmos_command_store(struct device *dev,
4482 struct device_attribute *attr,
4483 const char *buf, size_t count)
4484{
4485 unsigned long cmos_cmd;
4486 int res;
4487
4488 if (parse_strtoul(buf, 21, &cmos_cmd))
4489 return -EINVAL;
4490
4491 res = issue_thinkpad_cmos_command(cmos_cmd);
4492 return (res)? res : count;
4493}
4494
4495static struct device_attribute dev_attr_cmos_command =
4496 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4497
4498/* --------------------------------------------------------------------- */
4499
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004500static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004501{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004502 int res;
4503
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004504 vdbg_printk(TPACPI_DBG_INIT,
4505 "initializing cmos commands subdriver\n");
4506
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004507 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004508
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004509 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4510 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004511
4512 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4513 if (res)
4514 return res;
4515
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004516 return (cmos_handle)? 0 : 1;
4517}
4518
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004519static void cmos_exit(void)
4520{
4521 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4522}
4523
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004524static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 int len = 0;
4527
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004528 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4529 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 if (!cmos_handle)
4531 len += sprintf(p + len, "status:\t\tnot supported\n");
4532 else {
4533 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004534 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
4536
4537 return len;
4538}
4539
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004540static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
4542 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004543 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004546 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4547 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 /* cmos_cmd set */
4549 } else
4550 return -EINVAL;
4551
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004552 res = issue_thinkpad_cmos_command(cmos_cmd);
4553 if (res)
4554 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556
4557 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558}
4559
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004560static struct ibm_struct cmos_driver_data = {
4561 .name = "cmos",
4562 .read = cmos_read,
4563 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004564 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004565};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004566
4567/*************************************************************************
4568 * LED subdriver
4569 */
4570
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004571enum led_access_mode {
4572 TPACPI_LED_NONE = 0,
4573 TPACPI_LED_570, /* 570 */
4574 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4575 TPACPI_LED_NEW, /* all others */
4576};
4577
4578enum { /* For TPACPI_LED_OLD */
4579 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4580 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4581 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4582};
4583
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004584enum led_status_t {
4585 TPACPI_LED_OFF = 0,
4586 TPACPI_LED_ON,
4587 TPACPI_LED_BLINK,
4588};
4589
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004590static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004591
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004592TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004593 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4594 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004595 "LED", /* all others */
4596 ); /* R30, R31 */
4597
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004598#define TPACPI_LED_NUMLEDS 8
4599static struct tpacpi_led_classdev *tpacpi_leds;
4600static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004601static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004602 /* there's a limit of 19 chars + NULL before 2.6.26 */
4603 "tpacpi::power",
4604 "tpacpi:orange:batt",
4605 "tpacpi:green:batt",
4606 "tpacpi::dock_active",
4607 "tpacpi::bay_active",
4608 "tpacpi::dock_batt",
4609 "tpacpi::unknown_led",
4610 "tpacpi::standby",
4611};
4612
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004613static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004614{
4615 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004616 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004617
4618 switch (led_supported) {
4619 case TPACPI_LED_570:
4620 if (!acpi_evalf(ec_handle,
4621 &status, "GLED", "dd", 1 << led))
4622 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004623 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004624 TPACPI_LED_OFF :
4625 ((status == 1)?
4626 TPACPI_LED_ON :
4627 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004628 tpacpi_led_state_cache[led] = led_s;
4629 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004630 default:
4631 return -ENXIO;
4632 }
4633
4634 /* not reached */
4635}
4636
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004637static int led_set_status(const unsigned int led,
4638 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004639{
4640 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004641 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4642 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004643
4644 int rc = 0;
4645
4646 switch (led_supported) {
4647 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004648 /* 570 */
4649 if (led > 7)
4650 return -EINVAL;
4651 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4652 (1 << led), led_sled_arg1[ledstatus]))
4653 rc = -EIO;
4654 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004655 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004656 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4657 if (led > 7)
4658 return -EINVAL;
4659 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4660 if (rc >= 0)
4661 rc = ec_write(TPACPI_LED_EC_HLBL,
4662 (ledstatus == TPACPI_LED_BLINK) << led);
4663 if (rc >= 0)
4664 rc = ec_write(TPACPI_LED_EC_HLCL,
4665 (ledstatus != TPACPI_LED_OFF) << led);
4666 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004667 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004668 /* all others */
4669 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4670 led, led_led_arg1[ledstatus]))
4671 rc = -EIO;
4672 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004673 default:
4674 rc = -ENXIO;
4675 }
4676
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004677 if (!rc)
4678 tpacpi_led_state_cache[led] = ledstatus;
4679
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004680 return rc;
4681}
4682
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004683static void led_sysfs_set_status(unsigned int led,
4684 enum led_brightness brightness)
4685{
4686 led_set_status(led,
4687 (brightness == LED_OFF) ?
4688 TPACPI_LED_OFF :
4689 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4690 TPACPI_LED_BLINK : TPACPI_LED_ON);
4691}
4692
4693static void led_set_status_worker(struct work_struct *work)
4694{
4695 struct tpacpi_led_classdev *data =
4696 container_of(work, struct tpacpi_led_classdev, work);
4697
4698 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4699 led_sysfs_set_status(data->led, data->new_brightness);
4700}
4701
4702static void led_sysfs_set(struct led_classdev *led_cdev,
4703 enum led_brightness brightness)
4704{
4705 struct tpacpi_led_classdev *data = container_of(led_cdev,
4706 struct tpacpi_led_classdev, led_classdev);
4707
4708 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004709 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004710}
4711
4712static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4713 unsigned long *delay_on, unsigned long *delay_off)
4714{
4715 struct tpacpi_led_classdev *data = container_of(led_cdev,
4716 struct tpacpi_led_classdev, led_classdev);
4717
4718 /* Can we choose the flash rate? */
4719 if (*delay_on == 0 && *delay_off == 0) {
4720 /* yes. set them to the hardware blink rate (1 Hz) */
4721 *delay_on = 500; /* ms */
4722 *delay_off = 500; /* ms */
4723 } else if ((*delay_on != 500) || (*delay_off != 500))
4724 return -EINVAL;
4725
4726 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004727 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004728
4729 return 0;
4730}
4731
4732static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4733{
4734 int rc;
4735
4736 struct tpacpi_led_classdev *data = container_of(led_cdev,
4737 struct tpacpi_led_classdev, led_classdev);
4738
4739 rc = led_get_status(data->led);
4740
4741 if (rc == TPACPI_LED_OFF || rc < 0)
4742 rc = LED_OFF; /* no error handling in led class :( */
4743 else
4744 rc = LED_FULL;
4745
4746 return rc;
4747}
4748
4749static void led_exit(void)
4750{
4751 unsigned int i;
4752
4753 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4754 if (tpacpi_leds[i].led_classdev.name)
4755 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4756 }
4757
4758 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004759}
4760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004761static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004762{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004763 unsigned int i;
4764 int rc;
4765
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004766 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4767
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004768 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004769
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004770 if (!led_handle)
4771 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004772 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004773 else if (strlencmp(led_path, "SLED") == 0)
4774 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004775 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004776 else if (strlencmp(led_path, "SYSL") == 0)
4777 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004778 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004779 else
4780 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004781 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004782
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004783 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4784 str_supported(led_supported), led_supported);
4785
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004786 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4787 GFP_KERNEL);
4788 if (!tpacpi_leds) {
4789 printk(TPACPI_ERR "Out of memory for LED data\n");
4790 return -ENOMEM;
4791 }
4792
4793 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4794 tpacpi_leds[i].led = i;
4795
4796 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4797 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4798 if (led_supported == TPACPI_LED_570)
4799 tpacpi_leds[i].led_classdev.brightness_get =
4800 &led_sysfs_get;
4801
4802 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4803
4804 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4805
4806 rc = led_classdev_register(&tpacpi_pdev->dev,
4807 &tpacpi_leds[i].led_classdev);
4808 if (rc < 0) {
4809 tpacpi_leds[i].led_classdev.name = NULL;
4810 led_exit();
4811 return rc;
4812 }
4813 }
4814
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004815 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004816}
4817
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004818#define str_led_status(s) \
4819 ((s) == TPACPI_LED_OFF ? "off" : \
4820 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821
4822static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
4824 int len = 0;
4825
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004826 if (!led_supported) {
4827 len += sprintf(p + len, "status:\t\tnot supported\n");
4828 return len;
4829 }
4830 len += sprintf(p + len, "status:\t\tsupported\n");
4831
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004832 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004833 /* 570 */
4834 int i, status;
4835 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004836 status = led_get_status(i);
4837 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004838 return -EIO;
4839 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004840 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004841 }
4842 }
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004845 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847 return len;
4848}
4849
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004850static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004853 int led, rc;
4854 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004855
4856 if (!led_supported)
4857 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
4859 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004860 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return -EINVAL;
4862
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004864 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004867 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004868 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004869 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004870 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004872
4873 rc = led_set_status(led, s);
4874 if (rc < 0)
4875 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 }
4877
4878 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004879}
4880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004881static struct ibm_struct led_driver_data = {
4882 .name = "led",
4883 .read = led_read,
4884 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004885 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004886};
4887
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004888/*************************************************************************
4889 * Beep subdriver
4890 */
4891
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004892TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004893
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004894static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004895{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004896 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4897
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004898 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004899
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004900 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4901 str_supported(beep_handle != NULL));
4902
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004903 return (beep_handle)? 0 : 1;
4904}
4905
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004906static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 int len = 0;
4909
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004910 if (!beep_handle)
4911 len += sprintf(p + len, "status:\t\tnot supported\n");
4912 else {
4913 len += sprintf(p + len, "status:\t\tsupported\n");
4914 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 return len;
4918}
4919
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004920static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
4922 char *cmd;
4923 int beep_cmd;
4924
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925 if (!beep_handle)
4926 return -ENODEV;
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004929 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4930 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 /* beep_cmd set */
4932 } else
4933 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004934 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 return -EIO;
4936 }
4937
4938 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004939}
4940
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004941static struct ibm_struct beep_driver_data = {
4942 .name = "beep",
4943 .read = beep_read,
4944 .write = beep_write,
4945};
4946
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004947/*************************************************************************
4948 * Thermal subdriver
4949 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004951enum thermal_access_mode {
4952 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4953 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4954 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4955 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4956 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4957};
4958
4959enum { /* TPACPI_THERMAL_TPEC_* */
4960 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4961 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4962 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4963};
4964
4965#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4966struct ibm_thermal_sensors_struct {
4967 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4968};
4969
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004970static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004971
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004972/* idx is zero-based */
4973static int thermal_get_sensor(int idx, s32 *value)
4974{
4975 int t;
4976 s8 tmp;
4977 char tmpi[5];
4978
4979 t = TP_EC_THERMAL_TMP0;
4980
4981 switch (thermal_read_mode) {
4982#if TPACPI_MAX_THERMAL_SENSORS >= 16
4983 case TPACPI_THERMAL_TPEC_16:
4984 if (idx >= 8 && idx <= 15) {
4985 t = TP_EC_THERMAL_TMP8;
4986 idx -= 8;
4987 }
4988 /* fallthrough */
4989#endif
4990 case TPACPI_THERMAL_TPEC_8:
4991 if (idx <= 7) {
4992 if (!acpi_ec_read(t + idx, &tmp))
4993 return -EIO;
4994 *value = tmp * 1000;
4995 return 0;
4996 }
4997 break;
4998
4999 case TPACPI_THERMAL_ACPI_UPDT:
5000 if (idx <= 7) {
5001 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5002 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5003 return -EIO;
5004 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5005 return -EIO;
5006 *value = (t - 2732) * 100;
5007 return 0;
5008 }
5009 break;
5010
5011 case TPACPI_THERMAL_ACPI_TMP07:
5012 if (idx <= 7) {
5013 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5014 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5015 return -EIO;
5016 if (t > 127 || t < -127)
5017 t = TP_EC_THERMAL_TMP_NA;
5018 *value = t * 1000;
5019 return 0;
5020 }
5021 break;
5022
5023 case TPACPI_THERMAL_NONE:
5024 default:
5025 return -ENOSYS;
5026 }
5027
5028 return -EINVAL;
5029}
5030
5031static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5032{
5033 int res, i;
5034 int n;
5035
5036 n = 8;
5037 i = 0;
5038
5039 if (!s)
5040 return -EINVAL;
5041
5042 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5043 n = 16;
5044
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005045 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005046 res = thermal_get_sensor(i, &s->temp[i]);
5047 if (res)
5048 return res;
5049 }
5050
5051 return n;
5052}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005053
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005054/* sysfs temp##_input -------------------------------------------------- */
5055
5056static ssize_t thermal_temp_input_show(struct device *dev,
5057 struct device_attribute *attr,
5058 char *buf)
5059{
5060 struct sensor_device_attribute *sensor_attr =
5061 to_sensor_dev_attr(attr);
5062 int idx = sensor_attr->index;
5063 s32 value;
5064 int res;
5065
5066 res = thermal_get_sensor(idx, &value);
5067 if (res)
5068 return res;
5069 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5070 return -ENXIO;
5071
5072 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5073}
5074
5075#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005076 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5077 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005078
5079static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5080 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5081 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5082 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5083 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5084 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5085 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5086 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5087 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5088 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5089 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5090 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5091 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5092 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5093 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5094 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5095 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5096};
5097
5098#define THERMAL_ATTRS(X) \
5099 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5100
5101static struct attribute *thermal_temp_input_attr[] = {
5102 THERMAL_ATTRS(8),
5103 THERMAL_ATTRS(9),
5104 THERMAL_ATTRS(10),
5105 THERMAL_ATTRS(11),
5106 THERMAL_ATTRS(12),
5107 THERMAL_ATTRS(13),
5108 THERMAL_ATTRS(14),
5109 THERMAL_ATTRS(15),
5110 THERMAL_ATTRS(0),
5111 THERMAL_ATTRS(1),
5112 THERMAL_ATTRS(2),
5113 THERMAL_ATTRS(3),
5114 THERMAL_ATTRS(4),
5115 THERMAL_ATTRS(5),
5116 THERMAL_ATTRS(6),
5117 THERMAL_ATTRS(7),
5118 NULL
5119};
5120
5121static const struct attribute_group thermal_temp_input16_group = {
5122 .attrs = thermal_temp_input_attr
5123};
5124
5125static const struct attribute_group thermal_temp_input8_group = {
5126 .attrs = &thermal_temp_input_attr[8]
5127};
5128
5129#undef THERMAL_SENSOR_ATTR_TEMP
5130#undef THERMAL_ATTRS
5131
5132/* --------------------------------------------------------------------- */
5133
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005134static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005135{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005136 u8 t, ta1, ta2;
5137 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005138 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005139 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005140
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005141 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5142
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005143 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005145 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005146 /*
5147 * Direct EC access mode: sensors at registers
5148 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5149 * non-implemented, thermal sensors return 0x80 when
5150 * not available
5151 */
5152
5153 ta1 = ta2 = 0;
5154 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005155 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005156 ta1 |= t;
5157 } else {
5158 ta1 = 0;
5159 break;
5160 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005161 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005162 ta2 |= t;
5163 } else {
5164 ta1 = 0;
5165 break;
5166 }
5167 }
5168 if (ta1 == 0) {
5169 /* This is sheer paranoia, but we handle it anyway */
5170 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005171 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005172 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005173 "falling back to ACPI TMPx access "
5174 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005175 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005176 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005177 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005178 "ThinkPad ACPI EC access misbehaving, "
5179 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005180 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005181 }
5182 } else {
5183 thermal_read_mode =
5184 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005185 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005186 }
5187 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005188 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5189 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005190 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005191 } else {
5192 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005193 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005194 }
5195 } else {
5196 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005197 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005198 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005199
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005200 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5201 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5202 thermal_read_mode);
5203
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005204 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005205 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005206 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005207 &thermal_temp_input16_group);
5208 if (res)
5209 return res;
5210 break;
5211 case TPACPI_THERMAL_TPEC_8:
5212 case TPACPI_THERMAL_ACPI_TMP07:
5213 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005214 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005215 &thermal_temp_input8_group);
5216 if (res)
5217 return res;
5218 break;
5219 case TPACPI_THERMAL_NONE:
5220 default:
5221 return 1;
5222 }
5223
5224 return 0;
5225}
5226
5227static void thermal_exit(void)
5228{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005229 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005230 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005231 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005232 &thermal_temp_input16_group);
5233 break;
5234 case TPACPI_THERMAL_TPEC_8:
5235 case TPACPI_THERMAL_ACPI_TMP07:
5236 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005237 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005238 &thermal_temp_input16_group);
5239 break;
5240 case TPACPI_THERMAL_NONE:
5241 default:
5242 break;
5243 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005244}
5245
5246static int thermal_read(char *p)
5247{
5248 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005249 int n, i;
5250 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005251
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005252 n = thermal_get_sensors(&t);
5253 if (unlikely(n < 0))
5254 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005255
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005256 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005257
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005258 if (n > 0) {
5259 for (i = 0; i < (n - 1); i++)
5260 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5261 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5262 } else
5263 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005264
5265 return len;
5266}
5267
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005268static struct ibm_struct thermal_driver_data = {
5269 .name = "thermal",
5270 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005271 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005272};
5273
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005274/*************************************************************************
5275 * EC Dump subdriver
5276 */
5277
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005278static u8 ecdump_regs[256];
5279
5280static int ecdump_read(char *p)
5281{
5282 int len = 0;
5283 int i, j;
5284 u8 v;
5285
5286 len += sprintf(p + len, "EC "
5287 " +00 +01 +02 +03 +04 +05 +06 +07"
5288 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5289 for (i = 0; i < 256; i += 16) {
5290 len += sprintf(p + len, "EC 0x%02x:", i);
5291 for (j = 0; j < 16; j++) {
5292 if (!acpi_ec_read(i + j, &v))
5293 break;
5294 if (v != ecdump_regs[i + j])
5295 len += sprintf(p + len, " *%02x", v);
5296 else
5297 len += sprintf(p + len, " %02x", v);
5298 ecdump_regs[i + j] = v;
5299 }
5300 len += sprintf(p + len, "\n");
5301 if (j != 16)
5302 break;
5303 }
5304
5305 /* These are way too dangerous to advertise openly... */
5306#if 0
5307 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5308 " (<offset> is 00-ff, <value> is 00-ff)\n");
5309 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5310 " (<offset> is 00-ff, <value> is 0-255)\n");
5311#endif
5312 return len;
5313}
5314
5315static int ecdump_write(char *buf)
5316{
5317 char *cmd;
5318 int i, v;
5319
5320 while ((cmd = next_cmd(&buf))) {
5321 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5322 /* i and v set */
5323 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5324 /* i and v set */
5325 } else
5326 return -EINVAL;
5327 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5328 if (!acpi_ec_write(i, v))
5329 return -EIO;
5330 } else
5331 return -EINVAL;
5332 }
5333
5334 return 0;
5335}
5336
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005337static struct ibm_struct ecdump_driver_data = {
5338 .name = "ecdump",
5339 .read = ecdump_read,
5340 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005341 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005342};
5343
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005344/*************************************************************************
5345 * Backlight/brightness subdriver
5346 */
Holger Macht8acb0252006-10-20 14:30:28 -07005347
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005348#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5349
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005350enum {
5351 TP_EC_BACKLIGHT = 0x31,
5352
5353 /* TP_EC_BACKLIGHT bitmasks */
5354 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5355 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5356 TP_EC_BACKLIGHT_MAPSW = 0x20,
5357};
5358
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005359static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005360static int brightness_mode;
5361static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5362
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005363static struct mutex brightness_mutex;
5364
5365/*
5366 * ThinkPads can read brightness from two places: EC 0x31, or
5367 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005368 *
5369 * EC 0x31 has the following layout
5370 * Bit 7: unknown function
5371 * Bit 6: unknown function
5372 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5373 * Bit 4: must be set to zero to avoid problems
5374 * Bit 3-0: backlight brightness level
5375 *
5376 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005377 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005378static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005379{
5380 u8 lec = 0, lcmos = 0, level = 0;
5381
5382 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005383 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005384 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005385 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005386 };
5387 if (brightness_mode & 2) {
5388 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5389 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5390 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5391 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5392 level = lcmos;
5393 }
5394
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005395 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005396 *status = lec; /* Prefer EC, CMOS is just a backing store */
5397 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005398 if (lec == lcmos)
5399 tp_warned.bright_cmos_ec_unsync = 0;
5400 else {
5401 if (!tp_warned.bright_cmos_ec_unsync) {
5402 printk(TPACPI_ERR
5403 "CMOS NVRAM (%u) and EC (%u) do not "
5404 "agree on display brightness level\n",
5405 (unsigned int) lcmos,
5406 (unsigned int) lec);
5407 tp_warned.bright_cmos_ec_unsync = 1;
5408 }
5409 return -EIO;
5410 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005411 } else {
5412 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005413 }
5414
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005415 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005416}
5417
5418/* May return EINTR which can always be mapped to ERESTARTSYS */
5419static int brightness_set(int value)
5420{
5421 int cmos_cmd, inc, i, res;
5422 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005423 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005424
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005425 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5426 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005427 return -EINVAL;
5428
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02005429 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005430 if (res < 0)
5431 return res;
5432
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005433 res = brightness_get_raw(&current_value);
5434 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005435 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005436
5437 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5438 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005439
5440 cmos_cmd = value > current_value ?
5441 TP_CMOS_BRIGHTNESS_UP :
5442 TP_CMOS_BRIGHTNESS_DOWN;
5443 inc = (value > current_value)? 1 : -1;
5444
5445 res = 0;
5446 for (i = current_value; i != value; i += inc) {
5447 if ((brightness_mode & 2) &&
5448 issue_thinkpad_cmos_command(cmos_cmd)) {
5449 res = -EIO;
5450 goto errout;
5451 }
5452 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005453 !acpi_ec_write(TP_EC_BACKLIGHT,
5454 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005455 res = -EIO;
5456 goto errout;;
5457 }
5458 }
5459
5460errout:
5461 mutex_unlock(&brightness_mutex);
5462 return res;
5463}
5464
5465/* sysfs backlight class ----------------------------------------------- */
5466
5467static int brightness_update_status(struct backlight_device *bd)
5468{
5469 /* it is the backlight class's job (caller) to handle
5470 * EINTR and other errors properly */
5471 return brightness_set(
5472 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5473 bd->props.power == FB_BLANK_UNBLANK) ?
5474 bd->props.brightness : 0);
5475}
Holger Macht8acb0252006-10-20 14:30:28 -07005476
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005477static int brightness_get(struct backlight_device *bd)
5478{
5479 int status, res;
5480
5481 res = brightness_get_raw(&status);
5482 if (res < 0)
5483 return 0; /* FIXME: teach backlight about error handling */
5484
5485 return status & TP_EC_BACKLIGHT_LVLMSK;
5486}
5487
Richard Purdie599a52d2007-02-10 23:07:48 +00005488static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005489 .get_brightness = brightness_get,
5490 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005491};
5492
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005493/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005495static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005496{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005497 int b;
5498
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005499 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5500
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005501 mutex_init(&brightness_mutex);
5502
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005503 /*
5504 * We always attempt to detect acpi support, so as to switch
5505 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5506 * going to publish a backlight interface
5507 */
5508 b = tpacpi_check_std_acpi_brightness_support();
5509 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005510
5511 if (acpi_video_backlight_support()) {
5512 if (brightness_enable > 1) {
5513 printk(TPACPI_NOTICE
5514 "Standard ACPI backlight interface "
5515 "available, not loading native one.\n");
5516 return 1;
5517 } else if (brightness_enable == 1) {
5518 printk(TPACPI_NOTICE
5519 "Backlight control force enabled, even if standard "
5520 "ACPI backlight interface is available\n");
5521 }
5522 } else {
5523 if (brightness_enable > 1) {
5524 printk(TPACPI_NOTICE
5525 "Standard ACPI backlight interface not "
5526 "available, thinkpad_acpi native "
5527 "brightness control enabled\n");
5528 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005529 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005530 }
5531
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005532 if (!brightness_enable) {
5533 dbg_printk(TPACPI_DBG_INIT,
5534 "brightness support disabled by "
5535 "module parameter\n");
5536 return 1;
5537 }
5538
5539 if (b > 16) {
5540 printk(TPACPI_ERR
5541 "Unsupported brightness interface, "
5542 "please contact %s\n", TPACPI_MAIL);
5543 return 1;
5544 }
5545 if (b == 16)
5546 tp_features.bright_16levels = 1;
5547
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005548 if (!brightness_mode) {
5549 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5550 brightness_mode = 2;
5551 else
5552 brightness_mode = 3;
5553
5554 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5555 brightness_mode);
5556 }
5557
5558 if (brightness_mode > 3)
5559 return -EINVAL;
5560
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005561 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005562 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005563
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005564 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 printk(TPACPI_INFO
5566 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005567
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005568 ibm_backlight_device = backlight_device_register(
5569 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5570 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005571 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005572 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005573 return PTR_ERR(ibm_backlight_device);
5574 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005575 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005576
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005577 ibm_backlight_device->props.max_brightness =
5578 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005579 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005580 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005581
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005582 return 0;
5583}
5584
5585static void brightness_exit(void)
5586{
5587 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005588 vdbg_printk(TPACPI_DBG_EXIT,
5589 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005590 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005591 }
5592}
5593
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005594static int brightness_read(char *p)
5595{
5596 int len = 0;
5597 int level;
5598
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005599 level = brightness_get(NULL);
5600 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005601 len += sprintf(p + len, "level:\t\tunreadable\n");
5602 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005603 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005604 len += sprintf(p + len, "commands:\tup, down\n");
5605 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005606 " (<level> is 0-%d)\n",
5607 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005608 }
5609
5610 return len;
5611}
5612
5613static int brightness_write(char *buf)
5614{
5615 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005616 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005617 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005618 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005619
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005620 level = brightness_get(NULL);
5621 if (level < 0)
5622 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005623
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005624 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005625 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005626 if (level < max_level)
5627 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005628 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005629 if (level > 0)
5630 level--;
5631 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5632 level >= 0 && level <= max_level) {
5633 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005634 } else
5635 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005636 }
5637
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005638 /*
5639 * Now we know what the final level should be, so we try to set it.
5640 * Doing it this way makes the syscall restartable in case of EINTR
5641 */
5642 rc = brightness_set(level);
5643 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005644}
5645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005646static struct ibm_struct brightness_driver_data = {
5647 .name = "brightness",
5648 .read = brightness_read,
5649 .write = brightness_write,
5650 .exit = brightness_exit,
5651};
5652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005653/*************************************************************************
5654 * Volume subdriver
5655 */
5656
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005657static int volume_offset = 0x30;
5658
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005659static int volume_read(char *p)
5660{
5661 int len = 0;
5662 u8 level;
5663
5664 if (!acpi_ec_read(volume_offset, &level)) {
5665 len += sprintf(p + len, "level:\t\tunreadable\n");
5666 } else {
5667 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5668 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5669 len += sprintf(p + len, "commands:\tup, down, mute\n");
5670 len += sprintf(p + len, "commands:\tlevel <level>"
5671 " (<level> is 0-15)\n");
5672 }
5673
5674 return len;
5675}
5676
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005677static int volume_write(char *buf)
5678{
5679 int cmos_cmd, inc, i;
5680 u8 level, mute;
5681 int new_level, new_mute;
5682 char *cmd;
5683
5684 while ((cmd = next_cmd(&buf))) {
5685 if (!acpi_ec_read(volume_offset, &level))
5686 return -EIO;
5687 new_mute = mute = level & 0x40;
5688 new_level = level = level & 0xf;
5689
5690 if (strlencmp(cmd, "up") == 0) {
5691 if (mute)
5692 new_mute = 0;
5693 else
5694 new_level = level == 15 ? 15 : level + 1;
5695 } else if (strlencmp(cmd, "down") == 0) {
5696 if (mute)
5697 new_mute = 0;
5698 else
5699 new_level = level == 0 ? 0 : level - 1;
5700 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5701 new_level >= 0 && new_level <= 15) {
5702 /* new_level set */
5703 } else if (strlencmp(cmd, "mute") == 0) {
5704 new_mute = 0x40;
5705 } else
5706 return -EINVAL;
5707
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005708 if (new_level != level) {
5709 /* mute doesn't change */
5710
5711 cmos_cmd = (new_level > level) ?
5712 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005713 inc = new_level > level ? 1 : -1;
5714
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005715 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005716 !acpi_ec_write(volume_offset, level)))
5717 return -EIO;
5718
5719 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005720 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005721 !acpi_ec_write(volume_offset, i + inc))
5722 return -EIO;
5723
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005724 if (mute &&
5725 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5726 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005727 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005728 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005729 }
5730
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005731 if (new_mute != mute) {
5732 /* level doesn't change */
5733
5734 cmos_cmd = (new_mute) ?
5735 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005736
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005737 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005738 !acpi_ec_write(volume_offset, level + new_mute))
5739 return -EIO;
5740 }
5741 }
5742
5743 return 0;
5744}
5745
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005746static struct ibm_struct volume_driver_data = {
5747 .name = "volume",
5748 .read = volume_read,
5749 .write = volume_write,
5750};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005751
5752/*************************************************************************
5753 * Fan subdriver
5754 */
5755
5756/*
5757 * FAN ACCESS MODES
5758 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005759 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005760 * ACPI GFAN method: returns fan level
5761 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005762 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005763 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005764 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005765 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005766 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5767 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005768 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5769 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005770 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005771 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005772 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5773 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005774 *
5775 * Fan speed changes of any sort (including those caused by the
5776 * disengaged mode) are usually done slowly by the firmware as the
5777 * maximum ammount of fan duty cycle change per second seems to be
5778 * limited.
5779 *
5780 * Reading is not available if GFAN exists.
5781 * Writing is not available if SFAN exists.
5782 *
5783 * Bits
5784 * 7 automatic mode engaged;
5785 * (default operation mode of the ThinkPad)
5786 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005787 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005788 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005789 * the tachometer while the fan controller ramps up
5790 * the speed (which can take up to a few *minutes*).
5791 * Speeds up fan to 100% duty-cycle, which is far above
5792 * the standard RPM levels. It is not impossible that
5793 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005794 * 5-3 unused in some models. Extra bits for fan level
5795 * in others, but still useless as all values above
5796 * 7 map to the same speed as level 7 in these models.
5797 * 2-0 fan level (0..7 usually)
5798 * 0x00 = stop
5799 * 0x07 = max (set when temperatures critical)
5800 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005801 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005802 *
5803 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5804 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5805 * does so, its initial value is meaningless (0x07).
5806 *
5807 * For firmware bugs, refer to:
5808 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5809 *
5810 * ----
5811 *
5812 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5813 * Main fan tachometer reading (in RPM)
5814 *
5815 * This register is present on all ThinkPads with a new-style EC, and
5816 * it is known not to be present on the A21m/e, and T22, as there is
5817 * something else in offset 0x84 according to the ACPI DSDT. Other
5818 * ThinkPads from this same time period (and earlier) probably lack the
5819 * tachometer as well.
5820 *
5821 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5822 * was never fixed by IBM to report the EC firmware version string
5823 * probably support the tachometer (like the early X models), so
5824 * detecting it is quite hard. We need more data to know for sure.
5825 *
5826 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5827 * might result.
5828 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005829 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5830 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831 *
5832 * For firmware bugs, refer to:
5833 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5834 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005835 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005836 * ThinkPad X31, X40, X41. Not available in the X60.
5837 *
5838 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5839 * high speed. ACPI DSDT seems to map these three speeds to levels
5840 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5841 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5842 *
5843 * The speeds are stored on handles
5844 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5845 *
5846 * There are three default speed sets, acessible as handles:
5847 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5848 *
5849 * ACPI DSDT switches which set is in use depending on various
5850 * factors.
5851 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005852 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005853 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5854 * but the ACPI tables just mention level 7.
5855 */
5856
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005857enum { /* Fan control constants */
5858 fan_status_offset = 0x2f, /* EC register 0x2f */
5859 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5860 * 0x84 must be read before 0x85 */
5861
5862 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5863 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5864
5865 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5866};
5867
5868enum fan_status_access_mode {
5869 TPACPI_FAN_NONE = 0, /* No fan status or control */
5870 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5871 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5872};
5873
5874enum fan_control_access_mode {
5875 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5876 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5877 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5878 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5879};
5880
5881enum fan_control_commands {
5882 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5883 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5884 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5885 * and also watchdog cmd */
5886};
5887
5888static int fan_control_allowed;
5889
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005890static enum fan_status_access_mode fan_status_access_mode;
5891static enum fan_control_access_mode fan_control_access_mode;
5892static enum fan_control_commands fan_control_commands;
5893
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005894static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005895static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005896static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005897static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005898
5899static struct mutex fan_mutex;
5900
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005901static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005902static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005903
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005904TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5905TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005906 "\\FSPD", /* 600e/x, 770e, 770x */
5907 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005908TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005909 "JFNS", /* 770x-JL */
5910 ); /* all others */
5911
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005912/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02005913 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5914 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5915 * be in auto mode (0x80).
5916 *
5917 * This is corrected by any write to HFSP either by the driver, or
5918 * by the firmware.
5919 *
5920 * We assume 0x07 really means auto mode while this quirk is active,
5921 * as this is far more likely than the ThinkPad being in level 7,
5922 * which is only used by the firmware during thermal emergencies.
5923 */
5924
5925static void fan_quirk1_detect(void)
5926{
5927 /* In some ThinkPads, neither the EC nor the ACPI
5928 * DSDT initialize the HFSP register, and it ends up
5929 * being initially set to 0x07 when it *could* be
5930 * either 0x07 or 0x80.
5931 *
5932 * Enable for TP-1Y (T43), TP-78 (R51e),
5933 * TP-76 (R52), TP-70 (T43, R52), which are known
5934 * to be buggy. */
5935 if (fan_control_initial_status == 0x07) {
5936 switch (thinkpad_id.ec_model) {
5937 case 0x5931: /* TP-1Y */
5938 case 0x3837: /* TP-78 */
5939 case 0x3637: /* TP-76 */
5940 case 0x3037: /* TP-70 */
5941 printk(TPACPI_NOTICE
5942 "fan_init: initial fan status is unknown, "
5943 "assuming it is in auto mode\n");
5944 tp_features.fan_ctrl_status_undef = 1;
5945 ;;
5946 }
5947 }
5948}
5949
5950static void fan_quirk1_handle(u8 *fan_status)
5951{
5952 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5953 if (*fan_status != fan_control_initial_status) {
5954 /* something changed the HFSP regisnter since
5955 * driver init time, so it is not undefined
5956 * anymore */
5957 tp_features.fan_ctrl_status_undef = 0;
5958 } else {
5959 /* Return most likely status. In fact, it
5960 * might be the only possible status */
5961 *fan_status = TP_EC_FAN_AUTO;
5962 }
5963 }
5964}
5965
5966/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005967 * Call with fan_mutex held
5968 */
5969static void fan_update_desired_level(u8 status)
5970{
5971 if ((status &
5972 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5973 if (status > 7)
5974 fan_control_desired_level = 7;
5975 else
5976 fan_control_desired_level = status;
5977 }
5978}
5979
5980static int fan_get_status(u8 *status)
5981{
5982 u8 s;
5983
5984 /* TODO:
5985 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5986
5987 switch (fan_status_access_mode) {
5988 case TPACPI_FAN_RD_ACPI_GFAN:
5989 /* 570, 600e/x, 770e, 770x */
5990
5991 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5992 return -EIO;
5993
5994 if (likely(status))
5995 *status = s & 0x07;
5996
5997 break;
5998
5999 case TPACPI_FAN_RD_TPEC:
6000 /* all except 570, 600e/x, 770e, 770x */
6001 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6002 return -EIO;
6003
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006004 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006005 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006006 fan_quirk1_handle(status);
6007 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006008
6009 break;
6010
6011 default:
6012 return -ENXIO;
6013 }
6014
6015 return 0;
6016}
6017
6018static int fan_get_status_safe(u8 *status)
6019{
6020 int rc;
6021 u8 s;
6022
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006023 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006024 return -ERESTARTSYS;
6025 rc = fan_get_status(&s);
6026 if (!rc)
6027 fan_update_desired_level(s);
6028 mutex_unlock(&fan_mutex);
6029
6030 if (status)
6031 *status = s;
6032
6033 return rc;
6034}
6035
6036static int fan_get_speed(unsigned int *speed)
6037{
6038 u8 hi, lo;
6039
6040 switch (fan_status_access_mode) {
6041 case TPACPI_FAN_RD_TPEC:
6042 /* all except 570, 600e/x, 770e, 770x */
6043 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6044 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6045 return -EIO;
6046
6047 if (likely(speed))
6048 *speed = (hi << 8) | lo;
6049
6050 break;
6051
6052 default:
6053 return -ENXIO;
6054 }
6055
6056 return 0;
6057}
6058
6059static int fan_set_level(int level)
6060{
6061 if (!fan_control_allowed)
6062 return -EPERM;
6063
6064 switch (fan_control_access_mode) {
6065 case TPACPI_FAN_WR_ACPI_SFAN:
6066 if (level >= 0 && level <= 7) {
6067 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6068 return -EIO;
6069 } else
6070 return -EINVAL;
6071 break;
6072
6073 case TPACPI_FAN_WR_ACPI_FANS:
6074 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006075 if (!(level & TP_EC_FAN_AUTO) &&
6076 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006077 ((level < 0) || (level > 7)))
6078 return -EINVAL;
6079
6080 /* safety net should the EC not support AUTO
6081 * or FULLSPEED mode bits and just ignore them */
6082 if (level & TP_EC_FAN_FULLSPEED)
6083 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01006084 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006085 level |= 4; /* safety min speed 4 */
6086
6087 if (!acpi_ec_write(fan_status_offset, level))
6088 return -EIO;
6089 else
6090 tp_features.fan_ctrl_status_undef = 0;
6091 break;
6092
6093 default:
6094 return -ENXIO;
6095 }
6096 return 0;
6097}
6098
6099static int fan_set_level_safe(int level)
6100{
6101 int rc;
6102
6103 if (!fan_control_allowed)
6104 return -EPERM;
6105
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006106 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006107 return -ERESTARTSYS;
6108
6109 if (level == TPACPI_FAN_LAST_LEVEL)
6110 level = fan_control_desired_level;
6111
6112 rc = fan_set_level(level);
6113 if (!rc)
6114 fan_update_desired_level(level);
6115
6116 mutex_unlock(&fan_mutex);
6117 return rc;
6118}
6119
6120static int fan_set_enable(void)
6121{
6122 u8 s;
6123 int rc;
6124
6125 if (!fan_control_allowed)
6126 return -EPERM;
6127
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006128 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006129 return -ERESTARTSYS;
6130
6131 switch (fan_control_access_mode) {
6132 case TPACPI_FAN_WR_ACPI_FANS:
6133 case TPACPI_FAN_WR_TPEC:
6134 rc = fan_get_status(&s);
6135 if (rc < 0)
6136 break;
6137
6138 /* Don't go out of emergency fan mode */
6139 if (s != 7) {
6140 s &= 0x07;
6141 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6142 }
6143
6144 if (!acpi_ec_write(fan_status_offset, s))
6145 rc = -EIO;
6146 else {
6147 tp_features.fan_ctrl_status_undef = 0;
6148 rc = 0;
6149 }
6150 break;
6151
6152 case TPACPI_FAN_WR_ACPI_SFAN:
6153 rc = fan_get_status(&s);
6154 if (rc < 0)
6155 break;
6156
6157 s &= 0x07;
6158
6159 /* Set fan to at least level 4 */
6160 s |= 4;
6161
6162 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006163 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006164 else
6165 rc = 0;
6166 break;
6167
6168 default:
6169 rc = -ENXIO;
6170 }
6171
6172 mutex_unlock(&fan_mutex);
6173 return rc;
6174}
6175
6176static int fan_set_disable(void)
6177{
6178 int rc;
6179
6180 if (!fan_control_allowed)
6181 return -EPERM;
6182
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006183 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006184 return -ERESTARTSYS;
6185
6186 rc = 0;
6187 switch (fan_control_access_mode) {
6188 case TPACPI_FAN_WR_ACPI_FANS:
6189 case TPACPI_FAN_WR_TPEC:
6190 if (!acpi_ec_write(fan_status_offset, 0x00))
6191 rc = -EIO;
6192 else {
6193 fan_control_desired_level = 0;
6194 tp_features.fan_ctrl_status_undef = 0;
6195 }
6196 break;
6197
6198 case TPACPI_FAN_WR_ACPI_SFAN:
6199 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6200 rc = -EIO;
6201 else
6202 fan_control_desired_level = 0;
6203 break;
6204
6205 default:
6206 rc = -ENXIO;
6207 }
6208
6209
6210 mutex_unlock(&fan_mutex);
6211 return rc;
6212}
6213
6214static int fan_set_speed(int speed)
6215{
6216 int rc;
6217
6218 if (!fan_control_allowed)
6219 return -EPERM;
6220
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006221 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006222 return -ERESTARTSYS;
6223
6224 rc = 0;
6225 switch (fan_control_access_mode) {
6226 case TPACPI_FAN_WR_ACPI_FANS:
6227 if (speed >= 0 && speed <= 65535) {
6228 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6229 speed, speed, speed))
6230 rc = -EIO;
6231 } else
6232 rc = -EINVAL;
6233 break;
6234
6235 default:
6236 rc = -ENXIO;
6237 }
6238
6239 mutex_unlock(&fan_mutex);
6240 return rc;
6241}
6242
6243static void fan_watchdog_reset(void)
6244{
6245 static int fan_watchdog_active;
6246
6247 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6248 return;
6249
6250 if (fan_watchdog_active)
6251 cancel_delayed_work(&fan_watchdog_task);
6252
6253 if (fan_watchdog_maxinterval > 0 &&
6254 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6255 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006256 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006257 msecs_to_jiffies(fan_watchdog_maxinterval
6258 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006259 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006260 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006261 "watchdog will not trigger\n");
6262 }
6263 } else
6264 fan_watchdog_active = 0;
6265}
6266
6267static void fan_watchdog_fire(struct work_struct *ignored)
6268{
6269 int rc;
6270
6271 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6272 return;
6273
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006274 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006275 rc = fan_set_enable();
6276 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006277 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006278 "will try again later...\n", -rc);
6279 /* reschedule for later */
6280 fan_watchdog_reset();
6281 }
6282}
6283
6284/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006285 * SYSFS fan layout: hwmon compatible (device)
6286 *
6287 * pwm*_enable:
6288 * 0: "disengaged" mode
6289 * 1: manual mode
6290 * 2: native EC "auto" mode (recommended, hardware default)
6291 *
6292 * pwm*: set speed in manual mode, ignored otherwise.
6293 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6294 * interpolation.
6295 *
6296 * fan*_input: tachometer reading, RPM
6297 *
6298 *
6299 * SYSFS fan layout: extensions
6300 *
6301 * fan_watchdog (driver):
6302 * fan watchdog interval in seconds, 0 disables (default), max 120
6303 */
6304
6305/* sysfs fan pwm1_enable ----------------------------------------------- */
6306static ssize_t fan_pwm1_enable_show(struct device *dev,
6307 struct device_attribute *attr,
6308 char *buf)
6309{
6310 int res, mode;
6311 u8 status;
6312
6313 res = fan_get_status_safe(&status);
6314 if (res)
6315 return res;
6316
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006317 if (status & TP_EC_FAN_FULLSPEED) {
6318 mode = 0;
6319 } else if (status & TP_EC_FAN_AUTO) {
6320 mode = 2;
6321 } else
6322 mode = 1;
6323
6324 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6325}
6326
6327static ssize_t fan_pwm1_enable_store(struct device *dev,
6328 struct device_attribute *attr,
6329 const char *buf, size_t count)
6330{
6331 unsigned long t;
6332 int res, level;
6333
6334 if (parse_strtoul(buf, 2, &t))
6335 return -EINVAL;
6336
6337 switch (t) {
6338 case 0:
6339 level = TP_EC_FAN_FULLSPEED;
6340 break;
6341 case 1:
6342 level = TPACPI_FAN_LAST_LEVEL;
6343 break;
6344 case 2:
6345 level = TP_EC_FAN_AUTO;
6346 break;
6347 case 3:
6348 /* reserved for software-controlled auto mode */
6349 return -ENOSYS;
6350 default:
6351 return -EINVAL;
6352 }
6353
6354 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006355 if (res == -ENXIO)
6356 return -EINVAL;
6357 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006358 return res;
6359
6360 fan_watchdog_reset();
6361
6362 return count;
6363}
6364
6365static struct device_attribute dev_attr_fan_pwm1_enable =
6366 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6367 fan_pwm1_enable_show, fan_pwm1_enable_store);
6368
6369/* sysfs fan pwm1 ------------------------------------------------------ */
6370static ssize_t fan_pwm1_show(struct device *dev,
6371 struct device_attribute *attr,
6372 char *buf)
6373{
6374 int res;
6375 u8 status;
6376
6377 res = fan_get_status_safe(&status);
6378 if (res)
6379 return res;
6380
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006381 if ((status &
6382 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6383 status = fan_control_desired_level;
6384
6385 if (status > 7)
6386 status = 7;
6387
6388 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6389}
6390
6391static ssize_t fan_pwm1_store(struct device *dev,
6392 struct device_attribute *attr,
6393 const char *buf, size_t count)
6394{
6395 unsigned long s;
6396 int rc;
6397 u8 status, newlevel;
6398
6399 if (parse_strtoul(buf, 255, &s))
6400 return -EINVAL;
6401
6402 /* scale down from 0-255 to 0-7 */
6403 newlevel = (s >> 5) & 0x07;
6404
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006405 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006406 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006407
6408 rc = fan_get_status(&status);
6409 if (!rc && (status &
6410 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6411 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006412 if (rc == -ENXIO)
6413 rc = -EINVAL;
6414 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006415 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006416 fan_watchdog_reset();
6417 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006418 }
6419
6420 mutex_unlock(&fan_mutex);
6421 return (rc)? rc : count;
6422}
6423
6424static struct device_attribute dev_attr_fan_pwm1 =
6425 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6426 fan_pwm1_show, fan_pwm1_store);
6427
6428/* sysfs fan fan1_input ------------------------------------------------ */
6429static ssize_t fan_fan1_input_show(struct device *dev,
6430 struct device_attribute *attr,
6431 char *buf)
6432{
6433 int res;
6434 unsigned int speed;
6435
6436 res = fan_get_speed(&speed);
6437 if (res < 0)
6438 return res;
6439
6440 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6441}
6442
6443static struct device_attribute dev_attr_fan_fan1_input =
6444 __ATTR(fan1_input, S_IRUGO,
6445 fan_fan1_input_show, NULL);
6446
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006447/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006448static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6449 char *buf)
6450{
6451 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6452}
6453
6454static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6455 const char *buf, size_t count)
6456{
6457 unsigned long t;
6458
6459 if (parse_strtoul(buf, 120, &t))
6460 return -EINVAL;
6461
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006462 if (!fan_control_allowed)
6463 return -EPERM;
6464
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006465 fan_watchdog_maxinterval = t;
6466 fan_watchdog_reset();
6467
6468 return count;
6469}
6470
6471static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6472 fan_fan_watchdog_show, fan_fan_watchdog_store);
6473
6474/* --------------------------------------------------------------------- */
6475static struct attribute *fan_attributes[] = {
6476 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6477 &dev_attr_fan_fan1_input.attr,
6478 NULL
6479};
6480
6481static const struct attribute_group fan_attr_group = {
6482 .attrs = fan_attributes,
6483};
6484
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006485static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006486{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006487 int rc;
6488
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006489 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6490
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006491 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006492 fan_status_access_mode = TPACPI_FAN_NONE;
6493 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006494 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006495 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006496 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006497 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006498
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006499 TPACPI_ACPIHANDLE_INIT(fans);
6500 TPACPI_ACPIHANDLE_INIT(gfan);
6501 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006502
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006503 if (gfan_handle) {
6504 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006505 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006506 } else {
6507 /* all other ThinkPads: note that even old-style
6508 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006509 if (likely(acpi_ec_read(fan_status_offset,
6510 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006511 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02006512 fan_quirk1_detect();
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006513 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006514 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006515 "ThinkPad ACPI EC access misbehaving, "
6516 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006517 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006518 }
6519 }
6520
6521 if (sfan_handle) {
6522 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006523 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006524 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006525 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006526 } else {
6527 if (!gfan_handle) {
6528 /* gfan without sfan means no fan control */
6529 /* all other models implement TP EC 0x2f control */
6530
6531 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006532 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006533 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006534 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006535 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006536 TPACPI_FAN_CMD_SPEED |
6537 TPACPI_FAN_CMD_LEVEL |
6538 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006539 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006540 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006541 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006542 TPACPI_FAN_CMD_LEVEL |
6543 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006544 }
6545 }
6546 }
6547
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006548 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6549 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6550 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6551 fan_status_access_mode, fan_control_access_mode);
6552
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006553 /* fan control master switch */
6554 if (!fan_control_allowed) {
6555 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6556 fan_control_commands = 0;
6557 dbg_printk(TPACPI_DBG_INIT,
6558 "fan control features disabled by parameter\n");
6559 }
6560
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006561 /* update fan_control_desired_level */
6562 if (fan_status_access_mode != TPACPI_FAN_NONE)
6563 fan_get_status_safe(NULL);
6564
6565 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6566 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006567 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006568 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006569 if (rc < 0)
6570 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006571
6572 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6573 &driver_attr_fan_watchdog);
6574 if (rc < 0) {
6575 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6576 &fan_attr_group);
6577 return rc;
6578 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006579 return 0;
6580 } else
6581 return 1;
6582}
6583
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006584static void fan_exit(void)
6585{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006586 vdbg_printk(TPACPI_DBG_EXIT,
6587 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006588
6589 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006590 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006591 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6592 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006593
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006594 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006595 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006596}
6597
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006598static void fan_suspend(pm_message_t state)
6599{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006600 int rc;
6601
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006602 if (!fan_control_allowed)
6603 return;
6604
6605 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006606 fan_control_resume_level = 0;
6607 rc = fan_get_status_safe(&fan_control_resume_level);
6608 if (rc < 0)
6609 printk(TPACPI_NOTICE
6610 "failed to read fan level for later "
6611 "restore during resume: %d\n", rc);
6612
6613 /* if it is undefined, don't attempt to restore it.
6614 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006615 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006616 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006617}
6618
6619static void fan_resume(void)
6620{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006621 u8 current_level = 7;
6622 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006623 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006624
6625 /* DSDT *always* updates status on resume */
6626 tp_features.fan_ctrl_status_undef = 0;
6627
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006628 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006629 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006630 (fan_get_status_safe(&current_level) < 0))
6631 return;
6632
6633 switch (fan_control_access_mode) {
6634 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006635 /* never decrease fan level */
6636 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006637 break;
6638 case TPACPI_FAN_WR_ACPI_FANS:
6639 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006640 /* never decrease fan level, scale is:
6641 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6642 *
6643 * We expect the firmware to set either 7 or AUTO, but we
6644 * handle FULLSPEED out of paranoia.
6645 *
6646 * So, we can safely only restore FULLSPEED or 7, anything
6647 * else could slow the fan. Restoring AUTO is useless, at
6648 * best that's exactly what the DSDT already set (it is the
6649 * slower it uses).
6650 *
6651 * Always keep in mind that the DSDT *will* have set the
6652 * fans to what the vendor supposes is the best level. We
6653 * muck with it only to speed the fan up.
6654 */
6655 if (fan_control_resume_level != 7 &&
6656 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6657 return;
6658 else
6659 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6660 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006661 break;
6662 default:
6663 return;
6664 }
6665 if (do_set) {
6666 printk(TPACPI_NOTICE
6667 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006668 fan_control_resume_level);
6669 rc = fan_set_level_safe(fan_control_resume_level);
6670 if (rc < 0)
6671 printk(TPACPI_NOTICE
6672 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006673 }
6674}
6675
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006676static int fan_read(char *p)
6677{
6678 int len = 0;
6679 int rc;
6680 u8 status;
6681 unsigned int speed = 0;
6682
6683 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006684 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006685 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006686 rc = fan_get_status_safe(&status);
6687 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006688 return rc;
6689
6690 len += sprintf(p + len, "status:\t\t%s\n"
6691 "level:\t\t%d\n",
6692 (status != 0) ? "enabled" : "disabled", status);
6693 break;
6694
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006695 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006696 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006697 rc = fan_get_status_safe(&status);
6698 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006699 return rc;
6700
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006701 len += sprintf(p + len, "status:\t\t%s\n",
6702 (status != 0) ? "enabled" : "disabled");
6703
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006704 rc = fan_get_speed(&speed);
6705 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006706 return rc;
6707
6708 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6709
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006710 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006711 /* Disengaged mode takes precedence */
6712 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006713 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006714 len += sprintf(p + len, "level:\t\tauto\n");
6715 else
6716 len += sprintf(p + len, "level:\t\t%d\n", status);
6717 break;
6718
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006719 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006720 default:
6721 len += sprintf(p + len, "status:\t\tnot supported\n");
6722 }
6723
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006724 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006725 len += sprintf(p + len, "commands:\tlevel <level>");
6726
6727 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006728 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006729 len += sprintf(p + len, " (<level> is 0-7)\n");
6730 break;
6731
6732 default:
6733 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006734 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006735 break;
6736 }
6737 }
6738
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006739 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006740 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006741 "commands:\twatchdog <timeout> (<timeout> "
6742 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006743
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006744 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006745 len += sprintf(p + len, "commands:\tspeed <speed>"
6746 " (<speed> is 0-65535)\n");
6747
6748 return len;
6749}
6750
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006751static int fan_write_cmd_level(const char *cmd, int *rc)
6752{
6753 int level;
6754
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006755 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006756 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006757 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006758 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006759 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006760 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006761 return 0;
6762
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006763 *rc = fan_set_level_safe(level);
6764 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006765 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006766 "access mode %d", fan_control_access_mode);
6767
6768 return 1;
6769}
6770
6771static int fan_write_cmd_enable(const char *cmd, int *rc)
6772{
6773 if (strlencmp(cmd, "enable") != 0)
6774 return 0;
6775
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006776 *rc = fan_set_enable();
6777 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006778 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006779 "access mode %d", fan_control_access_mode);
6780
6781 return 1;
6782}
6783
6784static int fan_write_cmd_disable(const char *cmd, int *rc)
6785{
6786 if (strlencmp(cmd, "disable") != 0)
6787 return 0;
6788
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006789 *rc = fan_set_disable();
6790 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006791 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006792 "access mode %d", fan_control_access_mode);
6793
6794 return 1;
6795}
6796
6797static int fan_write_cmd_speed(const char *cmd, int *rc)
6798{
6799 int speed;
6800
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006801 /* TODO:
6802 * Support speed <low> <medium> <high> ? */
6803
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006804 if (sscanf(cmd, "speed %d", &speed) != 1)
6805 return 0;
6806
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006807 *rc = fan_set_speed(speed);
6808 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006809 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006810 "access mode %d", fan_control_access_mode);
6811
6812 return 1;
6813}
6814
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006815static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6816{
6817 int interval;
6818
6819 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6820 return 0;
6821
6822 if (interval < 0 || interval > 120)
6823 *rc = -EINVAL;
6824 else
6825 fan_watchdog_maxinterval = interval;
6826
6827 return 1;
6828}
6829
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006830static int fan_write(char *buf)
6831{
6832 char *cmd;
6833 int rc = 0;
6834
6835 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006836 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006837 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006838 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006839 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006840 fan_write_cmd_disable(cmd, &rc) ||
6841 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006842 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006843 fan_write_cmd_speed(cmd, &rc))
6844 )
6845 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006846 else if (!rc)
6847 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006848 }
6849
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006850 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006851}
6852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006853static struct ibm_struct fan_driver_data = {
6854 .name = "fan",
6855 .read = fan_read,
6856 .write = fan_write,
6857 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006858 .suspend = fan_suspend,
6859 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006860};
6861
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006862/****************************************************************************
6863 ****************************************************************************
6864 *
6865 * Infrastructure
6866 *
6867 ****************************************************************************
6868 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006870/* sysfs name ---------------------------------------------------------- */
6871static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6872 struct device_attribute *attr,
6873 char *buf)
6874{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006875 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006876}
6877
6878static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6879 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6880
6881/* --------------------------------------------------------------------- */
6882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006883/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006884static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006885
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006886/*
6887 * Module and infrastructure proble, init and exit handling
6888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006890static int force_load;
6891
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006892#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006893static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006894{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006895 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006897 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006898}
6899#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6900
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006901static void ibm_exit(struct ibm_struct *ibm)
6902{
6903 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6904
6905 list_del_init(&ibm->all_drivers);
6906
6907 if (ibm->flags.acpi_notify_installed) {
6908 dbg_printk(TPACPI_DBG_EXIT,
6909 "%s: acpi_remove_notify_handler\n", ibm->name);
6910 BUG_ON(!ibm->acpi);
6911 acpi_remove_notify_handler(*ibm->acpi->handle,
6912 ibm->acpi->type,
6913 dispatch_acpi_notify);
6914 ibm->flags.acpi_notify_installed = 0;
6915 ibm->flags.acpi_notify_installed = 0;
6916 }
6917
6918 if (ibm->flags.proc_created) {
6919 dbg_printk(TPACPI_DBG_EXIT,
6920 "%s: remove_proc_entry\n", ibm->name);
6921 remove_proc_entry(ibm->name, proc_dir);
6922 ibm->flags.proc_created = 0;
6923 }
6924
6925 if (ibm->flags.acpi_driver_registered) {
6926 dbg_printk(TPACPI_DBG_EXIT,
6927 "%s: acpi_bus_unregister_driver\n", ibm->name);
6928 BUG_ON(!ibm->acpi);
6929 acpi_bus_unregister_driver(ibm->acpi->driver);
6930 kfree(ibm->acpi->driver);
6931 ibm->acpi->driver = NULL;
6932 ibm->flags.acpi_driver_registered = 0;
6933 }
6934
6935 if (ibm->flags.init_called && ibm->exit) {
6936 ibm->exit();
6937 ibm->flags.init_called = 0;
6938 }
6939
6940 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6941}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006942
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006943static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944{
6945 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006946 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 struct proc_dir_entry *entry;
6948
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006949 BUG_ON(ibm == NULL);
6950
6951 INIT_LIST_HEAD(&ibm->all_drivers);
6952
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006953 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 return 0;
6955
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006956 dbg_printk(TPACPI_DBG_INIT,
6957 "probing for %s\n", ibm->name);
6958
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006959 if (iibm->init) {
6960 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006961 if (ret > 0)
6962 return 0; /* probe failed */
6963 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006965
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006966 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 }
6968
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006969 if (ibm->acpi) {
6970 if (ibm->acpi->hid) {
6971 ret = register_tpacpi_subdriver(ibm);
6972 if (ret)
6973 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006974 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006975
6976 if (ibm->acpi->notify) {
6977 ret = setup_acpi_notify(ibm);
6978 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006979 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006980 ibm->name);
6981 ret = 0;
6982 goto err_out;
6983 }
6984 if (ret < 0)
6985 goto err_out;
6986 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006987 }
6988
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006989 dbg_printk(TPACPI_DBG_INIT,
6990 "%s installed\n", ibm->name);
6991
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006992 if (ibm->read) {
6993 entry = create_proc_entry(ibm->name,
6994 S_IFREG | S_IRUGO | S_IWUSR,
6995 proc_dir);
6996 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006997 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006998 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006999 ret = -ENODEV;
7000 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007001 }
7002 entry->owner = THIS_MODULE;
7003 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007004 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007005 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03007006 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03007007 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007010 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7011
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007013
7014err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007015 dbg_printk(TPACPI_DBG_INIT,
7016 "%s: at error exit path with result %d\n",
7017 ibm->name, ret);
7018
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007019 ibm_exit(ibm);
7020 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
7022
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007023/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007025/* returns 0 - probe ok, or < 0 - probe error.
7026 * Probe ok doesn't mean thinkpad found.
7027 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7028static int __must_check __init get_thinkpad_model_data(
7029 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007030{
Jeff Garzik18552562007-10-03 15:15:40 -04007031 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007032 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007033 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007034
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007035 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007036 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007037
7038 memset(tp, 0, sizeof(*tp));
7039
7040 if (dmi_name_in_vendors("IBM"))
7041 tp->vendor = PCI_VENDOR_ID_IBM;
7042 else if (dmi_name_in_vendors("LENOVO"))
7043 tp->vendor = PCI_VENDOR_ID_LENOVO;
7044 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007045 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007046
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007047 s = dmi_get_system_info(DMI_BIOS_VERSION);
7048 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7049 if (s && !tp->bios_version_str)
7050 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007051 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007052 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007053 tp->bios_model = tp->bios_version_str[0]
7054 | (tp->bios_version_str[1] << 8);
7055
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007056 /*
7057 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7058 * X32 or newer, all Z series; Some models must have an
7059 * up-to-date BIOS or they will not be detected.
7060 *
7061 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7062 */
7063 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007064 if (sscanf(dev->name,
7065 "IBM ThinkPad Embedded Controller -[%17c",
7066 ec_fw_string) == 1) {
7067 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7068 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007069
7070 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007071 if (!tp->ec_version_str)
7072 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007073 tp->ec_model = ec_fw_string[0]
7074 | (ec_fw_string[1] << 8);
7075 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02007076 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02007077 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007078
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007079 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7080 if (s && !strnicmp(s, "ThinkPad", 8)) {
7081 tp->model_str = kstrdup(s, GFP_KERNEL);
7082 if (!tp->model_str)
7083 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007084 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03007085
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007086 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7087 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7088 if (s && !tp->nummodel_str)
7089 return -ENOMEM;
7090
7091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007094static int __init probe_for_thinkpad(void)
7095{
7096 int is_thinkpad;
7097
7098 if (acpi_disabled)
7099 return -ENODEV;
7100
7101 /*
7102 * Non-ancient models have better DMI tagging, but very old models
7103 * don't.
7104 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007105 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007106
7107 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007108 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007109 if (!ec_handle) {
7110 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007111 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007112 "Not yet supported ThinkPad detected!\n");
7113 return -ENODEV;
7114 }
7115
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007116 /*
7117 * Risks a regression on very old machines, but reduces potential
7118 * false positives a damn great deal
7119 */
7120 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007121 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007122
7123 if (!is_thinkpad && !force_load)
7124 return -ENODEV;
7125
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007126 return 0;
7127}
7128
7129
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007130/* Module init, exit, parameters */
7131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007132static struct ibm_init_struct ibms_init[] __initdata = {
7133 {
7134 .init = thinkpad_acpi_driver_init,
7135 .data = &thinkpad_acpi_driver_data,
7136 },
7137 {
7138 .init = hotkey_init,
7139 .data = &hotkey_driver_data,
7140 },
7141 {
7142 .init = bluetooth_init,
7143 .data = &bluetooth_driver_data,
7144 },
7145 {
7146 .init = wan_init,
7147 .data = &wan_driver_data,
7148 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007149 {
7150 .init = uwb_init,
7151 .data = &uwb_driver_data,
7152 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007153#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007154 {
7155 .init = video_init,
7156 .data = &video_driver_data,
7157 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007158#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007159 {
7160 .init = light_init,
7161 .data = &light_driver_data,
7162 },
7163#ifdef CONFIG_THINKPAD_ACPI_DOCK
7164 {
7165 .init = dock_init,
7166 .data = &dock_driver_data[0],
7167 },
7168 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007169 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007170 .data = &dock_driver_data[1],
7171 },
7172#endif
7173#ifdef CONFIG_THINKPAD_ACPI_BAY
7174 {
7175 .init = bay_init,
7176 .data = &bay_driver_data,
7177 },
7178#endif
7179 {
7180 .init = cmos_init,
7181 .data = &cmos_driver_data,
7182 },
7183 {
7184 .init = led_init,
7185 .data = &led_driver_data,
7186 },
7187 {
7188 .init = beep_init,
7189 .data = &beep_driver_data,
7190 },
7191 {
7192 .init = thermal_init,
7193 .data = &thermal_driver_data,
7194 },
7195 {
7196 .data = &ecdump_driver_data,
7197 },
7198 {
7199 .init = brightness_init,
7200 .data = &brightness_driver_data,
7201 },
7202 {
7203 .data = &volume_driver_data,
7204 },
7205 {
7206 .init = fan_init,
7207 .data = &fan_driver_data,
7208 },
7209};
7210
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007211static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7212{
7213 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007214 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007215
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007216 if (!kp || !kp->name || !val)
7217 return -EINVAL;
7218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007219 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7220 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007221 WARN_ON(ibm == NULL);
7222
7223 if (!ibm || !ibm->name)
7224 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007225
7226 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7227 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007228 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007229 strcpy(ibms_init[i].param, val);
7230 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007231 return 0;
7232 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007233 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007234
7235 return -EINVAL;
7236}
7237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007238module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007239MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007240 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007241
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007242module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007243MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007244
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007245module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007246MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007247 "Attempts to load the driver even on a "
7248 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007249
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007250module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007251MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007252 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007253
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007254module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007255MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007256 "Selects brightness control strategy: "
7257 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007258
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007259module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007260MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007261 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007262
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007263module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007264MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007265 "used for backwards compatibility with userspace, "
7266 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007267
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007268#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007269 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007270 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007271 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007272
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007273TPACPI_PARAM(hotkey);
7274TPACPI_PARAM(bluetooth);
7275TPACPI_PARAM(video);
7276TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007277#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007278TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007279#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007280#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007281TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007282#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007283TPACPI_PARAM(cmos);
7284TPACPI_PARAM(led);
7285TPACPI_PARAM(beep);
7286TPACPI_PARAM(ecdump);
7287TPACPI_PARAM(brightness);
7288TPACPI_PARAM(volume);
7289TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007290
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007291#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7292module_param(dbg_wlswemul, uint, 0);
7293MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7294module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7295MODULE_PARM_DESC(wlsw_state,
7296 "Initial state of the emulated WLSW switch");
7297
7298module_param(dbg_bluetoothemul, uint, 0);
7299MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7300module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7301MODULE_PARM_DESC(bluetooth_state,
7302 "Initial state of the emulated bluetooth switch");
7303
7304module_param(dbg_wwanemul, uint, 0);
7305MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7306module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7307MODULE_PARM_DESC(wwan_state,
7308 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007309
7310module_param(dbg_uwbemul, uint, 0);
7311MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7312module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7313MODULE_PARM_DESC(uwb_state,
7314 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007315#endif
7316
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007317static void thinkpad_acpi_module_exit(void)
7318{
7319 struct ibm_struct *ibm, *itmp;
7320
7321 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7322
7323 list_for_each_entry_safe_reverse(ibm, itmp,
7324 &tpacpi_all_drivers,
7325 all_drivers) {
7326 ibm_exit(ibm);
7327 }
7328
7329 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7330
7331 if (tpacpi_inputdev) {
7332 if (tp_features.input_device_registered)
7333 input_unregister_device(tpacpi_inputdev);
7334 else
7335 input_free_device(tpacpi_inputdev);
7336 }
7337
7338 if (tpacpi_hwmon)
7339 hwmon_device_unregister(tpacpi_hwmon);
7340
7341 if (tp_features.sensors_pdev_attrs_registered)
7342 device_remove_file(&tpacpi_sensors_pdev->dev,
7343 &dev_attr_thinkpad_acpi_pdev_name);
7344 if (tpacpi_sensors_pdev)
7345 platform_device_unregister(tpacpi_sensors_pdev);
7346 if (tpacpi_pdev)
7347 platform_device_unregister(tpacpi_pdev);
7348
7349 if (tp_features.sensors_pdrv_attrs_registered)
7350 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7351 if (tp_features.platform_drv_attrs_registered)
7352 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7353
7354 if (tp_features.sensors_pdrv_registered)
7355 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7356
7357 if (tp_features.platform_drv_registered)
7358 platform_driver_unregister(&tpacpi_pdriver);
7359
7360 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007361 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007362
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007363 if (tpacpi_wq)
7364 destroy_workqueue(tpacpi_wq);
7365
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007366 kfree(thinkpad_id.bios_version_str);
7367 kfree(thinkpad_id.ec_version_str);
7368 kfree(thinkpad_id.model_str);
7369}
7370
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007371
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007372static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373{
7374 int ret, i;
7375
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007376 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7377
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007378 /* Parameter checking */
7379 if (hotkey_report_mode > 2)
7380 return -EINVAL;
7381
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007382 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007383
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007384 ret = get_thinkpad_model_data(&thinkpad_id);
7385 if (ret) {
7386 printk(TPACPI_ERR
7387 "unable to get DMI data: %d\n", ret);
7388 thinkpad_acpi_module_exit();
7389 return ret;
7390 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007391 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007392 if (ret) {
7393 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007394 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007397 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007398
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007399 TPACPI_ACPIHANDLE_INIT(ecrd);
7400 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007402 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7403 if (!tpacpi_wq) {
7404 thinkpad_acpi_module_exit();
7405 return -ENOMEM;
7406 }
7407
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007408 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007410 printk(TPACPI_ERR
7411 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007412 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 return -ENODEV;
7414 }
7415 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007416
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007417 ret = platform_driver_register(&tpacpi_pdriver);
7418 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007419 printk(TPACPI_ERR
7420 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007421 thinkpad_acpi_module_exit();
7422 return ret;
7423 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007424 tp_features.platform_drv_registered = 1;
7425
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007426 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7427 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007428 printk(TPACPI_ERR
7429 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007430 thinkpad_acpi_module_exit();
7431 return ret;
7432 }
7433 tp_features.sensors_pdrv_registered = 1;
7434
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007435 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007436 if (!ret) {
7437 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007438 ret = tpacpi_create_driver_attributes(
7439 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007440 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007441 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007442 printk(TPACPI_ERR
7443 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007444 thinkpad_acpi_module_exit();
7445 return ret;
7446 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007447 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007448
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007449
7450 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007451 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007452 NULL, 0);
7453 if (IS_ERR(tpacpi_pdev)) {
7454 ret = PTR_ERR(tpacpi_pdev);
7455 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007456 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007457 thinkpad_acpi_module_exit();
7458 return ret;
7459 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007460 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007461 TPACPI_HWMON_DRVR_NAME,
7462 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007463 if (IS_ERR(tpacpi_sensors_pdev)) {
7464 ret = PTR_ERR(tpacpi_sensors_pdev);
7465 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007466 printk(TPACPI_ERR
7467 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007468 thinkpad_acpi_module_exit();
7469 return ret;
7470 }
7471 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7472 &dev_attr_thinkpad_acpi_pdev_name);
7473 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007474 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007475 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007476 thinkpad_acpi_module_exit();
7477 return ret;
7478 }
7479 tp_features.sensors_pdev_attrs_registered = 1;
7480 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007481 if (IS_ERR(tpacpi_hwmon)) {
7482 ret = PTR_ERR(tpacpi_hwmon);
7483 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007484 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007485 thinkpad_acpi_module_exit();
7486 return ret;
7487 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007488 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007489 tpacpi_inputdev = input_allocate_device();
7490 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007491 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007492 thinkpad_acpi_module_exit();
7493 return -ENOMEM;
7494 } else {
7495 /* Prepare input device, but don't register */
7496 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007497 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007498 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007499 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7500 thinkpad_id.vendor :
7501 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007502 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7503 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7504 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007505 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7506 ret = ibm_init(&ibms_init[i]);
7507 if (ret >= 0 && *ibms_init[i].param)
7508 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007510 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 return ret;
7512 }
7513 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007514 ret = input_register_device(tpacpi_inputdev);
7515 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007516 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007517 thinkpad_acpi_module_exit();
7518 return ret;
7519 } else {
7520 tp_features.input_device_registered = 1;
7521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007523 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 return 0;
7525}
7526
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007527MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7528
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007529/*
7530 * DMI matching for module autoloading
7531 *
7532 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7533 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7534 *
7535 * Only models listed in thinkwiki will be supported, so add yours
7536 * if it is not there yet.
7537 */
7538#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007539 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007540
7541/* Non-ancient thinkpads */
7542MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7543MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7544
7545/* Ancient thinkpad BIOSes have to be identified by
7546 * BIOS type or model number, and there are far less
7547 * BIOS types than model numbers... */
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01007548IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7549IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7550IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007551
7552MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007553MODULE_DESCRIPTION(TPACPI_DESC);
7554MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007555MODULE_LICENSE("GPL");
7556
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007557module_init(thinkpad_acpi_module_init);
7558module_exit(thinkpad_acpi_module_exit);