blob: b2c5913ff72e478fa2b9374eab9fe70571f53329 [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 Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 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 Holschuh490673d2008-07-21 09:15:51 -030024#define TPACPI_VERSION "0.21"
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,
172};
173
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200174/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200175#define TPACPI_LOG TPACPI_FILE ": "
176#define TPACPI_ERR KERN_ERR TPACPI_LOG
177#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
178#define TPACPI_INFO KERN_INFO TPACPI_LOG
179#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200180
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200182#define TPACPI_DBG_INIT 0x0001
183#define TPACPI_DBG_EXIT 0x0002
184#define dbg_printk(a_dbg_level, format, arg...) \
185 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200186 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
187 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200188#ifdef CONFIG_THINKPAD_ACPI_DEBUG
189#define vdbg_printk(a_dbg_level, format, arg...) \
190 dbg_printk(a_dbg_level, format, ## arg)
191static const char *str_supported(int is_supported);
192#else
193#define vdbg_printk(a_dbg_level, format, arg...)
194#endif
195
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200196#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
197#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
198#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200199
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200
201/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200202 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200203 */
204
205struct ibm_struct;
206
207struct tp_acpi_drv_struct {
208 const struct acpi_device_id *hid;
209 struct acpi_driver *driver;
210
211 void (*notify) (struct ibm_struct *, u32);
212 acpi_handle *handle;
213 u32 type;
214 struct acpi_device *device;
215};
216
217struct ibm_struct {
218 char *name;
219
220 int (*read) (char *);
221 int (*write) (char *);
222 void (*exit) (void);
223 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200224 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200225
226 struct list_head all_drivers;
227
228 struct tp_acpi_drv_struct *acpi;
229
230 struct {
231 u8 acpi_driver_registered:1;
232 u8 acpi_notify_installed:1;
233 u8 proc_created:1;
234 u8 init_called:1;
235 u8 experimental:1;
236 } flags;
237};
238
239struct ibm_init_struct {
240 char param[32];
241
242 int (*init) (struct ibm_init_struct *);
243 struct ibm_struct *data;
244};
245
246static struct {
247#ifdef CONFIG_THINKPAD_ACPI_BAY
248 u32 bay_status:1;
249 u32 bay_eject:1;
250 u32 bay_status2:1;
251 u32 bay_eject2:1;
252#endif
253 u32 bluetooth:1;
254 u32 hotkey:1;
255 u32 hotkey_mask:1;
256 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200257 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200258 u32 light:1;
259 u32 light_status:1;
260 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300261 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200262 u32 wan:1;
263 u32 fan_ctrl_status_undef:1;
264 u32 input_device_registered:1;
265 u32 platform_drv_registered:1;
266 u32 platform_drv_attrs_registered:1;
267 u32 sensors_pdrv_registered:1;
268 u32 sensors_pdrv_attrs_registered:1;
269 u32 sensors_pdev_attrs_registered:1;
270 u32 hotkey_poll_active:1;
271} tp_features;
272
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300273static struct {
274 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300275 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300276} tp_warned;
277
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200278struct thinkpad_id_data {
279 unsigned int vendor; /* ThinkPad vendor:
280 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
281
282 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
283 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
284
285 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
286 u16 ec_model;
287
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200290};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200291static struct thinkpad_id_data thinkpad_id;
292
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300293static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297} tpacpi_lifecycle;
298
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200299static int experimental;
300static u32 dbg_level;
301
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300302static struct workqueue_struct *tpacpi_wq;
303
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300304/* Special LED class that can defer work */
305struct tpacpi_led_classdev {
306 struct led_classdev led_classdev;
307 struct work_struct work;
308 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300309 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300310};
311
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200312#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
313static int dbg_wlswemul;
314static int tpacpi_wlsw_emulstate;
315static int dbg_bluetoothemul;
316static int tpacpi_bluetooth_emulstate;
317static int dbg_wwanemul;
318static int tpacpi_wwan_emulstate;
319#endif
320
321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300322/****************************************************************************
323 ****************************************************************************
324 *
325 * ACPI Helpers and device model
326 *
327 ****************************************************************************
328 ****************************************************************************/
329
330/*************************************************************************
331 * ACPI basic handles
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300334static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200336#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 static acpi_handle object##_handle; \
338 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400339 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 static char *object##_paths[] = { paths }
341
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200342TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400343 "\\_SB.PCI.ISA.EC", /* 570 */
344 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
345 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
346 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
347 "\\_SB.PCI0.ICH3.EC0", /* R31 */
348 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200351TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
352TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200354TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
355 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
357 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400359
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200360TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400361 "^HKEY", /* R30, R31 */
362 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300363 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400364
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200365TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
366 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
367 "\\_SB.PCI0.VID0", /* 770e */
368 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
369 "\\_SB.PCI0.AGP.VID", /* all others */
370 ); /* R30, R31 */
371
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300373/*************************************************************************
374 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200375 */
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static int acpi_evalf(acpi_handle handle,
378 void *res, char *method, char *fmt, ...)
379{
380 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400381 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200382 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400383 struct acpi_buffer result, *resultp;
384 union acpi_object out_obj;
385 acpi_status status;
386 va_list ap;
387 char res_type;
388 int success;
389 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200392 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394 }
395
396 if (*fmt == 'q') {
397 quiet = 1;
398 fmt++;
399 } else
400 quiet = 0;
401
402 res_type = *(fmt++);
403
404 params.count = 0;
405 params.pointer = &in_objs[0];
406
407 va_start(ap, fmt);
408 while (*fmt) {
409 char c = *(fmt++);
410 switch (c) {
411 case 'd': /* int */
412 in_objs[params.count].integer.value = va_arg(ap, int);
413 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
414 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400415 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200417 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 "with invalid format character '%c'\n", c);
419 return 0;
420 }
421 }
422 va_end(ap);
423
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400424 if (res_type != 'v') {
425 result.length = sizeof(out_obj);
426 result.pointer = &out_obj;
427 resultp = &result;
428 } else
429 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400431 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400434 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (res)
436 *(int *)res = out_obj.integer.value;
437 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
438 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400439 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 success = status == AE_OK;
441 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400442 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200444 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 "with invalid format character '%c'\n", res_type);
446 return 0;
447 }
448
449 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200450 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 method, fmt0, status);
452
453 return success;
454}
455
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200456static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300457{
458 int v;
459
460 if (ecrd_handle) {
461 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
462 return 0;
463 *p = v;
464 } else {
465 if (ec_read(i, p) < 0)
466 return 0;
467 }
468
469 return 1;
470}
471
472static int acpi_ec_write(int i, u8 v)
473{
474 if (ecwr_handle) {
475 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
476 return 0;
477 } else {
478 if (ec_write(i, v) < 0)
479 return 0;
480 }
481
482 return 1;
483}
484
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200485#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300486static int _sta(acpi_handle handle)
487{
488 int status;
489
490 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
491 status = 0;
492
493 return status;
494}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200495#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300497static int issue_thinkpad_cmos_command(int cmos_cmd)
498{
499 if (!cmos_handle)
500 return -ENXIO;
501
502 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
503 return -EIO;
504
505 return 0;
506}
507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508/*************************************************************************
509 * ACPI device model
510 */
511
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200512#define TPACPI_ACPIHANDLE_INIT(object) \
513 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200514 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300517 acpi_handle *handle, acpi_handle parent,
518 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519{
520 int i;
521 acpi_status status;
522
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300523 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
524 name);
525
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300526 for (i = 0; i < num_paths; i++) {
527 status = acpi_get_handle(parent, paths[i], handle);
528 if (ACPI_SUCCESS(status)) {
529 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300530 dbg_printk(TPACPI_DBG_INIT,
531 "Found ACPI handle %s for %s\n",
532 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533 return;
534 }
535 }
536
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300537 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
538 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539 *handle = NULL;
540}
541
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300542static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543{
544 struct ibm_struct *ibm = data;
545
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300546 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
547 return;
548
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300549 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300550 return;
551
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300552 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300553}
554
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300555static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556{
557 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300558 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300559
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300560 BUG_ON(!ibm->acpi);
561
562 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563 return 0;
564
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300565 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300566 "setting up ACPI notify for %s\n", ibm->name);
567
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300568 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
569 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200570 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300571 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300572 return -ENODEV;
573 }
574
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700575 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300576 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200577 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300578 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300579
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300580 status = acpi_install_notify_handler(*ibm->acpi->handle,
581 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300582 if (ACPI_FAILURE(status)) {
583 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200584 printk(TPACPI_NOTICE
585 "another device driver is already "
586 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200588 printk(TPACPI_ERR
589 "acpi_install_notify_handler(%s) failed: %d\n",
590 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300591 }
592 return -ENODEV;
593 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300595 return 0;
596}
597
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300598static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300599{
600 return 0;
601}
602
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300603static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300605 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300606
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300607 dbg_printk(TPACPI_DBG_INIT,
608 "registering %s as an ACPI driver\n", ibm->name);
609
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300610 BUG_ON(!ibm->acpi);
611
612 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
613 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300614 printk(TPACPI_ERR
615 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300616 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617 }
618
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200619 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300620 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200621
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300622 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300623
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300624 rc = acpi_bus_register_driver(ibm->acpi->driver);
625 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200626 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200627 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300628 kfree(ibm->acpi->driver);
629 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300630 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300633 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300634}
635
636
637/****************************************************************************
638 ****************************************************************************
639 *
640 * Procfs Helpers
641 *
642 ****************************************************************************
643 ****************************************************************************/
644
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300645static int dispatch_procfs_read(char *page, char **start, off_t off,
646 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300647{
648 struct ibm_struct *ibm = data;
649 int len;
650
651 if (!ibm || !ibm->read)
652 return -EINVAL;
653
654 len = ibm->read(page);
655 if (len < 0)
656 return len;
657
658 if (len <= off + count)
659 *eof = 1;
660 *start = page + off;
661 len -= off;
662 if (len > count)
663 len = count;
664 if (len < 0)
665 len = 0;
666
667 return len;
668}
669
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300670static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200671 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300672 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673{
674 struct ibm_struct *ibm = data;
675 char *kernbuf;
676 int ret;
677
678 if (!ibm || !ibm->write)
679 return -EINVAL;
680
681 kernbuf = kmalloc(count + 2, GFP_KERNEL);
682 if (!kernbuf)
683 return -ENOMEM;
684
685 if (copy_from_user(kernbuf, userbuf, count)) {
686 kfree(kernbuf);
687 return -EFAULT;
688 }
689
690 kernbuf[count] = 0;
691 strcat(kernbuf, ",");
692 ret = ibm->write(kernbuf);
693 if (ret == 0)
694 ret = count;
695
696 kfree(kernbuf);
697
698 return ret;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static char *next_cmd(char **cmds)
702{
703 char *start = *cmds;
704 char *end;
705
706 while ((end = strchr(start, ',')) && end == start)
707 start = end + 1;
708
709 if (!end)
710 return NULL;
711
712 *end = 0;
713 *cmds = end + 1;
714 return start;
715}
716
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300717
718/****************************************************************************
719 ****************************************************************************
720 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300721 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300722 *
723 ****************************************************************************
724 ****************************************************************************/
725
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300726static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300727static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700728static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300729static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300730static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200731static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300732
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200733static int tpacpi_suspend_handler(struct platform_device *pdev,
734 pm_message_t state)
735{
736 struct ibm_struct *ibm, *itmp;
737
738 list_for_each_entry_safe(ibm, itmp,
739 &tpacpi_all_drivers,
740 all_drivers) {
741 if (ibm->suspend)
742 (ibm->suspend)(state);
743 }
744
745 return 0;
746}
747
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300748static int tpacpi_resume_handler(struct platform_device *pdev)
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->resume)
756 (ibm->resume)();
757 }
758
759 return 0;
760}
761
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300762static struct platform_driver tpacpi_pdriver = {
763 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200764 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300765 .owner = THIS_MODULE,
766 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200767 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300768 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300769};
770
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300771static struct platform_driver tpacpi_hwmon_pdriver = {
772 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200773 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300774 .owner = THIS_MODULE,
775 },
776};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300777
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300778/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300779 * sysfs support helpers
780 */
781
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200782struct attribute_set {
783 unsigned int members, max_members;
784 struct attribute_group group;
785};
786
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300787struct attribute_set_obj {
788 struct attribute_set s;
789 struct attribute *a;
790} __attribute__((packed));
791
792static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200793 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300794{
795 struct attribute_set_obj *sobj;
796
797 if (max_members == 0)
798 return NULL;
799
800 /* Allocates space for implicit NULL at the end too */
801 sobj = kzalloc(sizeof(struct attribute_set_obj) +
802 max_members * sizeof(struct attribute *),
803 GFP_KERNEL);
804 if (!sobj)
805 return NULL;
806 sobj->s.max_members = max_members;
807 sobj->s.group.attrs = &sobj->a;
808 sobj->s.group.name = name;
809
810 return &sobj->s;
811}
812
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200813#define destroy_attr_set(_set) \
814 kfree(_set);
815
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300816/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200817static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300818{
819 if (!s || !attr)
820 return -EINVAL;
821
822 if (s->members >= s->max_members)
823 return -ENOMEM;
824
825 s->group.attrs[s->members] = attr;
826 s->members++;
827
828 return 0;
829}
830
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200831static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300832 struct attribute **attr,
833 unsigned int count)
834{
835 int i, res;
836
837 for (i = 0; i < count; i++) {
838 res = add_to_attr_set(s, attr[i]);
839 if (res)
840 return res;
841 }
842
843 return 0;
844}
845
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200846static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300847{
848 sysfs_remove_group(kobj, &s->group);
849 destroy_attr_set(s);
850}
851
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200852#define register_attr_set_with_sysfs(_attr_set, _kobj) \
853 sysfs_create_group(_kobj, &_attr_set->group)
854
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300855static int parse_strtoul(const char *buf,
856 unsigned long max, unsigned long *value)
857{
858 char *endp;
859
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300860 while (*buf && isspace(*buf))
861 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300862 *value = simple_strtoul(buf, &endp, 0);
863 while (*endp && isspace(*endp))
864 endp++;
865 if (*endp || *value > max)
866 return -EINVAL;
867
868 return 0;
869}
870
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300871static void tpacpi_disable_brightness_delay(void)
872{
873 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
874 printk(TPACPI_NOTICE
875 "ACPI backlight control delay disabled\n");
876}
877
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300878static int __init tpacpi_query_bcl_levels(acpi_handle handle)
879{
880 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
881 union acpi_object *obj;
882 int rc;
883
884 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
885 obj = (union acpi_object *)buffer.pointer;
886 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
887 printk(TPACPI_ERR "Unknown _BCL data, "
888 "please report this to %s\n", TPACPI_MAIL);
889 rc = 0;
890 } else {
891 rc = obj->package.count;
892 }
893 } else {
894 return 0;
895 }
896
897 kfree(buffer.pointer);
898 return rc;
899}
900
901static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
902 u32 lvl, void *context, void **rv)
903{
904 char name[ACPI_PATH_SEGMENT_LENGTH];
905 struct acpi_buffer buffer = { sizeof(name), &name };
906
907 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
908 !strncmp("_BCL", name, sizeof(name) - 1)) {
909 BUG_ON(!rv || !*rv);
910 **(int **)rv = tpacpi_query_bcl_levels(handle);
911 return AE_CTRL_TERMINATE;
912 } else {
913 return AE_OK;
914 }
915}
916
917/*
918 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
919 */
920static int __init tpacpi_check_std_acpi_brightness_support(void)
921{
922 int status;
923 int bcl_levels = 0;
924 void *bcl_ptr = &bcl_levels;
925
926 if (!vid_handle) {
927 TPACPI_ACPIHANDLE_INIT(vid);
928 }
929 if (!vid_handle)
930 return 0;
931
932 /*
933 * Search for a _BCL method, and execute it. This is safe on all
934 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
935 * BIOS in ACPI backlight control mode. We do NOT have to care
936 * about calling the _BCL method in an enabled video device, any
937 * will do for our purposes.
938 */
939
940 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
941 tpacpi_acpi_walk_find_bcl, NULL,
942 &bcl_ptr);
943
944 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
945 tp_features.bright_acpimode = 1;
946 return (bcl_levels - 2);
947 }
948
949 return 0;
950}
951
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300952static int __init tpacpi_new_rfkill(const unsigned int id,
953 struct rfkill **rfk,
954 const enum rfkill_type rfktype,
955 const char *name,
956 int (*toggle_radio)(void *, enum rfkill_state),
957 int (*get_state)(void *, enum rfkill_state *))
958{
959 int res;
960 enum rfkill_state initial_state;
961
962 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
963 if (!*rfk) {
964 printk(TPACPI_ERR
965 "failed to allocate memory for rfkill class\n");
966 return -ENOMEM;
967 }
968
969 (*rfk)->name = name;
970 (*rfk)->get_state = get_state;
971 (*rfk)->toggle_radio = toggle_radio;
972
973 if (!get_state(NULL, &initial_state))
974 (*rfk)->state = initial_state;
975
976 res = rfkill_register(*rfk);
977 if (res < 0) {
978 printk(TPACPI_ERR
979 "failed to register %s rfkill switch: %d\n",
980 name, res);
981 rfkill_free(*rfk);
982 *rfk = NULL;
983 return res;
984 }
985
986 return 0;
987}
988
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200989/*************************************************************************
990 * thinkpad-acpi driver attributes
991 */
992
993/* interface_version --------------------------------------------------- */
994static ssize_t tpacpi_driver_interface_version_show(
995 struct device_driver *drv,
996 char *buf)
997{
998 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
999}
1000
1001static DRIVER_ATTR(interface_version, S_IRUGO,
1002 tpacpi_driver_interface_version_show, NULL);
1003
1004/* debug_level --------------------------------------------------------- */
1005static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1006 char *buf)
1007{
1008 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1009}
1010
1011static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1012 const char *buf, size_t count)
1013{
1014 unsigned long t;
1015
1016 if (parse_strtoul(buf, 0xffff, &t))
1017 return -EINVAL;
1018
1019 dbg_level = t;
1020
1021 return count;
1022}
1023
1024static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1025 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1026
1027/* version ------------------------------------------------------------- */
1028static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1029 char *buf)
1030{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001031 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1032 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001033}
1034
1035static DRIVER_ATTR(version, S_IRUGO,
1036 tpacpi_driver_version_show, NULL);
1037
1038/* --------------------------------------------------------------------- */
1039
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001040#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1041
1042static void tpacpi_send_radiosw_update(void);
1043
1044/* wlsw_emulstate ------------------------------------------------------ */
1045static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1046 char *buf)
1047{
1048 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1049}
1050
1051static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1052 const char *buf, size_t count)
1053{
1054 unsigned long t;
1055
1056 if (parse_strtoul(buf, 1, &t))
1057 return -EINVAL;
1058
1059 if (tpacpi_wlsw_emulstate != t) {
1060 tpacpi_wlsw_emulstate = !!t;
1061 tpacpi_send_radiosw_update();
1062 } else
1063 tpacpi_wlsw_emulstate = !!t;
1064
1065 return count;
1066}
1067
1068static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1069 tpacpi_driver_wlsw_emulstate_show,
1070 tpacpi_driver_wlsw_emulstate_store);
1071
1072/* bluetooth_emulstate ------------------------------------------------- */
1073static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1074 struct device_driver *drv,
1075 char *buf)
1076{
1077 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1078}
1079
1080static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1081 struct device_driver *drv,
1082 const char *buf, size_t count)
1083{
1084 unsigned long t;
1085
1086 if (parse_strtoul(buf, 1, &t))
1087 return -EINVAL;
1088
1089 tpacpi_bluetooth_emulstate = !!t;
1090
1091 return count;
1092}
1093
1094static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1095 tpacpi_driver_bluetooth_emulstate_show,
1096 tpacpi_driver_bluetooth_emulstate_store);
1097
1098/* wwan_emulstate ------------------------------------------------- */
1099static ssize_t tpacpi_driver_wwan_emulstate_show(
1100 struct device_driver *drv,
1101 char *buf)
1102{
1103 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1104}
1105
1106static ssize_t tpacpi_driver_wwan_emulstate_store(
1107 struct device_driver *drv,
1108 const char *buf, size_t count)
1109{
1110 unsigned long t;
1111
1112 if (parse_strtoul(buf, 1, &t))
1113 return -EINVAL;
1114
1115 tpacpi_wwan_emulstate = !!t;
1116
1117 return count;
1118}
1119
1120static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1121 tpacpi_driver_wwan_emulstate_show,
1122 tpacpi_driver_wwan_emulstate_store);
1123
1124#endif
1125
1126/* --------------------------------------------------------------------- */
1127
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001128static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001129 &driver_attr_debug_level, &driver_attr_version,
1130 &driver_attr_interface_version,
1131};
1132
1133static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1134{
1135 int i, res;
1136
1137 i = 0;
1138 res = 0;
1139 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1140 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1141 i++;
1142 }
1143
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001144#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1145 if (!res && dbg_wlswemul)
1146 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1147 if (!res && dbg_bluetoothemul)
1148 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1149 if (!res && dbg_wwanemul)
1150 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1151#endif
1152
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001153 return res;
1154}
1155
1156static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1157{
1158 int i;
1159
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001160 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001161 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001162
1163#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1164 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1165 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1166 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1167#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001168}
1169
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001170/****************************************************************************
1171 ****************************************************************************
1172 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001173 * Subdrivers
1174 *
1175 ****************************************************************************
1176 ****************************************************************************/
1177
1178/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001179 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001180 */
1181
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001182static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001184 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1185 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001187 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001188 (thinkpad_id.bios_version_str) ?
1189 thinkpad_id.bios_version_str : "unknown",
1190 (thinkpad_id.ec_version_str) ?
1191 thinkpad_id.ec_version_str : "unknown");
1192
1193 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001194 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001195 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1196 "IBM" : ((thinkpad_id.vendor ==
1197 PCI_VENDOR_ID_LENOVO) ?
1198 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001199 thinkpad_id.model_str,
1200 (thinkpad_id.nummodel_str) ?
1201 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204}
1205
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001206static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 int len = 0;
1209
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001210 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1211 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 return len;
1214}
1215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001216static struct ibm_struct thinkpad_acpi_driver_data = {
1217 .name = "driver",
1218 .read = thinkpad_acpi_driver_read,
1219};
1220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001221/*************************************************************************
1222 * Hotkey subdriver
1223 */
1224
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001225enum { /* hot key scan codes (derived from ACPI DSDT) */
1226 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1227 TP_ACPI_HOTKEYSCAN_FNF2,
1228 TP_ACPI_HOTKEYSCAN_FNF3,
1229 TP_ACPI_HOTKEYSCAN_FNF4,
1230 TP_ACPI_HOTKEYSCAN_FNF5,
1231 TP_ACPI_HOTKEYSCAN_FNF6,
1232 TP_ACPI_HOTKEYSCAN_FNF7,
1233 TP_ACPI_HOTKEYSCAN_FNF8,
1234 TP_ACPI_HOTKEYSCAN_FNF9,
1235 TP_ACPI_HOTKEYSCAN_FNF10,
1236 TP_ACPI_HOTKEYSCAN_FNF11,
1237 TP_ACPI_HOTKEYSCAN_FNF12,
1238 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1239 TP_ACPI_HOTKEYSCAN_FNINSERT,
1240 TP_ACPI_HOTKEYSCAN_FNDELETE,
1241 TP_ACPI_HOTKEYSCAN_FNHOME,
1242 TP_ACPI_HOTKEYSCAN_FNEND,
1243 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1244 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1245 TP_ACPI_HOTKEYSCAN_FNSPACE,
1246 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1247 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1248 TP_ACPI_HOTKEYSCAN_MUTE,
1249 TP_ACPI_HOTKEYSCAN_THINKPAD,
1250};
1251
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001252enum { /* Keys available through NVRAM polling */
1253 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1254 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1255};
1256
1257enum { /* Positions of some of the keys in hotkey masks */
1258 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1259 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1260 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1261 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1262 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1263 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1264 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1265 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1266 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1267 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1268 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1269};
1270
1271enum { /* NVRAM to ACPI HKEY group map */
1272 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1273 TP_ACPI_HKEY_ZOOM_MASK |
1274 TP_ACPI_HKEY_DISPSWTCH_MASK |
1275 TP_ACPI_HKEY_HIBERNATE_MASK,
1276 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1277 TP_ACPI_HKEY_BRGHTDWN_MASK,
1278 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1279 TP_ACPI_HKEY_VOLDWN_MASK |
1280 TP_ACPI_HKEY_MUTE_MASK,
1281};
1282
1283#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1284struct tp_nvram_state {
1285 u16 thinkpad_toggle:1;
1286 u16 zoom_toggle:1;
1287 u16 display_toggle:1;
1288 u16 thinklight_toggle:1;
1289 u16 hibernate_toggle:1;
1290 u16 displayexp_toggle:1;
1291 u16 display_state:1;
1292 u16 brightness_toggle:1;
1293 u16 volume_toggle:1;
1294 u16 mute:1;
1295
1296 u8 brightness_level;
1297 u8 volume_level;
1298};
1299
1300static struct task_struct *tpacpi_hotkey_task;
1301static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1302static int hotkey_poll_freq = 10; /* Hz */
1303static struct mutex hotkey_thread_mutex;
1304static struct mutex hotkey_thread_data_mutex;
1305static unsigned int hotkey_config_change;
1306
1307#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1308
1309#define hotkey_source_mask 0U
1310
1311#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1312
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001313static struct mutex hotkey_mutex;
1314
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001315static enum { /* Reasons for waking up */
1316 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1317 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1318 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1319} hotkey_wakeup_reason;
1320
1321static int hotkey_autosleep_ack;
1322
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001323static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001324static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001325static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001326static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001327static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001328
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001329static unsigned int hotkey_report_mode;
1330
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001331static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001332
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001333static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001334
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001335#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1336#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001337 do { \
1338 mutex_lock(&hotkey_thread_data_mutex); \
1339 hotkey_config_change++; \
1340 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001341#define HOTKEY_CONFIG_CRITICAL_END \
1342 mutex_unlock(&hotkey_thread_data_mutex);
1343#else
1344#define HOTKEY_CONFIG_CRITICAL_START
1345#define HOTKEY_CONFIG_CRITICAL_END
1346#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1347
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001348/* HKEY.MHKG() return bits */
1349#define TP_HOTKEY_TABLET_MASK (1 << 3)
1350
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001351static int hotkey_get_wlsw(int *status)
1352{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001353#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1354 if (dbg_wlswemul) {
1355 *status = !!tpacpi_wlsw_emulstate;
1356 return 0;
1357 }
1358#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001359 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1360 return -EIO;
1361 return 0;
1362}
1363
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001364static int hotkey_get_tablet_mode(int *status)
1365{
1366 int s;
1367
1368 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1369 return -EIO;
1370
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001371 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1372 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001373}
1374
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001375/*
1376 * Call with hotkey_mutex held
1377 */
1378static int hotkey_mask_get(void)
1379{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001380 u32 m = 0;
1381
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001382 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001383 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001384 return -EIO;
1385 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001386 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001387
1388 return 0;
1389}
1390
1391/*
1392 * Call with hotkey_mutex held
1393 */
1394static int hotkey_mask_set(u32 mask)
1395{
1396 int i;
1397 int rc = 0;
1398
1399 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001400 if (!tp_warned.hotkey_mask_ff &&
1401 (mask == 0xffff || mask == 0xffffff ||
1402 mask == 0xffffffff)) {
1403 tp_warned.hotkey_mask_ff = 1;
1404 printk(TPACPI_NOTICE
1405 "setting the hotkey mask to 0x%08x is likely "
1406 "not the best way to go about it\n", mask);
1407 printk(TPACPI_NOTICE
1408 "please consider using the driver defaults, "
1409 "and refer to up-to-date thinkpad-acpi "
1410 "documentation\n");
1411 }
1412
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001413 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001414 for (i = 0; i < 32; i++) {
1415 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001416 /* enable in firmware mask only keys not in NVRAM
1417 * mode, but enable the key in the cached hotkey_mask
1418 * regardless of mode, or the key will end up
1419 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001420 if (!acpi_evalf(hkey_handle,
1421 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001422 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001423 rc = -EIO;
1424 break;
1425 } else {
1426 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1427 }
1428 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001429 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001430
1431 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001432 if (!hotkey_mask_get() && !rc &&
1433 (hotkey_mask & ~hotkey_source_mask) !=
1434 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001435 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001436 "requested hot key mask 0x%08x, but "
1437 "firmware forced it to 0x%08x\n",
1438 mask, hotkey_mask);
1439 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001440 } else {
1441#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1442 HOTKEY_CONFIG_CRITICAL_START
1443 hotkey_mask = mask & hotkey_source_mask;
1444 HOTKEY_CONFIG_CRITICAL_END
1445 hotkey_mask_get();
1446 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001447 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001448 "requested hot key mask 0x%08x, "
1449 "forced to 0x%08x (NVRAM poll mask is "
1450 "0x%08x): no firmware mask support\n",
1451 mask, hotkey_mask, hotkey_source_mask);
1452 }
1453#else
1454 hotkey_mask_get();
1455 rc = -ENXIO;
1456#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001457 }
1458
1459 return rc;
1460}
1461
1462static int hotkey_status_get(int *status)
1463{
1464 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1465 return -EIO;
1466
1467 return 0;
1468}
1469
1470static int hotkey_status_set(int status)
1471{
1472 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1473 return -EIO;
1474
1475 return 0;
1476}
1477
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001478static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001479{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001480 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001481
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001482 if (tp_features.hotkey_tablet &&
1483 !hotkey_get_tablet_mode(&state)) {
1484 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001485
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001486 input_report_switch(tpacpi_inputdev,
1487 SW_TABLET_MODE, !!state);
1488 input_sync(tpacpi_inputdev);
1489
1490 mutex_unlock(&tpacpi_inputdev_send_mutex);
1491 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001492}
1493
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001494static void tpacpi_input_send_key(unsigned int scancode)
1495{
1496 unsigned int keycode;
1497
1498 keycode = hotkey_keycode_map[scancode];
1499
1500 if (keycode != KEY_RESERVED) {
1501 mutex_lock(&tpacpi_inputdev_send_mutex);
1502
1503 input_report_key(tpacpi_inputdev, keycode, 1);
1504 if (keycode == KEY_UNKNOWN)
1505 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1506 scancode);
1507 input_sync(tpacpi_inputdev);
1508
1509 input_report_key(tpacpi_inputdev, keycode, 0);
1510 if (keycode == KEY_UNKNOWN)
1511 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1512 scancode);
1513 input_sync(tpacpi_inputdev);
1514
1515 mutex_unlock(&tpacpi_inputdev_send_mutex);
1516 }
1517}
1518
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001519#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1520static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1521
1522static void tpacpi_hotkey_send_key(unsigned int scancode)
1523{
1524 tpacpi_input_send_key(scancode);
1525 if (hotkey_report_mode < 2) {
1526 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1527 0x80, 0x1001 + scancode);
1528 }
1529}
1530
1531static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1532{
1533 u8 d;
1534
1535 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1536 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1537 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1538 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1539 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1540 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1541 }
1542 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1543 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1544 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1545 }
1546 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1547 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1548 n->displayexp_toggle =
1549 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1550 }
1551 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1552 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1553 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1554 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1555 n->brightness_toggle =
1556 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1557 }
1558 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1559 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1560 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1561 >> TP_NVRAM_POS_LEVEL_VOLUME;
1562 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1563 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1564 }
1565}
1566
1567#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001568 do { \
1569 if ((mask & (1 << __scancode)) && \
1570 oldn->__member != newn->__member) \
1571 tpacpi_hotkey_send_key(__scancode); \
1572 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001573
1574#define TPACPI_MAY_SEND_KEY(__scancode) \
1575 do { if (mask & (1 << __scancode)) \
1576 tpacpi_hotkey_send_key(__scancode); } while (0)
1577
1578static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001579 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001580 u32 mask)
1581{
1582 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1583 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1584 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1585 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1586
1587 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1588
1589 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1590
1591 /* handle volume */
1592 if (oldn->volume_toggle != newn->volume_toggle) {
1593 if (oldn->mute != newn->mute) {
1594 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1595 }
1596 if (oldn->volume_level > newn->volume_level) {
1597 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1598 } else if (oldn->volume_level < newn->volume_level) {
1599 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1600 } else if (oldn->mute == newn->mute) {
1601 /* repeated key presses that didn't change state */
1602 if (newn->mute) {
1603 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1604 } else if (newn->volume_level != 0) {
1605 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1606 } else {
1607 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1608 }
1609 }
1610 }
1611
1612 /* handle brightness */
1613 if (oldn->brightness_toggle != newn->brightness_toggle) {
1614 if (oldn->brightness_level < newn->brightness_level) {
1615 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1616 } else if (oldn->brightness_level > newn->brightness_level) {
1617 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1618 } else {
1619 /* repeated key presses that didn't change state */
1620 if (newn->brightness_level != 0) {
1621 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1622 } else {
1623 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1624 }
1625 }
1626 }
1627}
1628
1629#undef TPACPI_COMPARE_KEY
1630#undef TPACPI_MAY_SEND_KEY
1631
1632static int hotkey_kthread(void *data)
1633{
1634 struct tp_nvram_state s[2];
1635 u32 mask;
1636 unsigned int si, so;
1637 unsigned long t;
1638 unsigned int change_detector, must_reset;
1639
1640 mutex_lock(&hotkey_thread_mutex);
1641
1642 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1643 goto exit;
1644
1645 set_freezable();
1646
1647 so = 0;
1648 si = 1;
1649 t = 0;
1650
1651 /* Initial state for compares */
1652 mutex_lock(&hotkey_thread_data_mutex);
1653 change_detector = hotkey_config_change;
1654 mask = hotkey_source_mask & hotkey_mask;
1655 mutex_unlock(&hotkey_thread_data_mutex);
1656 hotkey_read_nvram(&s[so], mask);
1657
1658 while (!kthread_should_stop() && hotkey_poll_freq) {
1659 if (t == 0)
1660 t = 1000/hotkey_poll_freq;
1661 t = msleep_interruptible(t);
1662 if (unlikely(kthread_should_stop()))
1663 break;
1664 must_reset = try_to_freeze();
1665 if (t > 0 && !must_reset)
1666 continue;
1667
1668 mutex_lock(&hotkey_thread_data_mutex);
1669 if (must_reset || hotkey_config_change != change_detector) {
1670 /* forget old state on thaw or config change */
1671 si = so;
1672 t = 0;
1673 change_detector = hotkey_config_change;
1674 }
1675 mask = hotkey_source_mask & hotkey_mask;
1676 mutex_unlock(&hotkey_thread_data_mutex);
1677
1678 if (likely(mask)) {
1679 hotkey_read_nvram(&s[si], mask);
1680 if (likely(si != so)) {
1681 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001682 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001683 }
1684 }
1685
1686 so = si;
1687 si ^= 1;
1688 }
1689
1690exit:
1691 mutex_unlock(&hotkey_thread_mutex);
1692 return 0;
1693}
1694
1695static void hotkey_poll_stop_sync(void)
1696{
1697 if (tpacpi_hotkey_task) {
1698 if (frozen(tpacpi_hotkey_task) ||
1699 freezing(tpacpi_hotkey_task))
1700 thaw_process(tpacpi_hotkey_task);
1701
1702 kthread_stop(tpacpi_hotkey_task);
1703 tpacpi_hotkey_task = NULL;
1704 mutex_lock(&hotkey_thread_mutex);
1705 /* at this point, the thread did exit */
1706 mutex_unlock(&hotkey_thread_mutex);
1707 }
1708}
1709
1710/* call with hotkey_mutex held */
1711static void hotkey_poll_setup(int may_warn)
1712{
1713 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1714 hotkey_poll_freq > 0 &&
1715 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1716 if (!tpacpi_hotkey_task) {
1717 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001718 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001719 if (IS_ERR(tpacpi_hotkey_task)) {
1720 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001721 printk(TPACPI_ERR
1722 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001723 "for hotkey polling\n");
1724 }
1725 }
1726 } else {
1727 hotkey_poll_stop_sync();
1728 if (may_warn &&
1729 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001730 printk(TPACPI_NOTICE
1731 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001732 "which is currently disabled\n",
1733 hotkey_source_mask);
1734 }
1735 }
1736}
1737
1738static void hotkey_poll_setup_safe(int may_warn)
1739{
1740 mutex_lock(&hotkey_mutex);
1741 hotkey_poll_setup(may_warn);
1742 mutex_unlock(&hotkey_mutex);
1743}
1744
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001745#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1746
1747static void hotkey_poll_setup_safe(int __unused)
1748{
1749}
1750
1751#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1752
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001753static int hotkey_inputdev_open(struct input_dev *dev)
1754{
1755 switch (tpacpi_lifecycle) {
1756 case TPACPI_LIFE_INIT:
1757 /*
1758 * hotkey_init will call hotkey_poll_setup_safe
1759 * at the appropriate moment
1760 */
1761 return 0;
1762 case TPACPI_LIFE_EXITING:
1763 return -EBUSY;
1764 case TPACPI_LIFE_RUNNING:
1765 hotkey_poll_setup_safe(0);
1766 return 0;
1767 }
1768
1769 /* Should only happen if tpacpi_lifecycle is corrupt */
1770 BUG();
1771 return -EBUSY;
1772}
1773
1774static void hotkey_inputdev_close(struct input_dev *dev)
1775{
1776 /* disable hotkey polling when possible */
1777 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1778 hotkey_poll_setup_safe(0);
1779}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001780
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001781/* sysfs hotkey enable ------------------------------------------------- */
1782static ssize_t hotkey_enable_show(struct device *dev,
1783 struct device_attribute *attr,
1784 char *buf)
1785{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001786 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001787
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001788 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001789 if (res)
1790 return res;
1791
1792 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1793}
1794
1795static ssize_t hotkey_enable_store(struct device *dev,
1796 struct device_attribute *attr,
1797 const char *buf, size_t count)
1798{
1799 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001800 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001801
1802 if (parse_strtoul(buf, 1, &t))
1803 return -EINVAL;
1804
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001805 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001806
1807 return (res) ? res : count;
1808}
1809
1810static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001811 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001812 hotkey_enable_show, hotkey_enable_store);
1813
1814/* sysfs hotkey mask --------------------------------------------------- */
1815static ssize_t hotkey_mask_show(struct device *dev,
1816 struct device_attribute *attr,
1817 char *buf)
1818{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001819 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001820
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001821 if (mutex_lock_interruptible(&hotkey_mutex))
1822 return -ERESTARTSYS;
1823 res = hotkey_mask_get();
1824 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001825
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001826 return (res)?
1827 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001828}
1829
1830static ssize_t hotkey_mask_store(struct device *dev,
1831 struct device_attribute *attr,
1832 const char *buf, size_t count)
1833{
1834 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001835 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001836
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001837 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001838 return -EINVAL;
1839
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001840 if (mutex_lock_interruptible(&hotkey_mutex))
1841 return -ERESTARTSYS;
1842
1843 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001844
1845#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1846 hotkey_poll_setup(1);
1847#endif
1848
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001849 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001850
1851 return (res) ? res : count;
1852}
1853
1854static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001855 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001856 hotkey_mask_show, hotkey_mask_store);
1857
1858/* sysfs hotkey bios_enabled ------------------------------------------- */
1859static ssize_t hotkey_bios_enabled_show(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1862{
1863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1864}
1865
1866static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001867 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001868
1869/* sysfs hotkey bios_mask ---------------------------------------------- */
1870static ssize_t hotkey_bios_mask_show(struct device *dev,
1871 struct device_attribute *attr,
1872 char *buf)
1873{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001875}
1876
1877static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001878 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001879
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001880/* sysfs hotkey all_mask ----------------------------------------------- */
1881static ssize_t hotkey_all_mask_show(struct device *dev,
1882 struct device_attribute *attr,
1883 char *buf)
1884{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1886 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001887}
1888
1889static struct device_attribute dev_attr_hotkey_all_mask =
1890 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1891
1892/* sysfs hotkey recommended_mask --------------------------------------- */
1893static ssize_t hotkey_recommended_mask_show(struct device *dev,
1894 struct device_attribute *attr,
1895 char *buf)
1896{
1897 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001898 (hotkey_all_mask | hotkey_source_mask)
1899 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001900}
1901
1902static struct device_attribute dev_attr_hotkey_recommended_mask =
1903 __ATTR(hotkey_recommended_mask, S_IRUGO,
1904 hotkey_recommended_mask_show, NULL);
1905
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001906#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1907
1908/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1909static ssize_t hotkey_source_mask_show(struct device *dev,
1910 struct device_attribute *attr,
1911 char *buf)
1912{
1913 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1914}
1915
1916static ssize_t hotkey_source_mask_store(struct device *dev,
1917 struct device_attribute *attr,
1918 const char *buf, size_t count)
1919{
1920 unsigned long t;
1921
1922 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1923 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1924 return -EINVAL;
1925
1926 if (mutex_lock_interruptible(&hotkey_mutex))
1927 return -ERESTARTSYS;
1928
1929 HOTKEY_CONFIG_CRITICAL_START
1930 hotkey_source_mask = t;
1931 HOTKEY_CONFIG_CRITICAL_END
1932
1933 hotkey_poll_setup(1);
1934
1935 mutex_unlock(&hotkey_mutex);
1936
1937 return count;
1938}
1939
1940static struct device_attribute dev_attr_hotkey_source_mask =
1941 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1942 hotkey_source_mask_show, hotkey_source_mask_store);
1943
1944/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1945static ssize_t hotkey_poll_freq_show(struct device *dev,
1946 struct device_attribute *attr,
1947 char *buf)
1948{
1949 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1950}
1951
1952static ssize_t hotkey_poll_freq_store(struct device *dev,
1953 struct device_attribute *attr,
1954 const char *buf, size_t count)
1955{
1956 unsigned long t;
1957
1958 if (parse_strtoul(buf, 25, &t))
1959 return -EINVAL;
1960
1961 if (mutex_lock_interruptible(&hotkey_mutex))
1962 return -ERESTARTSYS;
1963
1964 hotkey_poll_freq = t;
1965
1966 hotkey_poll_setup(1);
1967 mutex_unlock(&hotkey_mutex);
1968
1969 return count;
1970}
1971
1972static struct device_attribute dev_attr_hotkey_poll_freq =
1973 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1974 hotkey_poll_freq_show, hotkey_poll_freq_store);
1975
1976#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1977
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001978/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001979static ssize_t hotkey_radio_sw_show(struct device *dev,
1980 struct device_attribute *attr,
1981 char *buf)
1982{
1983 int res, s;
1984 res = hotkey_get_wlsw(&s);
1985 if (res < 0)
1986 return res;
1987
1988 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1989}
1990
1991static struct device_attribute dev_attr_hotkey_radio_sw =
1992 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1993
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001994static void hotkey_radio_sw_notify_change(void)
1995{
1996 if (tp_features.hotkey_wlsw)
1997 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1998 "hotkey_radio_sw");
1999}
2000
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002001/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2002static ssize_t hotkey_tablet_mode_show(struct device *dev,
2003 struct device_attribute *attr,
2004 char *buf)
2005{
2006 int res, s;
2007 res = hotkey_get_tablet_mode(&s);
2008 if (res < 0)
2009 return res;
2010
2011 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2012}
2013
2014static struct device_attribute dev_attr_hotkey_tablet_mode =
2015 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2016
2017static void hotkey_tablet_mode_notify_change(void)
2018{
2019 if (tp_features.hotkey_tablet)
2020 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2021 "hotkey_tablet_mode");
2022}
2023
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002024/* sysfs hotkey report_mode -------------------------------------------- */
2025static ssize_t hotkey_report_mode_show(struct device *dev,
2026 struct device_attribute *attr,
2027 char *buf)
2028{
2029 return snprintf(buf, PAGE_SIZE, "%d\n",
2030 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2031}
2032
2033static struct device_attribute dev_attr_hotkey_report_mode =
2034 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2035
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002036/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002037static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2038 struct device_attribute *attr,
2039 char *buf)
2040{
2041 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2042}
2043
2044static struct device_attribute dev_attr_hotkey_wakeup_reason =
2045 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2046
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002047static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002048{
2049 if (tp_features.hotkey_mask)
2050 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2051 "wakeup_reason");
2052}
2053
2054/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002055static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2056 struct device_attribute *attr,
2057 char *buf)
2058{
2059 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2060}
2061
2062static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2063 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2064 hotkey_wakeup_hotunplug_complete_show, NULL);
2065
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002066static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002067{
2068 if (tp_features.hotkey_mask)
2069 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2070 "wakeup_hotunplug_complete");
2071}
2072
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002073/* --------------------------------------------------------------------- */
2074
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002075static struct attribute *hotkey_attributes[] __initdata = {
2076 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002077 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002078 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002079#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2080 &dev_attr_hotkey_mask.attr,
2081 &dev_attr_hotkey_all_mask.attr,
2082 &dev_attr_hotkey_recommended_mask.attr,
2083 &dev_attr_hotkey_source_mask.attr,
2084 &dev_attr_hotkey_poll_freq.attr,
2085#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002086};
2087
2088static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002089 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002090#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2091 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002092 &dev_attr_hotkey_all_mask.attr,
2093 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002094#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002095 &dev_attr_hotkey_wakeup_reason.attr,
2096 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002097};
2098
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002099static void bluetooth_update_rfk(void);
2100static void wan_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002101static void tpacpi_send_radiosw_update(void)
2102{
2103 int wlsw;
2104
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002105 /* Sync these BEFORE sending any rfkill events */
2106 if (tp_features.bluetooth)
2107 bluetooth_update_rfk();
2108 if (tp_features.wan)
2109 wan_update_rfk();
2110
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002111 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2112 mutex_lock(&tpacpi_inputdev_send_mutex);
2113
2114 input_report_switch(tpacpi_inputdev,
2115 SW_RFKILL_ALL, !!wlsw);
2116 input_sync(tpacpi_inputdev);
2117
2118 mutex_unlock(&tpacpi_inputdev_send_mutex);
2119 }
2120 hotkey_radio_sw_notify_change();
2121}
2122
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002123static void hotkey_exit(void)
2124{
2125#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2126 hotkey_poll_stop_sync();
2127#endif
2128
2129 if (hotkey_dev_attributes)
2130 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2131
2132 kfree(hotkey_keycode_map);
2133
2134 if (tp_features.hotkey) {
2135 dbg_printk(TPACPI_DBG_EXIT,
2136 "restoring original hot key mask\n");
2137 /* no short-circuit boolean operator below! */
2138 if ((hotkey_mask_set(hotkey_orig_mask) |
2139 hotkey_status_set(hotkey_orig_status)) != 0)
2140 printk(TPACPI_ERR
2141 "failed to restore hot key mask "
2142 "to BIOS defaults\n");
2143 }
2144}
2145
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002146static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002147{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002148 /* Requirements for changing the default keymaps:
2149 *
2150 * 1. Many of the keys are mapped to KEY_RESERVED for very
2151 * good reasons. Do not change them unless you have deep
2152 * knowledge on the IBM and Lenovo ThinkPad firmware for
2153 * the various ThinkPad models. The driver behaves
2154 * differently for KEY_RESERVED: such keys have their
2155 * hot key mask *unset* in mask_recommended, and also
2156 * in the initial hot key mask programmed into the
2157 * firmware at driver load time, which means the firm-
2158 * ware may react very differently if you change them to
2159 * something else;
2160 *
2161 * 2. You must be subscribed to the linux-thinkpad and
2162 * ibm-acpi-devel mailing lists, and you should read the
2163 * list archives since 2007 if you want to change the
2164 * keymaps. This requirement exists so that you will
2165 * know the past history of problems with the thinkpad-
2166 * acpi driver keymaps, and also that you will be
2167 * listening to any bug reports;
2168 *
2169 * 3. Do not send thinkpad-acpi specific patches directly to
2170 * for merging, *ever*. Send them to the linux-acpi
2171 * mailinglist for comments. Merging is to be done only
2172 * through acpi-test and the ACPI maintainer.
2173 *
2174 * If the above is too much to ask, don't change the keymap.
2175 * Ask the thinkpad-acpi maintainer to do it, instead.
2176 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002177 static u16 ibm_keycode_map[] __initdata = {
2178 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2179 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2180 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2181 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002182
2183 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002184 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2185 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2186 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002187
2188 /* brightness: firmware always reacts to them, unless
2189 * X.org did some tricks in the radeon BIOS scratch
2190 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002191 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002192 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002193
2194 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002195 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002196
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002197 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2198 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002199
2200 /* Volume: firmware always react to it and reprograms
2201 * the built-in *extra* mixer. Never map it to control
2202 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002203 KEY_RESERVED, /* 0x14: VOLUME UP */
2204 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2205 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002206
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002207 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002208
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002209 /* (assignments unknown, please report if found) */
2210 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2211 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2212 };
2213 static u16 lenovo_keycode_map[] __initdata = {
2214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2215 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2216 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2217 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002218
2219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002220 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2221 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2222 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002223
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002224 /* These either have to go through ACPI video, or
2225 * act like in the IBM ThinkPads, so don't ever
2226 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002227 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002228 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002229
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002230 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002231
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002232 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2233 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002234
2235 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2236 * react to it and reprograms the built-in *extra* mixer.
2237 * Never map it to control another mixer by default.
2238 *
2239 * T60?, T61, R60?, R61: firmware and EC tries to send
2240 * these over the regular keyboard, so these are no-ops,
2241 * but there are still weird bugs re. MUTE, so do not
2242 * change unless you get test reports from all Lenovo
2243 * models. May cause the BIOS to interfere with the
2244 * HDA mixer.
2245 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002246 KEY_RESERVED, /* 0x14: VOLUME UP */
2247 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2248 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002249
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002250 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002251
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002252 /* (assignments unknown, please report if found) */
2253 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2254 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2255 };
2256
2257#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2258#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2259#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2260
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002261 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002262 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002263 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002264
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002265 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2266
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002267 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002268 BUG_ON(tpacpi_inputdev->open != NULL ||
2269 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002270
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002271 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002272 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002273
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002274#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2275 mutex_init(&hotkey_thread_mutex);
2276 mutex_init(&hotkey_thread_data_mutex);
2277#endif
2278
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002279 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002280 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002281
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002282 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002283 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002284
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002285 if (!tp_features.hotkey)
2286 return 1;
2287
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002288 tpacpi_disable_brightness_delay();
2289
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002290 hotkey_dev_attributes = create_attr_set(13, NULL);
2291 if (!hotkey_dev_attributes)
2292 return -ENOMEM;
2293 res = add_many_to_attr_set(hotkey_dev_attributes,
2294 hotkey_attributes,
2295 ARRAY_SIZE(hotkey_attributes));
2296 if (res)
2297 goto err_exit;
2298
2299 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2300 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2301 for HKEY interface version 0x100 */
2302 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2303 if ((hkeyv >> 8) != 1) {
2304 printk(TPACPI_ERR "unknown version of the "
2305 "HKEY interface: 0x%x\n", hkeyv);
2306 printk(TPACPI_ERR "please report this to %s\n",
2307 TPACPI_MAIL);
2308 } else {
2309 /*
2310 * MHKV 0x100 in A31, R40, R40e,
2311 * T4x, X31, and later
2312 */
2313 tp_features.hotkey_mask = 1;
2314 }
2315 }
2316
2317 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2318 str_supported(tp_features.hotkey_mask));
2319
2320 if (tp_features.hotkey_mask) {
2321 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2322 "MHKA", "qd")) {
2323 printk(TPACPI_ERR
2324 "missing MHKA handler, "
2325 "please report this to %s\n",
2326 TPACPI_MAIL);
2327 /* FN+F12, FN+F4, FN+F3 */
2328 hotkey_all_mask = 0x080cU;
2329 }
2330 }
2331
2332 /* hotkey_source_mask *must* be zero for
2333 * the first hotkey_mask_get */
2334 res = hotkey_status_get(&hotkey_orig_status);
2335 if (res)
2336 goto err_exit;
2337
2338 if (tp_features.hotkey_mask) {
2339 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002340 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002341 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002342
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002343 hotkey_orig_mask = hotkey_mask;
2344 res = add_many_to_attr_set(
2345 hotkey_dev_attributes,
2346 hotkey_mask_attributes,
2347 ARRAY_SIZE(hotkey_mask_attributes));
2348 if (res)
2349 goto err_exit;
2350 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002351
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002352#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002353 if (tp_features.hotkey_mask) {
2354 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2355 & ~hotkey_all_mask;
2356 } else {
2357 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2358 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002359
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002360 vdbg_printk(TPACPI_DBG_INIT,
2361 "hotkey source mask 0x%08x, polling freq %d\n",
2362 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002363#endif
2364
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002365#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2366 if (dbg_wlswemul) {
2367 tp_features.hotkey_wlsw = 1;
2368 printk(TPACPI_INFO
2369 "radio switch emulation enabled\n");
2370 } else
2371#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002372 /* Not all thinkpads have a hardware radio switch */
2373 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2374 tp_features.hotkey_wlsw = 1;
2375 printk(TPACPI_INFO
2376 "radio switch found; radios are %s\n",
2377 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002378 }
2379 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002380 res = add_to_attr_set(hotkey_dev_attributes,
2381 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002382
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002383 /* For X41t, X60t, X61t Tablets... */
2384 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2385 tp_features.hotkey_tablet = 1;
2386 printk(TPACPI_INFO
2387 "possible tablet mode switch found; "
2388 "ThinkPad in %s mode\n",
2389 (status & TP_HOTKEY_TABLET_MASK)?
2390 "tablet" : "laptop");
2391 res = add_to_attr_set(hotkey_dev_attributes,
2392 &dev_attr_hotkey_tablet_mode.attr);
2393 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002394
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002395 if (!res)
2396 res = register_attr_set_with_sysfs(
2397 hotkey_dev_attributes,
2398 &tpacpi_pdev->dev.kobj);
2399 if (res)
2400 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002401
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002402 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002403
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002404 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2405 GFP_KERNEL);
2406 if (!hotkey_keycode_map) {
2407 printk(TPACPI_ERR
2408 "failed to allocate memory for key map\n");
2409 res = -ENOMEM;
2410 goto err_exit;
2411 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002412
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002413 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2414 dbg_printk(TPACPI_DBG_INIT,
2415 "using Lenovo default hot key map\n");
2416 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2417 TPACPI_HOTKEY_MAP_SIZE);
2418 } else {
2419 dbg_printk(TPACPI_DBG_INIT,
2420 "using IBM default hot key map\n");
2421 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2422 TPACPI_HOTKEY_MAP_SIZE);
2423 }
2424
2425 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2426 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2427 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2428 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2429 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2430 tpacpi_inputdev->keycode = hotkey_keycode_map;
2431 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2432 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2433 set_bit(hotkey_keycode_map[i],
2434 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002435 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002436 if (i < sizeof(hotkey_reserved_mask)*8)
2437 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002438 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002439 }
2440
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002441 if (tp_features.hotkey_wlsw) {
2442 set_bit(EV_SW, tpacpi_inputdev->evbit);
2443 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2444 }
2445 if (tp_features.hotkey_tablet) {
2446 set_bit(EV_SW, tpacpi_inputdev->evbit);
2447 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002448 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002449
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002450 /* Do not issue duplicate brightness change events to
2451 * userspace */
2452 if (!tp_features.bright_acpimode)
2453 /* update bright_acpimode... */
2454 tpacpi_check_std_acpi_brightness_support();
2455
2456 if (tp_features.bright_acpimode) {
2457 printk(TPACPI_INFO
2458 "This ThinkPad has standard ACPI backlight "
2459 "brightness control, supported by the ACPI "
2460 "video driver\n");
2461 printk(TPACPI_NOTICE
2462 "Disabling thinkpad-acpi brightness events "
2463 "by default...\n");
2464
2465 /* The hotkey_reserved_mask change below is not
2466 * necessary while the keys are at KEY_RESERVED in the
2467 * default map, but better safe than sorry, leave it
2468 * here as a marker of what we have to do, especially
2469 * when we finally become able to set this at runtime
2470 * on response to X.org requests */
2471 hotkey_reserved_mask |=
2472 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2473 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002474 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002475
2476 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2477 res = hotkey_status_set(1);
2478 if (res) {
2479 hotkey_exit();
2480 return res;
2481 }
2482 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2483 & ~hotkey_reserved_mask)
2484 | hotkey_orig_mask);
2485 if (res < 0 && res != -ENXIO) {
2486 hotkey_exit();
2487 return res;
2488 }
2489
2490 dbg_printk(TPACPI_DBG_INIT,
2491 "legacy hot key reporting over procfs %s\n",
2492 (hotkey_report_mode < 2) ?
2493 "enabled" : "disabled");
2494
2495 tpacpi_inputdev->open = &hotkey_inputdev_open;
2496 tpacpi_inputdev->close = &hotkey_inputdev_close;
2497
2498 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002499 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002500 tpacpi_input_send_tabletsw();
2501
2502 return 0;
2503
2504err_exit:
2505 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2506 hotkey_dev_attributes = NULL;
2507
2508 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002509}
2510
2511static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2512{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002513 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002514 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002515 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002516 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002517 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002518
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002519 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002520 printk(TPACPI_ERR
2521 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002522 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002523 acpi_bus_generate_netlink_event(
2524 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002525 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002526 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002527 return;
2528 }
2529
2530 while (1) {
2531 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002532 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002533 return;
2534 }
2535
2536 if (hkey == 0) {
2537 /* queue empty */
2538 return;
2539 }
2540
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002541 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002542 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002543 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002544
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002545 switch (hkey >> 12) {
2546 case 1:
2547 /* 0x1000-0x1FFF: key presses */
2548 scancode = hkey & 0xfff;
2549 if (scancode > 0 && scancode < 0x21) {
2550 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 if (!(hotkey_source_mask & (1 << scancode))) {
2552 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002553 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 } else {
2555 ignore_acpi_ev = 1;
2556 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002557 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002558 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002559 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002560 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002561 case 2:
2562 /* Wakeup reason */
2563 switch (hkey) {
2564 case 0x2304: /* suspend, undock */
2565 case 0x2404: /* hibernation, undock */
2566 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2567 ignore_acpi_ev = 1;
2568 break;
2569 case 0x2305: /* suspend, bay eject */
2570 case 0x2405: /* hibernation, bay eject */
2571 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2572 ignore_acpi_ev = 1;
2573 break;
2574 default:
2575 unk_ev = 1;
2576 }
2577 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2578 printk(TPACPI_INFO
2579 "woke up due to a hot-unplug "
2580 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002581 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002582 }
2583 break;
2584 case 3:
2585 /* bay-related wakeups */
2586 if (hkey == 0x3003) {
2587 hotkey_autosleep_ack = 1;
2588 printk(TPACPI_INFO
2589 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002590 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002591 } else {
2592 unk_ev = 1;
2593 }
2594 break;
2595 case 4:
2596 /* dock-related wakeups */
2597 if (hkey == 0x4003) {
2598 hotkey_autosleep_ack = 1;
2599 printk(TPACPI_INFO
2600 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002601 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002602 } else {
2603 unk_ev = 1;
2604 }
2605 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002606 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002607 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002608 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002609 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002610 case 0x500b: /* X61t: tablet pen inserted into bay */
2611 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002612 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002613 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2614 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2615 tpacpi_input_send_tabletsw();
2616 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002617 send_acpi_ev = 0;
2618 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002619 case 0x5001:
2620 case 0x5002:
2621 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002622 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002623 break;
2624 default:
2625 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002626 }
2627 break;
2628 case 7:
2629 /* 0x7000-0x7FFF: misc */
2630 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002631 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002632 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002633 break;
2634 }
2635 /* fallthrough to default */
2636 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002637 unk_ev = 1;
2638 }
2639 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002640 printk(TPACPI_NOTICE
2641 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002642 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002643
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002644 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002645 if (!ignore_acpi_ev &&
2646 (send_acpi_ev || hotkey_report_mode < 2)) {
2647 acpi_bus_generate_proc_event(ibm->acpi->device,
2648 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002649 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002650
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002651 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002652 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002653 acpi_bus_generate_netlink_event(
2654 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002655 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002656 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002657 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002658 }
2659}
2660
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002661static void hotkey_suspend(pm_message_t state)
2662{
2663 /* Do these on suspend, we get the events on early resume! */
2664 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2665 hotkey_autosleep_ack = 0;
2666}
2667
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002668static void hotkey_resume(void)
2669{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002670 tpacpi_disable_brightness_delay();
2671
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002672 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002673 printk(TPACPI_ERR
2674 "error while trying to read hot key mask "
2675 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002676 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002677 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002678 hotkey_wakeup_reason_notify_change();
2679 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002680 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002681}
2682
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002683/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002686 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 int len = 0;
2688
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002689 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002690 len += sprintf(p + len, "status:\t\tnot supported\n");
2691 return len;
2692 }
2693
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002694 if (mutex_lock_interruptible(&hotkey_mutex))
2695 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002696 res = hotkey_status_get(&status);
2697 if (!res)
2698 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002699 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002700 if (res)
2701 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002704 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002705 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 len += sprintf(p + len,
2707 "commands:\tenable, disable, reset, <mask>\n");
2708 } else {
2709 len += sprintf(p + len, "mask:\t\tnot supported\n");
2710 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2711 }
2712
2713 return len;
2714}
2715
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002716static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002718 int res, status;
2719 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002722 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return -ENODEV;
2724
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002725 if (mutex_lock_interruptible(&hotkey_mutex))
2726 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002727
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002728 status = -1;
2729 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002730
2731 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 while ((cmd = next_cmd(&buf))) {
2733 if (strlencmp(cmd, "enable") == 0) {
2734 status = 1;
2735 } else if (strlencmp(cmd, "disable") == 0) {
2736 status = 0;
2737 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002738 status = hotkey_orig_status;
2739 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2741 /* mask set */
2742 } else if (sscanf(cmd, "%x", &mask) == 1) {
2743 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002744 } else {
2745 res = -EINVAL;
2746 goto errexit;
2747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002749 if (status != -1)
2750 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002752 if (!res && mask != hotkey_mask)
2753 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002755errexit:
2756 mutex_unlock(&hotkey_mutex);
2757 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002760static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002761 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002762 {"", 0},
2763};
2764
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002765static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002766 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002767 .notify = hotkey_notify,
2768 .handle = &hkey_handle,
2769 .type = ACPI_DEVICE_NOTIFY,
2770};
2771
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002772static struct ibm_struct hotkey_driver_data = {
2773 .name = "hotkey",
2774 .read = hotkey_read,
2775 .write = hotkey_write,
2776 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002777 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002778 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002779 .acpi = &ibm_hotkey_acpidriver,
2780};
2781
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002782/*************************************************************************
2783 * Bluetooth subdriver
2784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002786enum {
2787 /* ACPI GBDC/SBDC bits */
2788 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2789 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002790 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2791 off / last state */
2792};
2793
2794enum {
2795 /* ACPI \BLTH commands */
2796 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2797 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2798 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2799 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2800 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002801};
2802
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002803static struct rfkill *tpacpi_bluetooth_rfkill;
2804
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002805static void bluetooth_suspend(pm_message_t state)
2806{
2807 /* Try to make sure radio will resume powered off */
2808 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2809 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2810}
2811
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002812static int bluetooth_get_radiosw(void)
2813{
2814 int status;
2815
2816 if (!tp_features.bluetooth)
2817 return -ENODEV;
2818
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002819 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2820 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002821 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002822
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002823#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2824 if (dbg_bluetoothemul)
2825 return (tpacpi_bluetooth_emulstate) ?
2826 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2827#endif
2828
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002829 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2830 return -EIO;
2831
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002832 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2833 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002834}
2835
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002836static void bluetooth_update_rfk(void)
2837{
2838 int status;
2839
2840 if (!tpacpi_bluetooth_rfkill)
2841 return;
2842
2843 status = bluetooth_get_radiosw();
2844 if (status < 0)
2845 return;
2846 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2847}
2848
2849static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002850{
2851 int status;
2852
2853 if (!tp_features.bluetooth)
2854 return -ENODEV;
2855
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002856 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2857 * reason to risk weird behaviour. */
2858 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2859 && radio_on)
2860 return -EPERM;
2861
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002862#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2863 if (dbg_bluetoothemul) {
2864 tpacpi_bluetooth_emulstate = !!radio_on;
2865 if (update_rfk)
2866 bluetooth_update_rfk();
2867 return 0;
2868 }
2869#endif
2870
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002871 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002872 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002873 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002874 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002875 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002876 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2877 return -EIO;
2878
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002879 if (update_rfk)
2880 bluetooth_update_rfk();
2881
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002882 return 0;
2883}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002884
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002885/* sysfs bluetooth enable ---------------------------------------------- */
2886static ssize_t bluetooth_enable_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2889{
2890 int status;
2891
2892 status = bluetooth_get_radiosw();
2893 if (status < 0)
2894 return status;
2895
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002896 return snprintf(buf, PAGE_SIZE, "%d\n",
2897 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002898}
2899
2900static ssize_t bluetooth_enable_store(struct device *dev,
2901 struct device_attribute *attr,
2902 const char *buf, size_t count)
2903{
2904 unsigned long t;
2905 int res;
2906
2907 if (parse_strtoul(buf, 1, &t))
2908 return -EINVAL;
2909
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002910 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002911
2912 return (res) ? res : count;
2913}
2914
2915static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002916 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002917 bluetooth_enable_show, bluetooth_enable_store);
2918
2919/* --------------------------------------------------------------------- */
2920
2921static struct attribute *bluetooth_attributes[] = {
2922 &dev_attr_bluetooth_enable.attr,
2923 NULL
2924};
2925
2926static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002927 .attrs = bluetooth_attributes,
2928};
2929
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002930static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2931{
2932 int bts = bluetooth_get_radiosw();
2933
2934 if (bts < 0)
2935 return bts;
2936
2937 *state = bts;
2938 return 0;
2939}
2940
2941static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2942{
2943 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2944}
2945
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002946static void bluetooth_exit(void)
2947{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002948 if (tpacpi_bluetooth_rfkill)
2949 rfkill_unregister(tpacpi_bluetooth_rfkill);
2950
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002951 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2952 &bluetooth_attr_group);
2953}
2954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002955static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002957 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002958 int status = 0;
2959
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002960 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2961
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002962 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002963
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002964 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2965 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002966 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002967 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002969 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2970 str_supported(tp_features.bluetooth),
2971 status);
2972
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002973#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2974 if (dbg_bluetoothemul) {
2975 tp_features.bluetooth = 1;
2976 printk(TPACPI_INFO
2977 "bluetooth switch emulation enabled\n");
2978 } else
2979#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002980 if (tp_features.bluetooth &&
2981 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2982 /* no bluetooth hardware present in system */
2983 tp_features.bluetooth = 0;
2984 dbg_printk(TPACPI_DBG_INIT,
2985 "bluetooth hardware not installed\n");
2986 }
2987
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002988 if (!tp_features.bluetooth)
2989 return 1;
2990
2991 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002992 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002993 if (res)
2994 return res;
2995
2996 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2997 &tpacpi_bluetooth_rfkill,
2998 RFKILL_TYPE_BLUETOOTH,
2999 "tpacpi_bluetooth_sw",
3000 tpacpi_bluetooth_rfk_set,
3001 tpacpi_bluetooth_rfk_get);
3002 if (res) {
3003 bluetooth_exit();
3004 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003005 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003006
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003010/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003011static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003014 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003016 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003019 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003020 (status == RFKILL_STATE_UNBLOCKED) ?
3021 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 len += sprintf(p + len, "commands:\tenable, disable\n");
3023 }
3024
3025 return len;
3026}
3027
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003028static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003032 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003033 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 while ((cmd = next_cmd(&buf))) {
3036 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003037 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003039 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 } else
3041 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045}
3046
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003047static struct ibm_struct bluetooth_driver_data = {
3048 .name = "bluetooth",
3049 .read = bluetooth_read,
3050 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003051 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003052 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003053};
3054
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003055/*************************************************************************
3056 * Wan subdriver
3057 */
3058
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003059enum {
3060 /* ACPI GWAN/SWAN bits */
3061 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3062 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003063 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3064 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003065};
3066
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003067static struct rfkill *tpacpi_wan_rfkill;
3068
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003069static void wan_suspend(pm_message_t state)
3070{
3071 /* Try to make sure radio will resume powered off */
3072 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3073 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3074}
3075
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003076static int wan_get_radiosw(void)
3077{
3078 int status;
3079
3080 if (!tp_features.wan)
3081 return -ENODEV;
3082
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003083 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3084 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003085 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003086
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003087#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3088 if (dbg_wwanemul)
3089 return (tpacpi_wwan_emulstate) ?
3090 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3091#endif
3092
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003093 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3094 return -EIO;
3095
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003096 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3097 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003098}
3099
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003100static void wan_update_rfk(void)
3101{
3102 int status;
3103
3104 if (!tpacpi_wan_rfkill)
3105 return;
3106
3107 status = wan_get_radiosw();
3108 if (status < 0)
3109 return;
3110 rfkill_force_state(tpacpi_wan_rfkill, status);
3111}
3112
3113static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003114{
3115 int status;
3116
3117 if (!tp_features.wan)
3118 return -ENODEV;
3119
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003120 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3121 * reason to risk weird behaviour. */
3122 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3123 && radio_on)
3124 return -EPERM;
3125
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003126#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3127 if (dbg_wwanemul) {
3128 tpacpi_wwan_emulstate = !!radio_on;
3129 if (update_rfk)
3130 wan_update_rfk();
3131 return 0;
3132 }
3133#endif
3134
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003135 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003136 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003137 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003138 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003139 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003140 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3141 return -EIO;
3142
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003143 if (update_rfk)
3144 wan_update_rfk();
3145
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003146 return 0;
3147}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003148
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003149/* sysfs wan enable ---------------------------------------------------- */
3150static ssize_t wan_enable_show(struct device *dev,
3151 struct device_attribute *attr,
3152 char *buf)
3153{
3154 int status;
3155
3156 status = wan_get_radiosw();
3157 if (status < 0)
3158 return status;
3159
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003160 return snprintf(buf, PAGE_SIZE, "%d\n",
3161 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003162}
3163
3164static ssize_t wan_enable_store(struct device *dev,
3165 struct device_attribute *attr,
3166 const char *buf, size_t count)
3167{
3168 unsigned long t;
3169 int res;
3170
3171 if (parse_strtoul(buf, 1, &t))
3172 return -EINVAL;
3173
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003174 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003175
3176 return (res) ? res : count;
3177}
3178
3179static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003180 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003181 wan_enable_show, wan_enable_store);
3182
3183/* --------------------------------------------------------------------- */
3184
3185static struct attribute *wan_attributes[] = {
3186 &dev_attr_wan_enable.attr,
3187 NULL
3188};
3189
3190static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003191 .attrs = wan_attributes,
3192};
3193
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003194static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3195{
3196 int wans = wan_get_radiosw();
3197
3198 if (wans < 0)
3199 return wans;
3200
3201 *state = wans;
3202 return 0;
3203}
3204
3205static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3206{
3207 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3208}
3209
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003210static void wan_exit(void)
3211{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003212 if (tpacpi_wan_rfkill)
3213 rfkill_unregister(tpacpi_wan_rfkill);
3214
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003215 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3216 &wan_attr_group);
3217}
3218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003219static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003220{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003221 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003222 int status = 0;
3223
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003224 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3225
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003226 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003227
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003228 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003229 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003230
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003231 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3232 str_supported(tp_features.wan),
3233 status);
3234
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003235#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3236 if (dbg_wwanemul) {
3237 tp_features.wan = 1;
3238 printk(TPACPI_INFO
3239 "wwan switch emulation enabled\n");
3240 } else
3241#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003242 if (tp_features.wan &&
3243 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3244 /* no wan hardware present in system */
3245 tp_features.wan = 0;
3246 dbg_printk(TPACPI_DBG_INIT,
3247 "wan hardware not installed\n");
3248 }
3249
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003250 if (!tp_features.wan)
3251 return 1;
3252
3253 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003254 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003255 if (res)
3256 return res;
3257
3258 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3259 &tpacpi_wan_rfkill,
3260 RFKILL_TYPE_WWAN,
3261 "tpacpi_wwan_sw",
3262 tpacpi_wan_rfk_set,
3263 tpacpi_wan_rfk_get);
3264 if (res) {
3265 wan_exit();
3266 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003267 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003268
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003269 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003270}
3271
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003272/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003273static int wan_read(char *p)
3274{
3275 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003276 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003277
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003278 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003279 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003280 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003281 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003282 (status == RFKILL_STATE_UNBLOCKED) ?
3283 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003284 len += sprintf(p + len, "commands:\tenable, disable\n");
3285 }
3286
3287 return len;
3288}
3289
3290static int wan_write(char *buf)
3291{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003292 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003293
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003294 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003295 return -ENODEV;
3296
3297 while ((cmd = next_cmd(&buf))) {
3298 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003299 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003300 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003301 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003302 } else
3303 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003304 }
3305
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003306 return 0;
3307}
3308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003309static struct ibm_struct wan_driver_data = {
3310 .name = "wan",
3311 .read = wan_read,
3312 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003313 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003314 .suspend = wan_suspend,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003315};
3316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003317/*************************************************************************
3318 * Video subdriver
3319 */
3320
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003321#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3322
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003323enum video_access_mode {
3324 TPACPI_VIDEO_NONE = 0,
3325 TPACPI_VIDEO_570, /* 570 */
3326 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3327 TPACPI_VIDEO_NEW, /* all others */
3328};
3329
3330enum { /* video status flags, based on VIDEO_570 */
3331 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3332 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3333 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3334};
3335
3336enum { /* TPACPI_VIDEO_570 constants */
3337 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3338 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3339 * video_status_flags */
3340 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3341 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3342};
3343
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003344static enum video_access_mode video_supported;
3345static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003346
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003347static int video_autosw_get(void);
3348static int video_autosw_set(int enable);
3349
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003350TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003351
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003352static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003354 int ivga;
3355
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003356 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3357
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003358 TPACPI_ACPIHANDLE_INIT(vid);
3359 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003361 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3362 /* G41, assume IVGA doesn't change */
3363 vid_handle = vid2_handle;
3364
3365 if (!vid_handle)
3366 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003367 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003368 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3369 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003370 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003371 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3372 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003373 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003374 else
3375 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003376 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003378 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3379 str_supported(video_supported != TPACPI_VIDEO_NONE),
3380 video_supported);
3381
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003382 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003385static void video_exit(void)
3386{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003387 dbg_printk(TPACPI_DBG_EXIT,
3388 "restoring original video autoswitch mode\n");
3389 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003390 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003391 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003392}
3393
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003394static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 int status = 0;
3397 int i;
3398
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003399 switch (video_supported) {
3400 case TPACPI_VIDEO_570:
3401 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3402 TP_ACPI_VIDEO_570_PHSCMD))
3403 return -EIO;
3404 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3405 break;
3406 case TPACPI_VIDEO_770:
3407 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3408 return -EIO;
3409 if (i)
3410 status |= TP_ACPI_VIDEO_S_LCD;
3411 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3412 return -EIO;
3413 if (i)
3414 status |= TP_ACPI_VIDEO_S_CRT;
3415 break;
3416 case TPACPI_VIDEO_NEW:
3417 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3418 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3419 return -EIO;
3420 if (i)
3421 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003423 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3424 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3425 return -EIO;
3426 if (i)
3427 status |= TP_ACPI_VIDEO_S_LCD;
3428 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3429 return -EIO;
3430 if (i)
3431 status |= TP_ACPI_VIDEO_S_DVI;
3432 break;
3433 default:
3434 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 return status;
3438}
3439
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003440static int video_outputsw_set(int status)
3441{
3442 int autosw;
3443 int res = 0;
3444
3445 switch (video_supported) {
3446 case TPACPI_VIDEO_570:
3447 res = acpi_evalf(NULL, NULL,
3448 "\\_SB.PHS2", "vdd",
3449 TP_ACPI_VIDEO_570_PHS2CMD,
3450 status | TP_ACPI_VIDEO_570_PHS2SET);
3451 break;
3452 case TPACPI_VIDEO_770:
3453 autosw = video_autosw_get();
3454 if (autosw < 0)
3455 return autosw;
3456
3457 res = video_autosw_set(1);
3458 if (res)
3459 return res;
3460 res = acpi_evalf(vid_handle, NULL,
3461 "ASWT", "vdd", status * 0x100, 0);
3462 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003463 printk(TPACPI_ERR
3464 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003465 return -EIO;
3466 }
3467 break;
3468 case TPACPI_VIDEO_NEW:
3469 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003470 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003471 break;
3472 default:
3473 return -ENOSYS;
3474 }
3475
3476 return (res)? 0 : -EIO;
3477}
3478
3479static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003481 int autosw = 0;
3482
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003483 switch (video_supported) {
3484 case TPACPI_VIDEO_570:
3485 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3486 return -EIO;
3487 break;
3488 case TPACPI_VIDEO_770:
3489 case TPACPI_VIDEO_NEW:
3490 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3491 return -EIO;
3492 break;
3493 default:
3494 return -ENOSYS;
3495 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003496
3497 return autosw & 1;
3498}
3499
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003500static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003501{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003502 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003503 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003504 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003505}
3506
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003507static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003508{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003509 int autosw = video_autosw_get();
3510 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003511
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003512 if (autosw < 0)
3513 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003514
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003515 switch (video_supported) {
3516 case TPACPI_VIDEO_570:
3517 res = video_autosw_set(1);
3518 if (res)
3519 return res;
3520 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3521 break;
3522 case TPACPI_VIDEO_770:
3523 case TPACPI_VIDEO_NEW:
3524 res = video_autosw_set(1);
3525 if (res)
3526 return res;
3527 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3528 break;
3529 default:
3530 return -ENOSYS;
3531 }
3532 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003533 printk(TPACPI_ERR
3534 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003535 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003536 }
3537
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003538 return (res)? 0 : -EIO;
3539}
3540
3541static int video_expand_toggle(void)
3542{
3543 switch (video_supported) {
3544 case TPACPI_VIDEO_570:
3545 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3546 0 : -EIO;
3547 case TPACPI_VIDEO_770:
3548 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3549 0 : -EIO;
3550 case TPACPI_VIDEO_NEW:
3551 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3552 0 : -EIO;
3553 default:
3554 return -ENOSYS;
3555 }
3556 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003557}
3558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003559static int video_read(char *p)
3560{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003561 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003562 int len = 0;
3563
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003564 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003565 len += sprintf(p + len, "status:\t\tnot supported\n");
3566 return len;
3567 }
3568
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003569 status = video_outputsw_get();
3570 if (status < 0)
3571 return status;
3572
3573 autosw = video_autosw_get();
3574 if (autosw < 0)
3575 return autosw;
3576
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003577 len += sprintf(p + len, "status:\t\tsupported\n");
3578 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3579 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003580 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003581 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3582 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3583 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3584 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003585 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003586 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3587 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3588 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3589
3590 return len;
3591}
3592
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003593static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
3595 char *cmd;
3596 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003597 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003599 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003600 return -ENODEV;
3601
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003602 enable = 0;
3603 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 while ((cmd = next_cmd(&buf))) {
3606 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003607 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003609 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003611 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003613 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003614 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003615 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003616 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003617 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003618 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003619 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003621 res = video_autosw_set(1);
3622 if (res)
3623 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003625 res = video_autosw_set(0);
3626 if (res)
3627 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003629 res = video_outputsw_cycle();
3630 if (res)
3631 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003633 res = video_expand_toggle();
3634 if (res)
3635 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 } else
3637 return -EINVAL;
3638 }
3639
3640 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003641 status = video_outputsw_get();
3642 if (status < 0)
3643 return status;
3644 res = video_outputsw_set((status & ~disable) | enable);
3645 if (res)
3646 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 }
3648
3649 return 0;
3650}
3651
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003652static struct ibm_struct video_driver_data = {
3653 .name = "video",
3654 .read = video_read,
3655 .write = video_write,
3656 .exit = video_exit,
3657};
3658
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003659#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3660
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003661/*************************************************************************
3662 * Light (thinklight) subdriver
3663 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003665TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3666TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003667
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003668static int light_get_status(void)
3669{
3670 int status = 0;
3671
3672 if (tp_features.light_status) {
3673 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3674 return -EIO;
3675 return (!!status);
3676 }
3677
3678 return -ENXIO;
3679}
3680
3681static int light_set_status(int status)
3682{
3683 int rc;
3684
3685 if (tp_features.light) {
3686 if (cmos_handle) {
3687 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3688 (status)?
3689 TP_CMOS_THINKLIGHT_ON :
3690 TP_CMOS_THINKLIGHT_OFF);
3691 } else {
3692 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3693 (status)? 1 : 0);
3694 }
3695 return (rc)? 0 : -EIO;
3696 }
3697
3698 return -ENXIO;
3699}
3700
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003701static void light_set_status_worker(struct work_struct *work)
3702{
3703 struct tpacpi_led_classdev *data =
3704 container_of(work, struct tpacpi_led_classdev, work);
3705
3706 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3707 light_set_status((data->new_brightness != LED_OFF));
3708}
3709
3710static void light_sysfs_set(struct led_classdev *led_cdev,
3711 enum led_brightness brightness)
3712{
3713 struct tpacpi_led_classdev *data =
3714 container_of(led_cdev,
3715 struct tpacpi_led_classdev,
3716 led_classdev);
3717 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003718 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003719}
3720
3721static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3722{
3723 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3724}
3725
3726static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3727 .led_classdev = {
3728 .name = "tpacpi::thinklight",
3729 .brightness_set = &light_sysfs_set,
3730 .brightness_get = &light_sysfs_get,
3731 }
3732};
3733
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003734static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003736 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003737
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003738 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3739
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003740 TPACPI_ACPIHANDLE_INIT(ledb);
3741 TPACPI_ACPIHANDLE_INIT(lght);
3742 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003743 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003744
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003745 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003746 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003747
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003748 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003749 /* light status not supported on
3750 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003751 tp_features.light_status =
3752 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003754 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3755 str_supported(tp_features.light),
3756 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003757
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003758 if (!tp_features.light)
3759 return 1;
3760
3761 rc = led_classdev_register(&tpacpi_pdev->dev,
3762 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003763
3764 if (rc < 0) {
3765 tp_features.light = 0;
3766 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003767 } else {
3768 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003769 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003770
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003771 return rc;
3772}
3773
3774static void light_exit(void)
3775{
3776 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3777 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003778 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
3780
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003781static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003784 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003786 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003787 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003788 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003789 len += sprintf(p + len, "status:\t\tunknown\n");
3790 len += sprintf(p + len, "commands:\ton, off\n");
3791 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003792 status = light_get_status();
3793 if (status < 0)
3794 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003796 len += sprintf(p + len, "commands:\ton, off\n");
3797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 return len;
3800}
3801
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003802static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003805 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003806
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003807 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003808 return -ENODEV;
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 while ((cmd = next_cmd(&buf))) {
3811 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003812 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003814 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 } else
3816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003819 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003822static struct ibm_struct light_driver_data = {
3823 .name = "light",
3824 .read = light_read,
3825 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003826 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003827};
3828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003829/*************************************************************************
3830 * Dock subdriver
3831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003833#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003834
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003835static void dock_notify(struct ibm_struct *ibm, u32 event);
3836static int dock_read(char *p);
3837static int dock_write(char *buf);
3838
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003839TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003840 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3841 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3842 "\\_SB.PCI.ISA.SLCE", /* 570 */
3843 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3844
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003845/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003846TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003847
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003848static const struct acpi_device_id ibm_pci_device_ids[] = {
3849 {PCI_ROOT_HID_STRING, 0},
3850 {"", 0},
3851};
3852
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003853static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3854 {
3855 .notify = dock_notify,
3856 .handle = &dock_handle,
3857 .type = ACPI_SYSTEM_NOTIFY,
3858 },
3859 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003860 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3861 * We just use it to get notifications of dock hotplug
3862 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003863 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003864 .notify = dock_notify,
3865 .handle = &pci_handle,
3866 .type = ACPI_SYSTEM_NOTIFY,
3867 },
3868};
3869
3870static struct ibm_struct dock_driver_data[2] = {
3871 {
3872 .name = "dock",
3873 .read = dock_read,
3874 .write = dock_write,
3875 .acpi = &ibm_dock_acpidriver[0],
3876 },
3877 {
3878 .name = "dock",
3879 .acpi = &ibm_dock_acpidriver[1],
3880 },
3881};
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883#define dock_docked() (_sta(dock_handle) & 1)
3884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003885static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003886{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003887 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3888
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003889 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003890
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003891 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3892 str_supported(dock_handle != NULL));
3893
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003894 return (dock_handle)? 0 : 1;
3895}
3896
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003897static int __init dock_init2(struct ibm_init_struct *iibm)
3898{
3899 int dock2_needed;
3900
3901 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3902
3903 if (dock_driver_data[0].flags.acpi_driver_registered &&
3904 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003905 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003906 dock2_needed = (pci_handle != NULL);
3907 vdbg_printk(TPACPI_DBG_INIT,
3908 "dock PCI handler for the TP 570 is %s\n",
3909 str_supported(dock2_needed));
3910 } else {
3911 vdbg_printk(TPACPI_DBG_INIT,
3912 "dock subdriver part 2 not required\n");
3913 dock2_needed = 0;
3914 }
3915
3916 return (dock2_needed)? 0 : 1;
3917}
3918
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003919static void dock_notify(struct ibm_struct *ibm, u32 event)
3920{
3921 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003922 int pci = ibm->acpi->hid && ibm->acpi->device &&
3923 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003924 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003925
3926 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003927 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003928 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003929 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003930 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003931 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003932 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003933 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003934 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003935 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003936 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003937 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003938 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003939 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003940 }
Len Brown14e04fb32007-08-23 15:20:26 -04003941 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003942 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003943 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08003944 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003945}
3946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003947static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
3949 int len = 0;
3950 int docked = dock_docked();
3951
3952 if (!dock_handle)
3953 len += sprintf(p + len, "status:\t\tnot supported\n");
3954 else if (!docked)
3955 len += sprintf(p + len, "status:\t\tundocked\n");
3956 else {
3957 len += sprintf(p + len, "status:\t\tdocked\n");
3958 len += sprintf(p + len, "commands:\tdock, undock\n");
3959 }
3960
3961 return len;
3962}
3963
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003964static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 char *cmd;
3967
3968 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003969 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 while ((cmd = next_cmd(&buf))) {
3972 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003973 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3974 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 return -EIO;
3976 } else if (strlencmp(cmd, "dock") == 0) {
3977 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3978 return -EIO;
3979 } else
3980 return -EINVAL;
3981 }
3982
3983 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003986#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003988/*************************************************************************
3989 * Bay subdriver
3990 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003992#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003993
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003994TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003995 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3996 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3997 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3998 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003999TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004000 "_EJ0", /* all others */
4001 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004002TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004003 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4004 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004005TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004006 "_EJ0", /* 770x */
4007 ); /* all others */
4008
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004009static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004011 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4012
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004013 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004014 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004015 TPACPI_ACPIHANDLE_INIT(bay_ej);
4016 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004017 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004018 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004019
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004020 tp_features.bay_status = bay_handle &&
4021 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4022 tp_features.bay_status2 = bay2_handle &&
4023 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004024
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004025 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4026 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4027 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4028 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004030 vdbg_printk(TPACPI_DBG_INIT,
4031 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004032 str_supported(tp_features.bay_status),
4033 str_supported(tp_features.bay_eject),
4034 str_supported(tp_features.bay_status2),
4035 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004036
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004037 return (tp_features.bay_status || tp_features.bay_eject ||
4038 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004041static void bay_notify(struct ibm_struct *ibm, u32 event)
4042{
Len Brown14e04fb32007-08-23 15:20:26 -04004043 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004044 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004045 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004046 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004047}
4048
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004049#define bay_occupied(b) (_sta(b##_handle) & 1)
4050
4051static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
4053 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004054 int occupied = bay_occupied(bay);
4055 int occupied2 = bay_occupied(bay2);
4056 int eject, eject2;
4057
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004058 len += sprintf(p + len, "status:\t\t%s\n",
4059 tp_features.bay_status ?
4060 (occupied ? "occupied" : "unoccupied") :
4061 "not supported");
4062 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004063 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4064 "occupied" : "unoccupied");
4065
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004066 eject = tp_features.bay_eject && occupied;
4067 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004068
4069 if (eject && eject2)
4070 len += sprintf(p + len, "commands:\teject, eject2\n");
4071 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004073 else if (eject2)
4074 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
4076 return len;
4077}
4078
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004079static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
4081 char *cmd;
4082
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004083 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004084 return -ENODEV;
4085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004087 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004088 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4089 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004090 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004091 strlencmp(cmd, "eject2") == 0) {
4092 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return -EIO;
4094 } else
4095 return -EINVAL;
4096 }
4097
4098 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004099}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004100
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004101static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4102 .notify = bay_notify,
4103 .handle = &bay_handle,
4104 .type = ACPI_SYSTEM_NOTIFY,
4105};
4106
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004107static struct ibm_struct bay_driver_data = {
4108 .name = "bay",
4109 .read = bay_read,
4110 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004111 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004112};
4113
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004114#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004116/*************************************************************************
4117 * CMOS subdriver
4118 */
4119
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004120/* sysfs cmos_command -------------------------------------------------- */
4121static ssize_t cmos_command_store(struct device *dev,
4122 struct device_attribute *attr,
4123 const char *buf, size_t count)
4124{
4125 unsigned long cmos_cmd;
4126 int res;
4127
4128 if (parse_strtoul(buf, 21, &cmos_cmd))
4129 return -EINVAL;
4130
4131 res = issue_thinkpad_cmos_command(cmos_cmd);
4132 return (res)? res : count;
4133}
4134
4135static struct device_attribute dev_attr_cmos_command =
4136 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4137
4138/* --------------------------------------------------------------------- */
4139
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004140static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004141{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004142 int res;
4143
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004144 vdbg_printk(TPACPI_DBG_INIT,
4145 "initializing cmos commands subdriver\n");
4146
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004147 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004148
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004149 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4150 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004151
4152 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4153 if (res)
4154 return res;
4155
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004156 return (cmos_handle)? 0 : 1;
4157}
4158
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004159static void cmos_exit(void)
4160{
4161 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4162}
4163
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004164static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
4166 int len = 0;
4167
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004168 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4169 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 if (!cmos_handle)
4171 len += sprintf(p + len, "status:\t\tnot supported\n");
4172 else {
4173 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004174 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 }
4176
4177 return len;
4178}
4179
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004180static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004183 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004186 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4187 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 /* cmos_cmd set */
4189 } else
4190 return -EINVAL;
4191
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004192 res = issue_thinkpad_cmos_command(cmos_cmd);
4193 if (res)
4194 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
4196
4197 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004198}
4199
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004200static struct ibm_struct cmos_driver_data = {
4201 .name = "cmos",
4202 .read = cmos_read,
4203 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004204 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004205};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004206
4207/*************************************************************************
4208 * LED subdriver
4209 */
4210
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004211enum led_access_mode {
4212 TPACPI_LED_NONE = 0,
4213 TPACPI_LED_570, /* 570 */
4214 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4215 TPACPI_LED_NEW, /* all others */
4216};
4217
4218enum { /* For TPACPI_LED_OLD */
4219 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4220 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4221 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4222};
4223
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004224enum led_status_t {
4225 TPACPI_LED_OFF = 0,
4226 TPACPI_LED_ON,
4227 TPACPI_LED_BLINK,
4228};
4229
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004230static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004231
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004232TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004233 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4234 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004235 "LED", /* all others */
4236 ); /* R30, R31 */
4237
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004238#define TPACPI_LED_NUMLEDS 8
4239static struct tpacpi_led_classdev *tpacpi_leds;
4240static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004241static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004242 /* there's a limit of 19 chars + NULL before 2.6.26 */
4243 "tpacpi::power",
4244 "tpacpi:orange:batt",
4245 "tpacpi:green:batt",
4246 "tpacpi::dock_active",
4247 "tpacpi::bay_active",
4248 "tpacpi::dock_batt",
4249 "tpacpi::unknown_led",
4250 "tpacpi::standby",
4251};
4252
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004253static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004254{
4255 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004256 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004257
4258 switch (led_supported) {
4259 case TPACPI_LED_570:
4260 if (!acpi_evalf(ec_handle,
4261 &status, "GLED", "dd", 1 << led))
4262 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004263 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004264 TPACPI_LED_OFF :
4265 ((status == 1)?
4266 TPACPI_LED_ON :
4267 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004268 tpacpi_led_state_cache[led] = led_s;
4269 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004270 default:
4271 return -ENXIO;
4272 }
4273
4274 /* not reached */
4275}
4276
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004277static int led_set_status(const unsigned int led,
4278 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004279{
4280 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004281 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4282 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004283
4284 int rc = 0;
4285
4286 switch (led_supported) {
4287 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004288 /* 570 */
4289 if (led > 7)
4290 return -EINVAL;
4291 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4292 (1 << led), led_sled_arg1[ledstatus]))
4293 rc = -EIO;
4294 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004295 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004296 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4297 if (led > 7)
4298 return -EINVAL;
4299 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4300 if (rc >= 0)
4301 rc = ec_write(TPACPI_LED_EC_HLBL,
4302 (ledstatus == TPACPI_LED_BLINK) << led);
4303 if (rc >= 0)
4304 rc = ec_write(TPACPI_LED_EC_HLCL,
4305 (ledstatus != TPACPI_LED_OFF) << led);
4306 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004307 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004308 /* all others */
4309 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4310 led, led_led_arg1[ledstatus]))
4311 rc = -EIO;
4312 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004313 default:
4314 rc = -ENXIO;
4315 }
4316
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004317 if (!rc)
4318 tpacpi_led_state_cache[led] = ledstatus;
4319
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004320 return rc;
4321}
4322
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004323static void led_sysfs_set_status(unsigned int led,
4324 enum led_brightness brightness)
4325{
4326 led_set_status(led,
4327 (brightness == LED_OFF) ?
4328 TPACPI_LED_OFF :
4329 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4330 TPACPI_LED_BLINK : TPACPI_LED_ON);
4331}
4332
4333static void led_set_status_worker(struct work_struct *work)
4334{
4335 struct tpacpi_led_classdev *data =
4336 container_of(work, struct tpacpi_led_classdev, work);
4337
4338 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4339 led_sysfs_set_status(data->led, data->new_brightness);
4340}
4341
4342static void led_sysfs_set(struct led_classdev *led_cdev,
4343 enum led_brightness brightness)
4344{
4345 struct tpacpi_led_classdev *data = container_of(led_cdev,
4346 struct tpacpi_led_classdev, led_classdev);
4347
4348 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004349 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004350}
4351
4352static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4353 unsigned long *delay_on, unsigned long *delay_off)
4354{
4355 struct tpacpi_led_classdev *data = container_of(led_cdev,
4356 struct tpacpi_led_classdev, led_classdev);
4357
4358 /* Can we choose the flash rate? */
4359 if (*delay_on == 0 && *delay_off == 0) {
4360 /* yes. set them to the hardware blink rate (1 Hz) */
4361 *delay_on = 500; /* ms */
4362 *delay_off = 500; /* ms */
4363 } else if ((*delay_on != 500) || (*delay_off != 500))
4364 return -EINVAL;
4365
4366 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004367 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004368
4369 return 0;
4370}
4371
4372static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4373{
4374 int rc;
4375
4376 struct tpacpi_led_classdev *data = container_of(led_cdev,
4377 struct tpacpi_led_classdev, led_classdev);
4378
4379 rc = led_get_status(data->led);
4380
4381 if (rc == TPACPI_LED_OFF || rc < 0)
4382 rc = LED_OFF; /* no error handling in led class :( */
4383 else
4384 rc = LED_FULL;
4385
4386 return rc;
4387}
4388
4389static void led_exit(void)
4390{
4391 unsigned int i;
4392
4393 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4394 if (tpacpi_leds[i].led_classdev.name)
4395 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4396 }
4397
4398 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004399}
4400
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004401static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004402{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004403 unsigned int i;
4404 int rc;
4405
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004406 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4407
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004408 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004409
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004410 if (!led_handle)
4411 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004412 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004413 else if (strlencmp(led_path, "SLED") == 0)
4414 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004415 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004416 else if (strlencmp(led_path, "SYSL") == 0)
4417 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004418 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419 else
4420 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004421 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004422
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004423 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4424 str_supported(led_supported), led_supported);
4425
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004426 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4427 GFP_KERNEL);
4428 if (!tpacpi_leds) {
4429 printk(TPACPI_ERR "Out of memory for LED data\n");
4430 return -ENOMEM;
4431 }
4432
4433 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4434 tpacpi_leds[i].led = i;
4435
4436 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4437 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4438 if (led_supported == TPACPI_LED_570)
4439 tpacpi_leds[i].led_classdev.brightness_get =
4440 &led_sysfs_get;
4441
4442 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4443
4444 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4445
4446 rc = led_classdev_register(&tpacpi_pdev->dev,
4447 &tpacpi_leds[i].led_classdev);
4448 if (rc < 0) {
4449 tpacpi_leds[i].led_classdev.name = NULL;
4450 led_exit();
4451 return rc;
4452 }
4453 }
4454
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004455 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004456}
4457
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004458#define str_led_status(s) \
4459 ((s) == TPACPI_LED_OFF ? "off" : \
4460 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004461
4462static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 int len = 0;
4465
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004466 if (!led_supported) {
4467 len += sprintf(p + len, "status:\t\tnot supported\n");
4468 return len;
4469 }
4470 len += sprintf(p + len, "status:\t\tsupported\n");
4471
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004472 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004473 /* 570 */
4474 int i, status;
4475 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004476 status = led_get_status(i);
4477 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004478 return -EIO;
4479 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004480 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004481 }
4482 }
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004485 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 return len;
4488}
4489
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004490static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
4492 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004493 int led, rc;
4494 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495
4496 if (!led_supported)
4497 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004500 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return -EINVAL;
4502
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004503 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004504 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004506 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004507 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004508 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004509 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004510 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004511 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004512
4513 rc = led_set_status(led, s);
4514 if (rc < 0)
4515 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
4517
4518 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004519}
4520
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004521static struct ibm_struct led_driver_data = {
4522 .name = "led",
4523 .read = led_read,
4524 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004525 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004526};
4527
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004528/*************************************************************************
4529 * Beep subdriver
4530 */
4531
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004532TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004533
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004534static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004535{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004536 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004538 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004539
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004540 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4541 str_supported(beep_handle != NULL));
4542
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004543 return (beep_handle)? 0 : 1;
4544}
4545
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004546static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 int len = 0;
4549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004550 if (!beep_handle)
4551 len += sprintf(p + len, "status:\t\tnot supported\n");
4552 else {
4553 len += sprintf(p + len, "status:\t\tsupported\n");
4554 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557 return len;
4558}
4559
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
4562 char *cmd;
4563 int beep_cmd;
4564
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565 if (!beep_handle)
4566 return -ENODEV;
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4570 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 /* beep_cmd set */
4572 } else
4573 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004574 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return -EIO;
4576 }
4577
4578 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004579}
4580
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004581static struct ibm_struct beep_driver_data = {
4582 .name = "beep",
4583 .read = beep_read,
4584 .write = beep_write,
4585};
4586
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004587/*************************************************************************
4588 * Thermal subdriver
4589 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004590
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004591enum thermal_access_mode {
4592 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4593 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4594 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4595 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4596 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4597};
4598
4599enum { /* TPACPI_THERMAL_TPEC_* */
4600 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4601 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4602 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4603};
4604
4605#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4606struct ibm_thermal_sensors_struct {
4607 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4608};
4609
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004610static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004612/* idx is zero-based */
4613static int thermal_get_sensor(int idx, s32 *value)
4614{
4615 int t;
4616 s8 tmp;
4617 char tmpi[5];
4618
4619 t = TP_EC_THERMAL_TMP0;
4620
4621 switch (thermal_read_mode) {
4622#if TPACPI_MAX_THERMAL_SENSORS >= 16
4623 case TPACPI_THERMAL_TPEC_16:
4624 if (idx >= 8 && idx <= 15) {
4625 t = TP_EC_THERMAL_TMP8;
4626 idx -= 8;
4627 }
4628 /* fallthrough */
4629#endif
4630 case TPACPI_THERMAL_TPEC_8:
4631 if (idx <= 7) {
4632 if (!acpi_ec_read(t + idx, &tmp))
4633 return -EIO;
4634 *value = tmp * 1000;
4635 return 0;
4636 }
4637 break;
4638
4639 case TPACPI_THERMAL_ACPI_UPDT:
4640 if (idx <= 7) {
4641 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4642 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4643 return -EIO;
4644 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4645 return -EIO;
4646 *value = (t - 2732) * 100;
4647 return 0;
4648 }
4649 break;
4650
4651 case TPACPI_THERMAL_ACPI_TMP07:
4652 if (idx <= 7) {
4653 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4654 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4655 return -EIO;
4656 if (t > 127 || t < -127)
4657 t = TP_EC_THERMAL_TMP_NA;
4658 *value = t * 1000;
4659 return 0;
4660 }
4661 break;
4662
4663 case TPACPI_THERMAL_NONE:
4664 default:
4665 return -ENOSYS;
4666 }
4667
4668 return -EINVAL;
4669}
4670
4671static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4672{
4673 int res, i;
4674 int n;
4675
4676 n = 8;
4677 i = 0;
4678
4679 if (!s)
4680 return -EINVAL;
4681
4682 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4683 n = 16;
4684
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004685 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004686 res = thermal_get_sensor(i, &s->temp[i]);
4687 if (res)
4688 return res;
4689 }
4690
4691 return n;
4692}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004693
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004694/* sysfs temp##_input -------------------------------------------------- */
4695
4696static ssize_t thermal_temp_input_show(struct device *dev,
4697 struct device_attribute *attr,
4698 char *buf)
4699{
4700 struct sensor_device_attribute *sensor_attr =
4701 to_sensor_dev_attr(attr);
4702 int idx = sensor_attr->index;
4703 s32 value;
4704 int res;
4705
4706 res = thermal_get_sensor(idx, &value);
4707 if (res)
4708 return res;
4709 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4710 return -ENXIO;
4711
4712 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4713}
4714
4715#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004716 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4717 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004718
4719static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4720 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4721 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4722 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4723 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4724 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4725 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4726 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4727 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4728 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4729 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4730 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4731 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4732 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4733 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4734 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4735 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4736};
4737
4738#define THERMAL_ATTRS(X) \
4739 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4740
4741static struct attribute *thermal_temp_input_attr[] = {
4742 THERMAL_ATTRS(8),
4743 THERMAL_ATTRS(9),
4744 THERMAL_ATTRS(10),
4745 THERMAL_ATTRS(11),
4746 THERMAL_ATTRS(12),
4747 THERMAL_ATTRS(13),
4748 THERMAL_ATTRS(14),
4749 THERMAL_ATTRS(15),
4750 THERMAL_ATTRS(0),
4751 THERMAL_ATTRS(1),
4752 THERMAL_ATTRS(2),
4753 THERMAL_ATTRS(3),
4754 THERMAL_ATTRS(4),
4755 THERMAL_ATTRS(5),
4756 THERMAL_ATTRS(6),
4757 THERMAL_ATTRS(7),
4758 NULL
4759};
4760
4761static const struct attribute_group thermal_temp_input16_group = {
4762 .attrs = thermal_temp_input_attr
4763};
4764
4765static const struct attribute_group thermal_temp_input8_group = {
4766 .attrs = &thermal_temp_input_attr[8]
4767};
4768
4769#undef THERMAL_SENSOR_ATTR_TEMP
4770#undef THERMAL_ATTRS
4771
4772/* --------------------------------------------------------------------- */
4773
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004774static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004775{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004776 u8 t, ta1, ta2;
4777 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004778 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004779 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004780
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004781 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4782
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004783 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004784
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004785 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004786 /*
4787 * Direct EC access mode: sensors at registers
4788 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4789 * non-implemented, thermal sensors return 0x80 when
4790 * not available
4791 */
4792
4793 ta1 = ta2 = 0;
4794 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004795 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004796 ta1 |= t;
4797 } else {
4798 ta1 = 0;
4799 break;
4800 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004801 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004802 ta2 |= t;
4803 } else {
4804 ta1 = 0;
4805 break;
4806 }
4807 }
4808 if (ta1 == 0) {
4809 /* This is sheer paranoia, but we handle it anyway */
4810 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004811 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004812 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004813 "falling back to ACPI TMPx access "
4814 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004815 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004816 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004817 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004818 "ThinkPad ACPI EC access misbehaving, "
4819 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004820 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004821 }
4822 } else {
4823 thermal_read_mode =
4824 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004825 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004826 }
4827 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004828 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4829 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004830 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004831 } else {
4832 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004833 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004834 }
4835 } else {
4836 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004837 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004838 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004839
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004840 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4841 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4842 thermal_read_mode);
4843
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004844 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004845 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004846 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004847 &thermal_temp_input16_group);
4848 if (res)
4849 return res;
4850 break;
4851 case TPACPI_THERMAL_TPEC_8:
4852 case TPACPI_THERMAL_ACPI_TMP07:
4853 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004854 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004855 &thermal_temp_input8_group);
4856 if (res)
4857 return res;
4858 break;
4859 case TPACPI_THERMAL_NONE:
4860 default:
4861 return 1;
4862 }
4863
4864 return 0;
4865}
4866
4867static void thermal_exit(void)
4868{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004869 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004870 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004871 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004872 &thermal_temp_input16_group);
4873 break;
4874 case TPACPI_THERMAL_TPEC_8:
4875 case TPACPI_THERMAL_ACPI_TMP07:
4876 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004877 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004878 &thermal_temp_input16_group);
4879 break;
4880 case TPACPI_THERMAL_NONE:
4881 default:
4882 break;
4883 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004884}
4885
4886static int thermal_read(char *p)
4887{
4888 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004889 int n, i;
4890 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004891
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004892 n = thermal_get_sensors(&t);
4893 if (unlikely(n < 0))
4894 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004895
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004896 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004898 if (n > 0) {
4899 for (i = 0; i < (n - 1); i++)
4900 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4901 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4902 } else
4903 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004904
4905 return len;
4906}
4907
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004908static struct ibm_struct thermal_driver_data = {
4909 .name = "thermal",
4910 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004911 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004912};
4913
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004914/*************************************************************************
4915 * EC Dump subdriver
4916 */
4917
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004918static u8 ecdump_regs[256];
4919
4920static int ecdump_read(char *p)
4921{
4922 int len = 0;
4923 int i, j;
4924 u8 v;
4925
4926 len += sprintf(p + len, "EC "
4927 " +00 +01 +02 +03 +04 +05 +06 +07"
4928 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4929 for (i = 0; i < 256; i += 16) {
4930 len += sprintf(p + len, "EC 0x%02x:", i);
4931 for (j = 0; j < 16; j++) {
4932 if (!acpi_ec_read(i + j, &v))
4933 break;
4934 if (v != ecdump_regs[i + j])
4935 len += sprintf(p + len, " *%02x", v);
4936 else
4937 len += sprintf(p + len, " %02x", v);
4938 ecdump_regs[i + j] = v;
4939 }
4940 len += sprintf(p + len, "\n");
4941 if (j != 16)
4942 break;
4943 }
4944
4945 /* These are way too dangerous to advertise openly... */
4946#if 0
4947 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4948 " (<offset> is 00-ff, <value> is 00-ff)\n");
4949 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4950 " (<offset> is 00-ff, <value> is 0-255)\n");
4951#endif
4952 return len;
4953}
4954
4955static int ecdump_write(char *buf)
4956{
4957 char *cmd;
4958 int i, v;
4959
4960 while ((cmd = next_cmd(&buf))) {
4961 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4962 /* i and v set */
4963 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4964 /* i and v set */
4965 } else
4966 return -EINVAL;
4967 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4968 if (!acpi_ec_write(i, v))
4969 return -EIO;
4970 } else
4971 return -EINVAL;
4972 }
4973
4974 return 0;
4975}
4976
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004977static struct ibm_struct ecdump_driver_data = {
4978 .name = "ecdump",
4979 .read = ecdump_read,
4980 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004981 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004982};
4983
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004984/*************************************************************************
4985 * Backlight/brightness subdriver
4986 */
Holger Macht8acb0252006-10-20 14:30:28 -07004987
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004988#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4989
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004990enum {
4991 TP_EC_BACKLIGHT = 0x31,
4992
4993 /* TP_EC_BACKLIGHT bitmasks */
4994 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4995 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4996 TP_EC_BACKLIGHT_MAPSW = 0x20,
4997};
4998
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004999static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005000static int brightness_mode;
5001static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5002
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005003static struct mutex brightness_mutex;
5004
5005/*
5006 * ThinkPads can read brightness from two places: EC 0x31, or
5007 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005008 *
5009 * EC 0x31 has the following layout
5010 * Bit 7: unknown function
5011 * Bit 6: unknown function
5012 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5013 * Bit 4: must be set to zero to avoid problems
5014 * Bit 3-0: backlight brightness level
5015 *
5016 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005017 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005018static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005019{
5020 u8 lec = 0, lcmos = 0, level = 0;
5021
5022 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005023 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005024 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005025 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005026 };
5027 if (brightness_mode & 2) {
5028 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5029 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5030 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5031 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5032 level = lcmos;
5033 }
5034
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005035 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005036 *status = lec; /* Prefer EC, CMOS is just a backing store */
5037 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005038 if (lec == lcmos)
5039 tp_warned.bright_cmos_ec_unsync = 0;
5040 else {
5041 if (!tp_warned.bright_cmos_ec_unsync) {
5042 printk(TPACPI_ERR
5043 "CMOS NVRAM (%u) and EC (%u) do not "
5044 "agree on display brightness level\n",
5045 (unsigned int) lcmos,
5046 (unsigned int) lec);
5047 tp_warned.bright_cmos_ec_unsync = 1;
5048 }
5049 return -EIO;
5050 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005051 } else {
5052 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005053 }
5054
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005055 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005056}
5057
5058/* May return EINTR which can always be mapped to ERESTARTSYS */
5059static int brightness_set(int value)
5060{
5061 int cmos_cmd, inc, i, res;
5062 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005063 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005064
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005065 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5066 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005067 return -EINVAL;
5068
5069 res = mutex_lock_interruptible(&brightness_mutex);
5070 if (res < 0)
5071 return res;
5072
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005073 res = brightness_get_raw(&current_value);
5074 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005075 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005076
5077 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5078 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005079
5080 cmos_cmd = value > current_value ?
5081 TP_CMOS_BRIGHTNESS_UP :
5082 TP_CMOS_BRIGHTNESS_DOWN;
5083 inc = (value > current_value)? 1 : -1;
5084
5085 res = 0;
5086 for (i = current_value; i != value; i += inc) {
5087 if ((brightness_mode & 2) &&
5088 issue_thinkpad_cmos_command(cmos_cmd)) {
5089 res = -EIO;
5090 goto errout;
5091 }
5092 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005093 !acpi_ec_write(TP_EC_BACKLIGHT,
5094 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005095 res = -EIO;
5096 goto errout;;
5097 }
5098 }
5099
5100errout:
5101 mutex_unlock(&brightness_mutex);
5102 return res;
5103}
5104
5105/* sysfs backlight class ----------------------------------------------- */
5106
5107static int brightness_update_status(struct backlight_device *bd)
5108{
5109 /* it is the backlight class's job (caller) to handle
5110 * EINTR and other errors properly */
5111 return brightness_set(
5112 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5113 bd->props.power == FB_BLANK_UNBLANK) ?
5114 bd->props.brightness : 0);
5115}
Holger Macht8acb0252006-10-20 14:30:28 -07005116
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005117static int brightness_get(struct backlight_device *bd)
5118{
5119 int status, res;
5120
5121 res = brightness_get_raw(&status);
5122 if (res < 0)
5123 return 0; /* FIXME: teach backlight about error handling */
5124
5125 return status & TP_EC_BACKLIGHT_LVLMSK;
5126}
5127
Richard Purdie599a52d2007-02-10 23:07:48 +00005128static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005129 .get_brightness = brightness_get,
5130 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005131};
5132
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005133/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005134
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005135static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005136{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005137 int b;
5138
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005139 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5140
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005141 mutex_init(&brightness_mutex);
5142
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005143 /*
5144 * We always attempt to detect acpi support, so as to switch
5145 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5146 * going to publish a backlight interface
5147 */
5148 b = tpacpi_check_std_acpi_brightness_support();
5149 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005150
5151 if (acpi_video_backlight_support()) {
5152 if (brightness_enable > 1) {
5153 printk(TPACPI_NOTICE
5154 "Standard ACPI backlight interface "
5155 "available, not loading native one.\n");
5156 return 1;
5157 } else if (brightness_enable == 1) {
5158 printk(TPACPI_NOTICE
5159 "Backlight control force enabled, even if standard "
5160 "ACPI backlight interface is available\n");
5161 }
5162 } else {
5163 if (brightness_enable > 1) {
5164 printk(TPACPI_NOTICE
5165 "Standard ACPI backlight interface not "
5166 "available, thinkpad_acpi native "
5167 "brightness control enabled\n");
5168 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005169 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005170 }
5171
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005172 if (!brightness_enable) {
5173 dbg_printk(TPACPI_DBG_INIT,
5174 "brightness support disabled by "
5175 "module parameter\n");
5176 return 1;
5177 }
5178
5179 if (b > 16) {
5180 printk(TPACPI_ERR
5181 "Unsupported brightness interface, "
5182 "please contact %s\n", TPACPI_MAIL);
5183 return 1;
5184 }
5185 if (b == 16)
5186 tp_features.bright_16levels = 1;
5187
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005188 if (!brightness_mode) {
5189 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5190 brightness_mode = 2;
5191 else
5192 brightness_mode = 3;
5193
5194 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5195 brightness_mode);
5196 }
5197
5198 if (brightness_mode > 3)
5199 return -EINVAL;
5200
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005201 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005202 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005203
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005204 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005205 printk(TPACPI_INFO
5206 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005207
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005208 ibm_backlight_device = backlight_device_register(
5209 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5210 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005211 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005212 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005213 return PTR_ERR(ibm_backlight_device);
5214 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005215 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005216
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005217 ibm_backlight_device->props.max_brightness =
5218 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005219 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005220 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005221
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005222 return 0;
5223}
5224
5225static void brightness_exit(void)
5226{
5227 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005228 vdbg_printk(TPACPI_DBG_EXIT,
5229 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005230 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005231 }
5232}
5233
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005234static int brightness_read(char *p)
5235{
5236 int len = 0;
5237 int level;
5238
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005239 level = brightness_get(NULL);
5240 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005241 len += sprintf(p + len, "level:\t\tunreadable\n");
5242 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005243 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005244 len += sprintf(p + len, "commands:\tup, down\n");
5245 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005246 " (<level> is 0-%d)\n",
5247 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005248 }
5249
5250 return len;
5251}
5252
5253static int brightness_write(char *buf)
5254{
5255 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005256 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005257 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005258 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005259
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005260 level = brightness_get(NULL);
5261 if (level < 0)
5262 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005263
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005264 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005265 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005266 if (level < max_level)
5267 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005268 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005269 if (level > 0)
5270 level--;
5271 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5272 level >= 0 && level <= max_level) {
5273 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005274 } else
5275 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005276 }
5277
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005278 /*
5279 * Now we know what the final level should be, so we try to set it.
5280 * Doing it this way makes the syscall restartable in case of EINTR
5281 */
5282 rc = brightness_set(level);
5283 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005284}
5285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005286static struct ibm_struct brightness_driver_data = {
5287 .name = "brightness",
5288 .read = brightness_read,
5289 .write = brightness_write,
5290 .exit = brightness_exit,
5291};
5292
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005293/*************************************************************************
5294 * Volume subdriver
5295 */
5296
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005297static int volume_offset = 0x30;
5298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005299static int volume_read(char *p)
5300{
5301 int len = 0;
5302 u8 level;
5303
5304 if (!acpi_ec_read(volume_offset, &level)) {
5305 len += sprintf(p + len, "level:\t\tunreadable\n");
5306 } else {
5307 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5308 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5309 len += sprintf(p + len, "commands:\tup, down, mute\n");
5310 len += sprintf(p + len, "commands:\tlevel <level>"
5311 " (<level> is 0-15)\n");
5312 }
5313
5314 return len;
5315}
5316
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005317static int volume_write(char *buf)
5318{
5319 int cmos_cmd, inc, i;
5320 u8 level, mute;
5321 int new_level, new_mute;
5322 char *cmd;
5323
5324 while ((cmd = next_cmd(&buf))) {
5325 if (!acpi_ec_read(volume_offset, &level))
5326 return -EIO;
5327 new_mute = mute = level & 0x40;
5328 new_level = level = level & 0xf;
5329
5330 if (strlencmp(cmd, "up") == 0) {
5331 if (mute)
5332 new_mute = 0;
5333 else
5334 new_level = level == 15 ? 15 : level + 1;
5335 } else if (strlencmp(cmd, "down") == 0) {
5336 if (mute)
5337 new_mute = 0;
5338 else
5339 new_level = level == 0 ? 0 : level - 1;
5340 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5341 new_level >= 0 && new_level <= 15) {
5342 /* new_level set */
5343 } else if (strlencmp(cmd, "mute") == 0) {
5344 new_mute = 0x40;
5345 } else
5346 return -EINVAL;
5347
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005348 if (new_level != level) {
5349 /* mute doesn't change */
5350
5351 cmos_cmd = (new_level > level) ?
5352 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005353 inc = new_level > level ? 1 : -1;
5354
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005355 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005356 !acpi_ec_write(volume_offset, level)))
5357 return -EIO;
5358
5359 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005360 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 !acpi_ec_write(volume_offset, i + inc))
5362 return -EIO;
5363
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005364 if (mute &&
5365 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5366 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005367 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005368 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369 }
5370
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005371 if (new_mute != mute) {
5372 /* level doesn't change */
5373
5374 cmos_cmd = (new_mute) ?
5375 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005376
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005377 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005378 !acpi_ec_write(volume_offset, level + new_mute))
5379 return -EIO;
5380 }
5381 }
5382
5383 return 0;
5384}
5385
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005386static struct ibm_struct volume_driver_data = {
5387 .name = "volume",
5388 .read = volume_read,
5389 .write = volume_write,
5390};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005391
5392/*************************************************************************
5393 * Fan subdriver
5394 */
5395
5396/*
5397 * FAN ACCESS MODES
5398 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005399 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005400 * ACPI GFAN method: returns fan level
5401 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005402 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005403 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005404 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005405 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005406 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5407 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005408 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5409 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005410 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005411 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005412 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5413 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005414 *
5415 * Fan speed changes of any sort (including those caused by the
5416 * disengaged mode) are usually done slowly by the firmware as the
5417 * maximum ammount of fan duty cycle change per second seems to be
5418 * limited.
5419 *
5420 * Reading is not available if GFAN exists.
5421 * Writing is not available if SFAN exists.
5422 *
5423 * Bits
5424 * 7 automatic mode engaged;
5425 * (default operation mode of the ThinkPad)
5426 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005427 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005428 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005429 * the tachometer while the fan controller ramps up
5430 * the speed (which can take up to a few *minutes*).
5431 * Speeds up fan to 100% duty-cycle, which is far above
5432 * the standard RPM levels. It is not impossible that
5433 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005434 * 5-3 unused in some models. Extra bits for fan level
5435 * in others, but still useless as all values above
5436 * 7 map to the same speed as level 7 in these models.
5437 * 2-0 fan level (0..7 usually)
5438 * 0x00 = stop
5439 * 0x07 = max (set when temperatures critical)
5440 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005441 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005442 *
5443 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5444 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5445 * does so, its initial value is meaningless (0x07).
5446 *
5447 * For firmware bugs, refer to:
5448 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5449 *
5450 * ----
5451 *
5452 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5453 * Main fan tachometer reading (in RPM)
5454 *
5455 * This register is present on all ThinkPads with a new-style EC, and
5456 * it is known not to be present on the A21m/e, and T22, as there is
5457 * something else in offset 0x84 according to the ACPI DSDT. Other
5458 * ThinkPads from this same time period (and earlier) probably lack the
5459 * tachometer as well.
5460 *
5461 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5462 * was never fixed by IBM to report the EC firmware version string
5463 * probably support the tachometer (like the early X models), so
5464 * detecting it is quite hard. We need more data to know for sure.
5465 *
5466 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5467 * might result.
5468 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005469 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5470 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005471 *
5472 * For firmware bugs, refer to:
5473 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5474 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005475 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005476 * ThinkPad X31, X40, X41. Not available in the X60.
5477 *
5478 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5479 * high speed. ACPI DSDT seems to map these three speeds to levels
5480 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5481 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5482 *
5483 * The speeds are stored on handles
5484 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5485 *
5486 * There are three default speed sets, acessible as handles:
5487 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5488 *
5489 * ACPI DSDT switches which set is in use depending on various
5490 * factors.
5491 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005492 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005493 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5494 * but the ACPI tables just mention level 7.
5495 */
5496
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005497enum { /* Fan control constants */
5498 fan_status_offset = 0x2f, /* EC register 0x2f */
5499 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5500 * 0x84 must be read before 0x85 */
5501
5502 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5503 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5504
5505 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5506};
5507
5508enum fan_status_access_mode {
5509 TPACPI_FAN_NONE = 0, /* No fan status or control */
5510 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5511 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5512};
5513
5514enum fan_control_access_mode {
5515 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5516 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5517 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5518 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5519};
5520
5521enum fan_control_commands {
5522 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5523 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5524 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5525 * and also watchdog cmd */
5526};
5527
5528static int fan_control_allowed;
5529
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005530static enum fan_status_access_mode fan_status_access_mode;
5531static enum fan_control_access_mode fan_control_access_mode;
5532static enum fan_control_commands fan_control_commands;
5533
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005534static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005535static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005536static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005537static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005538
5539static struct mutex fan_mutex;
5540
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005541static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005542static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005543
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005544TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5545TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005546 "\\FSPD", /* 600e/x, 770e, 770x */
5547 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005548TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005549 "JFNS", /* 770x-JL */
5550 ); /* all others */
5551
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005552/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005553 * Call with fan_mutex held
5554 */
5555static void fan_update_desired_level(u8 status)
5556{
5557 if ((status &
5558 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5559 if (status > 7)
5560 fan_control_desired_level = 7;
5561 else
5562 fan_control_desired_level = status;
5563 }
5564}
5565
5566static int fan_get_status(u8 *status)
5567{
5568 u8 s;
5569
5570 /* TODO:
5571 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5572
5573 switch (fan_status_access_mode) {
5574 case TPACPI_FAN_RD_ACPI_GFAN:
5575 /* 570, 600e/x, 770e, 770x */
5576
5577 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5578 return -EIO;
5579
5580 if (likely(status))
5581 *status = s & 0x07;
5582
5583 break;
5584
5585 case TPACPI_FAN_RD_TPEC:
5586 /* all except 570, 600e/x, 770e, 770x */
5587 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5588 return -EIO;
5589
5590 if (likely(status))
5591 *status = s;
5592
5593 break;
5594
5595 default:
5596 return -ENXIO;
5597 }
5598
5599 return 0;
5600}
5601
5602static int fan_get_status_safe(u8 *status)
5603{
5604 int rc;
5605 u8 s;
5606
5607 if (mutex_lock_interruptible(&fan_mutex))
5608 return -ERESTARTSYS;
5609 rc = fan_get_status(&s);
5610 if (!rc)
5611 fan_update_desired_level(s);
5612 mutex_unlock(&fan_mutex);
5613
5614 if (status)
5615 *status = s;
5616
5617 return rc;
5618}
5619
5620static int fan_get_speed(unsigned int *speed)
5621{
5622 u8 hi, lo;
5623
5624 switch (fan_status_access_mode) {
5625 case TPACPI_FAN_RD_TPEC:
5626 /* all except 570, 600e/x, 770e, 770x */
5627 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5628 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5629 return -EIO;
5630
5631 if (likely(speed))
5632 *speed = (hi << 8) | lo;
5633
5634 break;
5635
5636 default:
5637 return -ENXIO;
5638 }
5639
5640 return 0;
5641}
5642
5643static int fan_set_level(int level)
5644{
5645 if (!fan_control_allowed)
5646 return -EPERM;
5647
5648 switch (fan_control_access_mode) {
5649 case TPACPI_FAN_WR_ACPI_SFAN:
5650 if (level >= 0 && level <= 7) {
5651 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5652 return -EIO;
5653 } else
5654 return -EINVAL;
5655 break;
5656
5657 case TPACPI_FAN_WR_ACPI_FANS:
5658 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005659 if (!(level & TP_EC_FAN_AUTO) &&
5660 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005661 ((level < 0) || (level > 7)))
5662 return -EINVAL;
5663
5664 /* safety net should the EC not support AUTO
5665 * or FULLSPEED mode bits and just ignore them */
5666 if (level & TP_EC_FAN_FULLSPEED)
5667 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005668 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005669 level |= 4; /* safety min speed 4 */
5670
5671 if (!acpi_ec_write(fan_status_offset, level))
5672 return -EIO;
5673 else
5674 tp_features.fan_ctrl_status_undef = 0;
5675 break;
5676
5677 default:
5678 return -ENXIO;
5679 }
5680 return 0;
5681}
5682
5683static int fan_set_level_safe(int level)
5684{
5685 int rc;
5686
5687 if (!fan_control_allowed)
5688 return -EPERM;
5689
5690 if (mutex_lock_interruptible(&fan_mutex))
5691 return -ERESTARTSYS;
5692
5693 if (level == TPACPI_FAN_LAST_LEVEL)
5694 level = fan_control_desired_level;
5695
5696 rc = fan_set_level(level);
5697 if (!rc)
5698 fan_update_desired_level(level);
5699
5700 mutex_unlock(&fan_mutex);
5701 return rc;
5702}
5703
5704static int fan_set_enable(void)
5705{
5706 u8 s;
5707 int rc;
5708
5709 if (!fan_control_allowed)
5710 return -EPERM;
5711
5712 if (mutex_lock_interruptible(&fan_mutex))
5713 return -ERESTARTSYS;
5714
5715 switch (fan_control_access_mode) {
5716 case TPACPI_FAN_WR_ACPI_FANS:
5717 case TPACPI_FAN_WR_TPEC:
5718 rc = fan_get_status(&s);
5719 if (rc < 0)
5720 break;
5721
5722 /* Don't go out of emergency fan mode */
5723 if (s != 7) {
5724 s &= 0x07;
5725 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5726 }
5727
5728 if (!acpi_ec_write(fan_status_offset, s))
5729 rc = -EIO;
5730 else {
5731 tp_features.fan_ctrl_status_undef = 0;
5732 rc = 0;
5733 }
5734 break;
5735
5736 case TPACPI_FAN_WR_ACPI_SFAN:
5737 rc = fan_get_status(&s);
5738 if (rc < 0)
5739 break;
5740
5741 s &= 0x07;
5742
5743 /* Set fan to at least level 4 */
5744 s |= 4;
5745
5746 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005747 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005748 else
5749 rc = 0;
5750 break;
5751
5752 default:
5753 rc = -ENXIO;
5754 }
5755
5756 mutex_unlock(&fan_mutex);
5757 return rc;
5758}
5759
5760static int fan_set_disable(void)
5761{
5762 int rc;
5763
5764 if (!fan_control_allowed)
5765 return -EPERM;
5766
5767 if (mutex_lock_interruptible(&fan_mutex))
5768 return -ERESTARTSYS;
5769
5770 rc = 0;
5771 switch (fan_control_access_mode) {
5772 case TPACPI_FAN_WR_ACPI_FANS:
5773 case TPACPI_FAN_WR_TPEC:
5774 if (!acpi_ec_write(fan_status_offset, 0x00))
5775 rc = -EIO;
5776 else {
5777 fan_control_desired_level = 0;
5778 tp_features.fan_ctrl_status_undef = 0;
5779 }
5780 break;
5781
5782 case TPACPI_FAN_WR_ACPI_SFAN:
5783 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5784 rc = -EIO;
5785 else
5786 fan_control_desired_level = 0;
5787 break;
5788
5789 default:
5790 rc = -ENXIO;
5791 }
5792
5793
5794 mutex_unlock(&fan_mutex);
5795 return rc;
5796}
5797
5798static int fan_set_speed(int speed)
5799{
5800 int rc;
5801
5802 if (!fan_control_allowed)
5803 return -EPERM;
5804
5805 if (mutex_lock_interruptible(&fan_mutex))
5806 return -ERESTARTSYS;
5807
5808 rc = 0;
5809 switch (fan_control_access_mode) {
5810 case TPACPI_FAN_WR_ACPI_FANS:
5811 if (speed >= 0 && speed <= 65535) {
5812 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5813 speed, speed, speed))
5814 rc = -EIO;
5815 } else
5816 rc = -EINVAL;
5817 break;
5818
5819 default:
5820 rc = -ENXIO;
5821 }
5822
5823 mutex_unlock(&fan_mutex);
5824 return rc;
5825}
5826
5827static void fan_watchdog_reset(void)
5828{
5829 static int fan_watchdog_active;
5830
5831 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5832 return;
5833
5834 if (fan_watchdog_active)
5835 cancel_delayed_work(&fan_watchdog_task);
5836
5837 if (fan_watchdog_maxinterval > 0 &&
5838 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5839 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005840 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005841 msecs_to_jiffies(fan_watchdog_maxinterval
5842 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005843 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005844 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005845 "watchdog will not trigger\n");
5846 }
5847 } else
5848 fan_watchdog_active = 0;
5849}
5850
5851static void fan_watchdog_fire(struct work_struct *ignored)
5852{
5853 int rc;
5854
5855 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5856 return;
5857
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005858 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005859 rc = fan_set_enable();
5860 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005861 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005862 "will try again later...\n", -rc);
5863 /* reschedule for later */
5864 fan_watchdog_reset();
5865 }
5866}
5867
5868/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005869 * SYSFS fan layout: hwmon compatible (device)
5870 *
5871 * pwm*_enable:
5872 * 0: "disengaged" mode
5873 * 1: manual mode
5874 * 2: native EC "auto" mode (recommended, hardware default)
5875 *
5876 * pwm*: set speed in manual mode, ignored otherwise.
5877 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5878 * interpolation.
5879 *
5880 * fan*_input: tachometer reading, RPM
5881 *
5882 *
5883 * SYSFS fan layout: extensions
5884 *
5885 * fan_watchdog (driver):
5886 * fan watchdog interval in seconds, 0 disables (default), max 120
5887 */
5888
5889/* sysfs fan pwm1_enable ----------------------------------------------- */
5890static ssize_t fan_pwm1_enable_show(struct device *dev,
5891 struct device_attribute *attr,
5892 char *buf)
5893{
5894 int res, mode;
5895 u8 status;
5896
5897 res = fan_get_status_safe(&status);
5898 if (res)
5899 return res;
5900
5901 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5902 if (status != fan_control_initial_status) {
5903 tp_features.fan_ctrl_status_undef = 0;
5904 } else {
5905 /* Return most likely status. In fact, it
5906 * might be the only possible status */
5907 status = TP_EC_FAN_AUTO;
5908 }
5909 }
5910
5911 if (status & TP_EC_FAN_FULLSPEED) {
5912 mode = 0;
5913 } else if (status & TP_EC_FAN_AUTO) {
5914 mode = 2;
5915 } else
5916 mode = 1;
5917
5918 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5919}
5920
5921static ssize_t fan_pwm1_enable_store(struct device *dev,
5922 struct device_attribute *attr,
5923 const char *buf, size_t count)
5924{
5925 unsigned long t;
5926 int res, level;
5927
5928 if (parse_strtoul(buf, 2, &t))
5929 return -EINVAL;
5930
5931 switch (t) {
5932 case 0:
5933 level = TP_EC_FAN_FULLSPEED;
5934 break;
5935 case 1:
5936 level = TPACPI_FAN_LAST_LEVEL;
5937 break;
5938 case 2:
5939 level = TP_EC_FAN_AUTO;
5940 break;
5941 case 3:
5942 /* reserved for software-controlled auto mode */
5943 return -ENOSYS;
5944 default:
5945 return -EINVAL;
5946 }
5947
5948 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005949 if (res == -ENXIO)
5950 return -EINVAL;
5951 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005952 return res;
5953
5954 fan_watchdog_reset();
5955
5956 return count;
5957}
5958
5959static struct device_attribute dev_attr_fan_pwm1_enable =
5960 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5961 fan_pwm1_enable_show, fan_pwm1_enable_store);
5962
5963/* sysfs fan pwm1 ------------------------------------------------------ */
5964static ssize_t fan_pwm1_show(struct device *dev,
5965 struct device_attribute *attr,
5966 char *buf)
5967{
5968 int res;
5969 u8 status;
5970
5971 res = fan_get_status_safe(&status);
5972 if (res)
5973 return res;
5974
5975 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5976 if (status != fan_control_initial_status) {
5977 tp_features.fan_ctrl_status_undef = 0;
5978 } else {
5979 status = TP_EC_FAN_AUTO;
5980 }
5981 }
5982
5983 if ((status &
5984 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5985 status = fan_control_desired_level;
5986
5987 if (status > 7)
5988 status = 7;
5989
5990 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5991}
5992
5993static ssize_t fan_pwm1_store(struct device *dev,
5994 struct device_attribute *attr,
5995 const char *buf, size_t count)
5996{
5997 unsigned long s;
5998 int rc;
5999 u8 status, newlevel;
6000
6001 if (parse_strtoul(buf, 255, &s))
6002 return -EINVAL;
6003
6004 /* scale down from 0-255 to 0-7 */
6005 newlevel = (s >> 5) & 0x07;
6006
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006007 if (mutex_lock_interruptible(&fan_mutex))
6008 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006009
6010 rc = fan_get_status(&status);
6011 if (!rc && (status &
6012 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6013 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006014 if (rc == -ENXIO)
6015 rc = -EINVAL;
6016 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006017 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006018 fan_watchdog_reset();
6019 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006020 }
6021
6022 mutex_unlock(&fan_mutex);
6023 return (rc)? rc : count;
6024}
6025
6026static struct device_attribute dev_attr_fan_pwm1 =
6027 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6028 fan_pwm1_show, fan_pwm1_store);
6029
6030/* sysfs fan fan1_input ------------------------------------------------ */
6031static ssize_t fan_fan1_input_show(struct device *dev,
6032 struct device_attribute *attr,
6033 char *buf)
6034{
6035 int res;
6036 unsigned int speed;
6037
6038 res = fan_get_speed(&speed);
6039 if (res < 0)
6040 return res;
6041
6042 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6043}
6044
6045static struct device_attribute dev_attr_fan_fan1_input =
6046 __ATTR(fan1_input, S_IRUGO,
6047 fan_fan1_input_show, NULL);
6048
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006049/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006050static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6051 char *buf)
6052{
6053 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6054}
6055
6056static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6057 const char *buf, size_t count)
6058{
6059 unsigned long t;
6060
6061 if (parse_strtoul(buf, 120, &t))
6062 return -EINVAL;
6063
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006064 if (!fan_control_allowed)
6065 return -EPERM;
6066
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006067 fan_watchdog_maxinterval = t;
6068 fan_watchdog_reset();
6069
6070 return count;
6071}
6072
6073static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6074 fan_fan_watchdog_show, fan_fan_watchdog_store);
6075
6076/* --------------------------------------------------------------------- */
6077static struct attribute *fan_attributes[] = {
6078 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6079 &dev_attr_fan_fan1_input.attr,
6080 NULL
6081};
6082
6083static const struct attribute_group fan_attr_group = {
6084 .attrs = fan_attributes,
6085};
6086
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006087static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006088{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006089 int rc;
6090
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006091 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6092
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006093 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006094 fan_status_access_mode = TPACPI_FAN_NONE;
6095 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006096 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006097 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006098 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006099 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006100
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006101 TPACPI_ACPIHANDLE_INIT(fans);
6102 TPACPI_ACPIHANDLE_INIT(gfan);
6103 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006104
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006105 if (gfan_handle) {
6106 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006107 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006108 } else {
6109 /* all other ThinkPads: note that even old-style
6110 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006111 if (likely(acpi_ec_read(fan_status_offset,
6112 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006113 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006114
6115 /* In some ThinkPads, neither the EC nor the ACPI
6116 * DSDT initialize the fan status, and it ends up
6117 * being set to 0x07 when it *could* be either
6118 * 0x07 or 0x80.
6119 *
6120 * Enable for TP-1Y (T43), TP-78 (R51e),
6121 * TP-76 (R52), TP-70 (T43, R52), which are known
6122 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006123 if (fan_control_initial_status == 0x07) {
6124 switch (thinkpad_id.ec_model) {
6125 case 0x5931: /* TP-1Y */
6126 case 0x3837: /* TP-78 */
6127 case 0x3637: /* TP-76 */
6128 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006129 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006130 "fan_init: initial fan status "
6131 "is unknown, assuming it is "
6132 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006133 tp_features.fan_ctrl_status_undef = 1;
6134 ;;
6135 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006136 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006137 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006138 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006139 "ThinkPad ACPI EC access misbehaving, "
6140 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006141 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006142 }
6143 }
6144
6145 if (sfan_handle) {
6146 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006147 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006148 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006149 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006150 } else {
6151 if (!gfan_handle) {
6152 /* gfan without sfan means no fan control */
6153 /* all other models implement TP EC 0x2f control */
6154
6155 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006156 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006157 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006158 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006159 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006160 TPACPI_FAN_CMD_SPEED |
6161 TPACPI_FAN_CMD_LEVEL |
6162 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006163 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006164 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006165 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006166 TPACPI_FAN_CMD_LEVEL |
6167 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006168 }
6169 }
6170 }
6171
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006172 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6173 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6174 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6175 fan_status_access_mode, fan_control_access_mode);
6176
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006177 /* fan control master switch */
6178 if (!fan_control_allowed) {
6179 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6180 fan_control_commands = 0;
6181 dbg_printk(TPACPI_DBG_INIT,
6182 "fan control features disabled by parameter\n");
6183 }
6184
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006185 /* update fan_control_desired_level */
6186 if (fan_status_access_mode != TPACPI_FAN_NONE)
6187 fan_get_status_safe(NULL);
6188
6189 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6190 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006191 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006192 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006193 if (rc < 0)
6194 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006195
6196 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6197 &driver_attr_fan_watchdog);
6198 if (rc < 0) {
6199 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6200 &fan_attr_group);
6201 return rc;
6202 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006203 return 0;
6204 } else
6205 return 1;
6206}
6207
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006208static void fan_exit(void)
6209{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006210 vdbg_printk(TPACPI_DBG_EXIT,
6211 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006212
6213 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006214 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006215 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6216 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006217
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006218 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006219 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006220}
6221
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006222static void fan_suspend(pm_message_t state)
6223{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006224 int rc;
6225
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006226 if (!fan_control_allowed)
6227 return;
6228
6229 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006230 fan_control_resume_level = 0;
6231 rc = fan_get_status_safe(&fan_control_resume_level);
6232 if (rc < 0)
6233 printk(TPACPI_NOTICE
6234 "failed to read fan level for later "
6235 "restore during resume: %d\n", rc);
6236
6237 /* if it is undefined, don't attempt to restore it.
6238 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006239 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006240 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006241}
6242
6243static void fan_resume(void)
6244{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006245 u8 current_level = 7;
6246 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006247 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006248
6249 /* DSDT *always* updates status on resume */
6250 tp_features.fan_ctrl_status_undef = 0;
6251
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006252 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006253 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006254 (fan_get_status_safe(&current_level) < 0))
6255 return;
6256
6257 switch (fan_control_access_mode) {
6258 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006259 /* never decrease fan level */
6260 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006261 break;
6262 case TPACPI_FAN_WR_ACPI_FANS:
6263 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006264 /* never decrease fan level, scale is:
6265 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6266 *
6267 * We expect the firmware to set either 7 or AUTO, but we
6268 * handle FULLSPEED out of paranoia.
6269 *
6270 * So, we can safely only restore FULLSPEED or 7, anything
6271 * else could slow the fan. Restoring AUTO is useless, at
6272 * best that's exactly what the DSDT already set (it is the
6273 * slower it uses).
6274 *
6275 * Always keep in mind that the DSDT *will* have set the
6276 * fans to what the vendor supposes is the best level. We
6277 * muck with it only to speed the fan up.
6278 */
6279 if (fan_control_resume_level != 7 &&
6280 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6281 return;
6282 else
6283 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6284 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006285 break;
6286 default:
6287 return;
6288 }
6289 if (do_set) {
6290 printk(TPACPI_NOTICE
6291 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006292 fan_control_resume_level);
6293 rc = fan_set_level_safe(fan_control_resume_level);
6294 if (rc < 0)
6295 printk(TPACPI_NOTICE
6296 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006297 }
6298}
6299
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006300static int fan_read(char *p)
6301{
6302 int len = 0;
6303 int rc;
6304 u8 status;
6305 unsigned int speed = 0;
6306
6307 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006308 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006309 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006310 rc = fan_get_status_safe(&status);
6311 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006312 return rc;
6313
6314 len += sprintf(p + len, "status:\t\t%s\n"
6315 "level:\t\t%d\n",
6316 (status != 0) ? "enabled" : "disabled", status);
6317 break;
6318
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006319 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006320 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006321 rc = fan_get_status_safe(&status);
6322 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006323 return rc;
6324
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006325 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006326 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006327 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006328 else
6329 /* Return most likely status. In fact, it
6330 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006331 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006332 }
6333
6334 len += sprintf(p + len, "status:\t\t%s\n",
6335 (status != 0) ? "enabled" : "disabled");
6336
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006337 rc = fan_get_speed(&speed);
6338 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339 return rc;
6340
6341 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6342
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006343 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006344 /* Disengaged mode takes precedence */
6345 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006346 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006347 len += sprintf(p + len, "level:\t\tauto\n");
6348 else
6349 len += sprintf(p + len, "level:\t\t%d\n", status);
6350 break;
6351
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006352 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006353 default:
6354 len += sprintf(p + len, "status:\t\tnot supported\n");
6355 }
6356
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006357 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006358 len += sprintf(p + len, "commands:\tlevel <level>");
6359
6360 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006361 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006362 len += sprintf(p + len, " (<level> is 0-7)\n");
6363 break;
6364
6365 default:
6366 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006367 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006368 break;
6369 }
6370 }
6371
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006372 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006373 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006374 "commands:\twatchdog <timeout> (<timeout> "
6375 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006376
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006377 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006378 len += sprintf(p + len, "commands:\tspeed <speed>"
6379 " (<speed> is 0-65535)\n");
6380
6381 return len;
6382}
6383
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006384static int fan_write_cmd_level(const char *cmd, int *rc)
6385{
6386 int level;
6387
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006388 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006389 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006390 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006391 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006392 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006393 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006394 return 0;
6395
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006396 *rc = fan_set_level_safe(level);
6397 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006398 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006399 "access mode %d", fan_control_access_mode);
6400
6401 return 1;
6402}
6403
6404static int fan_write_cmd_enable(const char *cmd, int *rc)
6405{
6406 if (strlencmp(cmd, "enable") != 0)
6407 return 0;
6408
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006409 *rc = fan_set_enable();
6410 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006411 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006412 "access mode %d", fan_control_access_mode);
6413
6414 return 1;
6415}
6416
6417static int fan_write_cmd_disable(const char *cmd, int *rc)
6418{
6419 if (strlencmp(cmd, "disable") != 0)
6420 return 0;
6421
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006422 *rc = fan_set_disable();
6423 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006424 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006425 "access mode %d", fan_control_access_mode);
6426
6427 return 1;
6428}
6429
6430static int fan_write_cmd_speed(const char *cmd, int *rc)
6431{
6432 int speed;
6433
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006434 /* TODO:
6435 * Support speed <low> <medium> <high> ? */
6436
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006437 if (sscanf(cmd, "speed %d", &speed) != 1)
6438 return 0;
6439
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006440 *rc = fan_set_speed(speed);
6441 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006442 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006443 "access mode %d", fan_control_access_mode);
6444
6445 return 1;
6446}
6447
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006448static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6449{
6450 int interval;
6451
6452 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6453 return 0;
6454
6455 if (interval < 0 || interval > 120)
6456 *rc = -EINVAL;
6457 else
6458 fan_watchdog_maxinterval = interval;
6459
6460 return 1;
6461}
6462
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006463static int fan_write(char *buf)
6464{
6465 char *cmd;
6466 int rc = 0;
6467
6468 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006469 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006470 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006471 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006472 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006473 fan_write_cmd_disable(cmd, &rc) ||
6474 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006475 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006476 fan_write_cmd_speed(cmd, &rc))
6477 )
6478 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006479 else if (!rc)
6480 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006481 }
6482
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006483 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006484}
6485
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006486static struct ibm_struct fan_driver_data = {
6487 .name = "fan",
6488 .read = fan_read,
6489 .write = fan_write,
6490 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006491 .suspend = fan_suspend,
6492 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006493};
6494
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006495/****************************************************************************
6496 ****************************************************************************
6497 *
6498 * Infrastructure
6499 *
6500 ****************************************************************************
6501 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006503/* sysfs name ---------------------------------------------------------- */
6504static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6505 struct device_attribute *attr,
6506 char *buf)
6507{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006508 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006509}
6510
6511static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6512 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6513
6514/* --------------------------------------------------------------------- */
6515
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006516/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006517static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006519/*
6520 * Module and infrastructure proble, init and exit handling
6521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006523static int force_load;
6524
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006525#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006526static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006527{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006528 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006529
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006530 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006531}
6532#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6533
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006534static void ibm_exit(struct ibm_struct *ibm)
6535{
6536 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6537
6538 list_del_init(&ibm->all_drivers);
6539
6540 if (ibm->flags.acpi_notify_installed) {
6541 dbg_printk(TPACPI_DBG_EXIT,
6542 "%s: acpi_remove_notify_handler\n", ibm->name);
6543 BUG_ON(!ibm->acpi);
6544 acpi_remove_notify_handler(*ibm->acpi->handle,
6545 ibm->acpi->type,
6546 dispatch_acpi_notify);
6547 ibm->flags.acpi_notify_installed = 0;
6548 ibm->flags.acpi_notify_installed = 0;
6549 }
6550
6551 if (ibm->flags.proc_created) {
6552 dbg_printk(TPACPI_DBG_EXIT,
6553 "%s: remove_proc_entry\n", ibm->name);
6554 remove_proc_entry(ibm->name, proc_dir);
6555 ibm->flags.proc_created = 0;
6556 }
6557
6558 if (ibm->flags.acpi_driver_registered) {
6559 dbg_printk(TPACPI_DBG_EXIT,
6560 "%s: acpi_bus_unregister_driver\n", ibm->name);
6561 BUG_ON(!ibm->acpi);
6562 acpi_bus_unregister_driver(ibm->acpi->driver);
6563 kfree(ibm->acpi->driver);
6564 ibm->acpi->driver = NULL;
6565 ibm->flags.acpi_driver_registered = 0;
6566 }
6567
6568 if (ibm->flags.init_called && ibm->exit) {
6569 ibm->exit();
6570 ibm->flags.init_called = 0;
6571 }
6572
6573 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6574}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006575
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006576static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577{
6578 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006579 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 struct proc_dir_entry *entry;
6581
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006582 BUG_ON(ibm == NULL);
6583
6584 INIT_LIST_HEAD(&ibm->all_drivers);
6585
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006586 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 return 0;
6588
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006589 dbg_printk(TPACPI_DBG_INIT,
6590 "probing for %s\n", ibm->name);
6591
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006592 if (iibm->init) {
6593 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006594 if (ret > 0)
6595 return 0; /* probe failed */
6596 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006598
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006599 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 }
6601
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006602 if (ibm->acpi) {
6603 if (ibm->acpi->hid) {
6604 ret = register_tpacpi_subdriver(ibm);
6605 if (ret)
6606 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006607 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006608
6609 if (ibm->acpi->notify) {
6610 ret = setup_acpi_notify(ibm);
6611 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006612 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006613 ibm->name);
6614 ret = 0;
6615 goto err_out;
6616 }
6617 if (ret < 0)
6618 goto err_out;
6619 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006620 }
6621
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006622 dbg_printk(TPACPI_DBG_INIT,
6623 "%s installed\n", ibm->name);
6624
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006625 if (ibm->read) {
6626 entry = create_proc_entry(ibm->name,
6627 S_IFREG | S_IRUGO | S_IWUSR,
6628 proc_dir);
6629 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006630 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006631 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006632 ret = -ENODEV;
6633 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006634 }
6635 entry->owner = THIS_MODULE;
6636 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006637 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006638 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006639 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006640 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006643 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6644
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006646
6647err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006648 dbg_printk(TPACPI_DBG_INIT,
6649 "%s: at error exit path with result %d\n",
6650 ibm->name, ret);
6651
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006652 ibm_exit(ibm);
6653 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654}
6655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006656/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006658/* returns 0 - probe ok, or < 0 - probe error.
6659 * Probe ok doesn't mean thinkpad found.
6660 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6661static int __must_check __init get_thinkpad_model_data(
6662 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006663{
Jeff Garzik18552562007-10-03 15:15:40 -04006664 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006665 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006666 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006667
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006668 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006669 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006670
6671 memset(tp, 0, sizeof(*tp));
6672
6673 if (dmi_name_in_vendors("IBM"))
6674 tp->vendor = PCI_VENDOR_ID_IBM;
6675 else if (dmi_name_in_vendors("LENOVO"))
6676 tp->vendor = PCI_VENDOR_ID_LENOVO;
6677 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006678 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006679
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006680 s = dmi_get_system_info(DMI_BIOS_VERSION);
6681 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6682 if (s && !tp->bios_version_str)
6683 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006684 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006685 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006686 tp->bios_model = tp->bios_version_str[0]
6687 | (tp->bios_version_str[1] << 8);
6688
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006689 /*
6690 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6691 * X32 or newer, all Z series; Some models must have an
6692 * up-to-date BIOS or they will not be detected.
6693 *
6694 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6695 */
6696 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006697 if (sscanf(dev->name,
6698 "IBM ThinkPad Embedded Controller -[%17c",
6699 ec_fw_string) == 1) {
6700 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6701 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006702
6703 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006704 if (!tp->ec_version_str)
6705 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006706 tp->ec_model = ec_fw_string[0]
6707 | (ec_fw_string[1] << 8);
6708 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006709 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006710 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006711
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006712 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6713 if (s && !strnicmp(s, "ThinkPad", 8)) {
6714 tp->model_str = kstrdup(s, GFP_KERNEL);
6715 if (!tp->model_str)
6716 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006717 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006718
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006719 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6720 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6721 if (s && !tp->nummodel_str)
6722 return -ENOMEM;
6723
6724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006727static int __init probe_for_thinkpad(void)
6728{
6729 int is_thinkpad;
6730
6731 if (acpi_disabled)
6732 return -ENODEV;
6733
6734 /*
6735 * Non-ancient models have better DMI tagging, but very old models
6736 * don't.
6737 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006738 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006739
6740 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006741 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006742 if (!ec_handle) {
6743 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006744 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006745 "Not yet supported ThinkPad detected!\n");
6746 return -ENODEV;
6747 }
6748
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006749 /*
6750 * Risks a regression on very old machines, but reduces potential
6751 * false positives a damn great deal
6752 */
6753 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006754 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006755
6756 if (!is_thinkpad && !force_load)
6757 return -ENODEV;
6758
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006759 return 0;
6760}
6761
6762
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006763/* Module init, exit, parameters */
6764
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006765static struct ibm_init_struct ibms_init[] __initdata = {
6766 {
6767 .init = thinkpad_acpi_driver_init,
6768 .data = &thinkpad_acpi_driver_data,
6769 },
6770 {
6771 .init = hotkey_init,
6772 .data = &hotkey_driver_data,
6773 },
6774 {
6775 .init = bluetooth_init,
6776 .data = &bluetooth_driver_data,
6777 },
6778 {
6779 .init = wan_init,
6780 .data = &wan_driver_data,
6781 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006782#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006783 {
6784 .init = video_init,
6785 .data = &video_driver_data,
6786 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006787#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006788 {
6789 .init = light_init,
6790 .data = &light_driver_data,
6791 },
6792#ifdef CONFIG_THINKPAD_ACPI_DOCK
6793 {
6794 .init = dock_init,
6795 .data = &dock_driver_data[0],
6796 },
6797 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006798 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006799 .data = &dock_driver_data[1],
6800 },
6801#endif
6802#ifdef CONFIG_THINKPAD_ACPI_BAY
6803 {
6804 .init = bay_init,
6805 .data = &bay_driver_data,
6806 },
6807#endif
6808 {
6809 .init = cmos_init,
6810 .data = &cmos_driver_data,
6811 },
6812 {
6813 .init = led_init,
6814 .data = &led_driver_data,
6815 },
6816 {
6817 .init = beep_init,
6818 .data = &beep_driver_data,
6819 },
6820 {
6821 .init = thermal_init,
6822 .data = &thermal_driver_data,
6823 },
6824 {
6825 .data = &ecdump_driver_data,
6826 },
6827 {
6828 .init = brightness_init,
6829 .data = &brightness_driver_data,
6830 },
6831 {
6832 .data = &volume_driver_data,
6833 },
6834 {
6835 .init = fan_init,
6836 .data = &fan_driver_data,
6837 },
6838};
6839
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006840static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6841{
6842 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006843 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006844
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006845 if (!kp || !kp->name || !val)
6846 return -EINVAL;
6847
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006848 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6849 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006850 WARN_ON(ibm == NULL);
6851
6852 if (!ibm || !ibm->name)
6853 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006854
6855 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6856 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006857 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006858 strcpy(ibms_init[i].param, val);
6859 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006860 return 0;
6861 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006862 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006863
6864 return -EINVAL;
6865}
6866
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006867module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006868MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006869 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006870
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006871module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006872MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006873
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006874module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006875MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006876 "Attempts to load the driver even on a "
6877 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006878
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006879module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006880MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006881 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006882
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006883module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006884MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006885 "Selects brightness control strategy: "
6886 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006887
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006888module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006889MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006890 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006891
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006892module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006893MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006894 "used for backwards compatibility with userspace, "
6895 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006896
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006897#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006898 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006899 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006900 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006901
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006902TPACPI_PARAM(hotkey);
6903TPACPI_PARAM(bluetooth);
6904TPACPI_PARAM(video);
6905TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006906#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006907TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006908#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006909#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006910TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006911#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006912TPACPI_PARAM(cmos);
6913TPACPI_PARAM(led);
6914TPACPI_PARAM(beep);
6915TPACPI_PARAM(ecdump);
6916TPACPI_PARAM(brightness);
6917TPACPI_PARAM(volume);
6918TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006919
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02006920#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
6921module_param(dbg_wlswemul, uint, 0);
6922MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
6923module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
6924MODULE_PARM_DESC(wlsw_state,
6925 "Initial state of the emulated WLSW switch");
6926
6927module_param(dbg_bluetoothemul, uint, 0);
6928MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
6929module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
6930MODULE_PARM_DESC(bluetooth_state,
6931 "Initial state of the emulated bluetooth switch");
6932
6933module_param(dbg_wwanemul, uint, 0);
6934MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
6935module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
6936MODULE_PARM_DESC(wwan_state,
6937 "Initial state of the emulated WWAN switch");
6938#endif
6939
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006940static void thinkpad_acpi_module_exit(void)
6941{
6942 struct ibm_struct *ibm, *itmp;
6943
6944 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6945
6946 list_for_each_entry_safe_reverse(ibm, itmp,
6947 &tpacpi_all_drivers,
6948 all_drivers) {
6949 ibm_exit(ibm);
6950 }
6951
6952 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6953
6954 if (tpacpi_inputdev) {
6955 if (tp_features.input_device_registered)
6956 input_unregister_device(tpacpi_inputdev);
6957 else
6958 input_free_device(tpacpi_inputdev);
6959 }
6960
6961 if (tpacpi_hwmon)
6962 hwmon_device_unregister(tpacpi_hwmon);
6963
6964 if (tp_features.sensors_pdev_attrs_registered)
6965 device_remove_file(&tpacpi_sensors_pdev->dev,
6966 &dev_attr_thinkpad_acpi_pdev_name);
6967 if (tpacpi_sensors_pdev)
6968 platform_device_unregister(tpacpi_sensors_pdev);
6969 if (tpacpi_pdev)
6970 platform_device_unregister(tpacpi_pdev);
6971
6972 if (tp_features.sensors_pdrv_attrs_registered)
6973 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6974 if (tp_features.platform_drv_attrs_registered)
6975 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6976
6977 if (tp_features.sensors_pdrv_registered)
6978 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6979
6980 if (tp_features.platform_drv_registered)
6981 platform_driver_unregister(&tpacpi_pdriver);
6982
6983 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006984 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006985
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006986 if (tpacpi_wq)
6987 destroy_workqueue(tpacpi_wq);
6988
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006989 kfree(thinkpad_id.bios_version_str);
6990 kfree(thinkpad_id.ec_version_str);
6991 kfree(thinkpad_id.model_str);
6992}
6993
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006994
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006995static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996{
6997 int ret, i;
6998
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006999 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7000
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007001 /* Parameter checking */
7002 if (hotkey_report_mode > 2)
7003 return -EINVAL;
7004
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007005 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007006
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007007 ret = get_thinkpad_model_data(&thinkpad_id);
7008 if (ret) {
7009 printk(TPACPI_ERR
7010 "unable to get DMI data: %d\n", ret);
7011 thinkpad_acpi_module_exit();
7012 return ret;
7013 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007014 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007015 if (ret) {
7016 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007017 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007020 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007021
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007022 TPACPI_ACPIHANDLE_INIT(ecrd);
7023 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007025 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7026 if (!tpacpi_wq) {
7027 thinkpad_acpi_module_exit();
7028 return -ENOMEM;
7029 }
7030
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007031 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007033 printk(TPACPI_ERR
7034 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007035 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 return -ENODEV;
7037 }
7038 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007039
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007040 ret = platform_driver_register(&tpacpi_pdriver);
7041 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007042 printk(TPACPI_ERR
7043 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007044 thinkpad_acpi_module_exit();
7045 return ret;
7046 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007047 tp_features.platform_drv_registered = 1;
7048
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007049 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7050 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007051 printk(TPACPI_ERR
7052 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007053 thinkpad_acpi_module_exit();
7054 return ret;
7055 }
7056 tp_features.sensors_pdrv_registered = 1;
7057
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007058 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007059 if (!ret) {
7060 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007061 ret = tpacpi_create_driver_attributes(
7062 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007063 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007064 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007065 printk(TPACPI_ERR
7066 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007067 thinkpad_acpi_module_exit();
7068 return ret;
7069 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007070 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007071
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007072
7073 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007074 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007075 NULL, 0);
7076 if (IS_ERR(tpacpi_pdev)) {
7077 ret = PTR_ERR(tpacpi_pdev);
7078 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007079 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007080 thinkpad_acpi_module_exit();
7081 return ret;
7082 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007083 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007084 TPACPI_HWMON_DRVR_NAME,
7085 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007086 if (IS_ERR(tpacpi_sensors_pdev)) {
7087 ret = PTR_ERR(tpacpi_sensors_pdev);
7088 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007089 printk(TPACPI_ERR
7090 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007091 thinkpad_acpi_module_exit();
7092 return ret;
7093 }
7094 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7095 &dev_attr_thinkpad_acpi_pdev_name);
7096 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007097 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007098 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007099 thinkpad_acpi_module_exit();
7100 return ret;
7101 }
7102 tp_features.sensors_pdev_attrs_registered = 1;
7103 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007104 if (IS_ERR(tpacpi_hwmon)) {
7105 ret = PTR_ERR(tpacpi_hwmon);
7106 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007107 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007108 thinkpad_acpi_module_exit();
7109 return ret;
7110 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007111 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007112 tpacpi_inputdev = input_allocate_device();
7113 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007114 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007115 thinkpad_acpi_module_exit();
7116 return -ENOMEM;
7117 } else {
7118 /* Prepare input device, but don't register */
7119 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007120 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007121 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007122 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7123 thinkpad_id.vendor :
7124 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007125 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7126 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7127 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007128 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7129 ret = ibm_init(&ibms_init[i]);
7130 if (ret >= 0 && *ibms_init[i].param)
7131 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007133 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 return ret;
7135 }
7136 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007137 ret = input_register_device(tpacpi_inputdev);
7138 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007139 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007140 thinkpad_acpi_module_exit();
7141 return ret;
7142 } else {
7143 tp_features.input_device_registered = 1;
7144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007146 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 return 0;
7148}
7149
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007150/* Please remove this in year 2009 */
7151MODULE_ALIAS("ibm_acpi");
7152
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007153MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7154
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007155/*
7156 * DMI matching for module autoloading
7157 *
7158 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7159 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7160 *
7161 * Only models listed in thinkwiki will be supported, so add yours
7162 * if it is not there yet.
7163 */
7164#define IBM_BIOS_MODULE_ALIAS(__type) \
7165 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7166
7167/* Non-ancient thinkpads */
7168MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7169MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7170
7171/* Ancient thinkpad BIOSes have to be identified by
7172 * BIOS type or model number, and there are far less
7173 * BIOS types than model numbers... */
7174IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7175IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7176IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7177
7178MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007179MODULE_DESCRIPTION(TPACPI_DESC);
7180MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007181MODULE_LICENSE("GPL");
7182
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007183module_init(thinkpad_acpi_module_init);
7184module_exit(thinkpad_acpi_module_exit);