blob: a5e99d749246df18148aa799717da46d301f73fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064
65#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
70#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <asm/uaccess.h>
78
79#include <linux/dmi.h>
80#include <linux/jiffies.h>
81#include <linux/workqueue.h>
82
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020083#include <sound/core.h>
84#include <sound/control.h>
85#include <sound/initval.h>
86
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020088
89#include <linux/pci_ids.h>
90
David Henningsson420f9732013-10-16 23:10:31 +020091#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* ThinkPad CMOS commands */
94#define TP_CMOS_VOLUME_DOWN 0
95#define TP_CMOS_VOLUME_UP 1
96#define TP_CMOS_VOLUME_MUTE 2
97#define TP_CMOS_BRIGHTNESS_UP 4
98#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030099#define TP_CMOS_THINKLIGHT_ON 12
100#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200101
102/* NVRAM Addresses */
103enum tp_nvram_addr {
104 TP_NVRAM_ADDR_HK2 = 0x57,
105 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
106 TP_NVRAM_ADDR_VIDEO = 0x59,
107 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
108 TP_NVRAM_ADDR_MIXER = 0x60,
109};
110
111/* NVRAM bit masks */
112enum {
113 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
114 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
115 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
116 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
117 TP_NVRAM_MASK_THINKLIGHT = 0x10,
118 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
119 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
120 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
121 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
122 TP_NVRAM_MASK_MUTE = 0x40,
123 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
124 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
125 TP_NVRAM_POS_LEVEL_VOLUME = 0,
126};
127
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300128/* Misc NVRAM-related */
129enum {
130 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
131};
132
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400134#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
135#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300136#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
138/* Input IDs */
139#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
140#define TPACPI_HKEY_INPUT_VERSION 0x4101
141
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200142/* ACPI \WGSV commands */
143enum {
144 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
145 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
146 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
147 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
148};
149
150/* TP_ACPI_WGSV_GET_STATE bits */
151enum {
152 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
153 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
154 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
155 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
156 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
157 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
158 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
159 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
160 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
161 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
162};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300164/* HKEY events */
165enum tpacpi_hkey_event_t {
166 /* Hotkey-related */
167 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
168 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
169 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300192
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
205
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300206 /* Thermal events */
207 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
208 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
209 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
210 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
211 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
212
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100213 /* AC-related events */
214 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300215
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300216 /* Misc */
217 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218};
219
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200220/****************************************************************************
221 * Main driver
222 */
223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200224#define TPACPI_NAME "thinkpad"
225#define TPACPI_DESC "ThinkPad ACPI Extras"
226#define TPACPI_FILE TPACPI_NAME "_acpi"
227#define TPACPI_URL "http://ibm-acpi.sf.net/"
228#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200229
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200230#define TPACPI_PROC_DIR "ibm"
231#define TPACPI_ACPI_EVENT_PREFIX "ibm"
232#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300233#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200234#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200235
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300236#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300237#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300238
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200239#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200240
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000241/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000243#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244#define TPACPI_DBG_INIT 0x0001
245#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000246#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000247#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000248#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000249#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200250#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200252#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
253#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
254#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
257/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259 */
260
261struct ibm_struct;
262
263struct tp_acpi_drv_struct {
264 const struct acpi_device_id *hid;
265 struct acpi_driver *driver;
266
267 void (*notify) (struct ibm_struct *, u32);
268 acpi_handle *handle;
269 u32 type;
270 struct acpi_device *device;
271};
272
273struct ibm_struct {
274 char *name;
275
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200276 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 int (*write) (char *);
278 void (*exit) (void);
279 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200280 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200281 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200282
283 struct list_head all_drivers;
284
285 struct tp_acpi_drv_struct *acpi;
286
287 struct {
288 u8 acpi_driver_registered:1;
289 u8 acpi_notify_installed:1;
290 u8 proc_created:1;
291 u8 init_called:1;
292 u8 experimental:1;
293 } flags;
294};
295
296struct ibm_init_struct {
297 char param[32];
298
299 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400300 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 struct ibm_struct *data;
302};
303
304static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 bluetooth:1;
306 u32 hotkey:1;
307 u32 hotkey_mask:1;
308 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200309 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200310 u32 light:1;
311 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300312 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300313 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200314 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200315 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300317 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300318 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200319 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200320 u32 input_device_registered:1;
321 u32 platform_drv_registered:1;
322 u32 platform_drv_attrs_registered:1;
323 u32 sensors_pdrv_registered:1;
324 u32 sensors_pdrv_attrs_registered:1;
325 u32 sensors_pdev_attrs_registered:1;
326 u32 hotkey_poll_active:1;
327} tp_features;
328
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300329static struct {
330 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200331 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300332} tp_warned;
333
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334struct thinkpad_id_data {
335 unsigned int vendor; /* ThinkPad vendor:
336 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
337
338 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
339 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
340
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300341 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300343 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
344 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200345
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300346 char *model_str; /* ThinkPad T43 */
347 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200348};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200349static struct thinkpad_id_data thinkpad_id;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351static enum {
352 TPACPI_LIFE_INIT = 0,
353 TPACPI_LIFE_RUNNING,
354 TPACPI_LIFE_EXITING,
355} tpacpi_lifecycle;
356
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200357static int experimental;
358static u32 dbg_level;
359
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300360static struct workqueue_struct *tpacpi_wq;
361
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000362enum led_status_t {
363 TPACPI_LED_OFF = 0,
364 TPACPI_LED_ON,
365 TPACPI_LED_BLINK,
366};
367
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300368/* Special LED class that can defer work */
369struct tpacpi_led_classdev {
370 struct led_classdev led_classdev;
371 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000372 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800373 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300374};
375
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300376/* brightness level capabilities */
377static unsigned int bright_maxlvl; /* 0 = unknown */
378
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200379#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
380static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030381static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200382static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030383static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200384static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030385static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200386static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388#endif
389
390
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000391/*************************************************************************
392 * Debugging helpers
393 */
394
Joe Perches0978e012011-04-04 10:06:25 -0700395#define dbg_printk(a_dbg_level, format, arg...) \
396do { \
397 if (dbg_level & (a_dbg_level)) \
398 printk(KERN_DEBUG pr_fmt("%s: " format), \
399 __func__, ##arg); \
400} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000401
402#ifdef CONFIG_THINKPAD_ACPI_DEBUG
403#define vdbg_printk dbg_printk
404static const char *str_supported(int is_supported);
405#else
Joe Perches0978e012011-04-04 10:06:25 -0700406static inline const char *str_supported(int is_supported) { return ""; }
407#define vdbg_printk(a_dbg_level, format, arg...) \
408 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000409#endif
410
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000411static void tpacpi_log_usertask(const char * const what)
412{
Joe Perches0978e012011-04-04 10:06:25 -0700413 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
414 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000415}
416
Joe Perches0978e012011-04-04 10:06:25 -0700417#define tpacpi_disclose_usertask(what, format, arg...) \
418do { \
419 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
420 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
421 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
422 what, task_tgid_vnr(current), ## arg); \
423 } \
424} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000425
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300426/*
427 * Quirk handling helpers
428 *
429 * ThinkPad IDs and versions seen in the field so far
430 * are two-characters from the set [0-9A-Z], i.e. base 36.
431 *
432 * We use values well outside that range as specials.
433 */
434
435#define TPACPI_MATCH_ANY 0xffffU
436#define TPACPI_MATCH_UNKNOWN 0U
437
438/* TPID('1', 'Y') == 0x5931 */
439#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
440
441#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_IBM, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
446
447#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200453#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPACPI_MATCH_ANY, \
456 .ec = TPID(__id1, __id2), \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300459struct tpacpi_quirk {
460 unsigned int vendor;
461 u16 bios;
462 u16 ec;
463 unsigned long quirks;
464};
465
466/**
467 * tpacpi_check_quirks() - search BIOS/EC version on a list
468 * @qlist: array of &struct tpacpi_quirk
469 * @qlist_size: number of elements in @qlist
470 *
471 * Iterates over a quirks list until one is found that matches the
472 * ThinkPad's vendor, BIOS and EC model.
473 *
474 * Returns 0 if nothing matches, otherwise returns the quirks field of
475 * the matching &struct tpacpi_quirk entry.
476 *
477 * The match criteria is: vendor, ec and bios much match.
478 */
479static unsigned long __init tpacpi_check_quirks(
480 const struct tpacpi_quirk *qlist,
481 unsigned int qlist_size)
482{
483 while (qlist_size) {
484 if ((qlist->vendor == thinkpad_id.vendor ||
485 qlist->vendor == TPACPI_MATCH_ANY) &&
486 (qlist->bios == thinkpad_id.bios_model ||
487 qlist->bios == TPACPI_MATCH_ANY) &&
488 (qlist->ec == thinkpad_id.ec_model ||
489 qlist->ec == TPACPI_MATCH_ANY))
490 return qlist->quirks;
491
492 qlist_size--;
493 qlist++;
494 }
495 return 0;
496}
497
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300498static inline bool __pure __init tpacpi_is_lenovo(void)
499{
500 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
501}
502
503static inline bool __pure __init tpacpi_is_ibm(void)
504{
505 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
506}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508/****************************************************************************
509 ****************************************************************************
510 *
511 * ACPI Helpers and device model
512 *
513 ****************************************************************************
514 ****************************************************************************/
515
516/*************************************************************************
517 * ACPI basic handles
518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300520static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300521static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200523#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700525 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300526 &parent##_handle; \
527 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
530TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200532TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
533 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400534 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
535 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200538TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539 "^HKEY", /* R30, R31 */
540 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543/*************************************************************************
544 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200545 */
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700548 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400551 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200552 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 struct acpi_buffer result, *resultp;
554 union acpi_object out_obj;
555 acpi_status status;
556 va_list ap;
557 char res_type;
558 int success;
559 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700562 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564 }
565
566 if (*fmt == 'q') {
567 quiet = 1;
568 fmt++;
569 } else
570 quiet = 0;
571
572 res_type = *(fmt++);
573
574 params.count = 0;
575 params.pointer = &in_objs[0];
576
577 va_start(ap, fmt);
578 while (*fmt) {
579 char c = *(fmt++);
580 switch (c) {
581 case 'd': /* int */
582 in_objs[params.count].integer.value = va_arg(ap, int);
583 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
584 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 default:
Joe Perches0978e012011-04-04 10:06:25 -0700587 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100589 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591 }
592 }
593 va_end(ap);
594
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400595 if (res_type != 'v') {
596 result.length = sizeof(out_obj);
597 result.pointer = &out_obj;
598 resultp = &result;
599 } else
600 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400605 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300606 success = (status == AE_OK &&
607 out_obj.type == ACPI_TYPE_INTEGER);
608 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700609 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 success = status == AE_OK;
613 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 default:
Joe Perches0978e012011-04-04 10:06:25 -0700616 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 "with invalid format character '%c'\n", res_type);
618 return 0;
619 }
620
621 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700622 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300623 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return success;
626}
627
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200628static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300629{
630 int v;
631
632 if (ecrd_handle) {
633 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
634 return 0;
635 *p = v;
636 } else {
637 if (ec_read(i, p) < 0)
638 return 0;
639 }
640
641 return 1;
642}
643
644static int acpi_ec_write(int i, u8 v)
645{
646 if (ecwr_handle) {
647 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
648 return 0;
649 } else {
650 if (ec_write(i, v) < 0)
651 return 0;
652 }
653
654 return 1;
655}
656
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300657static int issue_thinkpad_cmos_command(int cmos_cmd)
658{
659 if (!cmos_handle)
660 return -ENXIO;
661
662 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
663 return -EIO;
664
665 return 0;
666}
667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668/*************************************************************************
669 * ACPI device model
670 */
671
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200672#define TPACPI_ACPIHANDLE_INIT(object) \
673 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300674 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200675
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300676static void __init drv_acpi_handle_init(const char *name,
677 acpi_handle *handle, const acpi_handle parent,
678 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300679{
680 int i;
681 acpi_status status;
682
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300683 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
684 name);
685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686 for (i = 0; i < num_paths; i++) {
687 status = acpi_get_handle(parent, paths[i], handle);
688 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 dbg_printk(TPACPI_DBG_INIT,
690 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300691 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 return;
693 }
694 }
695
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300696 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
697 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 *handle = NULL;
699}
700
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300701static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
702 u32 level, void *context, void **return_value)
703{
Aaron Lu46445b62013-10-11 21:27:46 +0800704 struct acpi_device *dev;
705 if (!strcmp(context, "video")) {
706 if (acpi_bus_get_device(handle, &dev))
707 return AE_OK;
708 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
709 return AE_OK;
710 }
711
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300712 *(acpi_handle *)return_value = handle;
713
714 return AE_CTRL_TERMINATE;
715}
716
717static void __init tpacpi_acpi_handle_locate(const char *name,
718 const char *hid,
719 acpi_handle *handle)
720{
721 acpi_status status;
722 acpi_handle device_found;
723
Aaron Lu46445b62013-10-11 21:27:46 +0800724 BUG_ON(!name || !handle);
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300725 vdbg_printk(TPACPI_DBG_INIT,
726 "trying to locate ACPI handle for %s, using HID %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +0800727 name, hid ? hid : "NULL");
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300728
729 memset(&device_found, 0, sizeof(device_found));
730 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
731 (void *)name, &device_found);
732
733 *handle = NULL;
734
735 if (ACPI_SUCCESS(status)) {
736 *handle = device_found;
737 dbg_printk(TPACPI_DBG_INIT,
738 "Found ACPI handle for %s\n", name);
739 } else {
740 vdbg_printk(TPACPI_DBG_INIT,
741 "Could not locate an ACPI handle for %s: %s\n",
742 name, acpi_format_exception(status));
743 }
744}
745
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300746static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300747{
748 struct ibm_struct *ibm = data;
749
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300750 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
751 return;
752
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300753 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754 return;
755
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300756 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757}
758
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300759static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300760{
761 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300762 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300764 BUG_ON(!ibm->acpi);
765
766 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300767 return 0;
768
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300769 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300770 "setting up ACPI notify for %s\n", ibm->name);
771
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300772 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
773 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700774 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300775 return -ENODEV;
776 }
777
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700778 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300779 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200780 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300781 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300782
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300783 status = acpi_install_notify_handler(*ibm->acpi->handle,
784 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300785 if (ACPI_FAILURE(status)) {
786 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700787 pr_notice("another device driver is already "
788 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300789 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700790 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300791 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792 }
793 return -ENODEV;
794 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300795 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796 return 0;
797}
798
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300799static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300800{
801 return 0;
802}
803
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300804static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300805{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300806 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300807
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300808 dbg_printk(TPACPI_DBG_INIT,
809 "registering %s as an ACPI driver\n", ibm->name);
810
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300811 BUG_ON(!ibm->acpi);
812
813 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
814 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700815 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300816 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300817 }
818
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200819 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300820 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200821
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300822 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300824 rc = acpi_bus_register_driver(ibm->acpi->driver);
825 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700826 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200827 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300828 kfree(ibm->acpi->driver);
829 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300830 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300831 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300833 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300834}
835
836
837/****************************************************************************
838 ****************************************************************************
839 *
840 * Procfs Helpers
841 *
842 ****************************************************************************
843 ****************************************************************************/
844
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200845static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200847 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300848
849 if (!ibm || !ibm->read)
850 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200851 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300852}
853
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200854static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300855{
Al Virod9dda782013-03-31 18:16:14 -0400856 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200857}
858
859static ssize_t dispatch_proc_write(struct file *file,
860 const char __user *userbuf,
861 size_t count, loff_t *pos)
862{
Al Virod9dda782013-03-31 18:16:14 -0400863 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300864 char *kernbuf;
865 int ret;
866
867 if (!ibm || !ibm->write)
868 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300869 if (count > PAGE_SIZE - 2)
870 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300871
872 kernbuf = kmalloc(count + 2, GFP_KERNEL);
873 if (!kernbuf)
874 return -ENOMEM;
875
876 if (copy_from_user(kernbuf, userbuf, count)) {
877 kfree(kernbuf);
878 return -EFAULT;
879 }
880
881 kernbuf[count] = 0;
882 strcat(kernbuf, ",");
883 ret = ibm->write(kernbuf);
884 if (ret == 0)
885 ret = count;
886
887 kfree(kernbuf);
888
889 return ret;
890}
891
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200892static const struct file_operations dispatch_proc_fops = {
893 .owner = THIS_MODULE,
894 .open = dispatch_proc_open,
895 .read = seq_read,
896 .llseek = seq_lseek,
897 .release = single_release,
898 .write = dispatch_proc_write,
899};
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static char *next_cmd(char **cmds)
902{
903 char *start = *cmds;
904 char *end;
905
906 while ((end = strchr(start, ',')) && end == start)
907 start = end + 1;
908
909 if (!end)
910 return NULL;
911
912 *end = 0;
913 *cmds = end + 1;
914 return start;
915}
916
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300917
918/****************************************************************************
919 ****************************************************************************
920 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300921 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300922 *
923 ****************************************************************************
924 ****************************************************************************/
925
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300926static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300927static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700928static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300929static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300930static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200931static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300932
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200933#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200934static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200935{
936 struct ibm_struct *ibm, *itmp;
937
938 list_for_each_entry_safe(ibm, itmp,
939 &tpacpi_all_drivers,
940 all_drivers) {
941 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200942 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200943 }
944
945 return 0;
946}
947
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200948static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300949{
950 struct ibm_struct *ibm, *itmp;
951
952 list_for_each_entry_safe(ibm, itmp,
953 &tpacpi_all_drivers,
954 all_drivers) {
955 if (ibm->resume)
956 (ibm->resume)();
957 }
958
959 return 0;
960}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200961#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300962
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200963static SIMPLE_DEV_PM_OPS(tpacpi_pm,
964 tpacpi_suspend_handler, tpacpi_resume_handler);
965
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200966static void tpacpi_shutdown_handler(struct platform_device *pdev)
967{
968 struct ibm_struct *ibm, *itmp;
969
970 list_for_each_entry_safe(ibm, itmp,
971 &tpacpi_all_drivers,
972 all_drivers) {
973 if (ibm->shutdown)
974 (ibm->shutdown)();
975 }
976}
977
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300978static struct platform_driver tpacpi_pdriver = {
979 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200980 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300981 .owner = THIS_MODULE,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200982 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300983 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200984 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300985};
986
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300987static struct platform_driver tpacpi_hwmon_pdriver = {
988 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200989 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300990 .owner = THIS_MODULE,
991 },
992};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300993
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300994/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300995 * sysfs support helpers
996 */
997
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200998struct attribute_set {
999 unsigned int members, max_members;
1000 struct attribute_group group;
1001};
1002
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001003struct attribute_set_obj {
1004 struct attribute_set s;
1005 struct attribute *a;
1006} __attribute__((packed));
1007
1008static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001009 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001010{
1011 struct attribute_set_obj *sobj;
1012
1013 if (max_members == 0)
1014 return NULL;
1015
1016 /* Allocates space for implicit NULL at the end too */
1017 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1018 max_members * sizeof(struct attribute *),
1019 GFP_KERNEL);
1020 if (!sobj)
1021 return NULL;
1022 sobj->s.max_members = max_members;
1023 sobj->s.group.attrs = &sobj->a;
1024 sobj->s.group.name = name;
1025
1026 return &sobj->s;
1027}
1028
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001029#define destroy_attr_set(_set) \
1030 kfree(_set);
1031
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001032/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001033static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001034{
1035 if (!s || !attr)
1036 return -EINVAL;
1037
1038 if (s->members >= s->max_members)
1039 return -ENOMEM;
1040
1041 s->group.attrs[s->members] = attr;
1042 s->members++;
1043
1044 return 0;
1045}
1046
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001047static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001048 struct attribute **attr,
1049 unsigned int count)
1050{
1051 int i, res;
1052
1053 for (i = 0; i < count; i++) {
1054 res = add_to_attr_set(s, attr[i]);
1055 if (res)
1056 return res;
1057 }
1058
1059 return 0;
1060}
1061
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001062static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001063{
1064 sysfs_remove_group(kobj, &s->group);
1065 destroy_attr_set(s);
1066}
1067
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001068#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1069 sysfs_create_group(_kobj, &_attr_set->group)
1070
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001071static int parse_strtoul(const char *buf,
1072 unsigned long max, unsigned long *value)
1073{
1074 char *endp;
1075
André Goddard Rosae7d28602009-12-14 18:01:06 -08001076 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1077 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001078 if (*endp || *value > max)
1079 return -EINVAL;
1080
1081 return 0;
1082}
1083
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001084static void tpacpi_disable_brightness_delay(void)
1085{
1086 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001087 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001088}
1089
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001090static void printk_deprecated_attribute(const char * const what,
1091 const char * const details)
1092{
1093 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001094 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001095 "will be removed. %s\n",
1096 what, details);
1097}
1098
Johannes Berg19d337d2009-06-02 13:01:37 +02001099/*************************************************************************
1100 * rfkill and radio control support helpers
1101 */
1102
1103/*
1104 * ThinkPad-ACPI firmware handling model:
1105 *
1106 * WLSW (master wireless switch) is event-driven, and is common to all
1107 * firmware-controlled radios. It cannot be controlled, just monitored,
1108 * as expected. It overrides all radio state in firmware
1109 *
1110 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1111 * (TODO: verify how WLSW interacts with the returned radio state).
1112 *
1113 * The only time there are shadow radio state changes, is when
1114 * masked-off hotkeys are used.
1115 */
1116
1117/*
1118 * Internal driver API for radio state:
1119 *
1120 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1121 * bool: true means radio blocked (off)
1122 */
1123enum tpacpi_rfkill_state {
1124 TPACPI_RFK_RADIO_OFF = 0,
1125 TPACPI_RFK_RADIO_ON
1126};
1127
1128/* rfkill switches */
1129enum tpacpi_rfk_id {
1130 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1131 TPACPI_RFK_WWAN_SW_ID,
1132 TPACPI_RFK_UWB_SW_ID,
1133 TPACPI_RFK_SW_MAX
1134};
1135
1136static const char *tpacpi_rfkill_names[] = {
1137 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1138 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1139 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1140 [TPACPI_RFK_SW_MAX] = NULL
1141};
1142
1143/* ThinkPad-ACPI rfkill subdriver */
1144struct tpacpi_rfk {
1145 struct rfkill *rfkill;
1146 enum tpacpi_rfk_id id;
1147 const struct tpacpi_rfk_ops *ops;
1148};
1149
1150struct tpacpi_rfk_ops {
1151 /* firmware interface */
1152 int (*get_status)(void);
1153 int (*set_status)(const enum tpacpi_rfkill_state);
1154};
1155
1156static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1157
1158/* Query FW and update rfkill sw state for a given rfkill switch */
1159static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1160{
1161 int status;
1162
1163 if (!tp_rfk)
1164 return -ENODEV;
1165
1166 status = (tp_rfk->ops->get_status)();
1167 if (status < 0)
1168 return status;
1169
1170 rfkill_set_sw_state(tp_rfk->rfkill,
1171 (status == TPACPI_RFK_RADIO_OFF));
1172
1173 return status;
1174}
1175
1176/* Query FW and update rfkill sw state for all rfkill switches */
1177static void tpacpi_rfk_update_swstate_all(void)
1178{
1179 unsigned int i;
1180
1181 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1182 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1183}
1184
1185/*
1186 * Sync the HW-blocking state of all rfkill switches,
1187 * do notice it causes the rfkill core to schedule uevents
1188 */
1189static void tpacpi_rfk_update_hwblock_state(bool blocked)
1190{
1191 unsigned int i;
1192 struct tpacpi_rfk *tp_rfk;
1193
1194 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1195 tp_rfk = tpacpi_rfkill_switches[i];
1196 if (tp_rfk) {
1197 if (rfkill_set_hw_state(tp_rfk->rfkill,
1198 blocked)) {
1199 /* ignore -- we track sw block */
1200 }
1201 }
1202 }
1203}
1204
1205/* Call to get the WLSW state from the firmware */
1206static int hotkey_get_wlsw(void);
1207
1208/* Call to query WLSW state and update all rfkill switches */
1209static bool tpacpi_rfk_check_hwblock_state(void)
1210{
1211 int res = hotkey_get_wlsw();
1212 int hw_blocked;
1213
1214 /* When unknown or unsupported, we have to assume it is unblocked */
1215 if (res < 0)
1216 return false;
1217
1218 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1219 tpacpi_rfk_update_hwblock_state(hw_blocked);
1220
1221 return hw_blocked;
1222}
1223
1224static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1225{
1226 struct tpacpi_rfk *tp_rfk = data;
1227 int res;
1228
1229 dbg_printk(TPACPI_DBG_RFKILL,
1230 "request to change radio state to %s\n",
1231 blocked ? "blocked" : "unblocked");
1232
1233 /* try to set radio state */
1234 res = (tp_rfk->ops->set_status)(blocked ?
1235 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1236
1237 /* and update the rfkill core with whatever the FW really did */
1238 tpacpi_rfk_update_swstate(tp_rfk);
1239
1240 return (res < 0) ? res : 0;
1241}
1242
1243static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1244 .set_block = tpacpi_rfk_hook_set_block,
1245};
1246
1247static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1248 const struct tpacpi_rfk_ops *tp_rfkops,
1249 const enum rfkill_type rfktype,
1250 const char *name,
1251 const bool set_default)
1252{
1253 struct tpacpi_rfk *atp_rfk;
1254 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001255 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001256 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001257 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001258
1259 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1260
Johannes Berg19d337d2009-06-02 13:01:37 +02001261 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1262 if (atp_rfk)
1263 atp_rfk->rfkill = rfkill_alloc(name,
1264 &tpacpi_pdev->dev,
1265 rfktype,
1266 &tpacpi_rfk_rfkill_ops,
1267 atp_rfk);
1268 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001269 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001270 kfree(atp_rfk);
1271 return -ENOMEM;
1272 }
1273
1274 atp_rfk->id = id;
1275 atp_rfk->ops = tp_rfkops;
1276
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001277 sw_status = (tp_rfkops->get_status)();
1278 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001279 pr_err("failed to read initial state for %s, error %d\n",
1280 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001281 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001282 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001283 if (set_default) {
1284 /* try to keep the initial state, since we ask the
1285 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001286 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001287 }
1288 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001289 hw_state = tpacpi_rfk_check_hwblock_state();
1290 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001291
1292 res = rfkill_register(atp_rfk->rfkill);
1293 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001294 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001295 rfkill_destroy(atp_rfk->rfkill);
1296 kfree(atp_rfk);
1297 return res;
1298 }
1299
1300 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001301
Joe Perches0978e012011-04-04 10:06:25 -07001302 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001303 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001304 return 0;
1305}
1306
1307static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1308{
1309 struct tpacpi_rfk *tp_rfk;
1310
1311 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1312
1313 tp_rfk = tpacpi_rfkill_switches[id];
1314 if (tp_rfk) {
1315 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001316 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001317 tpacpi_rfkill_switches[id] = NULL;
1318 kfree(tp_rfk);
1319 }
1320}
1321
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001322static void printk_deprecated_rfkill_attribute(const char * const what)
1323{
1324 printk_deprecated_attribute(what,
1325 "Please switch to generic rfkill before year 2010");
1326}
1327
Johannes Berg19d337d2009-06-02 13:01:37 +02001328/* sysfs <radio> enable ------------------------------------------------ */
1329static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1330 struct device_attribute *attr,
1331 char *buf)
1332{
1333 int status;
1334
1335 printk_deprecated_rfkill_attribute(attr->attr.name);
1336
1337 /* This is in the ABI... */
1338 if (tpacpi_rfk_check_hwblock_state()) {
1339 status = TPACPI_RFK_RADIO_OFF;
1340 } else {
1341 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1342 if (status < 0)
1343 return status;
1344 }
1345
1346 return snprintf(buf, PAGE_SIZE, "%d\n",
1347 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1348}
1349
1350static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1351 struct device_attribute *attr,
1352 const char *buf, size_t count)
1353{
1354 unsigned long t;
1355 int res;
1356
1357 printk_deprecated_rfkill_attribute(attr->attr.name);
1358
1359 if (parse_strtoul(buf, 1, &t))
1360 return -EINVAL;
1361
1362 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1363
1364 /* This is in the ABI... */
1365 if (tpacpi_rfk_check_hwblock_state() && !!t)
1366 return -EPERM;
1367
1368 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1369 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1370 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1371
1372 return (res < 0) ? res : count;
1373}
1374
1375/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001376static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001377{
Johannes Berg19d337d2009-06-02 13:01:37 +02001378 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001379 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001380 else {
1381 int status;
1382
1383 /* This is in the ABI... */
1384 if (tpacpi_rfk_check_hwblock_state()) {
1385 status = TPACPI_RFK_RADIO_OFF;
1386 } else {
1387 status = tpacpi_rfk_update_swstate(
1388 tpacpi_rfkill_switches[id]);
1389 if (status < 0)
1390 return status;
1391 }
1392
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001393 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001394 (status == TPACPI_RFK_RADIO_ON) ?
1395 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001396 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001397 }
1398
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001399 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001400}
1401
1402static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1403{
1404 char *cmd;
1405 int status = -1;
1406 int res = 0;
1407
1408 if (id >= TPACPI_RFK_SW_MAX)
1409 return -ENODEV;
1410
1411 while ((cmd = next_cmd(&buf))) {
1412 if (strlencmp(cmd, "enable") == 0)
1413 status = TPACPI_RFK_RADIO_ON;
1414 else if (strlencmp(cmd, "disable") == 0)
1415 status = TPACPI_RFK_RADIO_OFF;
1416 else
1417 return -EINVAL;
1418 }
1419
1420 if (status != -1) {
1421 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1422 (status == TPACPI_RFK_RADIO_ON) ?
1423 "enable" : "disable",
1424 tpacpi_rfkill_names[id]);
1425 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1426 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1427 }
1428
1429 return res;
1430}
1431
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001432/*************************************************************************
1433 * thinkpad-acpi driver attributes
1434 */
1435
1436/* interface_version --------------------------------------------------- */
1437static ssize_t tpacpi_driver_interface_version_show(
1438 struct device_driver *drv,
1439 char *buf)
1440{
1441 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1442}
1443
1444static DRIVER_ATTR(interface_version, S_IRUGO,
1445 tpacpi_driver_interface_version_show, NULL);
1446
1447/* debug_level --------------------------------------------------------- */
1448static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1449 char *buf)
1450{
1451 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1452}
1453
1454static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1455 const char *buf, size_t count)
1456{
1457 unsigned long t;
1458
1459 if (parse_strtoul(buf, 0xffff, &t))
1460 return -EINVAL;
1461
1462 dbg_level = t;
1463
1464 return count;
1465}
1466
1467static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1468 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1469
1470/* version ------------------------------------------------------------- */
1471static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1472 char *buf)
1473{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001474 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1475 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001476}
1477
1478static DRIVER_ATTR(version, S_IRUGO,
1479 tpacpi_driver_version_show, NULL);
1480
1481/* --------------------------------------------------------------------- */
1482
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001483#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1484
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001485/* wlsw_emulstate ------------------------------------------------------ */
1486static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1487 char *buf)
1488{
1489 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1490}
1491
1492static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1493 const char *buf, size_t count)
1494{
1495 unsigned long t;
1496
1497 if (parse_strtoul(buf, 1, &t))
1498 return -EINVAL;
1499
Johannes Berg19d337d2009-06-02 13:01:37 +02001500 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001501 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001502 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1503 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001504
1505 return count;
1506}
1507
1508static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1509 tpacpi_driver_wlsw_emulstate_show,
1510 tpacpi_driver_wlsw_emulstate_store);
1511
1512/* bluetooth_emulstate ------------------------------------------------- */
1513static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1514 struct device_driver *drv,
1515 char *buf)
1516{
1517 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1518}
1519
1520static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1521 struct device_driver *drv,
1522 const char *buf, size_t count)
1523{
1524 unsigned long t;
1525
1526 if (parse_strtoul(buf, 1, &t))
1527 return -EINVAL;
1528
1529 tpacpi_bluetooth_emulstate = !!t;
1530
1531 return count;
1532}
1533
1534static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1535 tpacpi_driver_bluetooth_emulstate_show,
1536 tpacpi_driver_bluetooth_emulstate_store);
1537
1538/* wwan_emulstate ------------------------------------------------- */
1539static ssize_t tpacpi_driver_wwan_emulstate_show(
1540 struct device_driver *drv,
1541 char *buf)
1542{
1543 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1544}
1545
1546static ssize_t tpacpi_driver_wwan_emulstate_store(
1547 struct device_driver *drv,
1548 const char *buf, size_t count)
1549{
1550 unsigned long t;
1551
1552 if (parse_strtoul(buf, 1, &t))
1553 return -EINVAL;
1554
1555 tpacpi_wwan_emulstate = !!t;
1556
1557 return count;
1558}
1559
1560static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1561 tpacpi_driver_wwan_emulstate_show,
1562 tpacpi_driver_wwan_emulstate_store);
1563
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001564/* uwb_emulstate ------------------------------------------------- */
1565static ssize_t tpacpi_driver_uwb_emulstate_show(
1566 struct device_driver *drv,
1567 char *buf)
1568{
1569 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1570}
1571
1572static ssize_t tpacpi_driver_uwb_emulstate_store(
1573 struct device_driver *drv,
1574 const char *buf, size_t count)
1575{
1576 unsigned long t;
1577
1578 if (parse_strtoul(buf, 1, &t))
1579 return -EINVAL;
1580
1581 tpacpi_uwb_emulstate = !!t;
1582
1583 return count;
1584}
1585
1586static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1587 tpacpi_driver_uwb_emulstate_show,
1588 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001589#endif
1590
1591/* --------------------------------------------------------------------- */
1592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001593static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001594 &driver_attr_debug_level, &driver_attr_version,
1595 &driver_attr_interface_version,
1596};
1597
1598static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1599{
1600 int i, res;
1601
1602 i = 0;
1603 res = 0;
1604 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1605 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1606 i++;
1607 }
1608
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001609#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1610 if (!res && dbg_wlswemul)
1611 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1612 if (!res && dbg_bluetoothemul)
1613 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1614 if (!res && dbg_wwanemul)
1615 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001616 if (!res && dbg_uwbemul)
1617 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001618#endif
1619
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001620 return res;
1621}
1622
1623static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1624{
1625 int i;
1626
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001627 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001628 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001629
1630#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1631 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1632 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1633 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001634 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001635#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001636}
1637
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001638/*************************************************************************
1639 * Firmware Data
1640 */
1641
1642/*
1643 * Table of recommended minimum BIOS versions
1644 *
1645 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001646 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001647 * bugs and bad ACPI behaviour on older versions
1648 *
1649 * 2. BIOS or EC fw with known bugs that trigger on Linux
1650 *
1651 * 3. BIOS with known reduced functionality in older versions
1652 *
1653 * We recommend the latest BIOS and EC version.
1654 * We only support the latest BIOS and EC fw version as a rule.
1655 *
1656 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1657 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001658 *
1659 * WARNING: we use this table also to detect that the machine is
1660 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001661 */
1662
1663#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1664 { .vendor = (__v), \
1665 .bios = TPID(__id1, __id2), \
1666 .ec = TPACPI_MATCH_ANY, \
1667 .quirks = TPACPI_MATCH_ANY << 16 \
1668 | (__bv1) << 8 | (__bv2) }
1669
1670#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001671 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001672 { .vendor = (__v), \
1673 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001674 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001675 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1676 | (__bv1) << 8 | (__bv2) }
1677
1678#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1679 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1680
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001681/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001682#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1683 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001684 __bv1, __bv2, TPID(__id1, __id2), \
1685 __ev1, __ev2), \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1687 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1688 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001689
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001690/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1692 __eid1, __eid2, __ev1, __ev2) \
1693 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001694 __bv1, __bv2, TPID(__eid1, __eid2), \
1695 __ev1, __ev2), \
1696 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1697 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1698 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699
1700#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1701 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1702
1703#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1704 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001705 __bv1, __bv2, TPID(__id1, __id2), \
1706 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001707
1708#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1709 __eid1, __eid2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001711 __bv1, __bv2, TPID(__eid1, __eid2), \
1712 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001713
1714static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1715 /* Numeric models ------------------ */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1718 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1719 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1720 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1721 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1722 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1723 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1724 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1725 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1726
1727 /* A-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1730 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1731 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1732 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1733 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1734 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1735 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1736 TPV_QI0('1', '3', '2', '0'), /* A22m */
1737 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1738 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1739 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1740
1741 /* G-series ------------------------- */
1742 /* FW MODEL BIOS VERS */
1743 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1744 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1745
1746 /* R-series, T-series --------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1749 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1750 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1751 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1752 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1753 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1754 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1755 T40/p, T41/p, T42/p (1) */
1756 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1757 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1758 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1759 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1760
1761 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1762 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1763 TPV_QI0('1', '6', '3', '2'), /* T22 */
1764 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1765 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1766 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1767
1768 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1769 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001770 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001771
1772 /* BIOS FW BIOS VERS EC FW EC VERS */
1773 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1774 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1775
1776 /* X-series ------------------------- */
1777 /* FW MODEL BIOS VERS EC VERS */
1778 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1779 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1780 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1781 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1782 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1783 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1784 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1785
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001786 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1787 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001788
1789 /* (0) - older versions lack DMI EC fw string and functionality */
1790 /* (1) - older versions known to lack functionality */
1791};
1792
1793#undef TPV_QL1
1794#undef TPV_QL0
1795#undef TPV_QI2
1796#undef TPV_QI1
1797#undef TPV_QI0
1798#undef TPV_Q_X
1799#undef TPV_Q
1800
1801static void __init tpacpi_check_outdated_fw(void)
1802{
1803 unsigned long fwvers;
1804 u16 ec_version, bios_version;
1805
1806 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable));
1808
1809 if (!fwvers)
1810 return;
1811
1812 bios_version = fwvers & 0xffffU;
1813 ec_version = (fwvers >> 16) & 0xffffU;
1814
1815 /* note that unknown versions are set to 0x0000 and we use that */
1816 if ((bios_version > thinkpad_id.bios_release) ||
1817 (ec_version > thinkpad_id.ec_release &&
1818 ec_version != TPACPI_MATCH_ANY)) {
1819 /*
1820 * The changelogs would let us track down the exact
1821 * reason, but it is just too much of a pain to track
1822 * it. We only list BIOSes that are either really
1823 * broken, or really stable to begin with, so it is
1824 * best if the user upgrades the firmware anyway.
1825 */
Joe Perches0978e012011-04-04 10:06:25 -07001826 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1827 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001828 "fixes and/or important features\n");
1829 }
1830}
1831
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001832static bool __init tpacpi_is_fw_known(void)
1833{
1834 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1835 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1836}
1837
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001838/****************************************************************************
1839 ****************************************************************************
1840 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001841 * Subdrivers
1842 *
1843 ****************************************************************************
1844 ****************************************************************************/
1845
1846/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001847 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001848 */
1849
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001850static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001852 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1853 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001857static struct ibm_struct thinkpad_acpi_driver_data = {
1858 .name = "driver",
1859 .read = thinkpad_acpi_driver_read,
1860};
1861
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001862/*************************************************************************
1863 * Hotkey subdriver
1864 */
1865
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001866/*
1867 * ThinkPad firmware event model
1868 *
1869 * The ThinkPad firmware has two main event interfaces: normal ACPI
1870 * notifications (which follow the ACPI standard), and a private event
1871 * interface.
1872 *
1873 * The private event interface also issues events for the hotkeys. As
1874 * the driver gained features, the event handling code ended up being
1875 * built around the hotkey subdriver. This will need to be refactored
1876 * to a more formal event API eventually.
1877 *
1878 * Some "hotkeys" are actually supposed to be used as event reports,
1879 * such as "brightness has changed", "volume has changed", depending on
1880 * the ThinkPad model and how the firmware is operating.
1881 *
1882 * Unlike other classes, hotkey-class events have mask/unmask control on
1883 * non-ancient firmware. However, how it behaves changes a lot with the
1884 * firmware model and version.
1885 */
1886
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001887enum { /* hot key scan codes (derived from ACPI DSDT) */
1888 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1889 TP_ACPI_HOTKEYSCAN_FNF2,
1890 TP_ACPI_HOTKEYSCAN_FNF3,
1891 TP_ACPI_HOTKEYSCAN_FNF4,
1892 TP_ACPI_HOTKEYSCAN_FNF5,
1893 TP_ACPI_HOTKEYSCAN_FNF6,
1894 TP_ACPI_HOTKEYSCAN_FNF7,
1895 TP_ACPI_HOTKEYSCAN_FNF8,
1896 TP_ACPI_HOTKEYSCAN_FNF9,
1897 TP_ACPI_HOTKEYSCAN_FNF10,
1898 TP_ACPI_HOTKEYSCAN_FNF11,
1899 TP_ACPI_HOTKEYSCAN_FNF12,
1900 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1901 TP_ACPI_HOTKEYSCAN_FNINSERT,
1902 TP_ACPI_HOTKEYSCAN_FNDELETE,
1903 TP_ACPI_HOTKEYSCAN_FNHOME,
1904 TP_ACPI_HOTKEYSCAN_FNEND,
1905 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1906 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1907 TP_ACPI_HOTKEYSCAN_FNSPACE,
1908 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1909 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1910 TP_ACPI_HOTKEYSCAN_MUTE,
1911 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001912 TP_ACPI_HOTKEYSCAN_UNK1,
1913 TP_ACPI_HOTKEYSCAN_UNK2,
1914 TP_ACPI_HOTKEYSCAN_UNK3,
1915 TP_ACPI_HOTKEYSCAN_UNK4,
1916 TP_ACPI_HOTKEYSCAN_UNK5,
1917 TP_ACPI_HOTKEYSCAN_UNK6,
1918 TP_ACPI_HOTKEYSCAN_UNK7,
1919 TP_ACPI_HOTKEYSCAN_UNK8,
1920
1921 /* Hotkey keymap size */
1922 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001923};
1924
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001925enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001926 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1927 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1928};
1929
1930enum { /* Positions of some of the keys in hotkey masks */
1931 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1932 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1933 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1934 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1935 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1936 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1937 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1938 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1939 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1940 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1941 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1942};
1943
1944enum { /* NVRAM to ACPI HKEY group map */
1945 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1946 TP_ACPI_HKEY_ZOOM_MASK |
1947 TP_ACPI_HKEY_DISPSWTCH_MASK |
1948 TP_ACPI_HKEY_HIBERNATE_MASK,
1949 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1950 TP_ACPI_HKEY_BRGHTDWN_MASK,
1951 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1952 TP_ACPI_HKEY_VOLDWN_MASK |
1953 TP_ACPI_HKEY_MUTE_MASK,
1954};
1955
1956#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1957struct tp_nvram_state {
1958 u16 thinkpad_toggle:1;
1959 u16 zoom_toggle:1;
1960 u16 display_toggle:1;
1961 u16 thinklight_toggle:1;
1962 u16 hibernate_toggle:1;
1963 u16 displayexp_toggle:1;
1964 u16 display_state:1;
1965 u16 brightness_toggle:1;
1966 u16 volume_toggle:1;
1967 u16 mute:1;
1968
1969 u8 brightness_level;
1970 u8 volume_level;
1971};
1972
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001973/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001974static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001975
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001977 * Acquire mutex to write poller control variables as an
1978 * atomic block.
1979 *
1980 * Increment hotkey_config_change when changing them if you
1981 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001982 *
1983 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1984 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001985static struct mutex hotkey_thread_data_mutex;
1986static unsigned int hotkey_config_change;
1987
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001988/*
1989 * hotkey poller control variables
1990 *
1991 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001992 *
1993 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1994 * should be used only when the changes need to be taken as
1995 * a block, OR when one needs to force the kthread to forget
1996 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001997 */
1998static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1999static unsigned int hotkey_poll_freq = 10; /* Hz */
2000
2001#define HOTKEY_CONFIG_CRITICAL_START \
2002 do { \
2003 mutex_lock(&hotkey_thread_data_mutex); \
2004 hotkey_config_change++; \
2005 } while (0);
2006#define HOTKEY_CONFIG_CRITICAL_END \
2007 mutex_unlock(&hotkey_thread_data_mutex);
2008
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002009#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010
2011#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002012#define HOTKEY_CONFIG_CRITICAL_START
2013#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002014
2015#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2016
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002017static struct mutex hotkey_mutex;
2018
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002019static enum { /* Reasons for waking up */
2020 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2021 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2022 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2023} hotkey_wakeup_reason;
2024
2025static int hotkey_autosleep_ack;
2026
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002027static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2028static u32 hotkey_all_mask; /* all events supported in fw */
2029static u32 hotkey_reserved_mask; /* events better left disabled */
2030static u32 hotkey_driver_mask; /* events needed by the driver */
2031static u32 hotkey_user_mask; /* events visible to userspace */
2032static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002033
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002034static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002035
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002036static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002037
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002038static void tpacpi_driver_event(const unsigned int hkey_event);
2039static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002040static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002041
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002042/* HKEY.MHKG() return bits */
2043#define TP_HOTKEY_TABLET_MASK (1 << 3)
2044
Johannes Berg19d337d2009-06-02 13:01:37 +02002045static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002046{
Johannes Berg19d337d2009-06-02 13:01:37 +02002047 int status;
2048
2049 if (!tp_features.hotkey_wlsw)
2050 return -ENODEV;
2051
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002052#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002053 if (dbg_wlswemul)
2054 return (tpacpi_wlsw_emulstate) ?
2055 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002056#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002057
2058 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002059 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002060
2061 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002062}
2063
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002064static int hotkey_get_tablet_mode(int *status)
2065{
2066 int s;
2067
2068 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2069 return -EIO;
2070
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002071 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2072 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002073}
2074
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002075/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002076 * Reads current event mask from firmware, and updates
2077 * hotkey_acpi_mask accordingly. Also resets any bits
2078 * from hotkey_user_mask that are unavailable to be
2079 * delivered (shadow requirement of the userspace ABI).
2080 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002081 * Call with hotkey_mutex held
2082 */
2083static int hotkey_mask_get(void)
2084{
2085 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002086 u32 m = 0;
2087
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002088 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002089 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002090
2091 hotkey_acpi_mask = m;
2092 } else {
2093 /* no mask support doesn't mean no event support... */
2094 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002096
2097 /* sync userspace-visible mask */
2098 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002099
2100 return 0;
2101}
2102
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002103void static hotkey_mask_warn_incomplete_mask(void)
2104{
2105 /* log only what the user can fix... */
2106 const u32 wantedmask = hotkey_driver_mask &
2107 ~(hotkey_acpi_mask | hotkey_source_mask) &
2108 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2109
2110 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002111 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002112}
2113
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002114/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002115 * Set the firmware mask when supported
2116 *
2117 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2118 *
2119 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2120 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002121 * Call with hotkey_mutex held
2122 */
2123static int hotkey_mask_set(u32 mask)
2124{
2125 int i;
2126 int rc = 0;
2127
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002128 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002129
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002132 if (!acpi_evalf(hkey_handle,
2133 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002134 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002135 rc = -EIO;
2136 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002137 }
2138 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002139 }
2140
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002141 /*
2142 * We *must* make an inconditional call to hotkey_mask_get to
2143 * refresh hotkey_acpi_mask and update hotkey_user_mask
2144 *
2145 * Take the opportunity to also log when we cannot _enable_
2146 * a given event.
2147 */
2148 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002149 pr_notice("asked for hotkey mask 0x%08x, but "
2150 "firmware forced it to 0x%08x\n",
2151 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002152 }
2153
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002154 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2155 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002156
2157 return rc;
2158}
2159
2160/*
2161 * Sets hotkey_user_mask and tries to set the firmware mask
2162 *
2163 * Call with hotkey_mutex held
2164 */
2165static int hotkey_user_mask_set(const u32 mask)
2166{
2167 int rc;
2168
2169 /* Give people a chance to notice they are doing something that
2170 * is bound to go boom on their users sooner or later */
2171 if (!tp_warned.hotkey_mask_ff &&
2172 (mask == 0xffff || mask == 0xffffff ||
2173 mask == 0xffffffff)) {
2174 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002175 pr_notice("setting the hotkey mask to 0x%08x is likely "
2176 "not the best way to go about it\n", mask);
2177 pr_notice("please consider using the driver defaults, "
2178 "and refer to up-to-date thinkpad-acpi "
2179 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002180 }
2181
2182 /* Try to enable what the user asked for, plus whatever we need.
2183 * this syncs everything but won't enable bits in hotkey_user_mask */
2184 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2185
2186 /* Enable the available bits in hotkey_user_mask */
2187 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2188
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002189 return rc;
2190}
2191
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002192/*
2193 * Sets the driver hotkey mask.
2194 *
2195 * Can be called even if the hotkey subdriver is inactive
2196 */
2197static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2198{
2199 int rc;
2200
2201 /* Do the right thing if hotkey_init has not been called yet */
2202 if (!tp_features.hotkey) {
2203 hotkey_driver_mask = mask;
2204 return 0;
2205 }
2206
2207 mutex_lock(&hotkey_mutex);
2208
2209 HOTKEY_CONFIG_CRITICAL_START
2210 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002211#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002212 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002213#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002214 HOTKEY_CONFIG_CRITICAL_END
2215
2216 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2217 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002218 hotkey_poll_setup(true);
2219
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002220 mutex_unlock(&hotkey_mutex);
2221
2222 return rc;
2223}
2224
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002225static int hotkey_status_get(int *status)
2226{
2227 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2228 return -EIO;
2229
2230 return 0;
2231}
2232
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002233static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002234{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002235 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002236 return -EIO;
2237
2238 return 0;
2239}
2240
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002241static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002242{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002243 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002244
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002245 if (tp_features.hotkey_tablet &&
2246 !hotkey_get_tablet_mode(&state)) {
2247 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002248
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002249 input_report_switch(tpacpi_inputdev,
2250 SW_TABLET_MODE, !!state);
2251 input_sync(tpacpi_inputdev);
2252
2253 mutex_unlock(&tpacpi_inputdev_send_mutex);
2254 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002255}
2256
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002257/* Do NOT call without validating scancode first */
2258static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002259{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002260 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002261
2262 if (keycode != KEY_RESERVED) {
2263 mutex_lock(&tpacpi_inputdev_send_mutex);
2264
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002265 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002266 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002267 input_sync(tpacpi_inputdev);
2268
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002269 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002270 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002271 input_sync(tpacpi_inputdev);
2272
2273 mutex_unlock(&tpacpi_inputdev_send_mutex);
2274 }
2275}
2276
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002277/* Do NOT call without validating scancode first */
2278static void tpacpi_input_send_key_masked(const unsigned int scancode)
2279{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002280 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002281 if (hotkey_user_mask & (1 << scancode))
2282 tpacpi_input_send_key(scancode);
2283}
2284
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002285#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2286static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002288/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002289static void tpacpi_hotkey_send_key(unsigned int scancode)
2290{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002291 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002292}
2293
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002294static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002295{
2296 u8 d;
2297
2298 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2299 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2300 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2301 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2302 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2303 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2304 }
2305 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2306 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2307 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2308 }
2309 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2310 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2311 n->displayexp_toggle =
2312 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2313 }
2314 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2315 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2316 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2317 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2318 n->brightness_toggle =
2319 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2320 }
2321 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2322 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2323 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2324 >> TP_NVRAM_POS_LEVEL_VOLUME;
2325 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2326 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2327 }
2328}
2329
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2331 struct tp_nvram_state *newn,
2332 const u32 event_mask)
2333{
2334
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002335#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002336 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002337 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002338 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002339 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002340 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002341
2342#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002343 do { \
2344 if (event_mask & (1 << __scancode)) \
2345 tpacpi_hotkey_send_key(__scancode); \
2346 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002348 void issue_volchange(const unsigned int oldvol,
2349 const unsigned int newvol)
2350 {
2351 unsigned int i = oldvol;
2352
2353 while (i > newvol) {
2354 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2355 i--;
2356 }
2357 while (i < newvol) {
2358 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2359 i++;
2360 }
2361 }
2362
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002363 void issue_brightnesschange(const unsigned int oldbrt,
2364 const unsigned int newbrt)
2365 {
2366 unsigned int i = oldbrt;
2367
2368 while (i > newbrt) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2370 i--;
2371 }
2372 while (i < newbrt) {
2373 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2374 i++;
2375 }
2376 }
2377
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002378 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2381 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2382
2383 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2384
2385 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2386
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002387 /*
2388 * Handle volume
2389 *
2390 * This code is supposed to duplicate the IBM firmware behaviour:
2391 * - Pressing MUTE issues mute hotkey message, even when already mute
2392 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002393 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002394 * - The act of unmuting issues volume up/down notification,
2395 * depending which key was used to unmute
2396 *
2397 * We are constrained to what the NVRAM can tell us, which is not much
2398 * and certainly not enough if more than one volume hotkey was pressed
2399 * since the last poll cycle.
2400 *
2401 * Just to make our life interesting, some newer Lenovo ThinkPads have
2402 * bugs in the BIOS and may fail to update volume_toggle properly.
2403 */
2404 if (newn->mute) {
2405 /* muted */
2406 if (!oldn->mute ||
2407 oldn->volume_toggle != newn->volume_toggle ||
2408 oldn->volume_level != newn->volume_level) {
2409 /* recently muted, or repeated mute keypress, or
2410 * multiple presses ending in mute */
2411 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2413 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002414 } else {
2415 /* unmute */
2416 if (oldn->mute) {
2417 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002419 }
2420 if (oldn->volume_level != newn->volume_level) {
2421 issue_volchange(oldn->volume_level, newn->volume_level);
2422 } else if (oldn->volume_toggle != newn->volume_toggle) {
2423 /* repeated vol up/down keypress at end of scale ? */
2424 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002426 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002428 }
2429 }
2430
2431 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002432 if (oldn->brightness_level != newn->brightness_level) {
2433 issue_brightnesschange(oldn->brightness_level,
2434 newn->brightness_level);
2435 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2436 /* repeated key presses that didn't change state */
2437 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002439 else if (newn->brightness_level >= bright_maxlvl
2440 && !tp_features.bright_unkfw)
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002442 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002443
2444#undef TPACPI_COMPARE_KEY
2445#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002446}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002448/*
2449 * Polling driver
2450 *
2451 * We track all events in hotkey_source_mask all the time, since
2452 * most of them are edge-based. We only issue those requested by
2453 * hotkey_user_mask or hotkey_driver_mask, though.
2454 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455static int hotkey_kthread(void *data)
2456{
2457 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002458 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002459 unsigned int si, so;
2460 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002461 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002462 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002463 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002465 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2466 goto exit;
2467
2468 set_freezable();
2469
2470 so = 0;
2471 si = 1;
2472 t = 0;
2473
2474 /* Initial state for compares */
2475 mutex_lock(&hotkey_thread_data_mutex);
2476 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002477 poll_mask = hotkey_source_mask;
2478 event_mask = hotkey_source_mask &
2479 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002480 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002481 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002482 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002484 while (!kthread_should_stop()) {
2485 if (t == 0) {
2486 if (likely(poll_freq))
2487 t = 1000/poll_freq;
2488 else
2489 t = 100; /* should never happen... */
2490 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002492 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002494
2495 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002496 continue;
2497
2498 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002499 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 /* forget old state on thaw or config change */
2501 si = so;
2502 t = 0;
2503 change_detector = hotkey_config_change;
2504 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002505 poll_mask = hotkey_source_mask;
2506 event_mask = hotkey_source_mask &
2507 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002508 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002509 mutex_unlock(&hotkey_thread_data_mutex);
2510
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002511 if (likely(poll_mask)) {
2512 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002513 if (likely(si != so)) {
2514 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002515 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002516 }
2517 }
2518
2519 so = si;
2520 si ^= 1;
2521 }
2522
2523exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 return 0;
2525}
2526
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002527/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002528static void hotkey_poll_stop_sync(void)
2529{
2530 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002531 kthread_stop(tpacpi_hotkey_task);
2532 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533 }
2534}
2535
2536/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002537static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002538{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002539 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2540 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002541
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002542 if (hotkey_poll_freq > 0 &&
2543 (poll_driver_mask ||
2544 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002545 if (!tpacpi_hotkey_task) {
2546 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002547 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002548 if (IS_ERR(tpacpi_hotkey_task)) {
2549 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002550 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 "for hotkey polling\n");
2552 }
2553 }
2554 } else {
2555 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002556 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002557 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002558 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2559 "require polling, which is currently "
2560 "disabled\n",
2561 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002562 }
2563 }
2564}
2565
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002566static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002567{
2568 mutex_lock(&hotkey_mutex);
2569 hotkey_poll_setup(may_warn);
2570 mutex_unlock(&hotkey_mutex);
2571}
2572
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002573/* call with hotkey_mutex held */
2574static void hotkey_poll_set_freq(unsigned int freq)
2575{
2576 if (!freq)
2577 hotkey_poll_stop_sync();
2578
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002579 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002580}
2581
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002582#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2583
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002584static void hotkey_poll_setup(const bool __unused)
2585{
2586}
2587
2588static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002589{
2590}
2591
2592#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2593
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594static int hotkey_inputdev_open(struct input_dev *dev)
2595{
2596 switch (tpacpi_lifecycle) {
2597 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002598 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002599 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002600 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002601 case TPACPI_LIFE_EXITING:
2602 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002603 }
2604
2605 /* Should only happen if tpacpi_lifecycle is corrupt */
2606 BUG();
2607 return -EBUSY;
2608}
2609
2610static void hotkey_inputdev_close(struct input_dev *dev)
2611{
2612 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002613 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002614 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002615 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002616}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002617
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002618/* sysfs hotkey enable ------------------------------------------------- */
2619static ssize_t hotkey_enable_show(struct device *dev,
2620 struct device_attribute *attr,
2621 char *buf)
2622{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002623 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002624
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002625 printk_deprecated_attribute("hotkey_enable",
2626 "Hotkey reporting is always enabled");
2627
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002628 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002629 if (res)
2630 return res;
2631
2632 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2633}
2634
2635static ssize_t hotkey_enable_store(struct device *dev,
2636 struct device_attribute *attr,
2637 const char *buf, size_t count)
2638{
2639 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002640
2641 printk_deprecated_attribute("hotkey_enable",
2642 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002643
2644 if (parse_strtoul(buf, 1, &t))
2645 return -EINVAL;
2646
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002647 if (t == 0)
2648 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002650 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002651}
2652
2653static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002654 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002655 hotkey_enable_show, hotkey_enable_store);
2656
2657/* sysfs hotkey mask --------------------------------------------------- */
2658static ssize_t hotkey_mask_show(struct device *dev,
2659 struct device_attribute *attr,
2660 char *buf)
2661{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002662 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663}
2664
2665static ssize_t hotkey_mask_store(struct device *dev,
2666 struct device_attribute *attr,
2667 const char *buf, size_t count)
2668{
2669 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002670 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002671
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002672 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002673 return -EINVAL;
2674
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002675 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002676 return -ERESTARTSYS;
2677
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002678 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002679
2680#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002681 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002682#endif
2683
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002684 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002686 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2687
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002688 return (res) ? res : count;
2689}
2690
2691static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002692 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693 hotkey_mask_show, hotkey_mask_store);
2694
2695/* sysfs hotkey bios_enabled ------------------------------------------- */
2696static ssize_t hotkey_bios_enabled_show(struct device *dev,
2697 struct device_attribute *attr,
2698 char *buf)
2699{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002700 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002701}
2702
2703static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002704 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705
2706/* sysfs hotkey bios_mask ---------------------------------------------- */
2707static ssize_t hotkey_bios_mask_show(struct device *dev,
2708 struct device_attribute *attr,
2709 char *buf)
2710{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002711 printk_deprecated_attribute("hotkey_bios_mask",
2712 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002713 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002714}
2715
2716static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002717 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002718
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002719/* sysfs hotkey all_mask ----------------------------------------------- */
2720static ssize_t hotkey_all_mask_show(struct device *dev,
2721 struct device_attribute *attr,
2722 char *buf)
2723{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002724 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2725 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002726}
2727
2728static struct device_attribute dev_attr_hotkey_all_mask =
2729 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2730
2731/* sysfs hotkey recommended_mask --------------------------------------- */
2732static ssize_t hotkey_recommended_mask_show(struct device *dev,
2733 struct device_attribute *attr,
2734 char *buf)
2735{
2736 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002737 (hotkey_all_mask | hotkey_source_mask)
2738 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002739}
2740
2741static struct device_attribute dev_attr_hotkey_recommended_mask =
2742 __ATTR(hotkey_recommended_mask, S_IRUGO,
2743 hotkey_recommended_mask_show, NULL);
2744
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002745#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2746
2747/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2748static ssize_t hotkey_source_mask_show(struct device *dev,
2749 struct device_attribute *attr,
2750 char *buf)
2751{
2752 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2753}
2754
2755static ssize_t hotkey_source_mask_store(struct device *dev,
2756 struct device_attribute *attr,
2757 const char *buf, size_t count)
2758{
2759 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002760 u32 r_ev;
2761 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002762
2763 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2764 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2765 return -EINVAL;
2766
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002767 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002768 return -ERESTARTSYS;
2769
2770 HOTKEY_CONFIG_CRITICAL_START
2771 hotkey_source_mask = t;
2772 HOTKEY_CONFIG_CRITICAL_END
2773
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002774 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2775 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002776 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002777
2778 /* check if events needed by the driver got disabled */
2779 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2780 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002781
2782 mutex_unlock(&hotkey_mutex);
2783
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002784 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002785 pr_err("hotkey_source_mask: "
2786 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002787
2788 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002789 pr_notice("hotkey_source_mask: "
2790 "some important events were disabled: 0x%04x\n",
2791 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002792
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002793 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2794
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002795 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002796}
2797
2798static struct device_attribute dev_attr_hotkey_source_mask =
2799 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2800 hotkey_source_mask_show, hotkey_source_mask_store);
2801
2802/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2803static ssize_t hotkey_poll_freq_show(struct device *dev,
2804 struct device_attribute *attr,
2805 char *buf)
2806{
2807 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2808}
2809
2810static ssize_t hotkey_poll_freq_store(struct device *dev,
2811 struct device_attribute *attr,
2812 const char *buf, size_t count)
2813{
2814 unsigned long t;
2815
2816 if (parse_strtoul(buf, 25, &t))
2817 return -EINVAL;
2818
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002819 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002820 return -ERESTARTSYS;
2821
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002822 hotkey_poll_set_freq(t);
2823 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002824
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002825 mutex_unlock(&hotkey_mutex);
2826
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002827 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2828
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002829 return count;
2830}
2831
2832static struct device_attribute dev_attr_hotkey_poll_freq =
2833 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2834 hotkey_poll_freq_show, hotkey_poll_freq_store);
2835
2836#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2837
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002838/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002839static ssize_t hotkey_radio_sw_show(struct device *dev,
2840 struct device_attribute *attr,
2841 char *buf)
2842{
Johannes Berg19d337d2009-06-02 13:01:37 +02002843 int res;
2844 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002845 if (res < 0)
2846 return res;
2847
Johannes Berg19d337d2009-06-02 13:01:37 +02002848 /* Opportunistic update */
2849 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2850
2851 return snprintf(buf, PAGE_SIZE, "%d\n",
2852 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002853}
2854
2855static struct device_attribute dev_attr_hotkey_radio_sw =
2856 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002858static void hotkey_radio_sw_notify_change(void)
2859{
2860 if (tp_features.hotkey_wlsw)
2861 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2862 "hotkey_radio_sw");
2863}
2864
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002865/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2866static ssize_t hotkey_tablet_mode_show(struct device *dev,
2867 struct device_attribute *attr,
2868 char *buf)
2869{
2870 int res, s;
2871 res = hotkey_get_tablet_mode(&s);
2872 if (res < 0)
2873 return res;
2874
2875 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2876}
2877
2878static struct device_attribute dev_attr_hotkey_tablet_mode =
2879 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2880
2881static void hotkey_tablet_mode_notify_change(void)
2882{
2883 if (tp_features.hotkey_tablet)
2884 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2885 "hotkey_tablet_mode");
2886}
2887
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002888/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002889static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2890 struct device_attribute *attr,
2891 char *buf)
2892{
2893 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2894}
2895
2896static struct device_attribute dev_attr_hotkey_wakeup_reason =
2897 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2898
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002899static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002900{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002901 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2902 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002903}
2904
2905/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002906static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2907 struct device_attribute *attr,
2908 char *buf)
2909{
2910 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2911}
2912
2913static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2914 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2915 hotkey_wakeup_hotunplug_complete_show, NULL);
2916
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002917static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002918{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002919 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2920 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002921}
2922
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002923/* --------------------------------------------------------------------- */
2924
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002925static struct attribute *hotkey_attributes[] __initdata = {
2926 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002927 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002928 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002929 &dev_attr_hotkey_wakeup_reason.attr,
2930 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002931 &dev_attr_hotkey_mask.attr,
2932 &dev_attr_hotkey_all_mask.attr,
2933 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002934#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002935 &dev_attr_hotkey_source_mask.attr,
2936 &dev_attr_hotkey_poll_freq.attr,
2937#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002938};
2939
Johannes Berg19d337d2009-06-02 13:01:37 +02002940/*
2941 * Sync both the hw and sw blocking state of all switches
2942 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002943static void tpacpi_send_radiosw_update(void)
2944{
2945 int wlsw;
2946
Johannes Berg19d337d2009-06-02 13:01:37 +02002947 /*
2948 * We must sync all rfkill controllers *before* issuing any
2949 * rfkill input events, or we will race the rfkill core input
2950 * handler.
2951 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002952 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002953 * for the above.
2954 *
2955 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2956 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002957
Johannes Berg19d337d2009-06-02 13:01:37 +02002958 wlsw = hotkey_get_wlsw();
2959
2960 /* Sync hw blocking state first if it is hw-blocked */
2961 if (wlsw == TPACPI_RFK_RADIO_OFF)
2962 tpacpi_rfk_update_hwblock_state(true);
2963
2964 /* Sync sw blocking state */
2965 tpacpi_rfk_update_swstate_all();
2966
2967 /* Sync hw blocking state last if it is hw-unblocked */
2968 if (wlsw == TPACPI_RFK_RADIO_ON)
2969 tpacpi_rfk_update_hwblock_state(false);
2970
2971 /* Issue rfkill input event for WLSW switch */
2972 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002973 mutex_lock(&tpacpi_inputdev_send_mutex);
2974
2975 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002976 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002977 input_sync(tpacpi_inputdev);
2978
2979 mutex_unlock(&tpacpi_inputdev_send_mutex);
2980 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002981
2982 /*
2983 * this can be unconditional, as we will poll state again
2984 * if userspace uses the notify to read data
2985 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002986 hotkey_radio_sw_notify_change();
2987}
2988
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002989static void hotkey_exit(void)
2990{
2991#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002992 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002993 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002994 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002995#endif
2996
2997 if (hotkey_dev_attributes)
2998 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2999
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003000 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003001 "restoring original HKEY status and mask\n");
3002 /* yes, there is a bitwise or below, we want the
3003 * functions to be called even if one of them fail */
3004 if (((tp_features.hotkey_mask &&
3005 hotkey_mask_set(hotkey_orig_mask)) |
3006 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003007 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003008 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003009}
3010
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003011static void __init hotkey_unmap(const unsigned int scancode)
3012{
3013 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3014 clear_bit(hotkey_keycode_map[scancode],
3015 tpacpi_inputdev->keybit);
3016 hotkey_keycode_map[scancode] = KEY_RESERVED;
3017 }
3018}
3019
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003020/*
3021 * HKEY quirks:
3022 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3023 */
3024
3025#define TPACPI_HK_Q_INIMASK 0x0001
3026
3027static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3028 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3029 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3030 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3031 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3032 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3033 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3034 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3035 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3036 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3037 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3038 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3039 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3040 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3041 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3042 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3043 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3044 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3045 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3046 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3047};
3048
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003049typedef u16 tpacpi_keymap_entry_t;
3050typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003052static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003053{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003054 /* Requirements for changing the default keymaps:
3055 *
3056 * 1. Many of the keys are mapped to KEY_RESERVED for very
3057 * good reasons. Do not change them unless you have deep
3058 * knowledge on the IBM and Lenovo ThinkPad firmware for
3059 * the various ThinkPad models. The driver behaves
3060 * differently for KEY_RESERVED: such keys have their
3061 * hot key mask *unset* in mask_recommended, and also
3062 * in the initial hot key mask programmed into the
3063 * firmware at driver load time, which means the firm-
3064 * ware may react very differently if you change them to
3065 * something else;
3066 *
3067 * 2. You must be subscribed to the linux-thinkpad and
3068 * ibm-acpi-devel mailing lists, and you should read the
3069 * list archives since 2007 if you want to change the
3070 * keymaps. This requirement exists so that you will
3071 * know the past history of problems with the thinkpad-
3072 * acpi driver keymaps, and also that you will be
3073 * listening to any bug reports;
3074 *
3075 * 3. Do not send thinkpad-acpi specific patches directly to
3076 * for merging, *ever*. Send them to the linux-acpi
3077 * mailinglist for comments. Merging is to be done only
3078 * through acpi-test and the ACPI maintainer.
3079 *
3080 * If the above is too much to ask, don't change the keymap.
3081 * Ask the thinkpad-acpi maintainer to do it, instead.
3082 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003083
3084 enum keymap_index {
3085 TPACPI_KEYMAP_IBM_GENERIC = 0,
3086 TPACPI_KEYMAP_LENOVO_GENERIC,
3087 };
3088
3089 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3090 /* Generic keymap for IBM ThinkPads */
3091 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003092 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003093 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003094 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3095 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003096
3097 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003098 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3099 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3100 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003101
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003102 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003103 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003104 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003105
3106 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003107 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003108
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003109 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3110 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003111
3112 /* Volume: firmware always react to it and reprograms
3113 * the built-in *extra* mixer. Never map it to control
3114 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003115 KEY_RESERVED, /* 0x14: VOLUME UP */
3116 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3117 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003118
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003119 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003120
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003121 /* (assignments unknown, please report if found) */
3122 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3123 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003124 },
3125
3126 /* Generic keymap for Lenovo ThinkPads */
3127 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003128 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3129 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003130 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003131 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003132
3133 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003134 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3135 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3136 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003138 /* These should be enabled --only-- when ACPI video
3139 * is disabled (i.e. in "vendor" mode), and are handled
3140 * in a special way by the init code */
3141 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3142 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003143
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003144 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003145
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003146 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3147 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003148
3149 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3150 * react to it and reprograms the built-in *extra* mixer.
3151 * Never map it to control another mixer by default.
3152 *
3153 * T60?, T61, R60?, R61: firmware and EC tries to send
3154 * these over the regular keyboard, so these are no-ops,
3155 * but there are still weird bugs re. MUTE, so do not
3156 * change unless you get test reports from all Lenovo
3157 * models. May cause the BIOS to interfere with the
3158 * HDA mixer.
3159 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003160 KEY_RESERVED, /* 0x14: VOLUME UP */
3161 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3162 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003163
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003164 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003165
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003166 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003167 KEY_UNKNOWN, KEY_UNKNOWN,
3168
3169 /*
3170 * The mic mute button only sends 0x1a. It does not
3171 * automatically mute the mic or change the mute light.
3172 */
3173 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3174
3175 /* (assignments unknown, please report if found) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003176 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Andy Lutomirski33009552011-05-24 15:16:43 -04003177 KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003178 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003179 };
3180
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003181 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3182 /* Generic maps (fallback) */
3183 {
3184 .vendor = PCI_VENDOR_ID_IBM,
3185 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3186 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3187 },
3188 {
3189 .vendor = PCI_VENDOR_ID_LENOVO,
3190 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3191 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3192 },
3193 };
3194
3195#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003196#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003197
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003198 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003199 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003200 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003203
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003204 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003205 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003206
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3208 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003209
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003210 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003211 BUG_ON(tpacpi_inputdev->open != NULL ||
3212 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003213
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003214 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003215 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003216
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003217#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003218 mutex_init(&hotkey_thread_data_mutex);
3219#endif
3220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003221 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003222 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3225 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003226 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003228 if (!tp_features.hotkey)
3229 return 1;
3230
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003234 tpacpi_disable_brightness_delay();
3235
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3240 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003241 if (!hotkey_dev_attributes)
3242 return -ENOMEM;
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3244 hotkey_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3246 if (res)
3247 goto err_exit;
3248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003254 pr_err("unknown version of the HKEY interface: 0x%x\n",
3255 hkeyv);
3256 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003257 } else {
3258 /*
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3261 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3264 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003265
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3268 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003269 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003270 "please report this to %s\n",
3271 TPACPI_MAIL);
3272 /* Fallback: pre-init for FN+F3,F4,F12 */
3273 hotkey_all_mask = 0x080cU;
3274 } else {
3275 tp_features.hotkey_mask = 1;
3276 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003277 }
3278 }
3279
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003280 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3281 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003282 str_supported(tp_features.hotkey_mask));
3283
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003284 /* Init hotkey_all_mask if not initialized yet */
3285 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3286 (quirks & TPACPI_HK_Q_INIMASK))
3287 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003288
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003289 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003290 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003291 /* hotkey_source_mask *must* be zero for
3292 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003293 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003294 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003295 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003296
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003297 hotkey_orig_mask = hotkey_acpi_mask;
3298 } else {
3299 hotkey_orig_mask = hotkey_all_mask;
3300 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003301 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003302
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003303#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3304 if (dbg_wlswemul) {
3305 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003306 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003307 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003308 } else
3309#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 /* Not all thinkpads have a hardware radio switch */
3311 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3312 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003313 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003314 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003315 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003316 }
3317 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003318 res = add_to_attr_set(hotkey_dev_attributes,
3319 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003320
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 /* For X41t, X60t, X61t Tablets... */
3322 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3323 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003324 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003325 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003326 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003327 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_tablet_mode.attr);
3330 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003331
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003332 if (!res)
3333 res = register_attr_set_with_sysfs(
3334 hotkey_dev_attributes,
3335 &tpacpi_pdev->dev.kobj);
3336 if (res)
3337 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003338
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003339 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003340 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3341 GFP_KERNEL);
3342 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003343 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003344 res = -ENOMEM;
3345 goto err_exit;
3346 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003347
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003348 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3349 ARRAY_SIZE(tpacpi_keymap_qtable));
3350 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3351 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3352 "using keymap number %lu\n", keymap_id);
3353
3354 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3355 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003356
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003357 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3359 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3360 tpacpi_inputdev->keycode = hotkey_keycode_map;
3361 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3362 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003363 input_set_capability(tpacpi_inputdev, EV_KEY,
3364 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003365 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003366 if (i < sizeof(hotkey_reserved_mask)*8)
3367 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003368 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003369 }
3370
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003371 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003372 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003373 input_report_switch(tpacpi_inputdev,
3374 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003375 }
3376 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003377 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003378 input_report_switch(tpacpi_inputdev,
3379 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003380 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003381
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003383 * userspace. tpacpi_detect_brightness_capabilities() must have
3384 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003385 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003386 pr_info("This ThinkPad has standard ACPI backlight "
3387 "brightness control, supported by the ACPI "
3388 "video driver\n");
3389 pr_notice("Disabling thinkpad-acpi brightness events "
3390 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003391
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003392 /* Disable brightness up/down on Lenovo thinkpads when
3393 * ACPI is handling them, otherwise it is plain impossible
3394 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003395 hotkey_reserved_mask |=
3396 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3397 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003398 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3399 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003400 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003401
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003402#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003403 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3404 & ~hotkey_all_mask
3405 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003406
3407 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3408 "hotkey source mask 0x%08x, polling freq %u\n",
3409 hotkey_source_mask, hotkey_poll_freq);
3410#endif
3411
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003412 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3413 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003414 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003415 if (res) {
3416 hotkey_exit();
3417 return res;
3418 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003419 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3420 | hotkey_driver_mask)
3421 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003422 if (res < 0 && res != -ENXIO) {
3423 hotkey_exit();
3424 return res;
3425 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003426 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3427 & ~hotkey_reserved_mask;
3428 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3430 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003431
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003432 tpacpi_inputdev->open = &hotkey_inputdev_open;
3433 tpacpi_inputdev->close = &hotkey_inputdev_close;
3434
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003435 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003436
3437 return 0;
3438
3439err_exit:
3440 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3441 hotkey_dev_attributes = NULL;
3442
3443 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003444}
3445
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003446static bool hotkey_notify_hotkey(const u32 hkey,
3447 bool *send_acpi_ev,
3448 bool *ignore_acpi_ev)
3449{
3450 /* 0x1000-0x1FFF: key presses */
3451 unsigned int scancode = hkey & 0xfff;
3452 *send_acpi_ev = true;
3453 *ignore_acpi_ev = false;
3454
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003455 /* HKEY event 0x1001 is scancode 0x00 */
3456 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003457 scancode--;
3458 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003459 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003460 *send_acpi_ev = false;
3461 } else {
3462 *ignore_acpi_ev = true;
3463 }
3464 return true;
3465 }
3466 return false;
3467}
3468
3469static bool hotkey_notify_wakeup(const u32 hkey,
3470 bool *send_acpi_ev,
3471 bool *ignore_acpi_ev)
3472{
3473 /* 0x2000-0x2FFF: Wakeup reason */
3474 *send_acpi_ev = true;
3475 *ignore_acpi_ev = false;
3476
3477 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003478 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3479 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003480 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3481 *ignore_acpi_ev = true;
3482 break;
3483
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003484 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3485 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003486 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3487 *ignore_acpi_ev = true;
3488 break;
3489
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003490 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3491 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003492 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003493 /* how to auto-heal: */
3494 /* 2313: woke up from S3, go to S4/S5 */
3495 /* 2413: woke up from S4, go to S5 */
3496 break;
3497
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003498 default:
3499 return false;
3500 }
3501
3502 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003503 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003504 hotkey_wakeup_reason_notify_change();
3505 }
3506 return true;
3507}
3508
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003509static bool hotkey_notify_dockevent(const u32 hkey,
3510 bool *send_acpi_ev,
3511 bool *ignore_acpi_ev)
3512{
3513 /* 0x4000-0x4FFF: dock-related events */
3514 *send_acpi_ev = true;
3515 *ignore_acpi_ev = false;
3516
3517 switch (hkey) {
3518 case TP_HKEY_EV_UNDOCK_ACK:
3519 /* ACPI undock operation completed after wakeup */
3520 hotkey_autosleep_ack = 1;
3521 pr_info("undocked\n");
3522 hotkey_wakeup_hotunplug_complete_notify_change();
3523 return true;
3524
3525 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3526 pr_info("docked into hotplug port replicator\n");
3527 return true;
3528 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3529 pr_info("undocked from hotplug port replicator\n");
3530 return true;
3531
3532 default:
3533 return false;
3534 }
3535}
3536
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003537static bool hotkey_notify_usrevent(const u32 hkey,
3538 bool *send_acpi_ev,
3539 bool *ignore_acpi_ev)
3540{
3541 /* 0x5000-0x5FFF: human interface helpers */
3542 *send_acpi_ev = true;
3543 *ignore_acpi_ev = false;
3544
3545 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003546 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3547 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003548 return true;
3549
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003550 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3551 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003552 tpacpi_input_send_tabletsw();
3553 hotkey_tablet_mode_notify_change();
3554 *send_acpi_ev = false;
3555 return true;
3556
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003557 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3558 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3559 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003560 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003561 *ignore_acpi_ev = true;
3562 return true;
3563
3564 default:
3565 return false;
3566 }
3567}
3568
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003569static void thermal_dump_all_sensors(void);
3570
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003571static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003572 bool *send_acpi_ev,
3573 bool *ignore_acpi_ev)
3574{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003575 bool known = true;
3576
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003577 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003578 *send_acpi_ev = true;
3579 *ignore_acpi_ev = false;
3580
3581 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003582 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003583 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003584 /* recommended action: do nothing, we don't have
3585 * Lenovo ATM information */
3586 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003587 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003588 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003589 /* recommended action: warn user through gui */
3590 break;
3591 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003592 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003593 /* recommended action: immediate sleep/hibernate */
3594 break;
3595 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003596 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003597 "a sensor reports something is too hot!\n");
3598 /* recommended action: warn user through gui, that */
3599 /* some internal component is too hot */
3600 break;
3601 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003602 pr_alert("THERMAL EMERGENCY: "
3603 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003604 /* recommended action: immediate sleep/hibernate */
3605 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003606 case TP_HKEY_EV_AC_CHANGED:
3607 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3608 * AC status changed; can be triggered by plugging or
3609 * unplugging AC adapter, docking or undocking. */
3610
3611 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003612
3613 case TP_HKEY_EV_KEY_NUMLOCK:
3614 case TP_HKEY_EV_KEY_FN:
3615 /* key press events, we just ignore them as long as the EC
3616 * is still reporting them in the normal keyboard stream */
3617 *send_acpi_ev = false;
3618 *ignore_acpi_ev = true;
3619 return true;
3620
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003621 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003622 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003623 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003624 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003625
3626 thermal_dump_all_sensors();
3627
3628 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003629}
3630
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003631static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3632{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003633 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003634 bool send_acpi_ev;
3635 bool ignore_acpi_ev;
3636 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003637
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003638 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003639 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003640 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003641 acpi_bus_generate_netlink_event(
3642 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003643 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003644 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003645 return;
3646 }
3647
3648 while (1) {
3649 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003650 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003651 return;
3652 }
3653
3654 if (hkey == 0) {
3655 /* queue empty */
3656 return;
3657 }
3658
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003659 send_acpi_ev = true;
3660 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003661
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003662 switch (hkey >> 12) {
3663 case 1:
3664 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003665 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3666 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003667 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003668 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003669 /* 0x2000-0x2FFF: Wakeup reason */
3670 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3671 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003672 break;
3673 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003674 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003675 switch (hkey) {
3676 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003677 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003678 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003679 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003680 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003681 break;
3682 case TP_HKEY_EV_OPTDRV_EJ:
3683 /* FIXME: kick libata if SATA link offline */
3684 known_ev = true;
3685 break;
3686 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003687 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003688 }
3689 break;
3690 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003691 /* 0x4000-0x4FFF: dock-related events */
3692 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3693 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003694 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003695 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003696 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003697 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3698 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003699 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003700 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003701 /* 0x6000-0x6FFF: thermal alarms/notices and
3702 * keyboard events */
3703 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003704 &ignore_acpi_ev);
3705 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003706 case 7:
3707 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003708 if (tp_features.hotkey_wlsw &&
3709 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003710 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003711 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003712 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003713 break;
3714 }
3715 /* fallthrough to default */
3716 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003717 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003718 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003719 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003720 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3721 pr_notice("please report the conditions when this "
3722 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003723 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003724
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003725 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003726 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003727 acpi_bus_generate_netlink_event(
3728 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003729 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003730 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003731 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003732 }
3733}
3734
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003735static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003736{
3737 /* Do these on suspend, we get the events on early resume! */
3738 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3739 hotkey_autosleep_ack = 0;
3740}
3741
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003742static void hotkey_resume(void)
3743{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003744 tpacpi_disable_brightness_delay();
3745
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003746 if (hotkey_status_set(true) < 0 ||
3747 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003748 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003749 "firmware interface\n");
3750
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003751 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003752 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003753 hotkey_wakeup_reason_notify_change();
3754 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003755 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003756}
3757
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003758/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003759static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003761 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003763 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003764 seq_printf(m, "status:\t\tnot supported\n");
3765 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003766 }
3767
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003768 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003769 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003770 res = hotkey_status_get(&status);
3771 if (!res)
3772 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003773 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003774 if (res)
3775 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003777 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003778 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003779 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3780 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003782 seq_printf(m, "mask:\t\tnot supported\n");
3783 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003789static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003790{
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003793 pr_fmt("hotkey enable/disable functionality has been "
3794 "removed from the driver. "
3795 "Hotkeys are always enabled.\n")))
3796 pr_err("Please remove the hotkey=enable module "
3797 "parameter, it is deprecated. "
3798 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003799}
3800
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003801static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003803 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003804 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003807 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return -ENODEV;
3809
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003810 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003811 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003812
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003813 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003814
3815 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 while ((cmd = next_cmd(&buf))) {
3817 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003818 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003820 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003821 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003823 mask = (hotkey_all_mask | hotkey_source_mask)
3824 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3826 /* mask set */
3827 } else if (sscanf(cmd, "%x", &mask) == 1) {
3828 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003829 } else {
3830 res = -EINVAL;
3831 goto errexit;
3832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003834
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003835 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003836 tpacpi_disclose_usertask("procfs hotkey",
3837 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003838 res = hotkey_user_mask_set(mask);
3839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003841errexit:
3842 mutex_unlock(&hotkey_mutex);
3843 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003844}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003846static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003847 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3848 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003849 {"", 0},
3850};
3851
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003852static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003853 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003854 .notify = hotkey_notify,
3855 .handle = &hkey_handle,
3856 .type = ACPI_DEVICE_NOTIFY,
3857};
3858
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003859static struct ibm_struct hotkey_driver_data = {
3860 .name = "hotkey",
3861 .read = hotkey_read,
3862 .write = hotkey_write,
3863 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003864 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003865 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003866 .acpi = &ibm_hotkey_acpidriver,
3867};
3868
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003869/*************************************************************************
3870 * Bluetooth subdriver
3871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003873enum {
3874 /* ACPI GBDC/SBDC bits */
3875 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3876 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003877 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003878 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003879};
3880
3881enum {
3882 /* ACPI \BLTH commands */
3883 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3884 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3885 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3886 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3887 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003888};
3889
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003890#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3891
Johannes Berg19d337d2009-06-02 13:01:37 +02003892static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003893{
3894 int status;
3895
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003896#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3897 if (dbg_bluetoothemul)
3898 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003899 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003900#endif
3901
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003902 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3903 return -EIO;
3904
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003905 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003906 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003907}
3908
Johannes Berg19d337d2009-06-02 13:01:37 +02003909static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003910{
3911 int status;
3912
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003913 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003914 "will attempt to %s bluetooth\n",
3915 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003916
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003917#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3918 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003919 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003920 return 0;
3921 }
3922#endif
3923
Johannes Berg19d337d2009-06-02 13:01:37 +02003924 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003925 status = TP_ACPI_BLUETOOTH_RADIOSSW
3926 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3927 else
3928 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003929
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003930 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3931 return -EIO;
3932
3933 return 0;
3934}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003935
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003936/* sysfs bluetooth enable ---------------------------------------------- */
3937static ssize_t bluetooth_enable_show(struct device *dev,
3938 struct device_attribute *attr,
3939 char *buf)
3940{
Johannes Berg19d337d2009-06-02 13:01:37 +02003941 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3942 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003943}
3944
3945static ssize_t bluetooth_enable_store(struct device *dev,
3946 struct device_attribute *attr,
3947 const char *buf, size_t count)
3948{
Johannes Berg19d337d2009-06-02 13:01:37 +02003949 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3950 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003951}
3952
3953static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003954 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003955 bluetooth_enable_show, bluetooth_enable_store);
3956
3957/* --------------------------------------------------------------------- */
3958
3959static struct attribute *bluetooth_attributes[] = {
3960 &dev_attr_bluetooth_enable.attr,
3961 NULL
3962};
3963
3964static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003965 .attrs = bluetooth_attributes,
3966};
3967
Johannes Berg19d337d2009-06-02 13:01:37 +02003968static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3969 .get_status = bluetooth_get_status,
3970 .set_status = bluetooth_set_status,
3971};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003972
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003973static void bluetooth_shutdown(void)
3974{
3975 /* Order firmware to save current state to NVRAM */
3976 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3977 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07003978 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003979 else
3980 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02003981 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003982}
3983
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003984static void bluetooth_exit(void)
3985{
3986 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3987 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003988
3989 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3990
3991 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003992}
3993
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003994static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003996 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003997 int status = 0;
3998
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003999 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4000 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004001
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004002 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004004 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4005 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004006 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004007 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004009 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4010 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004011 str_supported(tp_features.bluetooth),
4012 status);
4013
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004014#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4015 if (dbg_bluetoothemul) {
4016 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004017 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004018 } else
4019#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004020 if (tp_features.bluetooth &&
4021 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4022 /* no bluetooth hardware present in system */
4023 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004025 "bluetooth hardware not installed\n");
4026 }
4027
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004028 if (!tp_features.bluetooth)
4029 return 1;
4030
Johannes Berg19d337d2009-06-02 13:01:37 +02004031 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4032 &bluetooth_tprfk_ops,
4033 RFKILL_TYPE_BLUETOOTH,
4034 TPACPI_RFK_BLUETOOTH_SW_NAME,
4035 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004036 if (res)
4037 return res;
4038
Johannes Berg19d337d2009-06-02 13:01:37 +02004039 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4040 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004041 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004042 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004043 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004044 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004045
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004049/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004050static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004052 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004055static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Johannes Berg19d337d2009-06-02 13:01:37 +02004057 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004060static struct ibm_struct bluetooth_driver_data = {
4061 .name = "bluetooth",
4062 .read = bluetooth_read,
4063 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004064 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004065 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004066};
4067
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004068/*************************************************************************
4069 * Wan subdriver
4070 */
4071
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004072enum {
4073 /* ACPI GWAN/SWAN bits */
4074 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4075 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004076 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004077 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004078};
4079
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004080#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4081
Johannes Berg19d337d2009-06-02 13:01:37 +02004082static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004083{
4084 int status;
4085
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004086#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4087 if (dbg_wwanemul)
4088 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004089 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004090#endif
4091
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004092 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4093 return -EIO;
4094
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004095 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004096 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004097}
4098
Johannes Berg19d337d2009-06-02 13:01:37 +02004099static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004100{
4101 int status;
4102
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004103 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004104 "will attempt to %s wwan\n",
4105 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004106
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004107#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4108 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004109 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004110 return 0;
4111 }
4112#endif
4113
Johannes Berg19d337d2009-06-02 13:01:37 +02004114 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004115 status = TP_ACPI_WANCARD_RADIOSSW
4116 | TP_ACPI_WANCARD_RESUMECTRL;
4117 else
4118 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004119
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004120 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4121 return -EIO;
4122
4123 return 0;
4124}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004125
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004126/* sysfs wan enable ---------------------------------------------------- */
4127static ssize_t wan_enable_show(struct device *dev,
4128 struct device_attribute *attr,
4129 char *buf)
4130{
Johannes Berg19d337d2009-06-02 13:01:37 +02004131 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4132 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004133}
4134
4135static ssize_t wan_enable_store(struct device *dev,
4136 struct device_attribute *attr,
4137 const char *buf, size_t count)
4138{
Johannes Berg19d337d2009-06-02 13:01:37 +02004139 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4140 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004141}
4142
4143static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004144 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004145 wan_enable_show, wan_enable_store);
4146
4147/* --------------------------------------------------------------------- */
4148
4149static struct attribute *wan_attributes[] = {
4150 &dev_attr_wan_enable.attr,
4151 NULL
4152};
4153
4154static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004155 .attrs = wan_attributes,
4156};
4157
Johannes Berg19d337d2009-06-02 13:01:37 +02004158static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4159 .get_status = wan_get_status,
4160 .set_status = wan_set_status,
4161};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004162
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004163static void wan_shutdown(void)
4164{
4165 /* Order firmware to save current state to NVRAM */
4166 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4167 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004168 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004169 else
4170 vdbg_printk(TPACPI_DBG_RFKILL,
4171 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004172}
4173
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004174static void wan_exit(void)
4175{
4176 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4177 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004178
4179 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4180
4181 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004182}
4183
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004184static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004185{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004186 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004187 int status = 0;
4188
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004189 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4190 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004191
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004192 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004193
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004194 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004195 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004196
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004197 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4198 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004199 str_supported(tp_features.wan),
4200 status);
4201
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004202#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4203 if (dbg_wwanemul) {
4204 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004205 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004206 } else
4207#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004208 if (tp_features.wan &&
4209 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4210 /* no wan hardware present in system */
4211 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004212 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004213 "wan hardware not installed\n");
4214 }
4215
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004216 if (!tp_features.wan)
4217 return 1;
4218
Johannes Berg19d337d2009-06-02 13:01:37 +02004219 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4220 &wan_tprfk_ops,
4221 RFKILL_TYPE_WWAN,
4222 TPACPI_RFK_WWAN_SW_NAME,
4223 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224 if (res)
4225 return res;
4226
Johannes Berg19d337d2009-06-02 13:01:37 +02004227 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4228 &wan_attr_group);
4229
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004230 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004231 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004232 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004233 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004234
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004235 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004236}
4237
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004238/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004239static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004240{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004241 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004242}
4243
4244static int wan_write(char *buf)
4245{
Johannes Berg19d337d2009-06-02 13:01:37 +02004246 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004247}
4248
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004249static struct ibm_struct wan_driver_data = {
4250 .name = "wan",
4251 .read = wan_read,
4252 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004253 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004254 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004255};
4256
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004257/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004258 * UWB subdriver
4259 */
4260
4261enum {
4262 /* ACPI GUWB/SUWB bits */
4263 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4264 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4265};
4266
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004267#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4268
Johannes Berg19d337d2009-06-02 13:01:37 +02004269static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004270{
4271 int status;
4272
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004273#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4274 if (dbg_uwbemul)
4275 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004276 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004277#endif
4278
4279 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4280 return -EIO;
4281
4282 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004284}
4285
Johannes Berg19d337d2009-06-02 13:01:37 +02004286static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004287{
4288 int status;
4289
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004290 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004291 "will attempt to %s UWB\n",
4292 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004293
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004294#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4295 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004296 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004297 return 0;
4298 }
4299#endif
4300
Johannes Berg19d337d2009-06-02 13:01:37 +02004301 if (state == TPACPI_RFK_RADIO_ON)
4302 status = TP_ACPI_UWB_RADIOSSW;
4303 else
4304 status = 0;
4305
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004306 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4307 return -EIO;
4308
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309 return 0;
4310}
4311
4312/* --------------------------------------------------------------------- */
4313
Johannes Berg19d337d2009-06-02 13:01:37 +02004314static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4315 .get_status = uwb_get_status,
4316 .set_status = uwb_set_status,
4317};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004318
4319static void uwb_exit(void)
4320{
Johannes Berg19d337d2009-06-02 13:01:37 +02004321 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004322}
4323
4324static int __init uwb_init(struct ibm_init_struct *iibm)
4325{
4326 int res;
4327 int status = 0;
4328
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004329 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4330 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004331
4332 TPACPI_ACPIHANDLE_INIT(hkey);
4333
4334 tp_features.uwb = hkey_handle &&
4335 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4336
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004337 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4338 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004339 str_supported(tp_features.uwb),
4340 status);
4341
4342#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4343 if (dbg_uwbemul) {
4344 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004345 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004346 } else
4347#endif
4348 if (tp_features.uwb &&
4349 !(status & TP_ACPI_UWB_HWPRESENT)) {
4350 /* no uwb hardware present in system */
4351 tp_features.uwb = 0;
4352 dbg_printk(TPACPI_DBG_INIT,
4353 "uwb hardware not installed\n");
4354 }
4355
4356 if (!tp_features.uwb)
4357 return 1;
4358
4359 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004360 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004361 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004362 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004363 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004364 return res;
4365}
4366
4367static struct ibm_struct uwb_driver_data = {
4368 .name = "uwb",
4369 .exit = uwb_exit,
4370 .flags.experimental = 1,
4371};
4372
4373/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004374 * Video subdriver
4375 */
4376
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004377#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4378
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004379enum video_access_mode {
4380 TPACPI_VIDEO_NONE = 0,
4381 TPACPI_VIDEO_570, /* 570 */
4382 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4383 TPACPI_VIDEO_NEW, /* all others */
4384};
4385
4386enum { /* video status flags, based on VIDEO_570 */
4387 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4388 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4389 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4390};
4391
4392enum { /* TPACPI_VIDEO_570 constants */
4393 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4394 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4395 * video_status_flags */
4396 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4397 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4398};
4399
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004400static enum video_access_mode video_supported;
4401static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004402
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004403static int video_autosw_get(void);
4404static int video_autosw_set(int enable);
4405
Joe Perches112a6ee2011-04-04 10:06:24 -07004406TPACPI_HANDLE(vid, root,
4407 "\\_SB.PCI.AGP.VGA", /* 570 */
4408 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4409 "\\_SB.PCI0.VID0", /* 770e */
4410 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4411 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4412 "\\_SB.PCI0.AGP.VID", /* all others */
4413 ); /* R30, R31 */
4414
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004415TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004417static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419 int ivga;
4420
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004421 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4422
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004423 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004424 if (tpacpi_is_ibm())
4425 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004426
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004427 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4428 /* G41, assume IVGA doesn't change */
4429 vid_handle = vid2_handle;
4430
4431 if (!vid_handle)
4432 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004433 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004434 else if (tpacpi_is_ibm() &&
4435 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004436 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004437 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004438 else if (tpacpi_is_ibm() &&
4439 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004440 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004441 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004442 else
4443 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004444 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004446 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4447 str_supported(video_supported != TPACPI_VIDEO_NONE),
4448 video_supported);
4449
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004450 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451}
4452
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004453static void video_exit(void)
4454{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004455 dbg_printk(TPACPI_DBG_EXIT,
4456 "restoring original video autoswitch mode\n");
4457 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004458 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004460}
4461
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004462static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 int status = 0;
4465 int i;
4466
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004467 switch (video_supported) {
4468 case TPACPI_VIDEO_570:
4469 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4470 TP_ACPI_VIDEO_570_PHSCMD))
4471 return -EIO;
4472 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4473 break;
4474 case TPACPI_VIDEO_770:
4475 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_LCD;
4479 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_CRT;
4483 break;
4484 case TPACPI_VIDEO_NEW:
4485 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4486 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4487 return -EIO;
4488 if (i)
4489 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004491 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4492 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_LCD;
4496 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4497 return -EIO;
4498 if (i)
4499 status |= TP_ACPI_VIDEO_S_DVI;
4500 break;
4501 default:
4502 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 return status;
4506}
4507
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004508static int video_outputsw_set(int status)
4509{
4510 int autosw;
4511 int res = 0;
4512
4513 switch (video_supported) {
4514 case TPACPI_VIDEO_570:
4515 res = acpi_evalf(NULL, NULL,
4516 "\\_SB.PHS2", "vdd",
4517 TP_ACPI_VIDEO_570_PHS2CMD,
4518 status | TP_ACPI_VIDEO_570_PHS2SET);
4519 break;
4520 case TPACPI_VIDEO_770:
4521 autosw = video_autosw_get();
4522 if (autosw < 0)
4523 return autosw;
4524
4525 res = video_autosw_set(1);
4526 if (res)
4527 return res;
4528 res = acpi_evalf(vid_handle, NULL,
4529 "ASWT", "vdd", status * 0x100, 0);
4530 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004531 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004532 return -EIO;
4533 }
4534 break;
4535 case TPACPI_VIDEO_NEW:
4536 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004537 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004538 break;
4539 default:
4540 return -ENOSYS;
4541 }
4542
4543 return (res)? 0 : -EIO;
4544}
4545
4546static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004548 int autosw = 0;
4549
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004550 switch (video_supported) {
4551 case TPACPI_VIDEO_570:
4552 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4553 return -EIO;
4554 break;
4555 case TPACPI_VIDEO_770:
4556 case TPACPI_VIDEO_NEW:
4557 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4558 return -EIO;
4559 break;
4560 default:
4561 return -ENOSYS;
4562 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563
4564 return autosw & 1;
4565}
4566
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004569 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004571 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572}
4573
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004574static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004576 int autosw = video_autosw_get();
4577 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004578
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004579 if (autosw < 0)
4580 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004581
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004582 switch (video_supported) {
4583 case TPACPI_VIDEO_570:
4584 res = video_autosw_set(1);
4585 if (res)
4586 return res;
4587 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4588 break;
4589 case TPACPI_VIDEO_770:
4590 case TPACPI_VIDEO_NEW:
4591 res = video_autosw_set(1);
4592 if (res)
4593 return res;
4594 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4595 break;
4596 default:
4597 return -ENOSYS;
4598 }
4599 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004600 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004601 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004602 }
4603
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004604 return (res)? 0 : -EIO;
4605}
4606
4607static int video_expand_toggle(void)
4608{
4609 switch (video_supported) {
4610 case TPACPI_VIDEO_570:
4611 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4612 0 : -EIO;
4613 case TPACPI_VIDEO_770:
4614 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4615 0 : -EIO;
4616 case TPACPI_VIDEO_NEW:
4617 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4618 0 : -EIO;
4619 default:
4620 return -ENOSYS;
4621 }
4622 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004623}
4624
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004625static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004627 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004629 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004630 seq_printf(m, "status:\t\tnot supported\n");
4631 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632 }
4633
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004634 /* Even reads can crash X.org, so... */
4635 if (!capable(CAP_SYS_ADMIN))
4636 return -EPERM;
4637
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004638 status = video_outputsw_get();
4639 if (status < 0)
4640 return status;
4641
4642 autosw = video_autosw_get();
4643 if (autosw < 0)
4644 return autosw;
4645
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004646 seq_printf(m, "status:\t\tsupported\n");
4647 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4648 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004649 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004650 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4651 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4652 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4653 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004654 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004655 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4656 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4657 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004658
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004659 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004660}
4661
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004662static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 char *cmd;
4665 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004668 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669 return -ENODEV;
4670
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004671 /* Even reads can crash X.org, let alone writes... */
4672 if (!capable(CAP_SYS_ADMIN))
4673 return -EPERM;
4674
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 enable = 0;
4676 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 while ((cmd = next_cmd(&buf))) {
4679 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004682 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004687 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004688 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004689 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004690 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004691 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004692 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004694 res = video_autosw_set(1);
4695 if (res)
4696 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004698 res = video_autosw_set(0);
4699 if (res)
4700 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004702 res = video_outputsw_cycle();
4703 if (res)
4704 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004706 res = video_expand_toggle();
4707 if (res)
4708 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 } else
4710 return -EINVAL;
4711 }
4712
4713 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004714 status = video_outputsw_get();
4715 if (status < 0)
4716 return status;
4717 res = video_outputsw_set((status & ~disable) | enable);
4718 if (res)
4719 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
4721
4722 return 0;
4723}
4724
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004725static struct ibm_struct video_driver_data = {
4726 .name = "video",
4727 .read = video_read,
4728 .write = video_write,
4729 .exit = video_exit,
4730};
4731
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004732#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004734/*************************************************************************
4735 * Light (thinklight) subdriver
4736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004738TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4739TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004740
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004741static int light_get_status(void)
4742{
4743 int status = 0;
4744
4745 if (tp_features.light_status) {
4746 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4747 return -EIO;
4748 return (!!status);
4749 }
4750
4751 return -ENXIO;
4752}
4753
4754static int light_set_status(int status)
4755{
4756 int rc;
4757
4758 if (tp_features.light) {
4759 if (cmos_handle) {
4760 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4761 (status)?
4762 TP_CMOS_THINKLIGHT_ON :
4763 TP_CMOS_THINKLIGHT_OFF);
4764 } else {
4765 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4766 (status)? 1 : 0);
4767 }
4768 return (rc)? 0 : -EIO;
4769 }
4770
4771 return -ENXIO;
4772}
4773
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004774static void light_set_status_worker(struct work_struct *work)
4775{
4776 struct tpacpi_led_classdev *data =
4777 container_of(work, struct tpacpi_led_classdev, work);
4778
4779 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004780 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004781}
4782
4783static void light_sysfs_set(struct led_classdev *led_cdev,
4784 enum led_brightness brightness)
4785{
4786 struct tpacpi_led_classdev *data =
4787 container_of(led_cdev,
4788 struct tpacpi_led_classdev,
4789 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004790 data->new_state = (brightness != LED_OFF) ?
4791 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004792 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004793}
4794
4795static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4796{
4797 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4798}
4799
4800static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4801 .led_classdev = {
4802 .name = "tpacpi::thinklight",
4803 .brightness_set = &light_sysfs_set,
4804 .brightness_get = &light_sysfs_get,
4805 }
4806};
4807
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004808static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004810 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004811
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004812 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4813
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004814 if (tpacpi_is_ibm()) {
4815 TPACPI_ACPIHANDLE_INIT(ledb);
4816 TPACPI_ACPIHANDLE_INIT(lght);
4817 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004818 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004819 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004820
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004821 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004822 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004823
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004824 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004825 /* light status not supported on
4826 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004827 tp_features.light_status =
4828 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004830 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4831 str_supported(tp_features.light),
4832 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004833
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004834 if (!tp_features.light)
4835 return 1;
4836
4837 rc = led_classdev_register(&tpacpi_pdev->dev,
4838 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004839
4840 if (rc < 0) {
4841 tp_features.light = 0;
4842 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004843 } else {
4844 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004845 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004846
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004847 return rc;
4848}
4849
4850static void light_exit(void)
4851{
4852 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08004853 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004856static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004858 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004860 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004861 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004862 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004863 seq_printf(m, "status:\t\tunknown\n");
4864 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004865 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004866 status = light_get_status();
4867 if (status < 0)
4868 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004869 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4870 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004876static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004879 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004880
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004881 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004882 return -ENODEV;
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 while ((cmd = next_cmd(&buf))) {
4885 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004886 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004888 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 } else
4890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004893 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004896static struct ibm_struct light_driver_data = {
4897 .name = "light",
4898 .read = light_read,
4899 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004900 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004901};
4902
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004903/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004904 * CMOS subdriver
4905 */
4906
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004907/* sysfs cmos_command -------------------------------------------------- */
4908static ssize_t cmos_command_store(struct device *dev,
4909 struct device_attribute *attr,
4910 const char *buf, size_t count)
4911{
4912 unsigned long cmos_cmd;
4913 int res;
4914
4915 if (parse_strtoul(buf, 21, &cmos_cmd))
4916 return -EINVAL;
4917
4918 res = issue_thinkpad_cmos_command(cmos_cmd);
4919 return (res)? res : count;
4920}
4921
4922static struct device_attribute dev_attr_cmos_command =
4923 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4924
4925/* --------------------------------------------------------------------- */
4926
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004927static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004928{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004929 int res;
4930
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004931 vdbg_printk(TPACPI_DBG_INIT,
4932 "initializing cmos commands subdriver\n");
4933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004934 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004935
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004936 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4937 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004938
4939 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940 if (res)
4941 return res;
4942
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004943 return (cmos_handle)? 0 : 1;
4944}
4945
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004946static void cmos_exit(void)
4947{
4948 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4949}
4950
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004951static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4954 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004956 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004958 seq_printf(m, "status:\t\tsupported\n");
4959 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963}
4964
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004965static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
4967 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004968 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004971 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4972 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 /* cmos_cmd set */
4974 } else
4975 return -EINVAL;
4976
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004977 res = issue_thinkpad_cmos_command(cmos_cmd);
4978 if (res)
4979 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981
4982 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004983}
4984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004985static struct ibm_struct cmos_driver_data = {
4986 .name = "cmos",
4987 .read = cmos_read,
4988 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004989 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004990};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004991
4992/*************************************************************************
4993 * LED subdriver
4994 */
4995
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004996enum led_access_mode {
4997 TPACPI_LED_NONE = 0,
4998 TPACPI_LED_570, /* 570 */
4999 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5000 TPACPI_LED_NEW, /* all others */
5001};
5002
5003enum { /* For TPACPI_LED_OLD */
5004 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5005 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5006 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5007};
5008
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005009static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005010
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005011static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005012
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005013#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005014static struct tpacpi_led_classdev *tpacpi_leds;
5015static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005016static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005017 /* there's a limit of 19 chars + NULL before 2.6.26 */
5018 "tpacpi::power",
5019 "tpacpi:orange:batt",
5020 "tpacpi:green:batt",
5021 "tpacpi::dock_active",
5022 "tpacpi::bay_active",
5023 "tpacpi::dock_batt",
5024 "tpacpi::unknown_led",
5025 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005026 "tpacpi::dock_status1",
5027 "tpacpi::dock_status2",
5028 "tpacpi::unknown_led2",
5029 "tpacpi::unknown_led3",
5030 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005031};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005032#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005033
5034static inline bool tpacpi_is_led_restricted(const unsigned int led)
5035{
5036#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5037 return false;
5038#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005039 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005040#endif
5041}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005042
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005043static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005044{
5045 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005047
5048 switch (led_supported) {
5049 case TPACPI_LED_570:
5050 if (!acpi_evalf(ec_handle,
5051 &status, "GLED", "dd", 1 << led))
5052 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005053 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005054 TPACPI_LED_OFF :
5055 ((status == 1)?
5056 TPACPI_LED_ON :
5057 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005058 tpacpi_led_state_cache[led] = led_s;
5059 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005060 default:
5061 return -ENXIO;
5062 }
5063
5064 /* not reached */
5065}
5066
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005067static int led_set_status(const unsigned int led,
5068 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005069{
5070 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5072 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005073
5074 int rc = 0;
5075
5076 switch (led_supported) {
5077 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005078 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005079 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005080 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005081 if (unlikely(tpacpi_is_led_restricted(led)))
5082 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005083 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5084 (1 << led), led_sled_arg1[ledstatus]))
5085 rc = -EIO;
5086 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005087 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005088 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005089 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005090 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005091 if (unlikely(tpacpi_is_led_restricted(led)))
5092 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005093 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5094 if (rc >= 0)
5095 rc = ec_write(TPACPI_LED_EC_HLBL,
5096 (ledstatus == TPACPI_LED_BLINK) << led);
5097 if (rc >= 0)
5098 rc = ec_write(TPACPI_LED_EC_HLCL,
5099 (ledstatus != TPACPI_LED_OFF) << led);
5100 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005101 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005102 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005103 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5104 return -EINVAL;
5105 if (unlikely(tpacpi_is_led_restricted(led)))
5106 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005107 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5108 led, led_led_arg1[ledstatus]))
5109 rc = -EIO;
5110 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005111 default:
5112 rc = -ENXIO;
5113 }
5114
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005115 if (!rc)
5116 tpacpi_led_state_cache[led] = ledstatus;
5117
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005118 return rc;
5119}
5120
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005121static void led_set_status_worker(struct work_struct *work)
5122{
5123 struct tpacpi_led_classdev *data =
5124 container_of(work, struct tpacpi_led_classdev, work);
5125
5126 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005127 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005128}
5129
5130static void led_sysfs_set(struct led_classdev *led_cdev,
5131 enum led_brightness brightness)
5132{
5133 struct tpacpi_led_classdev *data = container_of(led_cdev,
5134 struct tpacpi_led_classdev, led_classdev);
5135
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005136 if (brightness == LED_OFF)
5137 data->new_state = TPACPI_LED_OFF;
5138 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5139 data->new_state = TPACPI_LED_ON;
5140 else
5141 data->new_state = TPACPI_LED_BLINK;
5142
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005143 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005144}
5145
5146static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5147 unsigned long *delay_on, unsigned long *delay_off)
5148{
5149 struct tpacpi_led_classdev *data = container_of(led_cdev,
5150 struct tpacpi_led_classdev, led_classdev);
5151
5152 /* Can we choose the flash rate? */
5153 if (*delay_on == 0 && *delay_off == 0) {
5154 /* yes. set them to the hardware blink rate (1 Hz) */
5155 *delay_on = 500; /* ms */
5156 *delay_off = 500; /* ms */
5157 } else if ((*delay_on != 500) || (*delay_off != 500))
5158 return -EINVAL;
5159
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005160 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005161 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005162
5163 return 0;
5164}
5165
5166static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5167{
5168 int rc;
5169
5170 struct tpacpi_led_classdev *data = container_of(led_cdev,
5171 struct tpacpi_led_classdev, led_classdev);
5172
5173 rc = led_get_status(data->led);
5174
5175 if (rc == TPACPI_LED_OFF || rc < 0)
5176 rc = LED_OFF; /* no error handling in led class :( */
5177 else
5178 rc = LED_FULL;
5179
5180 return rc;
5181}
5182
5183static void led_exit(void)
5184{
5185 unsigned int i;
5186
5187 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5188 if (tpacpi_leds[i].led_classdev.name)
5189 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5190 }
5191
Li Dongyange03e3892012-07-25 10:45:07 +10005192 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005193 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005194}
5195
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005196static int __init tpacpi_init_led(unsigned int led)
5197{
5198 int rc;
5199
5200 tpacpi_leds[led].led = led;
5201
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005202 /* LEDs with no name don't get registered */
5203 if (!tpacpi_led_names[led])
5204 return 0;
5205
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005206 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5207 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5208 if (led_supported == TPACPI_LED_570)
5209 tpacpi_leds[led].led_classdev.brightness_get =
5210 &led_sysfs_get;
5211
5212 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5213
5214 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5215
5216 rc = led_classdev_register(&tpacpi_pdev->dev,
5217 &tpacpi_leds[led].led_classdev);
5218 if (rc < 0)
5219 tpacpi_leds[led].led_classdev.name = NULL;
5220
5221 return rc;
5222}
5223
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005224static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5225 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5226 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5227 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5228
5229 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5230 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5231 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5232 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5233 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5234 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5235 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5236 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5237
5238 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5239 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5240 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5241 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5242 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5243
5244 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5245 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5246 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5247 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5248
5249 /* (1) - may have excess leds enabled on MSB */
5250
5251 /* Defaults (order matters, keep last, don't reorder!) */
5252 { /* Lenovo */
5253 .vendor = PCI_VENDOR_ID_LENOVO,
5254 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5255 .quirks = 0x1fffU,
5256 },
5257 { /* IBM ThinkPads with no EC version string */
5258 .vendor = PCI_VENDOR_ID_IBM,
5259 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5260 .quirks = 0x00ffU,
5261 },
5262 { /* IBM ThinkPads with EC version string */
5263 .vendor = PCI_VENDOR_ID_IBM,
5264 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5265 .quirks = 0x00bfU,
5266 },
5267};
5268
5269#undef TPACPI_LEDQ_IBM
5270#undef TPACPI_LEDQ_LNV
5271
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005272static enum led_access_mode __init led_init_detect_mode(void)
5273{
5274 acpi_status status;
5275
5276 if (tpacpi_is_ibm()) {
5277 /* 570 */
5278 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5279 if (ACPI_SUCCESS(status))
5280 return TPACPI_LED_570;
5281
5282 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5283 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5284 if (ACPI_SUCCESS(status))
5285 return TPACPI_LED_OLD;
5286 }
5287
5288 /* most others */
5289 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5290 if (ACPI_SUCCESS(status))
5291 return TPACPI_LED_NEW;
5292
5293 /* R30, R31, and unknown firmwares */
5294 led_handle = NULL;
5295 return TPACPI_LED_NONE;
5296}
5297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005298static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005299{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005300 unsigned int i;
5301 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005302 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005303
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005304 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5305
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005306 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005307
Adam Leefcb44e12013-06-07 16:20:08 +08005308 if (led_supported != TPACPI_LED_NONE) {
5309 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5310 ARRAY_SIZE(led_useful_qtable));
5311
5312 if (!useful_leds) {
5313 led_handle = NULL;
5314 led_supported = TPACPI_LED_NONE;
5315 }
5316 }
5317
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005318 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5319 str_supported(led_supported), led_supported);
5320
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005321 if (led_supported == TPACPI_LED_NONE)
5322 return 1;
5323
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005324 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5325 GFP_KERNEL);
5326 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005327 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005328 return -ENOMEM;
5329 }
5330
5331 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005332 tpacpi_leds[i].led = -1;
5333
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005334 if (!tpacpi_is_led_restricted(i) &&
5335 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005336 rc = tpacpi_init_led(i);
5337 if (rc < 0) {
5338 led_exit();
5339 return rc;
5340 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005341 }
5342 }
5343
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005344#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005345 pr_notice("warning: userspace override of important "
5346 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005347#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005348 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349}
5350
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005351#define str_led_status(s) \
5352 ((s) == TPACPI_LED_OFF ? "off" : \
5353 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005355static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005357 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005358 seq_printf(m, "status:\t\tnot supported\n");
5359 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005360 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005361 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005362
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005363 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005364 /* 570 */
5365 int i, status;
5366 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005367 status = led_get_status(i);
5368 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005369 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005370 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005371 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005372 }
5373 }
5374
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005375 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005376 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005381static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
5383 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005384 int led, rc;
5385 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005386
5387 if (!led_supported)
5388 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005391 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 return -EINVAL;
5393
Adam Leeedf2d772013-06-08 16:51:15 +08005394 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5395 tpacpi_leds[led].led < 0)
5396 return -ENODEV;
5397
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005398 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005399 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005401 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005402 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005403 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005404 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005405 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005406 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005407
5408 rc = led_set_status(led, s);
5409 if (rc < 0)
5410 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412
5413 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005414}
5415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005416static struct ibm_struct led_driver_data = {
5417 .name = "led",
5418 .read = led_read,
5419 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005420 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005421};
5422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005423/*************************************************************************
5424 * Beep subdriver
5425 */
5426
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005427TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005428
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005429#define TPACPI_BEEP_Q1 0x0001
5430
5431static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5432 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5433 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5434};
5435
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005436static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005437{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005438 unsigned long quirks;
5439
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005440 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5441
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005442 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005443
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005444 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5445 str_supported(beep_handle != NULL));
5446
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005447 quirks = tpacpi_check_quirks(beep_quirk_table,
5448 ARRAY_SIZE(beep_quirk_table));
5449
5450 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5451
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005452 return (beep_handle)? 0 : 1;
5453}
5454
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005455static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005457 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005458 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005459 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005460 seq_printf(m, "status:\t\tsupported\n");
5461 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465}
5466
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005467static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
5469 char *cmd;
5470 int beep_cmd;
5471
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005472 if (!beep_handle)
5473 return -ENODEV;
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005476 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5477 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 /* beep_cmd set */
5479 } else
5480 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005481 if (tp_features.beep_needs_two_args) {
5482 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5483 beep_cmd, 0))
5484 return -EIO;
5485 } else {
5486 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5487 beep_cmd))
5488 return -EIO;
5489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 }
5491
5492 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005493}
5494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005495static struct ibm_struct beep_driver_data = {
5496 .name = "beep",
5497 .read = beep_read,
5498 .write = beep_write,
5499};
5500
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005501/*************************************************************************
5502 * Thermal subdriver
5503 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005504
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005505enum thermal_access_mode {
5506 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5507 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5508 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5509 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5510 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5511};
5512
5513enum { /* TPACPI_THERMAL_TPEC_* */
5514 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5515 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5516 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005517
5518 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005519};
5520
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005521
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005522#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5523struct ibm_thermal_sensors_struct {
5524 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5525};
5526
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005527static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005528
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005529/* idx is zero-based */
5530static int thermal_get_sensor(int idx, s32 *value)
5531{
5532 int t;
5533 s8 tmp;
5534 char tmpi[5];
5535
5536 t = TP_EC_THERMAL_TMP0;
5537
5538 switch (thermal_read_mode) {
5539#if TPACPI_MAX_THERMAL_SENSORS >= 16
5540 case TPACPI_THERMAL_TPEC_16:
5541 if (idx >= 8 && idx <= 15) {
5542 t = TP_EC_THERMAL_TMP8;
5543 idx -= 8;
5544 }
5545 /* fallthrough */
5546#endif
5547 case TPACPI_THERMAL_TPEC_8:
5548 if (idx <= 7) {
5549 if (!acpi_ec_read(t + idx, &tmp))
5550 return -EIO;
5551 *value = tmp * 1000;
5552 return 0;
5553 }
5554 break;
5555
5556 case TPACPI_THERMAL_ACPI_UPDT:
5557 if (idx <= 7) {
5558 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5559 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5560 return -EIO;
5561 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5562 return -EIO;
5563 *value = (t - 2732) * 100;
5564 return 0;
5565 }
5566 break;
5567
5568 case TPACPI_THERMAL_ACPI_TMP07:
5569 if (idx <= 7) {
5570 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5571 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5572 return -EIO;
5573 if (t > 127 || t < -127)
5574 t = TP_EC_THERMAL_TMP_NA;
5575 *value = t * 1000;
5576 return 0;
5577 }
5578 break;
5579
5580 case TPACPI_THERMAL_NONE:
5581 default:
5582 return -ENOSYS;
5583 }
5584
5585 return -EINVAL;
5586}
5587
5588static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5589{
5590 int res, i;
5591 int n;
5592
5593 n = 8;
5594 i = 0;
5595
5596 if (!s)
5597 return -EINVAL;
5598
5599 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5600 n = 16;
5601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005602 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005603 res = thermal_get_sensor(i, &s->temp[i]);
5604 if (res)
5605 return res;
5606 }
5607
5608 return n;
5609}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005610
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005611static void thermal_dump_all_sensors(void)
5612{
5613 int n, i;
5614 struct ibm_thermal_sensors_struct t;
5615
5616 n = thermal_get_sensors(&t);
5617 if (n <= 0)
5618 return;
5619
Joe Perches0978e012011-04-04 10:06:25 -07005620 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005621
5622 for (i = 0; i < n; i++) {
5623 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005624 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005625 else
Joe Perches0978e012011-04-04 10:06:25 -07005626 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005627 }
5628
Joe Perches0978e012011-04-04 10:06:25 -07005629 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005630}
5631
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005632/* sysfs temp##_input -------------------------------------------------- */
5633
5634static ssize_t thermal_temp_input_show(struct device *dev,
5635 struct device_attribute *attr,
5636 char *buf)
5637{
5638 struct sensor_device_attribute *sensor_attr =
5639 to_sensor_dev_attr(attr);
5640 int idx = sensor_attr->index;
5641 s32 value;
5642 int res;
5643
5644 res = thermal_get_sensor(idx, &value);
5645 if (res)
5646 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005647 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005648 return -ENXIO;
5649
5650 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5651}
5652
5653#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005654 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5655 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005656
5657static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5658 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5659 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5660 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5661 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5662 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5663 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5664 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5665 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5666 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5667 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5668 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5669 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5670 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5671 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5672 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5673 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5674};
5675
5676#define THERMAL_ATTRS(X) \
5677 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5678
5679static struct attribute *thermal_temp_input_attr[] = {
5680 THERMAL_ATTRS(8),
5681 THERMAL_ATTRS(9),
5682 THERMAL_ATTRS(10),
5683 THERMAL_ATTRS(11),
5684 THERMAL_ATTRS(12),
5685 THERMAL_ATTRS(13),
5686 THERMAL_ATTRS(14),
5687 THERMAL_ATTRS(15),
5688 THERMAL_ATTRS(0),
5689 THERMAL_ATTRS(1),
5690 THERMAL_ATTRS(2),
5691 THERMAL_ATTRS(3),
5692 THERMAL_ATTRS(4),
5693 THERMAL_ATTRS(5),
5694 THERMAL_ATTRS(6),
5695 THERMAL_ATTRS(7),
5696 NULL
5697};
5698
5699static const struct attribute_group thermal_temp_input16_group = {
5700 .attrs = thermal_temp_input_attr
5701};
5702
5703static const struct attribute_group thermal_temp_input8_group = {
5704 .attrs = &thermal_temp_input_attr[8]
5705};
5706
5707#undef THERMAL_SENSOR_ATTR_TEMP
5708#undef THERMAL_ATTRS
5709
5710/* --------------------------------------------------------------------- */
5711
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005712static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005713{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005714 u8 t, ta1, ta2;
5715 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005716 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005717 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005718
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005719 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5720
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005721 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005722
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005723 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005724 /*
5725 * Direct EC access mode: sensors at registers
5726 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5727 * non-implemented, thermal sensors return 0x80 when
5728 * not available
5729 */
5730
5731 ta1 = ta2 = 0;
5732 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005733 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005734 ta1 |= t;
5735 } else {
5736 ta1 = 0;
5737 break;
5738 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005739 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005740 ta2 |= t;
5741 } else {
5742 ta1 = 0;
5743 break;
5744 }
5745 }
5746 if (ta1 == 0) {
5747 /* This is sheer paranoia, but we handle it anyway */
5748 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005749 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005750 "falling back to ACPI TMPx access "
5751 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005752 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005753 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005754 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005755 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005756 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005757 }
5758 } else {
5759 thermal_read_mode =
5760 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005761 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005762 }
5763 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005764 if (tpacpi_is_ibm() &&
5765 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005766 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005767 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005768 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005769 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005770 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005771 }
5772 } else {
5773 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005774 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005775 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005776
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005777 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5778 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5779 thermal_read_mode);
5780
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005781 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005782 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005783 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005784 &thermal_temp_input16_group);
5785 if (res)
5786 return res;
5787 break;
5788 case TPACPI_THERMAL_TPEC_8:
5789 case TPACPI_THERMAL_ACPI_TMP07:
5790 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005791 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005792 &thermal_temp_input8_group);
5793 if (res)
5794 return res;
5795 break;
5796 case TPACPI_THERMAL_NONE:
5797 default:
5798 return 1;
5799 }
5800
5801 return 0;
5802}
5803
5804static void thermal_exit(void)
5805{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005806 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005807 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005808 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005809 &thermal_temp_input16_group);
5810 break;
5811 case TPACPI_THERMAL_TPEC_8:
5812 case TPACPI_THERMAL_ACPI_TMP07:
5813 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005814 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005815 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005816 break;
5817 case TPACPI_THERMAL_NONE:
5818 default:
5819 break;
5820 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005821}
5822
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005823static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005824{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005825 int n, i;
5826 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005827
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005828 n = thermal_get_sensors(&t);
5829 if (unlikely(n < 0))
5830 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005831
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005832 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005833
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005834 if (n > 0) {
5835 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005836 seq_printf(m, "%d ", t.temp[i] / 1000);
5837 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005838 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005839 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005840
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005841 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005842}
5843
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005844static struct ibm_struct thermal_driver_data = {
5845 .name = "thermal",
5846 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005847 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005848};
5849
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005850/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005851 * Backlight/brightness subdriver
5852 */
Holger Macht8acb0252006-10-20 14:30:28 -07005853
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005854#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5855
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005856/*
5857 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5858 * CMOS NVRAM byte 0x5E, bits 0-3.
5859 *
5860 * EC HBRV (0x31) has the following layout
5861 * Bit 7: unknown function
5862 * Bit 6: unknown function
5863 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5864 * Bit 4: must be set to zero to avoid problems
5865 * Bit 3-0: backlight brightness level
5866 *
5867 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005868 *
5869 * WARNING: The X61 has been verified to use HBRV for something else, so
5870 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5871 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005872 */
5873
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005874enum {
5875 TP_EC_BACKLIGHT = 0x31,
5876
5877 /* TP_EC_BACKLIGHT bitmasks */
5878 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5879 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5880 TP_EC_BACKLIGHT_MAPSW = 0x20,
5881};
5882
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005883enum tpacpi_brightness_access_mode {
5884 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5885 TPACPI_BRGHT_MODE_EC, /* EC control */
5886 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5887 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5888 TPACPI_BRGHT_MODE_MAX
5889};
5890
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005891static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005892
5893static enum tpacpi_brightness_access_mode brightness_mode =
5894 TPACPI_BRGHT_MODE_MAX;
5895
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005896static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5897
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005898static struct mutex brightness_mutex;
5899
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005900/* NVRAM brightness access,
5901 * call with brightness_mutex held! */
5902static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005903{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005904 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005905
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005906 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5907 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5908 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005909 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005910
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005911 return lnvram;
5912}
5913
5914static void tpacpi_brightness_checkpoint_nvram(void)
5915{
5916 u8 lec = 0;
5917 u8 b_nvram;
5918
5919 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5920 return;
5921
5922 vdbg_printk(TPACPI_DBG_BRGHT,
5923 "trying to checkpoint backlight level to NVRAM...\n");
5924
5925 if (mutex_lock_killable(&brightness_mutex) < 0)
5926 return;
5927
5928 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5929 goto unlock;
5930 lec &= TP_EC_BACKLIGHT_LVLMSK;
5931 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5932
5933 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5934 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5935 /* NVRAM needs update */
5936 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5937 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5938 b_nvram |= lec;
5939 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5940 dbg_printk(TPACPI_DBG_BRGHT,
5941 "updated NVRAM backlight level to %u (0x%02x)\n",
5942 (unsigned int) lec, (unsigned int) b_nvram);
5943 } else
5944 vdbg_printk(TPACPI_DBG_BRGHT,
5945 "NVRAM backlight level already is %u (0x%02x)\n",
5946 (unsigned int) lec, (unsigned int) b_nvram);
5947
5948unlock:
5949 mutex_unlock(&brightness_mutex);
5950}
5951
5952
5953/* call with brightness_mutex held! */
5954static int tpacpi_brightness_get_raw(int *status)
5955{
5956 u8 lec = 0;
5957
5958 switch (brightness_mode) {
5959 case TPACPI_BRGHT_MODE_UCMS_STEP:
5960 *status = tpacpi_brightness_nvram_get();
5961 return 0;
5962 case TPACPI_BRGHT_MODE_EC:
5963 case TPACPI_BRGHT_MODE_ECNVRAM:
5964 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005965 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005966 *status = lec;
5967 return 0;
5968 default:
5969 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005970 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005971}
5972
5973/* call with brightness_mutex held! */
5974/* do NOT call with illegal backlight level value */
5975static int tpacpi_brightness_set_ec(unsigned int value)
5976{
5977 u8 lec = 0;
5978
5979 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5980 return -EIO;
5981
5982 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5983 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5984 (value & TP_EC_BACKLIGHT_LVLMSK))))
5985 return -EIO;
5986
5987 return 0;
5988}
5989
5990/* call with brightness_mutex held! */
5991static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5992{
5993 int cmos_cmd, inc;
5994 unsigned int current_value, i;
5995
5996 current_value = tpacpi_brightness_nvram_get();
5997
5998 if (value == current_value)
5999 return 0;
6000
6001 cmos_cmd = (value > current_value) ?
6002 TP_CMOS_BRIGHTNESS_UP :
6003 TP_CMOS_BRIGHTNESS_DOWN;
6004 inc = (value > current_value) ? 1 : -1;
6005
6006 for (i = current_value; i != value; i += inc)
6007 if (issue_thinkpad_cmos_command(cmos_cmd))
6008 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006009
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006010 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006011}
6012
6013/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006014static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006015{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006016 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006017
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006018 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006019 return -EINVAL;
6020
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006021 vdbg_printk(TPACPI_DBG_BRGHT,
6022 "set backlight level to %d\n", value);
6023
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006024 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006025 if (res < 0)
6026 return res;
6027
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006028 switch (brightness_mode) {
6029 case TPACPI_BRGHT_MODE_EC:
6030 case TPACPI_BRGHT_MODE_ECNVRAM:
6031 res = tpacpi_brightness_set_ec(value);
6032 break;
6033 case TPACPI_BRGHT_MODE_UCMS_STEP:
6034 res = tpacpi_brightness_set_ucmsstep(value);
6035 break;
6036 default:
6037 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006038 }
6039
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006040 mutex_unlock(&brightness_mutex);
6041 return res;
6042}
6043
6044/* sysfs backlight class ----------------------------------------------- */
6045
6046static int brightness_update_status(struct backlight_device *bd)
6047{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006048 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006049 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6050 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006051 bd->props.brightness : 0;
6052
6053 dbg_printk(TPACPI_DBG_BRGHT,
6054 "backlight: attempt to set level to %d\n",
6055 level);
6056
6057 /* it is the backlight class's job (caller) to handle
6058 * EINTR and other errors properly */
6059 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006060}
Holger Macht8acb0252006-10-20 14:30:28 -07006061
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006062static int brightness_get(struct backlight_device *bd)
6063{
6064 int status, res;
6065
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006066 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006067 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006068 return 0;
6069
6070 res = tpacpi_brightness_get_raw(&status);
6071
6072 mutex_unlock(&brightness_mutex);
6073
6074 if (res < 0)
6075 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006076
6077 return status & TP_EC_BACKLIGHT_LVLMSK;
6078}
6079
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006080static void tpacpi_brightness_notify_change(void)
6081{
6082 backlight_force_update(ibm_backlight_device,
6083 BACKLIGHT_UPDATE_HOTKEY);
6084}
6085
Lionel Debrouxacc24722010-11-16 14:14:02 +01006086static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006087 .get_brightness = brightness_get,
6088 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006089};
6090
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006091/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006092
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006093/*
6094 * Call _BCL method of video device. On some ThinkPads this will
6095 * switch the firmware to the ACPI brightness control mode.
6096 */
6097
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006098static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6099{
6100 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6101 union acpi_object *obj;
Aaron Lu46445b62013-10-11 21:27:46 +08006102 struct acpi_device *device, *child;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006103 int rc;
6104
Aaron Lu46445b62013-10-11 21:27:46 +08006105 if (acpi_bus_get_device(handle, &device))
6106 return 0;
6107
6108 rc = 0;
6109 list_for_each_entry(child, &device->children, node) {
6110 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6111 NULL, &buffer);
6112 if (ACPI_FAILURE(status))
6113 continue;
6114
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006115 obj = (union acpi_object *)buffer.pointer;
6116 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006117 pr_err("Unknown _BCL data, please report this to %s\n",
Aaron Lu46445b62013-10-11 21:27:46 +08006118 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006119 rc = 0;
6120 } else {
6121 rc = obj->package.count;
6122 }
Aaron Lu46445b62013-10-11 21:27:46 +08006123 break;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006124 }
6125
6126 kfree(buffer.pointer);
6127 return rc;
6128}
6129
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006130
6131/*
6132 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6133 */
6134static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6135{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006136 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006137 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006138
Aaron Lu46445b62013-10-11 21:27:46 +08006139 tpacpi_acpi_handle_locate("video", NULL, &video_device);
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006140 if (video_device)
6141 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006142
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006143 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006144
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006145 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006146}
6147
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006148/*
6149 * These are only useful for models that have only one possibility
6150 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6151 * these quirks.
6152 */
6153#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6154#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6155#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6156
6157static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6158 /* Models with ATI GPUs known to require ECNVRAM mode */
6159 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6160
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006161 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006162 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006163 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006164 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006165 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6166
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006167 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006168 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006169 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6170 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006171
6172 /* Models with Intel GMA900 */
6173 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6174 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6175 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6176};
6177
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006178/*
6179 * Returns < 0 for error, otherwise sets tp_features.bright_*
6180 * and bright_maxlvl.
6181 */
6182static void __init tpacpi_detect_brightness_capabilities(void)
6183{
6184 unsigned int b;
6185
6186 vdbg_printk(TPACPI_DBG_INIT,
6187 "detecting firmware brightness interface capabilities\n");
6188
6189 /* we could run a quirks check here (same table used by
6190 * brightness_init) if needed */
6191
6192 /*
6193 * We always attempt to detect acpi support, so as to switch
6194 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6195 * going to publish a backlight interface
6196 */
6197 b = tpacpi_check_std_acpi_brightness_support();
6198 switch (b) {
6199 case 16:
6200 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006201 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006202 break;
6203 case 8:
6204 case 0:
6205 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006206 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006207 break;
6208 default:
Joe Perches0978e012011-04-04 10:06:25 -07006209 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006210 "please contact %s\n", TPACPI_MAIL);
6211 tp_features.bright_unkfw = 1;
6212 bright_maxlvl = b - 1;
6213 }
6214}
6215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006216static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006217{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006218 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006219 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006220 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006221
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006222 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6223
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006224 mutex_init(&brightness_mutex);
6225
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006226 quirks = tpacpi_check_quirks(brightness_quirk_table,
6227 ARRAY_SIZE(brightness_quirk_table));
6228
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006229 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006230
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006231 /* if it is unknown, we don't handle it: it wouldn't be safe */
6232 if (tp_features.bright_unkfw)
6233 return 1;
6234
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006235 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006236 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006237 "brightness support disabled by "
6238 "module parameter\n");
6239 return 1;
6240 }
6241
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006242 if (acpi_video_backlight_support()) {
6243 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006244 pr_info("Standard ACPI backlight interface "
6245 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006246 return 1;
6247 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006248 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006249 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006250 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006251 return 1;
6252 }
6253 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006254 pr_notice("Standard ACPI backlight interface not "
6255 "available, thinkpad_acpi native "
6256 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006257 }
6258
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006259 /*
6260 * Check for module parameter bogosity, note that we
6261 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6262 * able to detect "unspecified"
6263 */
6264 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006265 return -EINVAL;
6266
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006267 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6268 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6269 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006270 if (quirks & TPACPI_BRGHT_Q_EC)
6271 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6272 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006273 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6274
6275 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006276 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006277 brightness_mode);
6278 }
6279
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006280 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006281 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006282 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6283 brightness_mode == TPACPI_BRGHT_MODE_EC))
6284 return -EINVAL;
6285
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006286 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006287 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006288
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006289 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006290 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006291 props.max_brightness = bright_maxlvl;
6292 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006293 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6294 NULL, NULL,
6295 &ibm_backlight_data,
6296 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006297 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006298 int rc = PTR_ERR(ibm_backlight_device);
6299 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006300 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006301 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006302 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006303 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6304 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006305
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006306 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006307 pr_notice("brightness: will use unverified default: "
6308 "brightness_mode=%d\n", brightness_mode);
6309 pr_notice("brightness: please report to %s whether it works well "
6310 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006311 }
6312
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006313 /* Added by mistake in early 2007. Probably useless, but it could
6314 * be working around some unknown firmware problem where the value
6315 * read at startup doesn't match the real hardware state... so leave
6316 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006317 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006318
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006319 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6320 "brightness: registering brightness hotkeys "
6321 "as change notification\n");
6322 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6323 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006324 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006325 return 0;
6326}
6327
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006328static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006329{
6330 tpacpi_brightness_checkpoint_nvram();
6331}
6332
6333static void brightness_shutdown(void)
6334{
6335 tpacpi_brightness_checkpoint_nvram();
6336}
6337
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006338static void brightness_exit(void)
6339{
6340 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006341 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006342 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006343 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006344 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006345
6346 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006347}
6348
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006349static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006350{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006351 int level;
6352
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006353 level = brightness_get(NULL);
6354 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006355 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006356 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006357 seq_printf(m, "level:\t\t%d\n", level);
6358 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006359 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6360 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006361 }
6362
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006363 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006364}
6365
6366static int brightness_write(char *buf)
6367{
6368 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006369 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006370 char *cmd;
6371
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006372 level = brightness_get(NULL);
6373 if (level < 0)
6374 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006375
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006376 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006377 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006378 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006379 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006380 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006381 if (level > 0)
6382 level--;
6383 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006384 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006385 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006386 } else
6387 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006388 }
6389
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006390 tpacpi_disclose_usertask("procfs brightness",
6391 "set level to %d\n", level);
6392
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006393 /*
6394 * Now we know what the final level should be, so we try to set it.
6395 * Doing it this way makes the syscall restartable in case of EINTR
6396 */
6397 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006398 if (!rc && ibm_backlight_device)
6399 backlight_force_update(ibm_backlight_device,
6400 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006401 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006402}
6403
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006404static struct ibm_struct brightness_driver_data = {
6405 .name = "brightness",
6406 .read = brightness_read,
6407 .write = brightness_write,
6408 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006409 .suspend = brightness_suspend,
6410 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006411};
6412
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006413/*************************************************************************
6414 * Volume subdriver
6415 */
6416
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006417/*
6418 * IBM ThinkPads have a simple volume controller with MUTE gating.
6419 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6420 *
6421 * Since the *61 series (and probably also the later *60 series), Lenovo
6422 * ThinkPads only implement the MUTE gate.
6423 *
6424 * EC register 0x30
6425 * Bit 6: MUTE (1 mutes sound)
6426 * Bit 3-0: Volume
6427 * Other bits should be zero as far as we know.
6428 *
6429 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6430 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6431 * such as bit 7 which is used to detect repeated presses of MUTE,
6432 * and we leave them unchanged.
6433 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006434
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006435#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6436
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006437#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6438#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6439#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6440
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006441static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006442static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306443static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006444
6445struct tpacpi_alsa_data {
6446 struct snd_card *card;
6447 struct snd_ctl_elem_id *ctl_mute_id;
6448 struct snd_ctl_elem_id *ctl_vol_id;
6449};
6450
6451static struct snd_card *alsa_card;
6452
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006453enum {
6454 TP_EC_AUDIO = 0x30,
6455
6456 /* TP_EC_AUDIO bits */
6457 TP_EC_AUDIO_MUTESW = 6,
6458
6459 /* TP_EC_AUDIO bitmasks */
6460 TP_EC_AUDIO_LVL_MSK = 0x0F,
6461 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6462
6463 /* Maximum volume */
6464 TP_EC_VOLUME_MAX = 14,
6465};
6466
6467enum tpacpi_volume_access_mode {
6468 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6469 TPACPI_VOL_MODE_EC, /* Pure EC control */
6470 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6471 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6472 TPACPI_VOL_MODE_MAX
6473};
6474
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006475enum tpacpi_volume_capabilities {
6476 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6477 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6478 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6479 TPACPI_VOL_CAP_MAX
6480};
6481
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006482static enum tpacpi_volume_access_mode volume_mode =
6483 TPACPI_VOL_MODE_MAX;
6484
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006485static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306486static bool volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006487
6488/*
6489 * Used to syncronize writers to TP_EC_AUDIO and
6490 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6491 */
6492static struct mutex volume_mutex;
6493
6494static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006495{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006496 u8 lec = 0;
6497 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006498 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006499
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006500 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6501 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006502 if (!volume_control_allowed)
6503 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006504
6505 vdbg_printk(TPACPI_DBG_MIXER,
6506 "trying to checkpoint mixer state to NVRAM...\n");
6507
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006508 if (tp_features.mixer_no_level_control)
6509 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6510 else
6511 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6512
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006513 if (mutex_lock_killable(&volume_mutex) < 0)
6514 return;
6515
6516 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6517 goto unlock;
6518 lec &= ec_mask;
6519 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6520
6521 if (lec != (b_nvram & ec_mask)) {
6522 /* NVRAM needs update */
6523 b_nvram &= ~ec_mask;
6524 b_nvram |= lec;
6525 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6526 dbg_printk(TPACPI_DBG_MIXER,
6527 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6528 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006529 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006530 vdbg_printk(TPACPI_DBG_MIXER,
6531 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6532 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006533 }
6534
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006535unlock:
6536 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006537}
6538
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006539static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006540{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006541 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006542
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006543 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6544 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006545
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006546 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006547
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006548 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006549
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006550 return 0;
6551}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006552
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006553static int volume_get_status(u8 *status)
6554{
6555 return volume_get_status_ec(status);
6556}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006557
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006558static int volume_set_status_ec(const u8 status)
6559{
6560 if (!acpi_ec_write(TP_EC_AUDIO, status))
6561 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006562
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006563 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6564
6565 return 0;
6566}
6567
6568static int volume_set_status(const u8 status)
6569{
6570 return volume_set_status_ec(status);
6571}
6572
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006573/* returns < 0 on error, 0 on no change, 1 on change */
6574static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006575{
6576 int rc;
6577 u8 s, n;
6578
6579 if (mutex_lock_killable(&volume_mutex) < 0)
6580 return -EINTR;
6581
6582 rc = volume_get_status_ec(&s);
6583 if (rc)
6584 goto unlock;
6585
6586 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6587 s & ~TP_EC_AUDIO_MUTESW_MSK;
6588
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006589 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006590 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006591 if (!rc)
6592 rc = 1;
6593 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006594
6595unlock:
6596 mutex_unlock(&volume_mutex);
6597 return rc;
6598}
6599
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006600static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006601{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006602 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006603 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006604 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006605}
6606
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006607static int volume_set_mute(const bool mute)
6608{
6609 int rc;
6610
6611 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6612 (mute) ? "" : "un");
6613
6614 rc = __volume_set_mute_ec(mute);
6615 return (rc < 0) ? rc : 0;
6616}
6617
6618/* returns < 0 on error, 0 on no change, 1 on change */
6619static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006620{
6621 int rc;
6622 u8 s, n;
6623
6624 if (vol > TP_EC_VOLUME_MAX)
6625 return -EINVAL;
6626
6627 if (mutex_lock_killable(&volume_mutex) < 0)
6628 return -EINTR;
6629
6630 rc = volume_get_status_ec(&s);
6631 if (rc)
6632 goto unlock;
6633
6634 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6635
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006636 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006637 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006638 if (!rc)
6639 rc = 1;
6640 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006641
6642unlock:
6643 mutex_unlock(&volume_mutex);
6644 return rc;
6645}
6646
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006647static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006648{
6649 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006650 "ALSA: trying to set volume level to %hu\n", vol);
6651 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006652}
6653
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006654static void volume_alsa_notify_change(void)
6655{
6656 struct tpacpi_alsa_data *d;
6657
6658 if (alsa_card && alsa_card->private_data) {
6659 d = alsa_card->private_data;
6660 if (d->ctl_mute_id)
6661 snd_ctl_notify(alsa_card,
6662 SNDRV_CTL_EVENT_MASK_VALUE,
6663 d->ctl_mute_id);
6664 if (d->ctl_vol_id)
6665 snd_ctl_notify(alsa_card,
6666 SNDRV_CTL_EVENT_MASK_VALUE,
6667 d->ctl_vol_id);
6668 }
6669}
6670
6671static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6672 struct snd_ctl_elem_info *uinfo)
6673{
6674 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6675 uinfo->count = 1;
6676 uinfo->value.integer.min = 0;
6677 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6678 return 0;
6679}
6680
6681static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6682 struct snd_ctl_elem_value *ucontrol)
6683{
6684 u8 s;
6685 int rc;
6686
6687 rc = volume_get_status(&s);
6688 if (rc < 0)
6689 return rc;
6690
6691 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6692 return 0;
6693}
6694
6695static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6696 struct snd_ctl_elem_value *ucontrol)
6697{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006698 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6699 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006700 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006701}
6702
6703#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6704
6705static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6706 struct snd_ctl_elem_value *ucontrol)
6707{
6708 u8 s;
6709 int rc;
6710
6711 rc = volume_get_status(&s);
6712 if (rc < 0)
6713 return rc;
6714
6715 ucontrol->value.integer.value[0] =
6716 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6717 return 0;
6718}
6719
6720static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6721 struct snd_ctl_elem_value *ucontrol)
6722{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006723 tpacpi_disclose_usertask("ALSA", "%smute\n",
6724 ucontrol->value.integer.value[0] ?
6725 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006726 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006727}
6728
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006729static struct snd_kcontrol_new volume_alsa_control_vol = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6731 .name = "Console Playback Volume",
6732 .index = 0,
6733 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6734 .info = volume_alsa_vol_info,
6735 .get = volume_alsa_vol_get,
6736};
6737
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006738static struct snd_kcontrol_new volume_alsa_control_mute = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6740 .name = "Console Playback Switch",
6741 .index = 0,
6742 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6743 .info = volume_alsa_mute_info,
6744 .get = volume_alsa_mute_get,
6745};
6746
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006747static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006748{
6749 tpacpi_volume_checkpoint_nvram();
6750}
6751
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006752static void volume_resume(void)
6753{
6754 volume_alsa_notify_change();
6755}
6756
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006757static void volume_shutdown(void)
6758{
6759 tpacpi_volume_checkpoint_nvram();
6760}
6761
6762static void volume_exit(void)
6763{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006764 if (alsa_card) {
6765 snd_card_free(alsa_card);
6766 alsa_card = NULL;
6767 }
6768
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006769 tpacpi_volume_checkpoint_nvram();
6770}
6771
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006772static int __init volume_create_alsa_mixer(void)
6773{
6774 struct snd_card *card;
6775 struct tpacpi_alsa_data *data;
6776 struct snd_kcontrol *ctl_vol;
6777 struct snd_kcontrol *ctl_mute;
6778 int rc;
6779
6780 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6781 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006782 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006783 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006784 return 1;
6785 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006786
6787 BUG_ON(!card->private_data);
6788 data = card->private_data;
6789 data->card = card;
6790
6791 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6792 sizeof(card->driver));
6793 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6794 sizeof(card->shortname));
6795 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6796 (thinkpad_id.ec_version_str) ?
6797 thinkpad_id.ec_version_str : "(unknown)");
6798 snprintf(card->longname, sizeof(card->longname),
6799 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6800 (thinkpad_id.ec_version_str) ?
6801 thinkpad_id.ec_version_str : "unknown");
6802
6803 if (volume_control_allowed) {
6804 volume_alsa_control_vol.put = volume_alsa_vol_put;
6805 volume_alsa_control_vol.access =
6806 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6807
6808 volume_alsa_control_mute.put = volume_alsa_mute_put;
6809 volume_alsa_control_mute.access =
6810 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6811 }
6812
6813 if (!tp_features.mixer_no_level_control) {
6814 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6815 rc = snd_ctl_add(card, ctl_vol);
6816 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006817 pr_err("Failed to create ALSA volume control: %d\n",
6818 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006819 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006820 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006821 data->ctl_vol_id = &ctl_vol->id;
6822 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006823
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006824 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6825 rc = snd_ctl_add(card, ctl_mute);
6826 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006827 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006828 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006829 }
6830 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006831
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006832 snd_card_set_dev(card, &tpacpi_pdev->dev);
6833 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006834 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006835 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006836 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006837 }
6838
6839 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006840 return 0;
6841
6842err_exit:
6843 snd_card_free(card);
6844 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006845}
6846
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006847#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6848#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6849
6850static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6851 /* Whitelist volume level on all IBM by default */
6852 { .vendor = PCI_VENDOR_ID_IBM,
6853 .bios = TPACPI_MATCH_ANY,
6854 .ec = TPACPI_MATCH_ANY,
6855 .quirks = TPACPI_VOL_Q_LEVEL },
6856
6857 /* Lenovo models with volume control (needs confirmation) */
6858 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6859 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6860 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6861 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6862 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6863 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6864 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6865
6866 /* Whitelist mute-only on all Lenovo by default */
6867 { .vendor = PCI_VENDOR_ID_LENOVO,
6868 .bios = TPACPI_MATCH_ANY,
6869 .ec = TPACPI_MATCH_ANY,
6870 .quirks = TPACPI_VOL_Q_MUTEONLY }
6871};
6872
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006873static int __init volume_init(struct ibm_init_struct *iibm)
6874{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006875 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006876 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006877
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006878 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6879
6880 mutex_init(&volume_mutex);
6881
6882 /*
6883 * Check for module parameter bogosity, note that we
6884 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6885 * able to detect "unspecified"
6886 */
6887 if (volume_mode > TPACPI_VOL_MODE_MAX)
6888 return -EINVAL;
6889
6890 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006891 pr_err("UCMS step volume mode not implemented, "
6892 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006893 return 1;
6894 }
6895
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006896 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6897 return -EINVAL;
6898
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006899 /*
6900 * The ALSA mixer is our primary interface.
6901 * When disabled, don't install the subdriver at all
6902 */
6903 if (!alsa_enable) {
6904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6905 "ALSA mixer disabled by parameter, "
6906 "not loading volume subdriver...\n");
6907 return 1;
6908 }
6909
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006910 quirks = tpacpi_check_quirks(volume_quirk_table,
6911 ARRAY_SIZE(volume_quirk_table));
6912
6913 switch (volume_capabilities) {
6914 case TPACPI_VOL_CAP_AUTO:
6915 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6916 tp_features.mixer_no_level_control = 1;
6917 else if (quirks & TPACPI_VOL_Q_LEVEL)
6918 tp_features.mixer_no_level_control = 0;
6919 else
6920 return 1; /* no mixer */
6921 break;
6922 case TPACPI_VOL_CAP_VOLMUTE:
6923 tp_features.mixer_no_level_control = 0;
6924 break;
6925 case TPACPI_VOL_CAP_MUTEONLY:
6926 tp_features.mixer_no_level_control = 1;
6927 break;
6928 default:
6929 return 1;
6930 }
6931
6932 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6933 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6934 "using user-supplied volume_capabilities=%d\n",
6935 volume_capabilities);
6936
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006937 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6938 volume_mode == TPACPI_VOL_MODE_MAX) {
6939 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6940
6941 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6942 "driver auto-selected volume_mode=%d\n",
6943 volume_mode);
6944 } else {
6945 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6946 "using user-supplied volume_mode=%d\n",
6947 volume_mode);
6948 }
6949
6950 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006951 "mute is supported, volume control is %s\n",
6952 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006953
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006954 rc = volume_create_alsa_mixer();
6955 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006956 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006957 return rc;
6958 }
6959
Joe Perches0978e012011-04-04 10:06:25 -07006960 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006961 (volume_control_allowed) ?
6962 "override (read/write)" :
6963 "monitor (read only)");
6964
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006965 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6966 "registering volume hotkeys as change notification\n");
6967 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6968 | TP_ACPI_HKEY_VOLUP_MASK
6969 | TP_ACPI_HKEY_VOLDWN_MASK
6970 | TP_ACPI_HKEY_MUTE_MASK);
6971
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006972 return 0;
6973}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006974
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006975static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006976{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006977 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006978
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006979 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006980 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006981 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006982 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006983 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006984 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006985 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006986 status & TP_EC_AUDIO_LVL_MSK);
6987
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006988 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006989 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006990
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006991 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006992 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006993 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006994 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006995 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006996 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006997 "commands:\tlevel <level>"
6998 " (<level> is 0-%d)\n",
6999 TP_EC_VOLUME_MAX);
7000 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007001 }
7002 }
7003
7004 return 0;
7005}
7006
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007007static int volume_write(char *buf)
7008{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007009 u8 s;
7010 u8 new_level, new_mute;
7011 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007012 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007013 int rc;
7014
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007015 /*
7016 * We do allow volume control at driver startup, so that the
7017 * user can set initial state through the volume=... parameter hack.
7018 */
7019 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7020 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7021 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007022 pr_notice("Console audio control in monitor mode, "
7023 "changes are not allowed\n");
7024 pr_notice("Use the volume_control=1 module parameter "
7025 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007026 }
7027 return -EPERM;
7028 }
7029
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007030 rc = volume_get_status(&s);
7031 if (rc < 0)
7032 return rc;
7033
7034 new_level = s & TP_EC_AUDIO_LVL_MSK;
7035 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007036
7037 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007038 if (!tp_features.mixer_no_level_control) {
7039 if (strlencmp(cmd, "up") == 0) {
7040 if (new_mute)
7041 new_mute = 0;
7042 else if (new_level < TP_EC_VOLUME_MAX)
7043 new_level++;
7044 continue;
7045 } else if (strlencmp(cmd, "down") == 0) {
7046 if (new_mute)
7047 new_mute = 0;
7048 else if (new_level > 0)
7049 new_level--;
7050 continue;
7051 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7052 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7053 new_level = l;
7054 continue;
7055 }
7056 }
7057 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007058 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007059 else if (strlencmp(cmd, "unmute") == 0)
7060 new_mute = 0;
7061 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007062 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007063 }
7064
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007065 if (tp_features.mixer_no_level_control) {
7066 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7067 new_mute ? "" : "un");
7068 rc = volume_set_mute(!!new_mute);
7069 } else {
7070 tpacpi_disclose_usertask("procfs volume",
7071 "%smute and set level to %d\n",
7072 new_mute ? "" : "un", new_level);
7073 rc = volume_set_status(new_mute | new_level);
7074 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007075 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007076
7077 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007078}
7079
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007080static struct ibm_struct volume_driver_data = {
7081 .name = "volume",
7082 .read = volume_read,
7083 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007084 .exit = volume_exit,
7085 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007086 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007087 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007088};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007089
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007090#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7091
7092#define alsa_card NULL
7093
7094static void inline volume_alsa_notify_change(void)
7095{
7096}
7097
7098static int __init volume_init(struct ibm_init_struct *iibm)
7099{
Joe Perches0978e012011-04-04 10:06:25 -07007100 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007101
7102 return 1;
7103}
7104
7105static struct ibm_struct volume_driver_data = {
7106 .name = "volume",
7107};
7108
7109#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007111/*************************************************************************
7112 * Fan subdriver
7113 */
7114
7115/*
7116 * FAN ACCESS MODES
7117 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007118 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007119 * ACPI GFAN method: returns fan level
7120 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007121 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007122 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007123 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007124 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007125 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7126 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007127 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7128 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007129 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007130 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007131 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7132 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007133 *
7134 * Fan speed changes of any sort (including those caused by the
7135 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007136 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007137 * limited.
7138 *
7139 * Reading is not available if GFAN exists.
7140 * Writing is not available if SFAN exists.
7141 *
7142 * Bits
7143 * 7 automatic mode engaged;
7144 * (default operation mode of the ThinkPad)
7145 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007146 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007147 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007148 * the tachometer while the fan controller ramps up
7149 * the speed (which can take up to a few *minutes*).
7150 * Speeds up fan to 100% duty-cycle, which is far above
7151 * the standard RPM levels. It is not impossible that
7152 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007153 * 5-3 unused in some models. Extra bits for fan level
7154 * in others, but still useless as all values above
7155 * 7 map to the same speed as level 7 in these models.
7156 * 2-0 fan level (0..7 usually)
7157 * 0x00 = stop
7158 * 0x07 = max (set when temperatures critical)
7159 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007160 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007161 *
7162 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007163 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164 * does so, its initial value is meaningless (0x07).
7165 *
7166 * For firmware bugs, refer to:
7167 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7168 *
7169 * ----
7170 *
7171 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7172 * Main fan tachometer reading (in RPM)
7173 *
7174 * This register is present on all ThinkPads with a new-style EC, and
7175 * it is known not to be present on the A21m/e, and T22, as there is
7176 * something else in offset 0x84 according to the ACPI DSDT. Other
7177 * ThinkPads from this same time period (and earlier) probably lack the
7178 * tachometer as well.
7179 *
Nick Andrew877d0312009-01-26 11:06:57 +01007180 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007181 * was never fixed by IBM to report the EC firmware version string
7182 * probably support the tachometer (like the early X models), so
7183 * detecting it is quite hard. We need more data to know for sure.
7184 *
7185 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7186 * might result.
7187 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007188 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7189 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007190 *
7191 * For firmware bugs, refer to:
7192 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7193 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007194 * ----
7195 *
7196 * ThinkPad EC register 0x31 bit 0 (only on select models)
7197 *
7198 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7199 * show the speed of the main fan. When bit 0 of EC register 0x31
7200 * is one, the tachometer registers show the speed of the auxiliary
7201 * fan.
7202 *
7203 * Fan control seems to affect both fans, regardless of the state
7204 * of this bit.
7205 *
7206 * So far, only the firmware for the X60/X61 non-tablet versions
7207 * seem to support this (firmware TP-7M).
7208 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007209 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007210 * ThinkPad X31, X40, X41. Not available in the X60.
7211 *
7212 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7213 * high speed. ACPI DSDT seems to map these three speeds to levels
7214 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7215 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7216 *
7217 * The speeds are stored on handles
7218 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7219 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007220 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007221 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7222 *
7223 * ACPI DSDT switches which set is in use depending on various
7224 * factors.
7225 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007226 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007227 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7228 * but the ACPI tables just mention level 7.
7229 */
7230
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007231enum { /* Fan control constants */
7232 fan_status_offset = 0x2f, /* EC register 0x2f */
7233 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7234 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007235 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7236 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007237
7238 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7239 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7240
7241 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7242};
7243
7244enum fan_status_access_mode {
7245 TPACPI_FAN_NONE = 0, /* No fan status or control */
7246 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7247 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7248};
7249
7250enum fan_control_access_mode {
7251 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7252 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7253 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7254 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7255};
7256
7257enum fan_control_commands {
7258 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7259 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7260 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7261 * and also watchdog cmd */
7262};
7263
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307264static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007265
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007266static enum fan_status_access_mode fan_status_access_mode;
7267static enum fan_control_access_mode fan_control_access_mode;
7268static enum fan_control_commands fan_control_commands;
7269
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007270static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007271static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007272static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007273static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007274
7275static struct mutex fan_mutex;
7276
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007277static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007278static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007279
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007280TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7281TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007282 "\\FSPD", /* 600e/x, 770e, 770x */
7283 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007284TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007285 "JFNS", /* 770x-JL */
7286 ); /* all others */
7287
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007288/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007289 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7290 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7291 * be in auto mode (0x80).
7292 *
7293 * This is corrected by any write to HFSP either by the driver, or
7294 * by the firmware.
7295 *
7296 * We assume 0x07 really means auto mode while this quirk is active,
7297 * as this is far more likely than the ThinkPad being in level 7,
7298 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007299 *
7300 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7301 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007302 */
7303
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007304static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007305{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007306 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007307 pr_notice("fan_init: initial fan status is unknown, "
7308 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007309 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007310 }
7311}
7312
7313static void fan_quirk1_handle(u8 *fan_status)
7314{
7315 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7316 if (*fan_status != fan_control_initial_status) {
7317 /* something changed the HFSP regisnter since
7318 * driver init time, so it is not undefined
7319 * anymore */
7320 tp_features.fan_ctrl_status_undef = 0;
7321 } else {
7322 /* Return most likely status. In fact, it
7323 * might be the only possible status */
7324 *fan_status = TP_EC_FAN_AUTO;
7325 }
7326 }
7327}
7328
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007329/* Select main fan on X60/X61, NOOP on others */
7330static bool fan_select_fan1(void)
7331{
7332 if (tp_features.second_fan) {
7333 u8 val;
7334
7335 if (ec_read(fan_select_offset, &val) < 0)
7336 return false;
7337 val &= 0xFEU;
7338 if (ec_write(fan_select_offset, val) < 0)
7339 return false;
7340 }
7341 return true;
7342}
7343
7344/* Select secondary fan on X60/X61 */
7345static bool fan_select_fan2(void)
7346{
7347 u8 val;
7348
7349 if (!tp_features.second_fan)
7350 return false;
7351
7352 if (ec_read(fan_select_offset, &val) < 0)
7353 return false;
7354 val |= 0x01U;
7355 if (ec_write(fan_select_offset, val) < 0)
7356 return false;
7357
7358 return true;
7359}
7360
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007361/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007362 * Call with fan_mutex held
7363 */
7364static void fan_update_desired_level(u8 status)
7365{
7366 if ((status &
7367 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7368 if (status > 7)
7369 fan_control_desired_level = 7;
7370 else
7371 fan_control_desired_level = status;
7372 }
7373}
7374
7375static int fan_get_status(u8 *status)
7376{
7377 u8 s;
7378
7379 /* TODO:
7380 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7381
7382 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007383 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007384 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007385 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007386
Dan Carpentereceeb432012-09-01 12:54:07 -07007387 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007388 return -EIO;
7389
7390 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007391 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007392
7393 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007394 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007395 case TPACPI_FAN_RD_TPEC:
7396 /* all except 570, 600e/x, 770e, 770x */
7397 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7398 return -EIO;
7399
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007400 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007401 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007402 fan_quirk1_handle(status);
7403 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007404
7405 break;
7406
7407 default:
7408 return -ENXIO;
7409 }
7410
7411 return 0;
7412}
7413
7414static int fan_get_status_safe(u8 *status)
7415{
7416 int rc;
7417 u8 s;
7418
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007419 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007420 return -ERESTARTSYS;
7421 rc = fan_get_status(&s);
7422 if (!rc)
7423 fan_update_desired_level(s);
7424 mutex_unlock(&fan_mutex);
7425
7426 if (status)
7427 *status = s;
7428
7429 return rc;
7430}
7431
7432static int fan_get_speed(unsigned int *speed)
7433{
7434 u8 hi, lo;
7435
7436 switch (fan_status_access_mode) {
7437 case TPACPI_FAN_RD_TPEC:
7438 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007439 if (unlikely(!fan_select_fan1()))
7440 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007441 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7442 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7443 return -EIO;
7444
7445 if (likely(speed))
7446 *speed = (hi << 8) | lo;
7447
7448 break;
7449
7450 default:
7451 return -ENXIO;
7452 }
7453
7454 return 0;
7455}
7456
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007457static int fan2_get_speed(unsigned int *speed)
7458{
7459 u8 hi, lo;
7460 bool rc;
7461
7462 switch (fan_status_access_mode) {
7463 case TPACPI_FAN_RD_TPEC:
7464 /* all except 570, 600e/x, 770e, 770x */
7465 if (unlikely(!fan_select_fan2()))
7466 return -EIO;
7467 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7468 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7469 fan_select_fan1(); /* play it safe */
7470 if (rc)
7471 return -EIO;
7472
7473 if (likely(speed))
7474 *speed = (hi << 8) | lo;
7475
7476 break;
7477
7478 default:
7479 return -ENXIO;
7480 }
7481
7482 return 0;
7483}
7484
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007485static int fan_set_level(int level)
7486{
7487 if (!fan_control_allowed)
7488 return -EPERM;
7489
7490 switch (fan_control_access_mode) {
7491 case TPACPI_FAN_WR_ACPI_SFAN:
7492 if (level >= 0 && level <= 7) {
7493 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7494 return -EIO;
7495 } else
7496 return -EINVAL;
7497 break;
7498
7499 case TPACPI_FAN_WR_ACPI_FANS:
7500 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007501 if (!(level & TP_EC_FAN_AUTO) &&
7502 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007503 ((level < 0) || (level > 7)))
7504 return -EINVAL;
7505
7506 /* safety net should the EC not support AUTO
7507 * or FULLSPEED mode bits and just ignore them */
7508 if (level & TP_EC_FAN_FULLSPEED)
7509 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007510 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007511 level |= 4; /* safety min speed 4 */
7512
7513 if (!acpi_ec_write(fan_status_offset, level))
7514 return -EIO;
7515 else
7516 tp_features.fan_ctrl_status_undef = 0;
7517 break;
7518
7519 default:
7520 return -ENXIO;
7521 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007522
7523 vdbg_printk(TPACPI_DBG_FAN,
7524 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007525 return 0;
7526}
7527
7528static int fan_set_level_safe(int level)
7529{
7530 int rc;
7531
7532 if (!fan_control_allowed)
7533 return -EPERM;
7534
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007535 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007536 return -ERESTARTSYS;
7537
7538 if (level == TPACPI_FAN_LAST_LEVEL)
7539 level = fan_control_desired_level;
7540
7541 rc = fan_set_level(level);
7542 if (!rc)
7543 fan_update_desired_level(level);
7544
7545 mutex_unlock(&fan_mutex);
7546 return rc;
7547}
7548
7549static int fan_set_enable(void)
7550{
7551 u8 s;
7552 int rc;
7553
7554 if (!fan_control_allowed)
7555 return -EPERM;
7556
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007557 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007558 return -ERESTARTSYS;
7559
7560 switch (fan_control_access_mode) {
7561 case TPACPI_FAN_WR_ACPI_FANS:
7562 case TPACPI_FAN_WR_TPEC:
7563 rc = fan_get_status(&s);
7564 if (rc < 0)
7565 break;
7566
7567 /* Don't go out of emergency fan mode */
7568 if (s != 7) {
7569 s &= 0x07;
7570 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7571 }
7572
7573 if (!acpi_ec_write(fan_status_offset, s))
7574 rc = -EIO;
7575 else {
7576 tp_features.fan_ctrl_status_undef = 0;
7577 rc = 0;
7578 }
7579 break;
7580
7581 case TPACPI_FAN_WR_ACPI_SFAN:
7582 rc = fan_get_status(&s);
7583 if (rc < 0)
7584 break;
7585
7586 s &= 0x07;
7587
7588 /* Set fan to at least level 4 */
7589 s |= 4;
7590
7591 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007592 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007593 else
7594 rc = 0;
7595 break;
7596
7597 default:
7598 rc = -ENXIO;
7599 }
7600
7601 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007602
7603 if (!rc)
7604 vdbg_printk(TPACPI_DBG_FAN,
7605 "fan control: set fan control register to 0x%02x\n",
7606 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007607 return rc;
7608}
7609
7610static int fan_set_disable(void)
7611{
7612 int rc;
7613
7614 if (!fan_control_allowed)
7615 return -EPERM;
7616
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007617 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007618 return -ERESTARTSYS;
7619
7620 rc = 0;
7621 switch (fan_control_access_mode) {
7622 case TPACPI_FAN_WR_ACPI_FANS:
7623 case TPACPI_FAN_WR_TPEC:
7624 if (!acpi_ec_write(fan_status_offset, 0x00))
7625 rc = -EIO;
7626 else {
7627 fan_control_desired_level = 0;
7628 tp_features.fan_ctrl_status_undef = 0;
7629 }
7630 break;
7631
7632 case TPACPI_FAN_WR_ACPI_SFAN:
7633 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7634 rc = -EIO;
7635 else
7636 fan_control_desired_level = 0;
7637 break;
7638
7639 default:
7640 rc = -ENXIO;
7641 }
7642
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007643 if (!rc)
7644 vdbg_printk(TPACPI_DBG_FAN,
7645 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007646
7647 mutex_unlock(&fan_mutex);
7648 return rc;
7649}
7650
7651static int fan_set_speed(int speed)
7652{
7653 int rc;
7654
7655 if (!fan_control_allowed)
7656 return -EPERM;
7657
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007658 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007659 return -ERESTARTSYS;
7660
7661 rc = 0;
7662 switch (fan_control_access_mode) {
7663 case TPACPI_FAN_WR_ACPI_FANS:
7664 if (speed >= 0 && speed <= 65535) {
7665 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7666 speed, speed, speed))
7667 rc = -EIO;
7668 } else
7669 rc = -EINVAL;
7670 break;
7671
7672 default:
7673 rc = -ENXIO;
7674 }
7675
7676 mutex_unlock(&fan_mutex);
7677 return rc;
7678}
7679
7680static void fan_watchdog_reset(void)
7681{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007682 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7683 return;
7684
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007685 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07007686 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7687 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7688 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7689 else
7690 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007691}
7692
7693static void fan_watchdog_fire(struct work_struct *ignored)
7694{
7695 int rc;
7696
7697 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7698 return;
7699
Joe Perches0978e012011-04-04 10:06:25 -07007700 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007701 rc = fan_set_enable();
7702 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007703 pr_err("fan watchdog: error %d while enabling fan, "
7704 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007705 /* reschedule for later */
7706 fan_watchdog_reset();
7707 }
7708}
7709
7710/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007711 * SYSFS fan layout: hwmon compatible (device)
7712 *
7713 * pwm*_enable:
7714 * 0: "disengaged" mode
7715 * 1: manual mode
7716 * 2: native EC "auto" mode (recommended, hardware default)
7717 *
7718 * pwm*: set speed in manual mode, ignored otherwise.
7719 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7720 * interpolation.
7721 *
7722 * fan*_input: tachometer reading, RPM
7723 *
7724 *
7725 * SYSFS fan layout: extensions
7726 *
7727 * fan_watchdog (driver):
7728 * fan watchdog interval in seconds, 0 disables (default), max 120
7729 */
7730
7731/* sysfs fan pwm1_enable ----------------------------------------------- */
7732static ssize_t fan_pwm1_enable_show(struct device *dev,
7733 struct device_attribute *attr,
7734 char *buf)
7735{
7736 int res, mode;
7737 u8 status;
7738
7739 res = fan_get_status_safe(&status);
7740 if (res)
7741 return res;
7742
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007743 if (status & TP_EC_FAN_FULLSPEED) {
7744 mode = 0;
7745 } else if (status & TP_EC_FAN_AUTO) {
7746 mode = 2;
7747 } else
7748 mode = 1;
7749
7750 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7751}
7752
7753static ssize_t fan_pwm1_enable_store(struct device *dev,
7754 struct device_attribute *attr,
7755 const char *buf, size_t count)
7756{
7757 unsigned long t;
7758 int res, level;
7759
7760 if (parse_strtoul(buf, 2, &t))
7761 return -EINVAL;
7762
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007763 tpacpi_disclose_usertask("hwmon pwm1_enable",
7764 "set fan mode to %lu\n", t);
7765
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007766 switch (t) {
7767 case 0:
7768 level = TP_EC_FAN_FULLSPEED;
7769 break;
7770 case 1:
7771 level = TPACPI_FAN_LAST_LEVEL;
7772 break;
7773 case 2:
7774 level = TP_EC_FAN_AUTO;
7775 break;
7776 case 3:
7777 /* reserved for software-controlled auto mode */
7778 return -ENOSYS;
7779 default:
7780 return -EINVAL;
7781 }
7782
7783 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007784 if (res == -ENXIO)
7785 return -EINVAL;
7786 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007787 return res;
7788
7789 fan_watchdog_reset();
7790
7791 return count;
7792}
7793
7794static struct device_attribute dev_attr_fan_pwm1_enable =
7795 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7796 fan_pwm1_enable_show, fan_pwm1_enable_store);
7797
7798/* sysfs fan pwm1 ------------------------------------------------------ */
7799static ssize_t fan_pwm1_show(struct device *dev,
7800 struct device_attribute *attr,
7801 char *buf)
7802{
7803 int res;
7804 u8 status;
7805
7806 res = fan_get_status_safe(&status);
7807 if (res)
7808 return res;
7809
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007810 if ((status &
7811 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7812 status = fan_control_desired_level;
7813
7814 if (status > 7)
7815 status = 7;
7816
7817 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7818}
7819
7820static ssize_t fan_pwm1_store(struct device *dev,
7821 struct device_attribute *attr,
7822 const char *buf, size_t count)
7823{
7824 unsigned long s;
7825 int rc;
7826 u8 status, newlevel;
7827
7828 if (parse_strtoul(buf, 255, &s))
7829 return -EINVAL;
7830
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007831 tpacpi_disclose_usertask("hwmon pwm1",
7832 "set fan speed to %lu\n", s);
7833
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007834 /* scale down from 0-255 to 0-7 */
7835 newlevel = (s >> 5) & 0x07;
7836
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007837 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007838 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007839
7840 rc = fan_get_status(&status);
7841 if (!rc && (status &
7842 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7843 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007844 if (rc == -ENXIO)
7845 rc = -EINVAL;
7846 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007847 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007848 fan_watchdog_reset();
7849 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007850 }
7851
7852 mutex_unlock(&fan_mutex);
7853 return (rc)? rc : count;
7854}
7855
7856static struct device_attribute dev_attr_fan_pwm1 =
7857 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7858 fan_pwm1_show, fan_pwm1_store);
7859
7860/* sysfs fan fan1_input ------------------------------------------------ */
7861static ssize_t fan_fan1_input_show(struct device *dev,
7862 struct device_attribute *attr,
7863 char *buf)
7864{
7865 int res;
7866 unsigned int speed;
7867
7868 res = fan_get_speed(&speed);
7869 if (res < 0)
7870 return res;
7871
7872 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7873}
7874
7875static struct device_attribute dev_attr_fan_fan1_input =
7876 __ATTR(fan1_input, S_IRUGO,
7877 fan_fan1_input_show, NULL);
7878
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007879/* sysfs fan fan2_input ------------------------------------------------ */
7880static ssize_t fan_fan2_input_show(struct device *dev,
7881 struct device_attribute *attr,
7882 char *buf)
7883{
7884 int res;
7885 unsigned int speed;
7886
7887 res = fan2_get_speed(&speed);
7888 if (res < 0)
7889 return res;
7890
7891 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7892}
7893
7894static struct device_attribute dev_attr_fan_fan2_input =
7895 __ATTR(fan2_input, S_IRUGO,
7896 fan_fan2_input_show, NULL);
7897
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007898/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007899static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7900 char *buf)
7901{
7902 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7903}
7904
7905static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7906 const char *buf, size_t count)
7907{
7908 unsigned long t;
7909
7910 if (parse_strtoul(buf, 120, &t))
7911 return -EINVAL;
7912
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007913 if (!fan_control_allowed)
7914 return -EPERM;
7915
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007916 fan_watchdog_maxinterval = t;
7917 fan_watchdog_reset();
7918
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007919 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7920
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007921 return count;
7922}
7923
7924static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7925 fan_fan_watchdog_show, fan_fan_watchdog_store);
7926
7927/* --------------------------------------------------------------------- */
7928static struct attribute *fan_attributes[] = {
7929 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7930 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007931 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007932 NULL
7933};
7934
7935static const struct attribute_group fan_attr_group = {
7936 .attrs = fan_attributes,
7937};
7938
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007939#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7940#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007941
7942#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7943 { .vendor = PCI_VENDOR_ID_IBM, \
7944 .bios = TPACPI_MATCH_ANY, \
7945 .ec = TPID(__id1, __id2), \
7946 .quirks = __quirks }
7947
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007948#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7949 { .vendor = PCI_VENDOR_ID_LENOVO, \
7950 .bios = TPACPI_MATCH_ANY, \
7951 .ec = TPID(__id1, __id2), \
7952 .quirks = __quirks }
7953
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007954static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7955 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7956 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7957 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7958 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007959 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007960};
7961
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007962#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007963#undef TPACPI_FAN_QI
7964
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007965static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007966{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007967 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007968 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007969
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7971 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007972
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007973 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007974 fan_status_access_mode = TPACPI_FAN_NONE;
7975 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007976 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007977 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007978 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007979 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007980 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007981
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007982 if (tpacpi_is_ibm()) {
7983 TPACPI_ACPIHANDLE_INIT(fans);
7984 TPACPI_ACPIHANDLE_INIT(gfan);
7985 TPACPI_ACPIHANDLE_INIT(sfan);
7986 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007987
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007988 quirks = tpacpi_check_quirks(fan_quirk_table,
7989 ARRAY_SIZE(fan_quirk_table));
7990
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007991 if (gfan_handle) {
7992 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007993 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007994 } else {
7995 /* all other ThinkPads: note that even old-style
7996 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007997 if (likely(acpi_ec_read(fan_status_offset,
7998 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007999 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008000 if (quirks & TPACPI_FAN_Q1)
8001 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008002 if (quirks & TPACPI_FAN_2FAN) {
8003 tp_features.second_fan = 1;
8004 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8005 "secondary fan support enabled\n");
8006 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008007 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008008 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008009 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008010 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008011 }
8012 }
8013
8014 if (sfan_handle) {
8015 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008016 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008017 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008018 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008019 } else {
8020 if (!gfan_handle) {
8021 /* gfan without sfan means no fan control */
8022 /* all other models implement TP EC 0x2f control */
8023
8024 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008025 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008026 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008027 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008028 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008029 TPACPI_FAN_CMD_SPEED |
8030 TPACPI_FAN_CMD_LEVEL |
8031 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008032 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008033 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008034 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008035 TPACPI_FAN_CMD_LEVEL |
8036 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008037 }
8038 }
8039 }
8040
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008041 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8042 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008043 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8044 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8045 fan_status_access_mode, fan_control_access_mode);
8046
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008047 /* fan control master switch */
8048 if (!fan_control_allowed) {
8049 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8050 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008051 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008052 "fan control features disabled by parameter\n");
8053 }
8054
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008055 /* update fan_control_desired_level */
8056 if (fan_status_access_mode != TPACPI_FAN_NONE)
8057 fan_get_status_safe(NULL);
8058
8059 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8060 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008061 if (tp_features.second_fan) {
8062 /* attach second fan tachometer */
8063 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8064 &dev_attr_fan_fan2_input.attr;
8065 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008066 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008067 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008068 if (rc < 0)
8069 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008070
8071 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8072 &driver_attr_fan_watchdog);
8073 if (rc < 0) {
8074 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8075 &fan_attr_group);
8076 return rc;
8077 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008078 return 0;
8079 } else
8080 return 1;
8081}
8082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008083static void fan_exit(void)
8084{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008085 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008086 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008087
8088 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008089 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008090 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8091 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008092
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008093 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008094 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008095}
8096
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008097static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008098{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008099 int rc;
8100
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008101 if (!fan_control_allowed)
8102 return;
8103
8104 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008105 fan_control_resume_level = 0;
8106 rc = fan_get_status_safe(&fan_control_resume_level);
8107 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008108 pr_notice("failed to read fan level for later "
8109 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008110
8111 /* if it is undefined, don't attempt to restore it.
8112 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008113 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008114 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008115}
8116
8117static void fan_resume(void)
8118{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008119 u8 current_level = 7;
8120 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008121 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008122
8123 /* DSDT *always* updates status on resume */
8124 tp_features.fan_ctrl_status_undef = 0;
8125
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008126 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008127 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008128 (fan_get_status_safe(&current_level) < 0))
8129 return;
8130
8131 switch (fan_control_access_mode) {
8132 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008133 /* never decrease fan level */
8134 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008135 break;
8136 case TPACPI_FAN_WR_ACPI_FANS:
8137 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008138 /* never decrease fan level, scale is:
8139 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8140 *
8141 * We expect the firmware to set either 7 or AUTO, but we
8142 * handle FULLSPEED out of paranoia.
8143 *
8144 * So, we can safely only restore FULLSPEED or 7, anything
8145 * else could slow the fan. Restoring AUTO is useless, at
8146 * best that's exactly what the DSDT already set (it is the
8147 * slower it uses).
8148 *
8149 * Always keep in mind that the DSDT *will* have set the
8150 * fans to what the vendor supposes is the best level. We
8151 * muck with it only to speed the fan up.
8152 */
8153 if (fan_control_resume_level != 7 &&
8154 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8155 return;
8156 else
8157 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8158 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008159 break;
8160 default:
8161 return;
8162 }
8163 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008164 pr_notice("restoring fan level to 0x%02x\n",
8165 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008166 rc = fan_set_level_safe(fan_control_resume_level);
8167 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008168 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008169 }
8170}
8171
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008172static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008173{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008174 int rc;
8175 u8 status;
8176 unsigned int speed = 0;
8177
8178 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008179 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008180 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008181 rc = fan_get_status_safe(&status);
8182 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008183 return rc;
8184
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008185 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008186 "level:\t\t%d\n",
8187 (status != 0) ? "enabled" : "disabled", status);
8188 break;
8189
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008190 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008191 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008192 rc = fan_get_status_safe(&status);
8193 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008194 return rc;
8195
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008196 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008197 (status != 0) ? "enabled" : "disabled");
8198
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008199 rc = fan_get_speed(&speed);
8200 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008201 return rc;
8202
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008203 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008204
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008205 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008206 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008207 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008208 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008209 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008210 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008211 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008212 break;
8213
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008214 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008215 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008216 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008217 }
8218
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008219 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008220 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008221
8222 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008223 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008224 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008225 break;
8226
8227 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008228 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008229 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008230 break;
8231 }
8232 }
8233
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008234 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008235 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008236 "commands:\twatchdog <timeout> (<timeout> "
8237 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008238
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008239 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008240 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008241 " (<speed> is 0-65535)\n");
8242
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008243 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008244}
8245
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008246static int fan_write_cmd_level(const char *cmd, int *rc)
8247{
8248 int level;
8249
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008250 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008251 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008252 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008253 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008254 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008255 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008256 return 0;
8257
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008258 *rc = fan_set_level_safe(level);
8259 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008260 pr_err("level command accepted for unsupported access mode %d\n",
8261 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008262 else if (!*rc)
8263 tpacpi_disclose_usertask("procfs fan",
8264 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008265
8266 return 1;
8267}
8268
8269static int fan_write_cmd_enable(const char *cmd, int *rc)
8270{
8271 if (strlencmp(cmd, "enable") != 0)
8272 return 0;
8273
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008274 *rc = fan_set_enable();
8275 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008276 pr_err("enable command accepted for unsupported access mode %d\n",
8277 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008278 else if (!*rc)
8279 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008280
8281 return 1;
8282}
8283
8284static int fan_write_cmd_disable(const char *cmd, int *rc)
8285{
8286 if (strlencmp(cmd, "disable") != 0)
8287 return 0;
8288
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008289 *rc = fan_set_disable();
8290 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008291 pr_err("disable command accepted for unsupported access mode %d\n",
8292 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008293 else if (!*rc)
8294 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008295
8296 return 1;
8297}
8298
8299static int fan_write_cmd_speed(const char *cmd, int *rc)
8300{
8301 int speed;
8302
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008303 /* TODO:
8304 * Support speed <low> <medium> <high> ? */
8305
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008306 if (sscanf(cmd, "speed %d", &speed) != 1)
8307 return 0;
8308
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008309 *rc = fan_set_speed(speed);
8310 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008311 pr_err("speed command accepted for unsupported access mode %d\n",
8312 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008313 else if (!*rc)
8314 tpacpi_disclose_usertask("procfs fan",
8315 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008316
8317 return 1;
8318}
8319
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008320static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8321{
8322 int interval;
8323
8324 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8325 return 0;
8326
8327 if (interval < 0 || interval > 120)
8328 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008329 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008330 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008331 tpacpi_disclose_usertask("procfs fan",
8332 "set watchdog timer to %d\n",
8333 interval);
8334 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008335
8336 return 1;
8337}
8338
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008339static int fan_write(char *buf)
8340{
8341 char *cmd;
8342 int rc = 0;
8343
8344 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008345 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008346 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008347 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008348 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008349 fan_write_cmd_disable(cmd, &rc) ||
8350 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008351 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008352 fan_write_cmd_speed(cmd, &rc))
8353 )
8354 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008355 else if (!rc)
8356 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008357 }
8358
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008359 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008360}
8361
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008362static struct ibm_struct fan_driver_data = {
8363 .name = "fan",
8364 .read = fan_read,
8365 .write = fan_write,
8366 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008367 .suspend = fan_suspend,
8368 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008369};
8370
David Henningsson420f9732013-10-16 23:10:31 +02008371/*************************************************************************
8372 * Mute LED subdriver
8373 */
8374
8375
8376struct tp_led_table {
8377 acpi_string name;
8378 int on_value;
8379 int off_value;
8380 int state;
8381};
8382
8383static struct tp_led_table led_tables[] = {
8384 [TPACPI_LED_MUTE] = {
8385 .name = "SSMS",
8386 .on_value = 1,
8387 .off_value = 0,
8388 },
8389 [TPACPI_LED_MICMUTE] = {
8390 .name = "MMTS",
8391 .on_value = 2,
8392 .off_value = 0,
8393 },
8394};
8395
8396static int mute_led_on_off(struct tp_led_table *t, bool state)
8397{
8398 acpi_handle temp;
8399 int output;
8400
8401 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
8402 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
8403 return -EIO;
8404 }
8405
8406 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
8407 state ? t->on_value : t->off_value))
8408 return -EIO;
8409
8410 t->state = state;
8411 return state;
8412}
8413
8414int tpacpi_led_set(int whichled, bool on)
8415{
8416 struct tp_led_table *t;
8417
8418 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
8419 return -EINVAL;
8420
8421 t = &led_tables[whichled];
8422 if (t->state < 0 || t->state == on)
8423 return t->state;
8424 return mute_led_on_off(t, on);
8425}
8426EXPORT_SYMBOL_GPL(tpacpi_led_set);
8427
8428static int mute_led_init(struct ibm_init_struct *iibm)
8429{
8430 acpi_handle temp;
8431 int i;
8432
8433 for (i = 0; i < TPACPI_LED_MAX; i++) {
8434 struct tp_led_table *t = &led_tables[i];
8435 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
8436 mute_led_on_off(t, false);
8437 else
8438 t->state = -ENODEV;
8439 }
8440 return 0;
8441}
8442
8443static void mute_led_exit(void)
8444{
8445 int i;
8446
8447 for (i = 0; i < TPACPI_LED_MAX; i++)
8448 tpacpi_led_set(i, false);
8449}
8450
8451static struct ibm_struct mute_led_driver_data = {
8452 .name = "mute_led",
8453 .exit = mute_led_exit,
8454};
8455
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008456/****************************************************************************
8457 ****************************************************************************
8458 *
8459 * Infrastructure
8460 *
8461 ****************************************************************************
8462 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008464/*
8465 * HKEY event callout for other subdrivers go here
8466 * (yes, it is ugly, but it is quick, safe, and gets the job done
8467 */
8468static void tpacpi_driver_event(const unsigned int hkey_event)
8469{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008470 if (ibm_backlight_device) {
8471 switch (hkey_event) {
8472 case TP_HKEY_EV_BRGHT_UP:
8473 case TP_HKEY_EV_BRGHT_DOWN:
8474 tpacpi_brightness_notify_change();
8475 }
8476 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008477 if (alsa_card) {
8478 switch (hkey_event) {
8479 case TP_HKEY_EV_VOL_UP:
8480 case TP_HKEY_EV_VOL_DOWN:
8481 case TP_HKEY_EV_VOL_MUTE:
8482 volume_alsa_notify_change();
8483 }
8484 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008485}
8486
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008487static void hotkey_driver_event(const unsigned int scancode)
8488{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008489 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008490}
8491
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008492/* sysfs name ---------------------------------------------------------- */
8493static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8494 struct device_attribute *attr,
8495 char *buf)
8496{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008497 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008498}
8499
8500static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8501 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8502
8503/* --------------------------------------------------------------------- */
8504
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008505/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008506static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008508/*
8509 * Module and infrastructure proble, init and exit handling
8510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308512static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008513
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008514#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008515static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008516{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008517 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008518
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008519 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008520}
8521#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8522
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008523static void ibm_exit(struct ibm_struct *ibm)
8524{
8525 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8526
8527 list_del_init(&ibm->all_drivers);
8528
8529 if (ibm->flags.acpi_notify_installed) {
8530 dbg_printk(TPACPI_DBG_EXIT,
8531 "%s: acpi_remove_notify_handler\n", ibm->name);
8532 BUG_ON(!ibm->acpi);
8533 acpi_remove_notify_handler(*ibm->acpi->handle,
8534 ibm->acpi->type,
8535 dispatch_acpi_notify);
8536 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008537 }
8538
8539 if (ibm->flags.proc_created) {
8540 dbg_printk(TPACPI_DBG_EXIT,
8541 "%s: remove_proc_entry\n", ibm->name);
8542 remove_proc_entry(ibm->name, proc_dir);
8543 ibm->flags.proc_created = 0;
8544 }
8545
8546 if (ibm->flags.acpi_driver_registered) {
8547 dbg_printk(TPACPI_DBG_EXIT,
8548 "%s: acpi_bus_unregister_driver\n", ibm->name);
8549 BUG_ON(!ibm->acpi);
8550 acpi_bus_unregister_driver(ibm->acpi->driver);
8551 kfree(ibm->acpi->driver);
8552 ibm->acpi->driver = NULL;
8553 ibm->flags.acpi_driver_registered = 0;
8554 }
8555
8556 if (ibm->flags.init_called && ibm->exit) {
8557 ibm->exit();
8558 ibm->flags.init_called = 0;
8559 }
8560
8561 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8562}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008564static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565{
8566 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008567 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568 struct proc_dir_entry *entry;
8569
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008570 BUG_ON(ibm == NULL);
8571
8572 INIT_LIST_HEAD(&ibm->all_drivers);
8573
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008574 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575 return 0;
8576
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008577 dbg_printk(TPACPI_DBG_INIT,
8578 "probing for %s\n", ibm->name);
8579
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008580 if (iibm->init) {
8581 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008582 if (ret > 0)
8583 return 0; /* probe failed */
8584 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008586
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008587 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588 }
8589
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008590 if (ibm->acpi) {
8591 if (ibm->acpi->hid) {
8592 ret = register_tpacpi_subdriver(ibm);
8593 if (ret)
8594 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008595 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008596
8597 if (ibm->acpi->notify) {
8598 ret = setup_acpi_notify(ibm);
8599 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008600 pr_notice("disabling subdriver %s\n",
8601 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008602 ret = 0;
8603 goto err_out;
8604 }
8605 if (ret < 0)
8606 goto err_out;
8607 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008608 }
8609
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008610 dbg_printk(TPACPI_DBG_INIT,
8611 "%s installed\n", ibm->name);
8612
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008613 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008614 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008615
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008616 if (!mode)
8617 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008618 if (ibm->write)
8619 mode |= S_IWUSR;
8620 entry = proc_create_data(ibm->name, mode, proc_dir,
8621 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008622 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008623 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008624 ret = -ENODEV;
8625 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008626 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008627 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008630 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8631
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008633
8634err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008635 dbg_printk(TPACPI_DBG_INIT,
8636 "%s: at error exit path with result %d\n",
8637 ibm->name, ret);
8638
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008639 ibm_exit(ibm);
8640 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641}
8642
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008643/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008645static bool __pure __init tpacpi_is_fw_digit(const char c)
8646{
8647 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8648}
8649
8650/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8651static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8652 const char t)
8653{
8654 return s && strlen(s) >= 8 &&
8655 tpacpi_is_fw_digit(s[0]) &&
8656 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008657 s[2] == t &&
8658 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008659 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008660 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008661}
8662
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008663/* returns 0 - probe ok, or < 0 - probe error.
8664 * Probe ok doesn't mean thinkpad found.
8665 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8666static int __must_check __init get_thinkpad_model_data(
8667 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008668{
Jeff Garzik18552562007-10-03 15:15:40 -04008669 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008670 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008671 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008672
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008673 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008674 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008675
8676 memset(tp, 0, sizeof(*tp));
8677
8678 if (dmi_name_in_vendors("IBM"))
8679 tp->vendor = PCI_VENDOR_ID_IBM;
8680 else if (dmi_name_in_vendors("LENOVO"))
8681 tp->vendor = PCI_VENDOR_ID_LENOVO;
8682 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008683 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008684
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008685 s = dmi_get_system_info(DMI_BIOS_VERSION);
8686 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8687 if (s && !tp->bios_version_str)
8688 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008689
8690 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008691 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8692 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008693 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008694
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008695 tp->bios_model = tp->bios_version_str[0]
8696 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008697 tp->bios_release = (tp->bios_version_str[4] << 8)
8698 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008699
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008700 /*
8701 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8702 * X32 or newer, all Z series; Some models must have an
8703 * up-to-date BIOS or they will not be detected.
8704 *
8705 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8706 */
8707 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008708 if (sscanf(dev->name,
8709 "IBM ThinkPad Embedded Controller -[%17c",
8710 ec_fw_string) == 1) {
8711 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8712 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008713
8714 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008715 if (!tp->ec_version_str)
8716 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008717
8718 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8719 tp->ec_model = ec_fw_string[0]
8720 | (ec_fw_string[1] << 8);
8721 tp->ec_release = (ec_fw_string[4] << 8)
8722 | ec_fw_string[5];
8723 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008724 pr_notice("ThinkPad firmware release %s "
8725 "doesn't match the known patterns\n",
8726 ec_fw_string);
8727 pr_notice("please report this to %s\n",
8728 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008729 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008730 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008731 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008732 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008733
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008734 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Manoj Iyer23b05312012-03-09 17:32:24 -06008735 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008736 tp->model_str = kstrdup(s, GFP_KERNEL);
8737 if (!tp->model_str)
8738 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05008739 } else {
8740 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8741 if (s && !(strnicmp(s, "Lenovo", 6))) {
8742 tp->model_str = kstrdup(s, GFP_KERNEL);
8743 if (!tp->model_str)
8744 return -ENOMEM;
8745 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008746 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008747
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008748 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8749 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8750 if (s && !tp->nummodel_str)
8751 return -ENOMEM;
8752
8753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754}
8755
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008756static int __init probe_for_thinkpad(void)
8757{
8758 int is_thinkpad;
8759
8760 if (acpi_disabled)
8761 return -ENODEV;
8762
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008763 /* It would be dangerous to run the driver in this case */
8764 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8765 return -ENODEV;
8766
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008767 /*
8768 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008769 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008770 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008771 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8772 (thinkpad_id.ec_model != 0) ||
8773 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008774
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008775 /* The EC handler is required */
8776 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008777 if (!ec_handle) {
8778 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008779 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008780 return -ENODEV;
8781 }
8782
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008783 if (!is_thinkpad && !force_load)
8784 return -ENODEV;
8785
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008786 return 0;
8787}
8788
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008789static void __init thinkpad_acpi_init_banner(void)
8790{
Joe Perches0978e012011-04-04 10:06:25 -07008791 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8792 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008793
Joe Perches0978e012011-04-04 10:06:25 -07008794 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008795 (thinkpad_id.bios_version_str) ?
8796 thinkpad_id.bios_version_str : "unknown",
8797 (thinkpad_id.ec_version_str) ?
8798 thinkpad_id.ec_version_str : "unknown");
8799
8800 BUG_ON(!thinkpad_id.vendor);
8801
8802 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008803 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008804 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8805 "IBM" : ((thinkpad_id.vendor ==
8806 PCI_VENDOR_ID_LENOVO) ?
8807 "Lenovo" : "Unknown vendor"),
8808 thinkpad_id.model_str,
8809 (thinkpad_id.nummodel_str) ?
8810 thinkpad_id.nummodel_str : "unknown");
8811}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008812
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008813/* Module init, exit, parameters */
8814
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008815static struct ibm_init_struct ibms_init[] __initdata = {
8816 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008817 .data = &thinkpad_acpi_driver_data,
8818 },
8819 {
8820 .init = hotkey_init,
8821 .data = &hotkey_driver_data,
8822 },
8823 {
8824 .init = bluetooth_init,
8825 .data = &bluetooth_driver_data,
8826 },
8827 {
8828 .init = wan_init,
8829 .data = &wan_driver_data,
8830 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008831 {
8832 .init = uwb_init,
8833 .data = &uwb_driver_data,
8834 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008835#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008836 {
8837 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008838 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008839 .data = &video_driver_data,
8840 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008841#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008842 {
8843 .init = light_init,
8844 .data = &light_driver_data,
8845 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008846 {
8847 .init = cmos_init,
8848 .data = &cmos_driver_data,
8849 },
8850 {
8851 .init = led_init,
8852 .data = &led_driver_data,
8853 },
8854 {
8855 .init = beep_init,
8856 .data = &beep_driver_data,
8857 },
8858 {
8859 .init = thermal_init,
8860 .data = &thermal_driver_data,
8861 },
8862 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008863 .init = brightness_init,
8864 .data = &brightness_driver_data,
8865 },
8866 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008867 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008868 .data = &volume_driver_data,
8869 },
8870 {
8871 .init = fan_init,
8872 .data = &fan_driver_data,
8873 },
David Henningsson420f9732013-10-16 23:10:31 +02008874 {
8875 .init = mute_led_init,
8876 .data = &mute_led_driver_data,
8877 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008878};
8879
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008880static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8881{
8882 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008883 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008884
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008885 if (!kp || !kp->name || !val)
8886 return -EINVAL;
8887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008888 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8889 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008890 WARN_ON(ibm == NULL);
8891
8892 if (!ibm || !ibm->name)
8893 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008894
8895 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8896 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008897 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008898 strcpy(ibms_init[i].param, val);
8899 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008900 return 0;
8901 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008902 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008903
8904 return -EINVAL;
8905}
8906
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008907module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008908MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008909 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008910
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008911module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008912MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008913
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008914module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008915MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008916 "Attempts to load the driver even on a "
8917 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008918
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008919module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008920MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008921 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008922
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008923module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008924MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008925 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008926 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008927
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008928module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008929MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008930 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008931
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008932#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008933module_param_named(volume_mode, volume_mode, uint, 0444);
8934MODULE_PARM_DESC(volume_mode,
8935 "Selects volume control strategy: "
8936 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8937
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008938module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8939MODULE_PARM_DESC(volume_capabilities,
8940 "Selects the mixer capabilites: "
8941 "0=auto, 1=volume and mute, 2=mute only");
8942
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008943module_param_named(volume_control, volume_control_allowed, bool, 0444);
8944MODULE_PARM_DESC(volume_control,
8945 "Enables software override for the console audio "
8946 "control when true");
8947
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008948/* ALSA module API parameters */
8949module_param_named(index, alsa_index, int, 0444);
8950MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8951module_param_named(id, alsa_id, charp, 0444);
8952MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8953module_param_named(enable, alsa_enable, bool, 0444);
8954MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008955#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008956
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008957#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008958 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008959 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008960 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008961
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008962TPACPI_PARAM(hotkey);
8963TPACPI_PARAM(bluetooth);
8964TPACPI_PARAM(video);
8965TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008966TPACPI_PARAM(cmos);
8967TPACPI_PARAM(led);
8968TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008969TPACPI_PARAM(brightness);
8970TPACPI_PARAM(volume);
8971TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008972
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008973#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008974module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008975MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8976module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8977MODULE_PARM_DESC(wlsw_state,
8978 "Initial state of the emulated WLSW switch");
8979
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008980module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008981MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8982module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8983MODULE_PARM_DESC(bluetooth_state,
8984 "Initial state of the emulated bluetooth switch");
8985
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008986module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008987MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8988module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8989MODULE_PARM_DESC(wwan_state,
8990 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008991
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008992module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008993MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8994module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8995MODULE_PARM_DESC(uwb_state,
8996 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008997#endif
8998
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008999static void thinkpad_acpi_module_exit(void)
9000{
9001 struct ibm_struct *ibm, *itmp;
9002
9003 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9004
9005 list_for_each_entry_safe_reverse(ibm, itmp,
9006 &tpacpi_all_drivers,
9007 all_drivers) {
9008 ibm_exit(ibm);
9009 }
9010
9011 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9012
9013 if (tpacpi_inputdev) {
9014 if (tp_features.input_device_registered)
9015 input_unregister_device(tpacpi_inputdev);
9016 else
9017 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009018 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009019 }
9020
9021 if (tpacpi_hwmon)
9022 hwmon_device_unregister(tpacpi_hwmon);
9023
9024 if (tp_features.sensors_pdev_attrs_registered)
9025 device_remove_file(&tpacpi_sensors_pdev->dev,
9026 &dev_attr_thinkpad_acpi_pdev_name);
9027 if (tpacpi_sensors_pdev)
9028 platform_device_unregister(tpacpi_sensors_pdev);
9029 if (tpacpi_pdev)
9030 platform_device_unregister(tpacpi_pdev);
9031
9032 if (tp_features.sensors_pdrv_attrs_registered)
9033 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9034 if (tp_features.platform_drv_attrs_registered)
9035 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9036
9037 if (tp_features.sensors_pdrv_registered)
9038 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9039
9040 if (tp_features.platform_drv_registered)
9041 platform_driver_unregister(&tpacpi_pdriver);
9042
9043 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009044 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009045
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009046 if (tpacpi_wq)
9047 destroy_workqueue(tpacpi_wq);
9048
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009049 kfree(thinkpad_id.bios_version_str);
9050 kfree(thinkpad_id.ec_version_str);
9051 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009052 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009053}
9054
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009055
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009056static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057{
9058 int ret, i;
9059
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009060 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9061
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009062 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009063
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009064 ret = get_thinkpad_model_data(&thinkpad_id);
9065 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009066 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009067 thinkpad_acpi_module_exit();
9068 return ret;
9069 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009070 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009071 if (ret) {
9072 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009073 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009076 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009077
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009078 thinkpad_acpi_init_banner();
9079 tpacpi_check_outdated_fw();
9080
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009081 TPACPI_ACPIHANDLE_INIT(ecrd);
9082 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009084 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9085 if (!tpacpi_wq) {
9086 thinkpad_acpi_module_exit();
9087 return -ENOMEM;
9088 }
9089
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009090 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009092 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009093 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094 return -ENODEV;
9095 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009096
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009097 ret = platform_driver_register(&tpacpi_pdriver);
9098 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009099 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009100 thinkpad_acpi_module_exit();
9101 return ret;
9102 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009103 tp_features.platform_drv_registered = 1;
9104
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009105 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9106 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009107 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009108 thinkpad_acpi_module_exit();
9109 return ret;
9110 }
9111 tp_features.sensors_pdrv_registered = 1;
9112
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009113 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009114 if (!ret) {
9115 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009116 ret = tpacpi_create_driver_attributes(
9117 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009118 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009119 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009120 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009121 thinkpad_acpi_module_exit();
9122 return ret;
9123 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009124 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009125
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009126
9127 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009128 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009129 NULL, 0);
9130 if (IS_ERR(tpacpi_pdev)) {
9131 ret = PTR_ERR(tpacpi_pdev);
9132 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009133 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009134 thinkpad_acpi_module_exit();
9135 return ret;
9136 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009137 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009138 TPACPI_HWMON_DRVR_NAME,
9139 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009140 if (IS_ERR(tpacpi_sensors_pdev)) {
9141 ret = PTR_ERR(tpacpi_sensors_pdev);
9142 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009143 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009144 thinkpad_acpi_module_exit();
9145 return ret;
9146 }
9147 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9148 &dev_attr_thinkpad_acpi_pdev_name);
9149 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009150 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009151 thinkpad_acpi_module_exit();
9152 return ret;
9153 }
9154 tp_features.sensors_pdev_attrs_registered = 1;
9155 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009156 if (IS_ERR(tpacpi_hwmon)) {
9157 ret = PTR_ERR(tpacpi_hwmon);
9158 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009159 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009160 thinkpad_acpi_module_exit();
9161 return ret;
9162 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009163 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009164 tpacpi_inputdev = input_allocate_device();
9165 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009166 thinkpad_acpi_module_exit();
9167 return -ENOMEM;
9168 } else {
9169 /* Prepare input device, but don't register */
9170 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009171 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009172 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009173 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009174 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9175 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009176 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009177 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009178
9179 /* Init subdriver dependencies */
9180 tpacpi_detect_brightness_capabilities();
9181
9182 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009183 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9184 ret = ibm_init(&ibms_init[i]);
9185 if (ret >= 0 && *ibms_init[i].param)
9186 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009188 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189 return ret;
9190 }
9191 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009192
9193 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9194
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009195 ret = input_register_device(tpacpi_inputdev);
9196 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009197 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009198 thinkpad_acpi_module_exit();
9199 return ret;
9200 } else {
9201 tp_features.input_device_registered = 1;
9202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203
9204 return 0;
9205}
9206
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009207MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9208
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009209/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009210 * This will autoload the driver in almost every ThinkPad
9211 * in widespread use.
9212 *
9213 * Only _VERY_ old models, like the 240, 240x and 570 lack
9214 * the HKEY event interface.
9215 */
9216MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9217
9218/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009219 * DMI matching for module autoloading
9220 *
9221 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9222 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9223 *
9224 * Only models listed in thinkwiki will be supported, so add yours
9225 * if it is not there yet.
9226 */
9227#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009228 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009229
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009230/* Ancient thinkpad BIOSes have to be identified by
9231 * BIOS type or model number, and there are far less
9232 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009233IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009234
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009235MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9236MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009237MODULE_DESCRIPTION(TPACPI_DESC);
9238MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009239MODULE_LICENSE("GPL");
9240
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009241module_init(thinkpad_acpi_module_init);
9242module_exit(thinkpad_acpi_module_exit);