blob: 359c87b3b1b23032b1cbaff30ac12c47c6164630 [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
Henrique de Moraes Holschuh5d2eb142009-12-15 21:51:13 -020026#define TPACPI_VERSION "0.24"
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
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* ThinkPad CMOS commands */
93#define TP_CMOS_VOLUME_DOWN 0
94#define TP_CMOS_VOLUME_UP 1
95#define TP_CMOS_VOLUME_MUTE 2
96#define TP_CMOS_BRIGHTNESS_UP 4
97#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030098#define TP_CMOS_THINKLIGHT_ON 12
99#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200100
101/* NVRAM Addresses */
102enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
108};
109
110/* NVRAM bit masks */
111enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125};
126
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300127/* Misc NVRAM-related */
128enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130};
131
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400133#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300135#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
137/* Input IDs */
138#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139#define TPACPI_HKEY_INPUT_VERSION 0x4101
140
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200141/* ACPI \WGSV commands */
142enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
147};
148
149/* TP_ACPI_WGSV_GET_STATE bits */
150enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
161};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300163/* HKEY events */
164enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
172
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
180
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
184
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300187 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
188 or port replicator */
189 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
190 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300191
192 /* User-interface events */
193 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
194 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
195 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
196 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
197 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
198 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
199 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
200
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300201 /* Key-related user-interface events */
202 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
203 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
204
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300205 /* Thermal events */
206 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
210 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
211
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100212 /* AC-related events */
213 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300214
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300215 /* Misc */
216 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
217};
218
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200219/****************************************************************************
220 * Main driver
221 */
222
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200223#define TPACPI_NAME "thinkpad"
224#define TPACPI_DESC "ThinkPad ACPI Extras"
225#define TPACPI_FILE TPACPI_NAME "_acpi"
226#define TPACPI_URL "http://ibm-acpi.sf.net/"
227#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200228
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200229#define TPACPI_PROC_DIR "ibm"
230#define TPACPI_ACPI_EVENT_PREFIX "ibm"
231#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300232#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200233#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200234
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300235#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300236#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300237
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200238#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200239
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000240/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200241#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000242#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243#define TPACPI_DBG_INIT 0x0001
244#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +0000245#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000246#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000247#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000248#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200249#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200251#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
252#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
253#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200254
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255
256/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200257 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200258 */
259
260struct ibm_struct;
261
262struct tp_acpi_drv_struct {
263 const struct acpi_device_id *hid;
264 struct acpi_driver *driver;
265
266 void (*notify) (struct ibm_struct *, u32);
267 acpi_handle *handle;
268 u32 type;
269 struct acpi_device *device;
270};
271
272struct ibm_struct {
273 char *name;
274
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200275 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200276 int (*write) (char *);
277 void (*exit) (void);
278 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200279 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200280 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200281
282 struct list_head all_drivers;
283
284 struct tp_acpi_drv_struct *acpi;
285
286 struct {
287 u8 acpi_driver_registered:1;
288 u8 acpi_notify_installed:1;
289 u8 proc_created:1;
290 u8 init_called:1;
291 u8 experimental:1;
292 } flags;
293};
294
295struct ibm_init_struct {
296 char param[32];
297
298 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400299 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200300 struct ibm_struct *data;
301};
302
303static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200304 u32 bluetooth:1;
305 u32 hotkey:1;
306 u32 hotkey_mask:1;
307 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200308 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200309 u32 light:1;
310 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300311 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300312 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200313 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200314 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200315 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300316 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300317 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200318 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200319 u32 input_device_registered:1;
320 u32 platform_drv_registered:1;
321 u32 platform_drv_attrs_registered:1;
322 u32 sensors_pdrv_registered:1;
323 u32 sensors_pdrv_attrs_registered:1;
324 u32 sensors_pdev_attrs_registered:1;
325 u32 hotkey_poll_active:1;
326} tp_features;
327
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300328static struct {
329 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200330 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300331} tp_warned;
332
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200333struct thinkpad_id_data {
334 unsigned int vendor; /* ThinkPad vendor:
335 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
336
337 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
338 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
339
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300340 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200341 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300342 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
343 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200344
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300345 char *model_str; /* ThinkPad T43 */
346 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200347};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200348static struct thinkpad_id_data thinkpad_id;
349
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300350static enum {
351 TPACPI_LIFE_INIT = 0,
352 TPACPI_LIFE_RUNNING,
353 TPACPI_LIFE_EXITING,
354} tpacpi_lifecycle;
355
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200356static int experimental;
357static u32 dbg_level;
358
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300359static struct workqueue_struct *tpacpi_wq;
360
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000361enum led_status_t {
362 TPACPI_LED_OFF = 0,
363 TPACPI_LED_ON,
364 TPACPI_LED_BLINK,
365};
366
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300367/* Special LED class that can defer work */
368struct tpacpi_led_classdev {
369 struct led_classdev led_classdev;
370 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000371 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800372 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300373};
374
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300375/* brightness level capabilities */
376static unsigned int bright_maxlvl; /* 0 = unknown */
377
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200378#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
379static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030380static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200381static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030382static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200383static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030384static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200385static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030386static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200387#endif
388
389
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000390/*************************************************************************
391 * Debugging helpers
392 */
393
Joe Perches0978e012011-04-04 10:06:25 -0700394#define dbg_printk(a_dbg_level, format, arg...) \
395do { \
396 if (dbg_level & (a_dbg_level)) \
397 printk(KERN_DEBUG pr_fmt("%s: " format), \
398 __func__, ##arg); \
399} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000400
401#ifdef CONFIG_THINKPAD_ACPI_DEBUG
402#define vdbg_printk dbg_printk
403static const char *str_supported(int is_supported);
404#else
Joe Perches0978e012011-04-04 10:06:25 -0700405static inline const char *str_supported(int is_supported) { return ""; }
406#define vdbg_printk(a_dbg_level, format, arg...) \
407 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000408#endif
409
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000410static void tpacpi_log_usertask(const char * const what)
411{
Joe Perches0978e012011-04-04 10:06:25 -0700412 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
413 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000414}
415
Joe Perches0978e012011-04-04 10:06:25 -0700416#define tpacpi_disclose_usertask(what, format, arg...) \
417do { \
418 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
419 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
420 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
421 what, task_tgid_vnr(current), ## arg); \
422 } \
423} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000424
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300425/*
426 * Quirk handling helpers
427 *
428 * ThinkPad IDs and versions seen in the field so far
429 * are two-characters from the set [0-9A-Z], i.e. base 36.
430 *
431 * We use values well outside that range as specials.
432 */
433
434#define TPACPI_MATCH_ANY 0xffffU
435#define TPACPI_MATCH_UNKNOWN 0U
436
437/* TPID('1', 'Y') == 0x5931 */
438#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
439
440#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_IBM, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
445
446#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPID(__id1, __id2), \
449 .ec = TPACPI_MATCH_ANY, \
450 .quirks = (__quirk) }
451
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200452#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
453 { .vendor = PCI_VENDOR_ID_LENOVO, \
454 .bios = TPACPI_MATCH_ANY, \
455 .ec = TPID(__id1, __id2), \
456 .quirks = (__quirk) }
457
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300458struct tpacpi_quirk {
459 unsigned int vendor;
460 u16 bios;
461 u16 ec;
462 unsigned long quirks;
463};
464
465/**
466 * tpacpi_check_quirks() - search BIOS/EC version on a list
467 * @qlist: array of &struct tpacpi_quirk
468 * @qlist_size: number of elements in @qlist
469 *
470 * Iterates over a quirks list until one is found that matches the
471 * ThinkPad's vendor, BIOS and EC model.
472 *
473 * Returns 0 if nothing matches, otherwise returns the quirks field of
474 * the matching &struct tpacpi_quirk entry.
475 *
476 * The match criteria is: vendor, ec and bios much match.
477 */
478static unsigned long __init tpacpi_check_quirks(
479 const struct tpacpi_quirk *qlist,
480 unsigned int qlist_size)
481{
482 while (qlist_size) {
483 if ((qlist->vendor == thinkpad_id.vendor ||
484 qlist->vendor == TPACPI_MATCH_ANY) &&
485 (qlist->bios == thinkpad_id.bios_model ||
486 qlist->bios == TPACPI_MATCH_ANY) &&
487 (qlist->ec == thinkpad_id.ec_model ||
488 qlist->ec == TPACPI_MATCH_ANY))
489 return qlist->quirks;
490
491 qlist_size--;
492 qlist++;
493 }
494 return 0;
495}
496
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300497static inline bool __pure __init tpacpi_is_lenovo(void)
498{
499 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
500}
501
502static inline bool __pure __init tpacpi_is_ibm(void)
503{
504 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
505}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300507/****************************************************************************
508 ****************************************************************************
509 *
510 * ACPI Helpers and device model
511 *
512 ****************************************************************************
513 ****************************************************************************/
514
515/*************************************************************************
516 * ACPI basic handles
517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300519static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300520static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200522#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700524 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300525 &parent##_handle; \
526 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200528TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
529TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200531TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
532 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
534 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200537TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538 "^HKEY", /* R30, R31 */
539 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542/*************************************************************************
543 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200544 */
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700547 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400550 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200551 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400552 struct acpi_buffer result, *resultp;
553 union acpi_object out_obj;
554 acpi_status status;
555 va_list ap;
556 char res_type;
557 int success;
558 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700561 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
563 }
564
565 if (*fmt == 'q') {
566 quiet = 1;
567 fmt++;
568 } else
569 quiet = 0;
570
571 res_type = *(fmt++);
572
573 params.count = 0;
574 params.pointer = &in_objs[0];
575
576 va_start(ap, fmt);
577 while (*fmt) {
578 char c = *(fmt++);
579 switch (c) {
580 case 'd': /* int */
581 in_objs[params.count].integer.value = va_arg(ap, int);
582 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
583 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400584 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 default:
Joe Perches0978e012011-04-04 10:06:25 -0700586 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100588 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590 }
591 }
592 va_end(ap);
593
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400594 if (res_type != 'v') {
595 result.length = sizeof(out_obj);
596 result.pointer = &out_obj;
597 resultp = &result;
598 } else
599 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300605 success = (status == AE_OK &&
606 out_obj.type == ACPI_TYPE_INTEGER);
607 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700608 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400610 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 success = status == AE_OK;
612 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400613 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 default:
Joe Perches0978e012011-04-04 10:06:25 -0700615 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 "with invalid format character '%c'\n", res_type);
617 return 0;
618 }
619
620 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700621 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300622 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 return success;
625}
626
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200627static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628{
629 int v;
630
631 if (ecrd_handle) {
632 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
633 return 0;
634 *p = v;
635 } else {
636 if (ec_read(i, p) < 0)
637 return 0;
638 }
639
640 return 1;
641}
642
643static int acpi_ec_write(int i, u8 v)
644{
645 if (ecwr_handle) {
646 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
647 return 0;
648 } else {
649 if (ec_write(i, v) < 0)
650 return 0;
651 }
652
653 return 1;
654}
655
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300656static int issue_thinkpad_cmos_command(int cmos_cmd)
657{
658 if (!cmos_handle)
659 return -ENXIO;
660
661 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
662 return -EIO;
663
664 return 0;
665}
666
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300667/*************************************************************************
668 * ACPI device model
669 */
670
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200671#define TPACPI_ACPIHANDLE_INIT(object) \
672 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300673 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200674
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300675static void __init drv_acpi_handle_init(const char *name,
676 acpi_handle *handle, const acpi_handle parent,
677 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300678{
679 int i;
680 acpi_status status;
681
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300682 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
683 name);
684
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300685 for (i = 0; i < num_paths; i++) {
686 status = acpi_get_handle(parent, paths[i], handle);
687 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300688 dbg_printk(TPACPI_DBG_INIT,
689 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5a2010-05-16 19:45:54 -0300690 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300691 return;
692 }
693 }
694
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300695 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
696 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300697 *handle = NULL;
698}
699
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300700static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
701 u32 level, void *context, void **return_value)
702{
703 *(acpi_handle *)return_value = handle;
704
705 return AE_CTRL_TERMINATE;
706}
707
708static void __init tpacpi_acpi_handle_locate(const char *name,
709 const char *hid,
710 acpi_handle *handle)
711{
712 acpi_status status;
713 acpi_handle device_found;
714
715 BUG_ON(!name || !hid || !handle);
716 vdbg_printk(TPACPI_DBG_INIT,
717 "trying to locate ACPI handle for %s, using HID %s\n",
718 name, hid);
719
720 memset(&device_found, 0, sizeof(device_found));
721 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
722 (void *)name, &device_found);
723
724 *handle = NULL;
725
726 if (ACPI_SUCCESS(status)) {
727 *handle = device_found;
728 dbg_printk(TPACPI_DBG_INIT,
729 "Found ACPI handle for %s\n", name);
730 } else {
731 vdbg_printk(TPACPI_DBG_INIT,
732 "Could not locate an ACPI handle for %s: %s\n",
733 name, acpi_format_exception(status));
734 }
735}
736
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300737static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300738{
739 struct ibm_struct *ibm = data;
740
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300741 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
742 return;
743
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300744 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300745 return;
746
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300747 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300748}
749
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300750static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300751{
752 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300753 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300754
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300755 BUG_ON(!ibm->acpi);
756
757 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300758 return 0;
759
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300760 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300761 "setting up ACPI notify for %s\n", ibm->name);
762
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300763 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
764 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700765 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300766 return -ENODEV;
767 }
768
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700769 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300770 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200771 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300772 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300773
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300774 status = acpi_install_notify_handler(*ibm->acpi->handle,
775 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300776 if (ACPI_FAILURE(status)) {
777 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700778 pr_notice("another device driver is already "
779 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300780 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700781 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300782 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300783 }
784 return -ENODEV;
785 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300786 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300787 return 0;
788}
789
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300790static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300791{
792 return 0;
793}
794
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300795static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300796{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300797 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300799 dbg_printk(TPACPI_DBG_INIT,
800 "registering %s as an ACPI driver\n", ibm->name);
801
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300802 BUG_ON(!ibm->acpi);
803
804 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
805 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700806 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300807 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300808 }
809
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200810 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300811 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200812
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300813 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300814
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300815 rc = acpi_bus_register_driver(ibm->acpi->driver);
816 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700817 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200818 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300819 kfree(ibm->acpi->driver);
820 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300821 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300822 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300823
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300824 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300825}
826
827
828/****************************************************************************
829 ****************************************************************************
830 *
831 * Procfs Helpers
832 *
833 ****************************************************************************
834 ****************************************************************************/
835
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200836static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300837{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200838 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300839
840 if (!ibm || !ibm->read)
841 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200842 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300843}
844
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200845static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300846{
Al Virod9dda782013-03-31 18:16:14 -0400847 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200848}
849
850static ssize_t dispatch_proc_write(struct file *file,
851 const char __user *userbuf,
852 size_t count, loff_t *pos)
853{
Al Virod9dda782013-03-31 18:16:14 -0400854 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300855 char *kernbuf;
856 int ret;
857
858 if (!ibm || !ibm->write)
859 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300860 if (count > PAGE_SIZE - 2)
861 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300862
863 kernbuf = kmalloc(count + 2, GFP_KERNEL);
864 if (!kernbuf)
865 return -ENOMEM;
866
867 if (copy_from_user(kernbuf, userbuf, count)) {
868 kfree(kernbuf);
869 return -EFAULT;
870 }
871
872 kernbuf[count] = 0;
873 strcat(kernbuf, ",");
874 ret = ibm->write(kernbuf);
875 if (ret == 0)
876 ret = count;
877
878 kfree(kernbuf);
879
880 return ret;
881}
882
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200883static const struct file_operations dispatch_proc_fops = {
884 .owner = THIS_MODULE,
885 .open = dispatch_proc_open,
886 .read = seq_read,
887 .llseek = seq_lseek,
888 .release = single_release,
889 .write = dispatch_proc_write,
890};
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static char *next_cmd(char **cmds)
893{
894 char *start = *cmds;
895 char *end;
896
897 while ((end = strchr(start, ',')) && end == start)
898 start = end + 1;
899
900 if (!end)
901 return NULL;
902
903 *end = 0;
904 *cmds = end + 1;
905 return start;
906}
907
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300908
909/****************************************************************************
910 ****************************************************************************
911 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300912 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300913 *
914 ****************************************************************************
915 ****************************************************************************/
916
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300917static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300918static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700919static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300920static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300921static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200922static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300923
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200924#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200925static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200926{
927 struct ibm_struct *ibm, *itmp;
928
929 list_for_each_entry_safe(ibm, itmp,
930 &tpacpi_all_drivers,
931 all_drivers) {
932 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200933 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200934 }
935
936 return 0;
937}
938
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200939static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300940{
941 struct ibm_struct *ibm, *itmp;
942
943 list_for_each_entry_safe(ibm, itmp,
944 &tpacpi_all_drivers,
945 all_drivers) {
946 if (ibm->resume)
947 (ibm->resume)();
948 }
949
950 return 0;
951}
Rafael J. Wysocki3567a4e2012-08-09 23:00:13 +0200952#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300953
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200954static SIMPLE_DEV_PM_OPS(tpacpi_pm,
955 tpacpi_suspend_handler, tpacpi_resume_handler);
956
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200957static void tpacpi_shutdown_handler(struct platform_device *pdev)
958{
959 struct ibm_struct *ibm, *itmp;
960
961 list_for_each_entry_safe(ibm, itmp,
962 &tpacpi_all_drivers,
963 all_drivers) {
964 if (ibm->shutdown)
965 (ibm->shutdown)();
966 }
967}
968
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300969static struct platform_driver tpacpi_pdriver = {
970 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200971 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300972 .owner = THIS_MODULE,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200973 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300974 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200975 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300976};
977
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300978static struct platform_driver tpacpi_hwmon_pdriver = {
979 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200980 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300981 .owner = THIS_MODULE,
982 },
983};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300984
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300985/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300986 * sysfs support helpers
987 */
988
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200989struct attribute_set {
990 unsigned int members, max_members;
991 struct attribute_group group;
992};
993
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300994struct attribute_set_obj {
995 struct attribute_set s;
996 struct attribute *a;
997} __attribute__((packed));
998
999static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001000 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001001{
1002 struct attribute_set_obj *sobj;
1003
1004 if (max_members == 0)
1005 return NULL;
1006
1007 /* Allocates space for implicit NULL at the end too */
1008 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1009 max_members * sizeof(struct attribute *),
1010 GFP_KERNEL);
1011 if (!sobj)
1012 return NULL;
1013 sobj->s.max_members = max_members;
1014 sobj->s.group.attrs = &sobj->a;
1015 sobj->s.group.name = name;
1016
1017 return &sobj->s;
1018}
1019
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001020#define destroy_attr_set(_set) \
1021 kfree(_set);
1022
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001023/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001024static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001025{
1026 if (!s || !attr)
1027 return -EINVAL;
1028
1029 if (s->members >= s->max_members)
1030 return -ENOMEM;
1031
1032 s->group.attrs[s->members] = attr;
1033 s->members++;
1034
1035 return 0;
1036}
1037
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001038static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001039 struct attribute **attr,
1040 unsigned int count)
1041{
1042 int i, res;
1043
1044 for (i = 0; i < count; i++) {
1045 res = add_to_attr_set(s, attr[i]);
1046 if (res)
1047 return res;
1048 }
1049
1050 return 0;
1051}
1052
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001053static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001054{
1055 sysfs_remove_group(kobj, &s->group);
1056 destroy_attr_set(s);
1057}
1058
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001059#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1060 sysfs_create_group(_kobj, &_attr_set->group)
1061
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001062static int parse_strtoul(const char *buf,
1063 unsigned long max, unsigned long *value)
1064{
1065 char *endp;
1066
André Goddard Rosae7d28602009-12-14 18:01:06 -08001067 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1068 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001069 if (*endp || *value > max)
1070 return -EINVAL;
1071
1072 return 0;
1073}
1074
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001075static void tpacpi_disable_brightness_delay(void)
1076{
1077 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001078 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03001079}
1080
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001081static void printk_deprecated_attribute(const char * const what,
1082 const char * const details)
1083{
1084 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001085 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001086 "will be removed. %s\n",
1087 what, details);
1088}
1089
Johannes Berg19d337d2009-06-02 13:01:37 +02001090/*************************************************************************
1091 * rfkill and radio control support helpers
1092 */
1093
1094/*
1095 * ThinkPad-ACPI firmware handling model:
1096 *
1097 * WLSW (master wireless switch) is event-driven, and is common to all
1098 * firmware-controlled radios. It cannot be controlled, just monitored,
1099 * as expected. It overrides all radio state in firmware
1100 *
1101 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1102 * (TODO: verify how WLSW interacts with the returned radio state).
1103 *
1104 * The only time there are shadow radio state changes, is when
1105 * masked-off hotkeys are used.
1106 */
1107
1108/*
1109 * Internal driver API for radio state:
1110 *
1111 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1112 * bool: true means radio blocked (off)
1113 */
1114enum tpacpi_rfkill_state {
1115 TPACPI_RFK_RADIO_OFF = 0,
1116 TPACPI_RFK_RADIO_ON
1117};
1118
1119/* rfkill switches */
1120enum tpacpi_rfk_id {
1121 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1122 TPACPI_RFK_WWAN_SW_ID,
1123 TPACPI_RFK_UWB_SW_ID,
1124 TPACPI_RFK_SW_MAX
1125};
1126
1127static const char *tpacpi_rfkill_names[] = {
1128 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1129 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1130 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1131 [TPACPI_RFK_SW_MAX] = NULL
1132};
1133
1134/* ThinkPad-ACPI rfkill subdriver */
1135struct tpacpi_rfk {
1136 struct rfkill *rfkill;
1137 enum tpacpi_rfk_id id;
1138 const struct tpacpi_rfk_ops *ops;
1139};
1140
1141struct tpacpi_rfk_ops {
1142 /* firmware interface */
1143 int (*get_status)(void);
1144 int (*set_status)(const enum tpacpi_rfkill_state);
1145};
1146
1147static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1148
1149/* Query FW and update rfkill sw state for a given rfkill switch */
1150static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1151{
1152 int status;
1153
1154 if (!tp_rfk)
1155 return -ENODEV;
1156
1157 status = (tp_rfk->ops->get_status)();
1158 if (status < 0)
1159 return status;
1160
1161 rfkill_set_sw_state(tp_rfk->rfkill,
1162 (status == TPACPI_RFK_RADIO_OFF));
1163
1164 return status;
1165}
1166
1167/* Query FW and update rfkill sw state for all rfkill switches */
1168static void tpacpi_rfk_update_swstate_all(void)
1169{
1170 unsigned int i;
1171
1172 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1173 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1174}
1175
1176/*
1177 * Sync the HW-blocking state of all rfkill switches,
1178 * do notice it causes the rfkill core to schedule uevents
1179 */
1180static void tpacpi_rfk_update_hwblock_state(bool blocked)
1181{
1182 unsigned int i;
1183 struct tpacpi_rfk *tp_rfk;
1184
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1186 tp_rfk = tpacpi_rfkill_switches[i];
1187 if (tp_rfk) {
1188 if (rfkill_set_hw_state(tp_rfk->rfkill,
1189 blocked)) {
1190 /* ignore -- we track sw block */
1191 }
1192 }
1193 }
1194}
1195
1196/* Call to get the WLSW state from the firmware */
1197static int hotkey_get_wlsw(void);
1198
1199/* Call to query WLSW state and update all rfkill switches */
1200static bool tpacpi_rfk_check_hwblock_state(void)
1201{
1202 int res = hotkey_get_wlsw();
1203 int hw_blocked;
1204
1205 /* When unknown or unsupported, we have to assume it is unblocked */
1206 if (res < 0)
1207 return false;
1208
1209 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1210 tpacpi_rfk_update_hwblock_state(hw_blocked);
1211
1212 return hw_blocked;
1213}
1214
1215static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1216{
1217 struct tpacpi_rfk *tp_rfk = data;
1218 int res;
1219
1220 dbg_printk(TPACPI_DBG_RFKILL,
1221 "request to change radio state to %s\n",
1222 blocked ? "blocked" : "unblocked");
1223
1224 /* try to set radio state */
1225 res = (tp_rfk->ops->set_status)(blocked ?
1226 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1227
1228 /* and update the rfkill core with whatever the FW really did */
1229 tpacpi_rfk_update_swstate(tp_rfk);
1230
1231 return (res < 0) ? res : 0;
1232}
1233
1234static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1235 .set_block = tpacpi_rfk_hook_set_block,
1236};
1237
1238static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1239 const struct tpacpi_rfk_ops *tp_rfkops,
1240 const enum rfkill_type rfktype,
1241 const char *name,
1242 const bool set_default)
1243{
1244 struct tpacpi_rfk *atp_rfk;
1245 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001246 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001247 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001248 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001249
1250 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1251
Johannes Berg19d337d2009-06-02 13:01:37 +02001252 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1253 if (atp_rfk)
1254 atp_rfk->rfkill = rfkill_alloc(name,
1255 &tpacpi_pdev->dev,
1256 rfktype,
1257 &tpacpi_rfk_rfkill_ops,
1258 atp_rfk);
1259 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001260 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001261 kfree(atp_rfk);
1262 return -ENOMEM;
1263 }
1264
1265 atp_rfk->id = id;
1266 atp_rfk->ops = tp_rfkops;
1267
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001268 sw_status = (tp_rfkops->get_status)();
1269 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001270 pr_err("failed to read initial state for %s, error %d\n",
1271 name, sw_status);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001272 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001273 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001274 if (set_default) {
1275 /* try to keep the initial state, since we ask the
1276 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001277 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa1322009-06-08 13:27:27 +01001278 }
1279 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001280 hw_state = tpacpi_rfk_check_hwblock_state();
1281 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001282
1283 res = rfkill_register(atp_rfk->rfkill);
1284 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001285 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001286 rfkill_destroy(atp_rfk->rfkill);
1287 kfree(atp_rfk);
1288 return res;
1289 }
1290
1291 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001292
Joe Perches0978e012011-04-04 10:06:25 -07001293 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001294 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001295 return 0;
1296}
1297
1298static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1299{
1300 struct tpacpi_rfk *tp_rfk;
1301
1302 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1303
1304 tp_rfk = tpacpi_rfkill_switches[id];
1305 if (tp_rfk) {
1306 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001307 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001308 tpacpi_rfkill_switches[id] = NULL;
1309 kfree(tp_rfk);
1310 }
1311}
1312
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001313static void printk_deprecated_rfkill_attribute(const char * const what)
1314{
1315 printk_deprecated_attribute(what,
1316 "Please switch to generic rfkill before year 2010");
1317}
1318
Johannes Berg19d337d2009-06-02 13:01:37 +02001319/* sysfs <radio> enable ------------------------------------------------ */
1320static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1321 struct device_attribute *attr,
1322 char *buf)
1323{
1324 int status;
1325
1326 printk_deprecated_rfkill_attribute(attr->attr.name);
1327
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state()) {
1330 status = TPACPI_RFK_RADIO_OFF;
1331 } else {
1332 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1333 if (status < 0)
1334 return status;
1335 }
1336
1337 return snprintf(buf, PAGE_SIZE, "%d\n",
1338 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1339}
1340
1341static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1342 struct device_attribute *attr,
1343 const char *buf, size_t count)
1344{
1345 unsigned long t;
1346 int res;
1347
1348 printk_deprecated_rfkill_attribute(attr->attr.name);
1349
1350 if (parse_strtoul(buf, 1, &t))
1351 return -EINVAL;
1352
1353 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1354
1355 /* This is in the ABI... */
1356 if (tpacpi_rfk_check_hwblock_state() && !!t)
1357 return -EPERM;
1358
1359 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1360 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1361 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1362
1363 return (res < 0) ? res : count;
1364}
1365
1366/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001367static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001368{
Johannes Berg19d337d2009-06-02 13:01:37 +02001369 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001370 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001371 else {
1372 int status;
1373
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state()) {
1376 status = TPACPI_RFK_RADIO_OFF;
1377 } else {
1378 status = tpacpi_rfk_update_swstate(
1379 tpacpi_rfkill_switches[id]);
1380 if (status < 0)
1381 return status;
1382 }
1383
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001384 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001385 (status == TPACPI_RFK_RADIO_ON) ?
1386 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001387 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001388 }
1389
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001390 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001391}
1392
1393static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1394{
1395 char *cmd;
1396 int status = -1;
1397 int res = 0;
1398
1399 if (id >= TPACPI_RFK_SW_MAX)
1400 return -ENODEV;
1401
1402 while ((cmd = next_cmd(&buf))) {
1403 if (strlencmp(cmd, "enable") == 0)
1404 status = TPACPI_RFK_RADIO_ON;
1405 else if (strlencmp(cmd, "disable") == 0)
1406 status = TPACPI_RFK_RADIO_OFF;
1407 else
1408 return -EINVAL;
1409 }
1410
1411 if (status != -1) {
1412 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1413 (status == TPACPI_RFK_RADIO_ON) ?
1414 "enable" : "disable",
1415 tpacpi_rfkill_names[id]);
1416 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1417 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1418 }
1419
1420 return res;
1421}
1422
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001423/*************************************************************************
1424 * thinkpad-acpi driver attributes
1425 */
1426
1427/* interface_version --------------------------------------------------- */
1428static ssize_t tpacpi_driver_interface_version_show(
1429 struct device_driver *drv,
1430 char *buf)
1431{
1432 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1433}
1434
1435static DRIVER_ATTR(interface_version, S_IRUGO,
1436 tpacpi_driver_interface_version_show, NULL);
1437
1438/* debug_level --------------------------------------------------------- */
1439static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1440 char *buf)
1441{
1442 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1443}
1444
1445static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1446 const char *buf, size_t count)
1447{
1448 unsigned long t;
1449
1450 if (parse_strtoul(buf, 0xffff, &t))
1451 return -EINVAL;
1452
1453 dbg_level = t;
1454
1455 return count;
1456}
1457
1458static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1459 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1460
1461/* version ------------------------------------------------------------- */
1462static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1463 char *buf)
1464{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001465 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1466 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001467}
1468
1469static DRIVER_ATTR(version, S_IRUGO,
1470 tpacpi_driver_version_show, NULL);
1471
1472/* --------------------------------------------------------------------- */
1473
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001474#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1475
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001476/* wlsw_emulstate ------------------------------------------------------ */
1477static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1478 char *buf)
1479{
1480 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1481}
1482
1483static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1484 const char *buf, size_t count)
1485{
1486 unsigned long t;
1487
1488 if (parse_strtoul(buf, 1, &t))
1489 return -EINVAL;
1490
Johannes Berg19d337d2009-06-02 13:01:37 +02001491 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001492 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001493 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1494 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001495
1496 return count;
1497}
1498
1499static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1500 tpacpi_driver_wlsw_emulstate_show,
1501 tpacpi_driver_wlsw_emulstate_store);
1502
1503/* bluetooth_emulstate ------------------------------------------------- */
1504static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1505 struct device_driver *drv,
1506 char *buf)
1507{
1508 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1509}
1510
1511static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1512 struct device_driver *drv,
1513 const char *buf, size_t count)
1514{
1515 unsigned long t;
1516
1517 if (parse_strtoul(buf, 1, &t))
1518 return -EINVAL;
1519
1520 tpacpi_bluetooth_emulstate = !!t;
1521
1522 return count;
1523}
1524
1525static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1526 tpacpi_driver_bluetooth_emulstate_show,
1527 tpacpi_driver_bluetooth_emulstate_store);
1528
1529/* wwan_emulstate ------------------------------------------------- */
1530static ssize_t tpacpi_driver_wwan_emulstate_show(
1531 struct device_driver *drv,
1532 char *buf)
1533{
1534 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1535}
1536
1537static ssize_t tpacpi_driver_wwan_emulstate_store(
1538 struct device_driver *drv,
1539 const char *buf, size_t count)
1540{
1541 unsigned long t;
1542
1543 if (parse_strtoul(buf, 1, &t))
1544 return -EINVAL;
1545
1546 tpacpi_wwan_emulstate = !!t;
1547
1548 return count;
1549}
1550
1551static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1552 tpacpi_driver_wwan_emulstate_show,
1553 tpacpi_driver_wwan_emulstate_store);
1554
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001555/* uwb_emulstate ------------------------------------------------- */
1556static ssize_t tpacpi_driver_uwb_emulstate_show(
1557 struct device_driver *drv,
1558 char *buf)
1559{
1560 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1561}
1562
1563static ssize_t tpacpi_driver_uwb_emulstate_store(
1564 struct device_driver *drv,
1565 const char *buf, size_t count)
1566{
1567 unsigned long t;
1568
1569 if (parse_strtoul(buf, 1, &t))
1570 return -EINVAL;
1571
1572 tpacpi_uwb_emulstate = !!t;
1573
1574 return count;
1575}
1576
1577static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1578 tpacpi_driver_uwb_emulstate_show,
1579 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001580#endif
1581
1582/* --------------------------------------------------------------------- */
1583
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001584static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001585 &driver_attr_debug_level, &driver_attr_version,
1586 &driver_attr_interface_version,
1587};
1588
1589static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1590{
1591 int i, res;
1592
1593 i = 0;
1594 res = 0;
1595 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1596 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1597 i++;
1598 }
1599
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001600#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1601 if (!res && dbg_wlswemul)
1602 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1603 if (!res && dbg_bluetoothemul)
1604 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1605 if (!res && dbg_wwanemul)
1606 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001607 if (!res && dbg_uwbemul)
1608 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001609#endif
1610
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001611 return res;
1612}
1613
1614static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1615{
1616 int i;
1617
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001618 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001619 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001620
1621#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1622 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1623 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1624 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001625 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001626#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001627}
1628
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001629/*************************************************************************
1630 * Firmware Data
1631 */
1632
1633/*
1634 * Table of recommended minimum BIOS versions
1635 *
1636 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001637 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001638 * bugs and bad ACPI behaviour on older versions
1639 *
1640 * 2. BIOS or EC fw with known bugs that trigger on Linux
1641 *
1642 * 3. BIOS with known reduced functionality in older versions
1643 *
1644 * We recommend the latest BIOS and EC version.
1645 * We only support the latest BIOS and EC fw version as a rule.
1646 *
1647 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1648 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001649 *
1650 * WARNING: we use this table also to detect that the machine is
1651 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001652 */
1653
1654#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1655 { .vendor = (__v), \
1656 .bios = TPID(__id1, __id2), \
1657 .ec = TPACPI_MATCH_ANY, \
1658 .quirks = TPACPI_MATCH_ANY << 16 \
1659 | (__bv1) << 8 | (__bv2) }
1660
1661#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001662 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001663 { .vendor = (__v), \
1664 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001665 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001666 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1667 | (__bv1) << 8 | (__bv2) }
1668
1669#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1670 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1671
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001672/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001673#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1674 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001675 __bv1, __bv2, TPID(__id1, __id2), \
1676 __ev1, __ev2), \
1677 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1678 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1679 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001680
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_QI2(__bid1, __bid2, __bv1, __bv2, \
1683 __eid1, __eid2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001685 __bv1, __bv2, TPID(__eid1, __eid2), \
1686 __ev1, __ev2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1689 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001690
1691#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1692 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1693
1694#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001696 __bv1, __bv2, TPID(__id1, __id2), \
1697 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001698
1699#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1700 __eid1, __eid2, __ev1, __ev2) \
1701 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001702 __bv1, __bv2, TPID(__eid1, __eid2), \
1703 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001704
1705static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1706 /* Numeric models ------------------ */
1707 /* FW MODEL BIOS VERS */
1708 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1709 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1710 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1711 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1712 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1713 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1714 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1715 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1716 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1717
1718 /* A-series ------------------------- */
1719 /* FW MODEL BIOS VERS EC VERS */
1720 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1721 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1722 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1723 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1724 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1725 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1726 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1727 TPV_QI0('1', '3', '2', '0'), /* A22m */
1728 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1729 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1730 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1731
1732 /* G-series ------------------------- */
1733 /* FW MODEL BIOS VERS */
1734 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1735 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1736
1737 /* R-series, T-series --------------- */
1738 /* FW MODEL BIOS VERS EC VERS */
1739 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1740 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1741 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1742 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1743 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1744 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1745 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1746 T40/p, T41/p, T42/p (1) */
1747 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1748 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1749 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1750 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1751
1752 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1753 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1754 TPV_QI0('1', '6', '3', '2'), /* T22 */
1755 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1756 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1757 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1758
1759 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1760 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001761 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001762
1763 /* BIOS FW BIOS VERS EC FW EC VERS */
1764 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1765 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1766
1767 /* X-series ------------------------- */
1768 /* FW MODEL BIOS VERS EC VERS */
1769 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1770 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1771 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1772 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1773 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1774 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1775 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1776
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001777 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1778 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001779
1780 /* (0) - older versions lack DMI EC fw string and functionality */
1781 /* (1) - older versions known to lack functionality */
1782};
1783
1784#undef TPV_QL1
1785#undef TPV_QL0
1786#undef TPV_QI2
1787#undef TPV_QI1
1788#undef TPV_QI0
1789#undef TPV_Q_X
1790#undef TPV_Q
1791
1792static void __init tpacpi_check_outdated_fw(void)
1793{
1794 unsigned long fwvers;
1795 u16 ec_version, bios_version;
1796
1797 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1798 ARRAY_SIZE(tpacpi_bios_version_qtable));
1799
1800 if (!fwvers)
1801 return;
1802
1803 bios_version = fwvers & 0xffffU;
1804 ec_version = (fwvers >> 16) & 0xffffU;
1805
1806 /* note that unknown versions are set to 0x0000 and we use that */
1807 if ((bios_version > thinkpad_id.bios_release) ||
1808 (ec_version > thinkpad_id.ec_release &&
1809 ec_version != TPACPI_MATCH_ANY)) {
1810 /*
1811 * The changelogs would let us track down the exact
1812 * reason, but it is just too much of a pain to track
1813 * it. We only list BIOSes that are either really
1814 * broken, or really stable to begin with, so it is
1815 * best if the user upgrades the firmware anyway.
1816 */
Joe Perches0978e012011-04-04 10:06:25 -07001817 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1818 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001819 "fixes and/or important features\n");
1820 }
1821}
1822
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001823static bool __init tpacpi_is_fw_known(void)
1824{
1825 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1826 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1827}
1828
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001829/****************************************************************************
1830 ****************************************************************************
1831 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001832 * Subdrivers
1833 *
1834 ****************************************************************************
1835 ****************************************************************************/
1836
1837/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001838 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001839 */
1840
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001841static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001843 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1844 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001848static struct ibm_struct thinkpad_acpi_driver_data = {
1849 .name = "driver",
1850 .read = thinkpad_acpi_driver_read,
1851};
1852
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853/*************************************************************************
1854 * Hotkey subdriver
1855 */
1856
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001857/*
1858 * ThinkPad firmware event model
1859 *
1860 * The ThinkPad firmware has two main event interfaces: normal ACPI
1861 * notifications (which follow the ACPI standard), and a private event
1862 * interface.
1863 *
1864 * The private event interface also issues events for the hotkeys. As
1865 * the driver gained features, the event handling code ended up being
1866 * built around the hotkey subdriver. This will need to be refactored
1867 * to a more formal event API eventually.
1868 *
1869 * Some "hotkeys" are actually supposed to be used as event reports,
1870 * such as "brightness has changed", "volume has changed", depending on
1871 * the ThinkPad model and how the firmware is operating.
1872 *
1873 * Unlike other classes, hotkey-class events have mask/unmask control on
1874 * non-ancient firmware. However, how it behaves changes a lot with the
1875 * firmware model and version.
1876 */
1877
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001878enum { /* hot key scan codes (derived from ACPI DSDT) */
1879 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1880 TP_ACPI_HOTKEYSCAN_FNF2,
1881 TP_ACPI_HOTKEYSCAN_FNF3,
1882 TP_ACPI_HOTKEYSCAN_FNF4,
1883 TP_ACPI_HOTKEYSCAN_FNF5,
1884 TP_ACPI_HOTKEYSCAN_FNF6,
1885 TP_ACPI_HOTKEYSCAN_FNF7,
1886 TP_ACPI_HOTKEYSCAN_FNF8,
1887 TP_ACPI_HOTKEYSCAN_FNF9,
1888 TP_ACPI_HOTKEYSCAN_FNF10,
1889 TP_ACPI_HOTKEYSCAN_FNF11,
1890 TP_ACPI_HOTKEYSCAN_FNF12,
1891 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1892 TP_ACPI_HOTKEYSCAN_FNINSERT,
1893 TP_ACPI_HOTKEYSCAN_FNDELETE,
1894 TP_ACPI_HOTKEYSCAN_FNHOME,
1895 TP_ACPI_HOTKEYSCAN_FNEND,
1896 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1898 TP_ACPI_HOTKEYSCAN_FNSPACE,
1899 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1901 TP_ACPI_HOTKEYSCAN_MUTE,
1902 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001903 TP_ACPI_HOTKEYSCAN_UNK1,
1904 TP_ACPI_HOTKEYSCAN_UNK2,
1905 TP_ACPI_HOTKEYSCAN_UNK3,
1906 TP_ACPI_HOTKEYSCAN_UNK4,
1907 TP_ACPI_HOTKEYSCAN_UNK5,
1908 TP_ACPI_HOTKEYSCAN_UNK6,
1909 TP_ACPI_HOTKEYSCAN_UNK7,
1910 TP_ACPI_HOTKEYSCAN_UNK8,
1911
1912 /* Hotkey keymap size */
1913 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001914};
1915
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001916enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001917 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1918 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1919};
1920
1921enum { /* Positions of some of the keys in hotkey masks */
1922 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1923 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1924 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1925 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1926 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1927 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1928 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1929 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1930 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1931 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1932 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1933};
1934
1935enum { /* NVRAM to ACPI HKEY group map */
1936 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1937 TP_ACPI_HKEY_ZOOM_MASK |
1938 TP_ACPI_HKEY_DISPSWTCH_MASK |
1939 TP_ACPI_HKEY_HIBERNATE_MASK,
1940 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1941 TP_ACPI_HKEY_BRGHTDWN_MASK,
1942 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1943 TP_ACPI_HKEY_VOLDWN_MASK |
1944 TP_ACPI_HKEY_MUTE_MASK,
1945};
1946
1947#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1948struct tp_nvram_state {
1949 u16 thinkpad_toggle:1;
1950 u16 zoom_toggle:1;
1951 u16 display_toggle:1;
1952 u16 thinklight_toggle:1;
1953 u16 hibernate_toggle:1;
1954 u16 displayexp_toggle:1;
1955 u16 display_state:1;
1956 u16 brightness_toggle:1;
1957 u16 volume_toggle:1;
1958 u16 mute:1;
1959
1960 u8 brightness_level;
1961 u8 volume_level;
1962};
1963
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001964/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001965static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001966
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001967/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001968 * Acquire mutex to write poller control variables as an
1969 * atomic block.
1970 *
1971 * Increment hotkey_config_change when changing them if you
1972 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001973 *
1974 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1975 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001976static struct mutex hotkey_thread_data_mutex;
1977static unsigned int hotkey_config_change;
1978
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001979/*
1980 * hotkey poller control variables
1981 *
1982 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001983 *
1984 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1985 * should be used only when the changes need to be taken as
1986 * a block, OR when one needs to force the kthread to forget
1987 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001988 */
1989static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1990static unsigned int hotkey_poll_freq = 10; /* Hz */
1991
1992#define HOTKEY_CONFIG_CRITICAL_START \
1993 do { \
1994 mutex_lock(&hotkey_thread_data_mutex); \
1995 hotkey_config_change++; \
1996 } while (0);
1997#define HOTKEY_CONFIG_CRITICAL_END \
1998 mutex_unlock(&hotkey_thread_data_mutex);
1999
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002000#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2001
2002#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002003#define HOTKEY_CONFIG_CRITICAL_START
2004#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002005
2006#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2007
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002008static struct mutex hotkey_mutex;
2009
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002010static enum { /* Reasons for waking up */
2011 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2012 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2013 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2014} hotkey_wakeup_reason;
2015
2016static int hotkey_autosleep_ack;
2017
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002018static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2019static u32 hotkey_all_mask; /* all events supported in fw */
2020static u32 hotkey_reserved_mask; /* events better left disabled */
2021static u32 hotkey_driver_mask; /* events needed by the driver */
2022static u32 hotkey_user_mask; /* events visible to userspace */
2023static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002024
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002025static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002026
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03002027static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002028
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002029static void tpacpi_driver_event(const unsigned int hkey_event);
2030static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002031static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002032
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002033/* HKEY.MHKG() return bits */
2034#define TP_HOTKEY_TABLET_MASK (1 << 3)
2035
Johannes Berg19d337d2009-06-02 13:01:37 +02002036static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002037{
Johannes Berg19d337d2009-06-02 13:01:37 +02002038 int status;
2039
2040 if (!tp_features.hotkey_wlsw)
2041 return -ENODEV;
2042
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002043#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002044 if (dbg_wlswemul)
2045 return (tpacpi_wlsw_emulstate) ?
2046 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002047#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002048
2049 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002050 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002051
2052 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002053}
2054
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002055static int hotkey_get_tablet_mode(int *status)
2056{
2057 int s;
2058
2059 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2060 return -EIO;
2061
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002062 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2063 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002064}
2065
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002066/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002067 * Reads current event mask from firmware, and updates
2068 * hotkey_acpi_mask accordingly. Also resets any bits
2069 * from hotkey_user_mask that are unavailable to be
2070 * delivered (shadow requirement of the userspace ABI).
2071 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002072 * Call with hotkey_mutex held
2073 */
2074static int hotkey_mask_get(void)
2075{
2076 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002077 u32 m = 0;
2078
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002079 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002080 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002081
2082 hotkey_acpi_mask = m;
2083 } else {
2084 /* no mask support doesn't mean no event support... */
2085 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002086 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002087
2088 /* sync userspace-visible mask */
2089 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002090
2091 return 0;
2092}
2093
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002094void static hotkey_mask_warn_incomplete_mask(void)
2095{
2096 /* log only what the user can fix... */
2097 const u32 wantedmask = hotkey_driver_mask &
2098 ~(hotkey_acpi_mask | hotkey_source_mask) &
2099 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2100
2101 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002102 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002103}
2104
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002105/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002106 * Set the firmware mask when supported
2107 *
2108 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2109 *
2110 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2111 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002112 * Call with hotkey_mutex held
2113 */
2114static int hotkey_mask_set(u32 mask)
2115{
2116 int i;
2117 int rc = 0;
2118
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002119 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002120
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002121 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002122 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002123 if (!acpi_evalf(hkey_handle,
2124 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002125 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002126 rc = -EIO;
2127 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002128 }
2129 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002130 }
2131
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002132 /*
2133 * We *must* make an inconditional call to hotkey_mask_get to
2134 * refresh hotkey_acpi_mask and update hotkey_user_mask
2135 *
2136 * Take the opportunity to also log when we cannot _enable_
2137 * a given event.
2138 */
2139 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002140 pr_notice("asked for hotkey mask 0x%08x, but "
2141 "firmware forced it to 0x%08x\n",
2142 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002143 }
2144
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002145 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2146 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002147
2148 return rc;
2149}
2150
2151/*
2152 * Sets hotkey_user_mask and tries to set the firmware mask
2153 *
2154 * Call with hotkey_mutex held
2155 */
2156static int hotkey_user_mask_set(const u32 mask)
2157{
2158 int rc;
2159
2160 /* Give people a chance to notice they are doing something that
2161 * is bound to go boom on their users sooner or later */
2162 if (!tp_warned.hotkey_mask_ff &&
2163 (mask == 0xffff || mask == 0xffffff ||
2164 mask == 0xffffffff)) {
2165 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002166 pr_notice("setting the hotkey mask to 0x%08x is likely "
2167 "not the best way to go about it\n", mask);
2168 pr_notice("please consider using the driver defaults, "
2169 "and refer to up-to-date thinkpad-acpi "
2170 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002171 }
2172
2173 /* Try to enable what the user asked for, plus whatever we need.
2174 * this syncs everything but won't enable bits in hotkey_user_mask */
2175 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2176
2177 /* Enable the available bits in hotkey_user_mask */
2178 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2179
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002180 return rc;
2181}
2182
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002183/*
2184 * Sets the driver hotkey mask.
2185 *
2186 * Can be called even if the hotkey subdriver is inactive
2187 */
2188static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2189{
2190 int rc;
2191
2192 /* Do the right thing if hotkey_init has not been called yet */
2193 if (!tp_features.hotkey) {
2194 hotkey_driver_mask = mask;
2195 return 0;
2196 }
2197
2198 mutex_lock(&hotkey_mutex);
2199
2200 HOTKEY_CONFIG_CRITICAL_START
2201 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002202#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002203 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002204#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002205 HOTKEY_CONFIG_CRITICAL_END
2206
2207 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2208 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002209 hotkey_poll_setup(true);
2210
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002211 mutex_unlock(&hotkey_mutex);
2212
2213 return rc;
2214}
2215
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216static int hotkey_status_get(int *status)
2217{
2218 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2219 return -EIO;
2220
2221 return 0;
2222}
2223
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002224static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002225{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002226 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002227 return -EIO;
2228
2229 return 0;
2230}
2231
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002232static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002233{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002234 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002235
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002236 if (tp_features.hotkey_tablet &&
2237 !hotkey_get_tablet_mode(&state)) {
2238 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002239
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002240 input_report_switch(tpacpi_inputdev,
2241 SW_TABLET_MODE, !!state);
2242 input_sync(tpacpi_inputdev);
2243
2244 mutex_unlock(&tpacpi_inputdev_send_mutex);
2245 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002246}
2247
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002248/* Do NOT call without validating scancode first */
2249static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002250{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002251 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002252
2253 if (keycode != KEY_RESERVED) {
2254 mutex_lock(&tpacpi_inputdev_send_mutex);
2255
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002256 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002257 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002258 input_sync(tpacpi_inputdev);
2259
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002260 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002261 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002262 input_sync(tpacpi_inputdev);
2263
2264 mutex_unlock(&tpacpi_inputdev_send_mutex);
2265 }
2266}
2267
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002268/* Do NOT call without validating scancode first */
2269static void tpacpi_input_send_key_masked(const unsigned int scancode)
2270{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002271 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002272 if (hotkey_user_mask & (1 << scancode))
2273 tpacpi_input_send_key(scancode);
2274}
2275
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002276#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2277static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2278
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002279/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002280static void tpacpi_hotkey_send_key(unsigned int scancode)
2281{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002282 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002283}
2284
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002285static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002286{
2287 u8 d;
2288
2289 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2290 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2291 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2292 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2293 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2294 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2295 }
2296 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2297 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2298 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2299 }
2300 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2302 n->displayexp_toggle =
2303 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2304 }
2305 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2306 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2307 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2308 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2309 n->brightness_toggle =
2310 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2311 }
2312 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2313 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2314 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2315 >> TP_NVRAM_POS_LEVEL_VOLUME;
2316 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2317 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2318 }
2319}
2320
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002321static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2322 struct tp_nvram_state *newn,
2323 const u32 event_mask)
2324{
2325
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002326#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002327 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002328 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002329 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002331 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002332
2333#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002334 do { \
2335 if (event_mask & (1 << __scancode)) \
2336 tpacpi_hotkey_send_key(__scancode); \
2337 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002338
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002339 void issue_volchange(const unsigned int oldvol,
2340 const unsigned int newvol)
2341 {
2342 unsigned int i = oldvol;
2343
2344 while (i > newvol) {
2345 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2346 i--;
2347 }
2348 while (i < newvol) {
2349 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2350 i++;
2351 }
2352 }
2353
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002354 void issue_brightnesschange(const unsigned int oldbrt,
2355 const unsigned int newbrt)
2356 {
2357 unsigned int i = oldbrt;
2358
2359 while (i > newbrt) {
2360 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2361 i--;
2362 }
2363 while (i < newbrt) {
2364 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2365 i++;
2366 }
2367 }
2368
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2370 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2373
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2375
2376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2377
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002378 /*
2379 * Handle volume
2380 *
2381 * This code is supposed to duplicate the IBM firmware behaviour:
2382 * - Pressing MUTE issues mute hotkey message, even when already mute
2383 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002384 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002385 * - The act of unmuting issues volume up/down notification,
2386 * depending which key was used to unmute
2387 *
2388 * We are constrained to what the NVRAM can tell us, which is not much
2389 * and certainly not enough if more than one volume hotkey was pressed
2390 * since the last poll cycle.
2391 *
2392 * Just to make our life interesting, some newer Lenovo ThinkPads have
2393 * bugs in the BIOS and may fail to update volume_toggle properly.
2394 */
2395 if (newn->mute) {
2396 /* muted */
2397 if (!oldn->mute ||
2398 oldn->volume_toggle != newn->volume_toggle ||
2399 oldn->volume_level != newn->volume_level) {
2400 /* recently muted, or repeated mute keypress, or
2401 * multiple presses ending in mute */
2402 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002403 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2404 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002405 } else {
2406 /* unmute */
2407 if (oldn->mute) {
2408 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002410 }
2411 if (oldn->volume_level != newn->volume_level) {
2412 issue_volchange(oldn->volume_level, newn->volume_level);
2413 } else if (oldn->volume_toggle != newn->volume_toggle) {
2414 /* repeated vol up/down keypress at end of scale ? */
2415 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002416 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002417 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002419 }
2420 }
2421
2422 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002423 if (oldn->brightness_level != newn->brightness_level) {
2424 issue_brightnesschange(oldn->brightness_level,
2425 newn->brightness_level);
2426 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2427 /* repeated key presses that didn't change state */
2428 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002430 else if (newn->brightness_level >= bright_maxlvl
2431 && !tp_features.bright_unkfw)
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002433 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002434
2435#undef TPACPI_COMPARE_KEY
2436#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002437}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002438
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002439/*
2440 * Polling driver
2441 *
2442 * We track all events in hotkey_source_mask all the time, since
2443 * most of them are edge-based. We only issue those requested by
2444 * hotkey_user_mask or hotkey_driver_mask, though.
2445 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002446static int hotkey_kthread(void *data)
2447{
2448 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002449 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450 unsigned int si, so;
2451 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002452 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002453 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002454 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2457 goto exit;
2458
2459 set_freezable();
2460
2461 so = 0;
2462 si = 1;
2463 t = 0;
2464
2465 /* Initial state for compares */
2466 mutex_lock(&hotkey_thread_data_mutex);
2467 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002468 poll_mask = hotkey_source_mask;
2469 event_mask = hotkey_source_mask &
2470 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002471 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002472 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002473 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002474
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002475 while (!kthread_should_stop()) {
2476 if (t == 0) {
2477 if (likely(poll_freq))
2478 t = 1000/poll_freq;
2479 else
2480 t = 100; /* should never happen... */
2481 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002482 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002483 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002484 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002485
2486 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002487 continue;
2488
2489 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002490 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002491 /* forget old state on thaw or config change */
2492 si = so;
2493 t = 0;
2494 change_detector = hotkey_config_change;
2495 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002496 poll_mask = hotkey_source_mask;
2497 event_mask = hotkey_source_mask &
2498 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002499 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002500 mutex_unlock(&hotkey_thread_data_mutex);
2501
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002502 if (likely(poll_mask)) {
2503 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002504 if (likely(si != so)) {
2505 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002506 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002507 }
2508 }
2509
2510 so = si;
2511 si ^= 1;
2512 }
2513
2514exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 return 0;
2516}
2517
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002518/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002519static void hotkey_poll_stop_sync(void)
2520{
2521 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002522 kthread_stop(tpacpi_hotkey_task);
2523 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 }
2525}
2526
2527/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002528static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002529{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002530 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2531 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002532
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002533 if (hotkey_poll_freq > 0 &&
2534 (poll_driver_mask ||
2535 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002536 if (!tpacpi_hotkey_task) {
2537 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002538 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002539 if (IS_ERR(tpacpi_hotkey_task)) {
2540 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002541 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002542 "for hotkey polling\n");
2543 }
2544 }
2545 } else {
2546 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002547 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002548 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002549 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2550 "require polling, which is currently "
2551 "disabled\n",
2552 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002553 }
2554 }
2555}
2556
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002557static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002558{
2559 mutex_lock(&hotkey_mutex);
2560 hotkey_poll_setup(may_warn);
2561 mutex_unlock(&hotkey_mutex);
2562}
2563
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002564/* call with hotkey_mutex held */
2565static void hotkey_poll_set_freq(unsigned int freq)
2566{
2567 if (!freq)
2568 hotkey_poll_stop_sync();
2569
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002570 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002571}
2572
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002573#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2574
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002575static void hotkey_poll_setup(const bool __unused)
2576{
2577}
2578
2579static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002580{
2581}
2582
2583#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2584
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002585static int hotkey_inputdev_open(struct input_dev *dev)
2586{
2587 switch (tpacpi_lifecycle) {
2588 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002589 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002590 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002591 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002592 case TPACPI_LIFE_EXITING:
2593 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002594 }
2595
2596 /* Should only happen if tpacpi_lifecycle is corrupt */
2597 BUG();
2598 return -EBUSY;
2599}
2600
2601static void hotkey_inputdev_close(struct input_dev *dev)
2602{
2603 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002604 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002605 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002606 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002607}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002608
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002609/* sysfs hotkey enable ------------------------------------------------- */
2610static ssize_t hotkey_enable_show(struct device *dev,
2611 struct device_attribute *attr,
2612 char *buf)
2613{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002614 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002615
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002616 printk_deprecated_attribute("hotkey_enable",
2617 "Hotkey reporting is always enabled");
2618
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002619 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002620 if (res)
2621 return res;
2622
2623 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2624}
2625
2626static ssize_t hotkey_enable_store(struct device *dev,
2627 struct device_attribute *attr,
2628 const char *buf, size_t count)
2629{
2630 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002631
2632 printk_deprecated_attribute("hotkey_enable",
2633 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002634
2635 if (parse_strtoul(buf, 1, &t))
2636 return -EINVAL;
2637
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002638 if (t == 0)
2639 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002640
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002641 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002642}
2643
2644static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002645 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002646 hotkey_enable_show, hotkey_enable_store);
2647
2648/* sysfs hotkey mask --------------------------------------------------- */
2649static ssize_t hotkey_mask_show(struct device *dev,
2650 struct device_attribute *attr,
2651 char *buf)
2652{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002653 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002654}
2655
2656static ssize_t hotkey_mask_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2659{
2660 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002661 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002662
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002663 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002664 return -EINVAL;
2665
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002666 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002667 return -ERESTARTSYS;
2668
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002669 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002670
2671#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002672 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002673#endif
2674
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002675 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002676
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002677 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2678
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002679 return (res) ? res : count;
2680}
2681
2682static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002683 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002684 hotkey_mask_show, hotkey_mask_store);
2685
2686/* sysfs hotkey bios_enabled ------------------------------------------- */
2687static ssize_t hotkey_bios_enabled_show(struct device *dev,
2688 struct device_attribute *attr,
2689 char *buf)
2690{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002691 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002692}
2693
2694static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002695 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002696
2697/* sysfs hotkey bios_mask ---------------------------------------------- */
2698static ssize_t hotkey_bios_mask_show(struct device *dev,
2699 struct device_attribute *attr,
2700 char *buf)
2701{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002702 printk_deprecated_attribute("hotkey_bios_mask",
2703 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002704 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002705}
2706
2707static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002708 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002709
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002710/* sysfs hotkey all_mask ----------------------------------------------- */
2711static ssize_t hotkey_all_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2713 char *buf)
2714{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002715 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2716 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002717}
2718
2719static struct device_attribute dev_attr_hotkey_all_mask =
2720 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2721
2722/* sysfs hotkey recommended_mask --------------------------------------- */
2723static ssize_t hotkey_recommended_mask_show(struct device *dev,
2724 struct device_attribute *attr,
2725 char *buf)
2726{
2727 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002728 (hotkey_all_mask | hotkey_source_mask)
2729 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002730}
2731
2732static struct device_attribute dev_attr_hotkey_recommended_mask =
2733 __ATTR(hotkey_recommended_mask, S_IRUGO,
2734 hotkey_recommended_mask_show, NULL);
2735
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002736#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2737
2738/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2739static ssize_t hotkey_source_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2742{
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2744}
2745
2746static ssize_t hotkey_source_mask_store(struct device *dev,
2747 struct device_attribute *attr,
2748 const char *buf, size_t count)
2749{
2750 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002751 u32 r_ev;
2752 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002753
2754 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2755 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2756 return -EINVAL;
2757
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002758 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002759 return -ERESTARTSYS;
2760
2761 HOTKEY_CONFIG_CRITICAL_START
2762 hotkey_source_mask = t;
2763 HOTKEY_CONFIG_CRITICAL_END
2764
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002765 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2766 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002767 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002768
2769 /* check if events needed by the driver got disabled */
2770 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2771 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002772
2773 mutex_unlock(&hotkey_mutex);
2774
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002775 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002776 pr_err("hotkey_source_mask: "
2777 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002778
2779 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002780 pr_notice("hotkey_source_mask: "
2781 "some important events were disabled: 0x%04x\n",
2782 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002783
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002784 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2785
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002786 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002787}
2788
2789static struct device_attribute dev_attr_hotkey_source_mask =
2790 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2791 hotkey_source_mask_show, hotkey_source_mask_store);
2792
2793/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2794static ssize_t hotkey_poll_freq_show(struct device *dev,
2795 struct device_attribute *attr,
2796 char *buf)
2797{
2798 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2799}
2800
2801static ssize_t hotkey_poll_freq_store(struct device *dev,
2802 struct device_attribute *attr,
2803 const char *buf, size_t count)
2804{
2805 unsigned long t;
2806
2807 if (parse_strtoul(buf, 25, &t))
2808 return -EINVAL;
2809
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002810 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002811 return -ERESTARTSYS;
2812
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002813 hotkey_poll_set_freq(t);
2814 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002815
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816 mutex_unlock(&hotkey_mutex);
2817
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002818 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2819
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002820 return count;
2821}
2822
2823static struct device_attribute dev_attr_hotkey_poll_freq =
2824 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2825 hotkey_poll_freq_show, hotkey_poll_freq_store);
2826
2827#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2828
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002829/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002830static ssize_t hotkey_radio_sw_show(struct device *dev,
2831 struct device_attribute *attr,
2832 char *buf)
2833{
Johannes Berg19d337d2009-06-02 13:01:37 +02002834 int res;
2835 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002836 if (res < 0)
2837 return res;
2838
Johannes Berg19d337d2009-06-02 13:01:37 +02002839 /* Opportunistic update */
2840 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2841
2842 return snprintf(buf, PAGE_SIZE, "%d\n",
2843 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002844}
2845
2846static struct device_attribute dev_attr_hotkey_radio_sw =
2847 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2848
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002849static void hotkey_radio_sw_notify_change(void)
2850{
2851 if (tp_features.hotkey_wlsw)
2852 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2853 "hotkey_radio_sw");
2854}
2855
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002856/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2857static ssize_t hotkey_tablet_mode_show(struct device *dev,
2858 struct device_attribute *attr,
2859 char *buf)
2860{
2861 int res, s;
2862 res = hotkey_get_tablet_mode(&s);
2863 if (res < 0)
2864 return res;
2865
2866 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2867}
2868
2869static struct device_attribute dev_attr_hotkey_tablet_mode =
2870 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2871
2872static void hotkey_tablet_mode_notify_change(void)
2873{
2874 if (tp_features.hotkey_tablet)
2875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2876 "hotkey_tablet_mode");
2877}
2878
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002879/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002880static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2881 struct device_attribute *attr,
2882 char *buf)
2883{
2884 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2885}
2886
2887static struct device_attribute dev_attr_hotkey_wakeup_reason =
2888 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2889
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002890static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002891{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002892 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2893 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002894}
2895
2896/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002897static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2898 struct device_attribute *attr,
2899 char *buf)
2900{
2901 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2902}
2903
2904static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2905 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2906 hotkey_wakeup_hotunplug_complete_show, NULL);
2907
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002908static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002909{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2911 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002912}
2913
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002914/* --------------------------------------------------------------------- */
2915
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002916static struct attribute *hotkey_attributes[] __initdata = {
2917 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002918 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002919 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002920 &dev_attr_hotkey_wakeup_reason.attr,
2921 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002922 &dev_attr_hotkey_mask.attr,
2923 &dev_attr_hotkey_all_mask.attr,
2924 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002925#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002926 &dev_attr_hotkey_source_mask.attr,
2927 &dev_attr_hotkey_poll_freq.attr,
2928#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002929};
2930
Johannes Berg19d337d2009-06-02 13:01:37 +02002931/*
2932 * Sync both the hw and sw blocking state of all switches
2933 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002934static void tpacpi_send_radiosw_update(void)
2935{
2936 int wlsw;
2937
Johannes Berg19d337d2009-06-02 13:01:37 +02002938 /*
2939 * We must sync all rfkill controllers *before* issuing any
2940 * rfkill input events, or we will race the rfkill core input
2941 * handler.
2942 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002943 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002944 * for the above.
2945 *
2946 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2947 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002948
Johannes Berg19d337d2009-06-02 13:01:37 +02002949 wlsw = hotkey_get_wlsw();
2950
2951 /* Sync hw blocking state first if it is hw-blocked */
2952 if (wlsw == TPACPI_RFK_RADIO_OFF)
2953 tpacpi_rfk_update_hwblock_state(true);
2954
2955 /* Sync sw blocking state */
2956 tpacpi_rfk_update_swstate_all();
2957
2958 /* Sync hw blocking state last if it is hw-unblocked */
2959 if (wlsw == TPACPI_RFK_RADIO_ON)
2960 tpacpi_rfk_update_hwblock_state(false);
2961
2962 /* Issue rfkill input event for WLSW switch */
2963 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002964 mutex_lock(&tpacpi_inputdev_send_mutex);
2965
2966 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002967 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002968 input_sync(tpacpi_inputdev);
2969
2970 mutex_unlock(&tpacpi_inputdev_send_mutex);
2971 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002972
2973 /*
2974 * this can be unconditional, as we will poll state again
2975 * if userspace uses the notify to read data
2976 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002977 hotkey_radio_sw_notify_change();
2978}
2979
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002980static void hotkey_exit(void)
2981{
2982#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002983 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002984 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002985 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002986#endif
2987
2988 if (hotkey_dev_attributes)
2989 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2990
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002991 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002992 "restoring original HKEY status and mask\n");
2993 /* yes, there is a bitwise or below, we want the
2994 * functions to be called even if one of them fail */
2995 if (((tp_features.hotkey_mask &&
2996 hotkey_mask_set(hotkey_orig_mask)) |
2997 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07002998 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002999 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003000}
3001
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003002static void __init hotkey_unmap(const unsigned int scancode)
3003{
3004 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3005 clear_bit(hotkey_keycode_map[scancode],
3006 tpacpi_inputdev->keybit);
3007 hotkey_keycode_map[scancode] = KEY_RESERVED;
3008 }
3009}
3010
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003011/*
3012 * HKEY quirks:
3013 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3014 */
3015
3016#define TPACPI_HK_Q_INIMASK 0x0001
3017
3018static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3019 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3020 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3021 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3022 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3023 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3024 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3025 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3026 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3027 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3028 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3029 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3030 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3031 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3032 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3033 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3034 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3035 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3036 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3037 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3038};
3039
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003040typedef u16 tpacpi_keymap_entry_t;
3041typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003043static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003044{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003045 /* Requirements for changing the default keymaps:
3046 *
3047 * 1. Many of the keys are mapped to KEY_RESERVED for very
3048 * good reasons. Do not change them unless you have deep
3049 * knowledge on the IBM and Lenovo ThinkPad firmware for
3050 * the various ThinkPad models. The driver behaves
3051 * differently for KEY_RESERVED: such keys have their
3052 * hot key mask *unset* in mask_recommended, and also
3053 * in the initial hot key mask programmed into the
3054 * firmware at driver load time, which means the firm-
3055 * ware may react very differently if you change them to
3056 * something else;
3057 *
3058 * 2. You must be subscribed to the linux-thinkpad and
3059 * ibm-acpi-devel mailing lists, and you should read the
3060 * list archives since 2007 if you want to change the
3061 * keymaps. This requirement exists so that you will
3062 * know the past history of problems with the thinkpad-
3063 * acpi driver keymaps, and also that you will be
3064 * listening to any bug reports;
3065 *
3066 * 3. Do not send thinkpad-acpi specific patches directly to
3067 * for merging, *ever*. Send them to the linux-acpi
3068 * mailinglist for comments. Merging is to be done only
3069 * through acpi-test and the ACPI maintainer.
3070 *
3071 * If the above is too much to ask, don't change the keymap.
3072 * Ask the thinkpad-acpi maintainer to do it, instead.
3073 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003074
3075 enum keymap_index {
3076 TPACPI_KEYMAP_IBM_GENERIC = 0,
3077 TPACPI_KEYMAP_LENOVO_GENERIC,
3078 };
3079
3080 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3081 /* Generic keymap for IBM ThinkPads */
3082 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003083 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003084 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003085 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3086 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003087
3088 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003089 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3090 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3091 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003092
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003093 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003094 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003095 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003096
3097 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003098 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003099
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003100 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3101 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003102
3103 /* Volume: firmware always react to it and reprograms
3104 * the built-in *extra* mixer. Never map it to control
3105 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003106 KEY_RESERVED, /* 0x14: VOLUME UP */
3107 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3108 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003109
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003110 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003111
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003112 /* (assignments unknown, please report if found) */
3113 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003115 },
3116
3117 /* Generic keymap for Lenovo ThinkPads */
3118 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003119 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3120 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003121 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003122 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003123
3124 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003125 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3126 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3127 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003128
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003129 /* These should be enabled --only-- when ACPI video
3130 * is disabled (i.e. in "vendor" mode), and are handled
3131 * in a special way by the init code */
3132 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3133 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003134
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003135 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003136
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003137 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3138 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003139
3140 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3141 * react to it and reprograms the built-in *extra* mixer.
3142 * Never map it to control another mixer by default.
3143 *
3144 * T60?, T61, R60?, R61: firmware and EC tries to send
3145 * these over the regular keyboard, so these are no-ops,
3146 * but there are still weird bugs re. MUTE, so do not
3147 * change unless you get test reports from all Lenovo
3148 * models. May cause the BIOS to interfere with the
3149 * HDA mixer.
3150 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003151 KEY_RESERVED, /* 0x14: VOLUME UP */
3152 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3153 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003154
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003155 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003156
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003157 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003158 KEY_UNKNOWN, KEY_UNKNOWN,
3159
3160 /*
3161 * The mic mute button only sends 0x1a. It does not
3162 * automatically mute the mic or change the mute light.
3163 */
3164 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3165
3166 /* (assignments unknown, please report if found) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003167 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Andy Lutomirski33009552011-05-24 15:16:43 -04003168 KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003169 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003170 };
3171
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003172 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3173 /* Generic maps (fallback) */
3174 {
3175 .vendor = PCI_VENDOR_ID_IBM,
3176 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3177 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3178 },
3179 {
3180 .vendor = PCI_VENDOR_ID_LENOVO,
3181 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3182 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3183 },
3184 };
3185
3186#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003187#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003188
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003189 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003190 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003191 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003192 bool radiosw_state = false;
3193 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003194
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003195 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003196 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003197
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003198 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3199 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003200
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003201 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003202 BUG_ON(tpacpi_inputdev->open != NULL ||
3203 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003204
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003205 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003206 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003207
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003208#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003209 mutex_init(&hotkey_thread_data_mutex);
3210#endif
3211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003212 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003213 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003215 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3216 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003217 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003218
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003219 if (!tp_features.hotkey)
3220 return 1;
3221
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003222 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3223 ARRAY_SIZE(tpacpi_hotkey_qtable));
3224
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003225 tpacpi_disable_brightness_delay();
3226
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003227 /* MUST have enough space for all attributes to be added to
3228 * hotkey_dev_attributes */
3229 hotkey_dev_attributes = create_attr_set(
3230 ARRAY_SIZE(hotkey_attributes) + 2,
3231 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003232 if (!hotkey_dev_attributes)
3233 return -ENOMEM;
3234 res = add_many_to_attr_set(hotkey_dev_attributes,
3235 hotkey_attributes,
3236 ARRAY_SIZE(hotkey_attributes));
3237 if (res)
3238 goto err_exit;
3239
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003240 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003241 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3242 for HKEY interface version 0x100 */
3243 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3244 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003245 pr_err("unknown version of the HKEY interface: 0x%x\n",
3246 hkeyv);
3247 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003248 } else {
3249 /*
3250 * MHKV 0x100 in A31, R40, R40e,
3251 * T4x, X31, and later
3252 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3254 "firmware HKEY interface version: 0x%x\n",
3255 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003256
3257 /* Paranoia check AND init hotkey_all_mask */
3258 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3259 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003260 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003261 "please report this to %s\n",
3262 TPACPI_MAIL);
3263 /* Fallback: pre-init for FN+F3,F4,F12 */
3264 hotkey_all_mask = 0x080cU;
3265 } else {
3266 tp_features.hotkey_mask = 1;
3267 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003268 }
3269 }
3270
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003271 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3272 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003273 str_supported(tp_features.hotkey_mask));
3274
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003275 /* Init hotkey_all_mask if not initialized yet */
3276 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3277 (quirks & TPACPI_HK_Q_INIMASK))
3278 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003279
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003280 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003281 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003282 /* hotkey_source_mask *must* be zero for
3283 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003284 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003285 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003286 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003287
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003288 hotkey_orig_mask = hotkey_acpi_mask;
3289 } else {
3290 hotkey_orig_mask = hotkey_all_mask;
3291 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003292 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003293
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003294#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3295 if (dbg_wlswemul) {
3296 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003297 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003298 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003299 } else
3300#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003301 /* Not all thinkpads have a hardware radio switch */
3302 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3303 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003304 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003305 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003306 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003307 }
3308 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003309 res = add_to_attr_set(hotkey_dev_attributes,
3310 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003311
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003312 /* For X41t, X60t, X61t Tablets... */
3313 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3314 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003315 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003316 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003317 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003318 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003319 res = add_to_attr_set(hotkey_dev_attributes,
3320 &dev_attr_hotkey_tablet_mode.attr);
3321 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003322
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003323 if (!res)
3324 res = register_attr_set_with_sysfs(
3325 hotkey_dev_attributes,
3326 &tpacpi_pdev->dev.kobj);
3327 if (res)
3328 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003329
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003331 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3332 GFP_KERNEL);
3333 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003334 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003335 res = -ENOMEM;
3336 goto err_exit;
3337 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003338
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003339 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3340 ARRAY_SIZE(tpacpi_keymap_qtable));
3341 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3342 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3343 "using keymap number %lu\n", keymap_id);
3344
3345 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3346 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003347
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003348 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003349 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3350 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3351 tpacpi_inputdev->keycode = hotkey_keycode_map;
3352 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3353 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003354 input_set_capability(tpacpi_inputdev, EV_KEY,
3355 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003356 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003357 if (i < sizeof(hotkey_reserved_mask)*8)
3358 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003359 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003360 }
3361
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003362 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003363 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003364 input_report_switch(tpacpi_inputdev,
3365 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003366 }
3367 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003368 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003369 input_report_switch(tpacpi_inputdev,
3370 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003371 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003372
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003374 * userspace. tpacpi_detect_brightness_capabilities() must have
3375 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003376 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003377 pr_info("This ThinkPad has standard ACPI backlight "
3378 "brightness control, supported by the ACPI "
3379 "video driver\n");
3380 pr_notice("Disabling thinkpad-acpi brightness events "
3381 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003382
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003383 /* Disable brightness up/down on Lenovo thinkpads when
3384 * ACPI is handling them, otherwise it is plain impossible
3385 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 hotkey_reserved_mask |=
3387 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3388 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003389 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3390 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003391 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003392
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003393#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003394 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3395 & ~hotkey_all_mask
3396 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003397
3398 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3399 "hotkey source mask 0x%08x, polling freq %u\n",
3400 hotkey_source_mask, hotkey_poll_freq);
3401#endif
3402
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003403 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3404 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003405 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003406 if (res) {
3407 hotkey_exit();
3408 return res;
3409 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003410 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3411 | hotkey_driver_mask)
3412 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003413 if (res < 0 && res != -ENXIO) {
3414 hotkey_exit();
3415 return res;
3416 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003417 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3418 & ~hotkey_reserved_mask;
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3421 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003422
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003423 tpacpi_inputdev->open = &hotkey_inputdev_open;
3424 tpacpi_inputdev->close = &hotkey_inputdev_close;
3425
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003426 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003427
3428 return 0;
3429
3430err_exit:
3431 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3432 hotkey_dev_attributes = NULL;
3433
3434 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003435}
3436
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003437static bool hotkey_notify_hotkey(const u32 hkey,
3438 bool *send_acpi_ev,
3439 bool *ignore_acpi_ev)
3440{
3441 /* 0x1000-0x1FFF: key presses */
3442 unsigned int scancode = hkey & 0xfff;
3443 *send_acpi_ev = true;
3444 *ignore_acpi_ev = false;
3445
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003446 /* HKEY event 0x1001 is scancode 0x00 */
3447 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003448 scancode--;
3449 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003450 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003451 *send_acpi_ev = false;
3452 } else {
3453 *ignore_acpi_ev = true;
3454 }
3455 return true;
3456 }
3457 return false;
3458}
3459
3460static bool hotkey_notify_wakeup(const u32 hkey,
3461 bool *send_acpi_ev,
3462 bool *ignore_acpi_ev)
3463{
3464 /* 0x2000-0x2FFF: Wakeup reason */
3465 *send_acpi_ev = true;
3466 *ignore_acpi_ev = false;
3467
3468 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003469 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3470 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003471 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3472 *ignore_acpi_ev = true;
3473 break;
3474
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003475 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3476 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003477 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3478 *ignore_acpi_ev = true;
3479 break;
3480
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003481 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3482 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003483 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003484 /* how to auto-heal: */
3485 /* 2313: woke up from S3, go to S4/S5 */
3486 /* 2413: woke up from S4, go to S5 */
3487 break;
3488
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003489 default:
3490 return false;
3491 }
3492
3493 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003494 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003495 hotkey_wakeup_reason_notify_change();
3496 }
3497 return true;
3498}
3499
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003500static bool hotkey_notify_dockevent(const u32 hkey,
3501 bool *send_acpi_ev,
3502 bool *ignore_acpi_ev)
3503{
3504 /* 0x4000-0x4FFF: dock-related events */
3505 *send_acpi_ev = true;
3506 *ignore_acpi_ev = false;
3507
3508 switch (hkey) {
3509 case TP_HKEY_EV_UNDOCK_ACK:
3510 /* ACPI undock operation completed after wakeup */
3511 hotkey_autosleep_ack = 1;
3512 pr_info("undocked\n");
3513 hotkey_wakeup_hotunplug_complete_notify_change();
3514 return true;
3515
3516 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3517 pr_info("docked into hotplug port replicator\n");
3518 return true;
3519 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3520 pr_info("undocked from hotplug port replicator\n");
3521 return true;
3522
3523 default:
3524 return false;
3525 }
3526}
3527
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003528static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3531{
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3535
3536 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003539 return true;
3540
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3547
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003551 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003552 *ignore_acpi_ev = true;
3553 return true;
3554
3555 default:
3556 return false;
3557 }
3558}
3559
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003560static void thermal_dump_all_sensors(void);
3561
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003562static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3565{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003566 bool known = true;
3567
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003568 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3571
3572 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003573 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003574 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003575 /* recommended action: do nothing, we don't have
3576 * Lenovo ATM information */
3577 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003578 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003579 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003580 /* recommended action: warn user through gui */
3581 break;
3582 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003583 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003584 /* recommended action: immediate sleep/hibernate */
3585 break;
3586 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003587 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003588 "a sensor reports something is too hot!\n");
3589 /* recommended action: warn user through gui, that */
3590 /* some internal component is too hot */
3591 break;
3592 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003593 pr_alert("THERMAL EMERGENCY: "
3594 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003595 /* recommended action: immediate sleep/hibernate */
3596 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003597 case TP_HKEY_EV_AC_CHANGED:
3598 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3599 * AC status changed; can be triggered by plugging or
3600 * unplugging AC adapter, docking or undocking. */
3601
3602 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003603
3604 case TP_HKEY_EV_KEY_NUMLOCK:
3605 case TP_HKEY_EV_KEY_FN:
3606 /* key press events, we just ignore them as long as the EC
3607 * is still reporting them in the normal keyboard stream */
3608 *send_acpi_ev = false;
3609 *ignore_acpi_ev = true;
3610 return true;
3611
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003612 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003613 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003614 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003615 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003616
3617 thermal_dump_all_sensors();
3618
3619 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003620}
3621
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003622static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3623{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003624 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003625 bool send_acpi_ev;
3626 bool ignore_acpi_ev;
3627 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003628
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003629 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003630 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003631 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003632 acpi_bus_generate_netlink_event(
3633 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003634 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003635 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003636 return;
3637 }
3638
3639 while (1) {
3640 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003641 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003642 return;
3643 }
3644
3645 if (hkey == 0) {
3646 /* queue empty */
3647 return;
3648 }
3649
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003650 send_acpi_ev = true;
3651 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003652
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003653 switch (hkey >> 12) {
3654 case 1:
3655 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003656 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3657 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003658 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003659 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003660 /* 0x2000-0x2FFF: Wakeup reason */
3661 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3662 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003663 break;
3664 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003665 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003666 switch (hkey) {
3667 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003668 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003669 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003670 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003671 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003672 break;
3673 case TP_HKEY_EV_OPTDRV_EJ:
3674 /* FIXME: kick libata if SATA link offline */
3675 known_ev = true;
3676 break;
3677 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003678 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003679 }
3680 break;
3681 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003682 /* 0x4000-0x4FFF: dock-related events */
3683 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3684 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003685 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003686 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003687 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003688 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003690 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003691 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003692 /* 0x6000-0x6FFF: thermal alarms/notices and
3693 * keyboard events */
3694 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003695 &ignore_acpi_ev);
3696 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003697 case 7:
3698 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003699 if (tp_features.hotkey_wlsw &&
3700 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003701 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003702 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003703 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003704 break;
3705 }
3706 /* fallthrough to default */
3707 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003708 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003709 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003710 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003711 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3712 pr_notice("please report the conditions when this "
3713 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003714 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003715
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003716 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003717 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003718 acpi_bus_generate_netlink_event(
3719 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003720 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003721 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003722 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003723 }
3724}
3725
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003726static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003727{
3728 /* Do these on suspend, we get the events on early resume! */
3729 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3730 hotkey_autosleep_ack = 0;
3731}
3732
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003733static void hotkey_resume(void)
3734{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03003735 tpacpi_disable_brightness_delay();
3736
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003737 if (hotkey_status_set(true) < 0 ||
3738 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003739 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003740 "firmware interface\n");
3741
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003742 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003743 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003744 hotkey_wakeup_reason_notify_change();
3745 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003746 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003747}
3748
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003749/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003750static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003752 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003754 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003755 seq_printf(m, "status:\t\tnot supported\n");
3756 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757 }
3758
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003759 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003760 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003761 res = hotkey_status_get(&status);
3762 if (!res)
3763 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003764 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003765 if (res)
3766 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003768 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003769 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003770 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3771 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003773 seq_printf(m, "mask:\t\tnot supported\n");
3774 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
3776
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003780static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003781{
3782 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003783 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003784 pr_fmt("hotkey enable/disable functionality has been "
3785 "removed from the driver. "
3786 "Hotkeys are always enabled.\n")))
3787 pr_err("Please remove the hotkey=enable module "
3788 "parameter, it is deprecated. "
3789 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003790}
3791
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003792static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003794 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003795 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003798 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return -ENODEV;
3800
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003801 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003802 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003803
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003804 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003805
3806 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 while ((cmd = next_cmd(&buf))) {
3808 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003809 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003811 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003812 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003814 mask = (hotkey_all_mask | hotkey_source_mask)
3815 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3817 /* mask set */
3818 } else if (sscanf(cmd, "%x", &mask) == 1) {
3819 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003820 } else {
3821 res = -EINVAL;
3822 goto errexit;
3823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003825
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003826 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003827 tpacpi_disclose_usertask("procfs hotkey",
3828 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003829 res = hotkey_user_mask_set(mask);
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003832errexit:
3833 mutex_unlock(&hotkey_mutex);
3834 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003835}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003837static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003838 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3839 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003840 {"", 0},
3841};
3842
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003843static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003844 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003845 .notify = hotkey_notify,
3846 .handle = &hkey_handle,
3847 .type = ACPI_DEVICE_NOTIFY,
3848};
3849
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003850static struct ibm_struct hotkey_driver_data = {
3851 .name = "hotkey",
3852 .read = hotkey_read,
3853 .write = hotkey_write,
3854 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003855 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003856 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003857 .acpi = &ibm_hotkey_acpidriver,
3858};
3859
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003860/*************************************************************************
3861 * Bluetooth subdriver
3862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003864enum {
3865 /* ACPI GBDC/SBDC bits */
3866 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3867 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003868 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003869 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003870};
3871
3872enum {
3873 /* ACPI \BLTH commands */
3874 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3875 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3876 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3877 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3878 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003879};
3880
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003881#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3882
Johannes Berg19d337d2009-06-02 13:01:37 +02003883static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003884{
3885 int status;
3886
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003887#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3888 if (dbg_bluetoothemul)
3889 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003890 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003891#endif
3892
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003893 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3894 return -EIO;
3895
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003896 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003897 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003898}
3899
Johannes Berg19d337d2009-06-02 13:01:37 +02003900static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003901{
3902 int status;
3903
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003904 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003905 "will attempt to %s bluetooth\n",
3906 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003907
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003908#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3909 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003910 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003911 return 0;
3912 }
3913#endif
3914
Johannes Berg19d337d2009-06-02 13:01:37 +02003915 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003916 status = TP_ACPI_BLUETOOTH_RADIOSSW
3917 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3918 else
3919 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003920
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003921 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3922 return -EIO;
3923
3924 return 0;
3925}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003926
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003927/* sysfs bluetooth enable ---------------------------------------------- */
3928static ssize_t bluetooth_enable_show(struct device *dev,
3929 struct device_attribute *attr,
3930 char *buf)
3931{
Johannes Berg19d337d2009-06-02 13:01:37 +02003932 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3933 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003934}
3935
3936static ssize_t bluetooth_enable_store(struct device *dev,
3937 struct device_attribute *attr,
3938 const char *buf, size_t count)
3939{
Johannes Berg19d337d2009-06-02 13:01:37 +02003940 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3941 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003942}
3943
3944static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003945 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003946 bluetooth_enable_show, bluetooth_enable_store);
3947
3948/* --------------------------------------------------------------------- */
3949
3950static struct attribute *bluetooth_attributes[] = {
3951 &dev_attr_bluetooth_enable.attr,
3952 NULL
3953};
3954
3955static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003956 .attrs = bluetooth_attributes,
3957};
3958
Johannes Berg19d337d2009-06-02 13:01:37 +02003959static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3960 .get_status = bluetooth_get_status,
3961 .set_status = bluetooth_set_status,
3962};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003963
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003964static void bluetooth_shutdown(void)
3965{
3966 /* Order firmware to save current state to NVRAM */
3967 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3968 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07003969 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003970 else
3971 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02003972 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003973}
3974
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003975static void bluetooth_exit(void)
3976{
3977 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3978 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003979
3980 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3981
3982 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003983}
3984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003985static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003987 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003988 int status = 0;
3989
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00003990 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3991 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003992
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003993 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003994
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003995 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3996 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003997 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003998 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004000 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4001 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004002 str_supported(tp_features.bluetooth),
4003 status);
4004
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004005#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4006 if (dbg_bluetoothemul) {
4007 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004008 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004009 } else
4010#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004011 if (tp_features.bluetooth &&
4012 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4013 /* no bluetooth hardware present in system */
4014 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004015 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004016 "bluetooth hardware not installed\n");
4017 }
4018
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004019 if (!tp_features.bluetooth)
4020 return 1;
4021
Johannes Berg19d337d2009-06-02 13:01:37 +02004022 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4023 &bluetooth_tprfk_ops,
4024 RFKILL_TYPE_BLUETOOTH,
4025 TPACPI_RFK_BLUETOOTH_SW_NAME,
4026 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004027 if (res)
4028 return res;
4029
Johannes Berg19d337d2009-06-02 13:01:37 +02004030 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4031 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004032 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004034 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004035 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004036
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004040/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004041static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004043 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004046static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Johannes Berg19d337d2009-06-02 13:01:37 +02004048 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004051static struct ibm_struct bluetooth_driver_data = {
4052 .name = "bluetooth",
4053 .read = bluetooth_read,
4054 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004055 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004056 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004057};
4058
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004059/*************************************************************************
4060 * Wan subdriver
4061 */
4062
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004063enum {
4064 /* ACPI GWAN/SWAN bits */
4065 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4066 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004067 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004068 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004069};
4070
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004071#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4072
Johannes Berg19d337d2009-06-02 13:01:37 +02004073static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004074{
4075 int status;
4076
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004077#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4078 if (dbg_wwanemul)
4079 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004080 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004081#endif
4082
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004083 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4084 return -EIO;
4085
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004086 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004087 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004088}
4089
Johannes Berg19d337d2009-06-02 13:01:37 +02004090static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004091{
4092 int status;
4093
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004094 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004095 "will attempt to %s wwan\n",
4096 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004097
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004098#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4099 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004100 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004101 return 0;
4102 }
4103#endif
4104
Johannes Berg19d337d2009-06-02 13:01:37 +02004105 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004106 status = TP_ACPI_WANCARD_RADIOSSW
4107 | TP_ACPI_WANCARD_RESUMECTRL;
4108 else
4109 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004110
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004111 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4112 return -EIO;
4113
4114 return 0;
4115}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004116
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004117/* sysfs wan enable ---------------------------------------------------- */
4118static ssize_t wan_enable_show(struct device *dev,
4119 struct device_attribute *attr,
4120 char *buf)
4121{
Johannes Berg19d337d2009-06-02 13:01:37 +02004122 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4123 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004124}
4125
4126static ssize_t wan_enable_store(struct device *dev,
4127 struct device_attribute *attr,
4128 const char *buf, size_t count)
4129{
Johannes Berg19d337d2009-06-02 13:01:37 +02004130 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4131 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004132}
4133
4134static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004135 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004136 wan_enable_show, wan_enable_store);
4137
4138/* --------------------------------------------------------------------- */
4139
4140static struct attribute *wan_attributes[] = {
4141 &dev_attr_wan_enable.attr,
4142 NULL
4143};
4144
4145static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004146 .attrs = wan_attributes,
4147};
4148
Johannes Berg19d337d2009-06-02 13:01:37 +02004149static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4150 .get_status = wan_get_status,
4151 .set_status = wan_set_status,
4152};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004153
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004154static void wan_shutdown(void)
4155{
4156 /* Order firmware to save current state to NVRAM */
4157 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4158 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004159 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004160 else
4161 vdbg_printk(TPACPI_DBG_RFKILL,
4162 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004163}
4164
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004165static void wan_exit(void)
4166{
4167 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4168 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004169
4170 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4171
4172 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004173}
4174
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004175static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004176{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004177 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004178 int status = 0;
4179
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004180 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4181 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004182
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004183 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004184
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004185 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004186 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004187
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4189 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004190 str_supported(tp_features.wan),
4191 status);
4192
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004193#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4194 if (dbg_wwanemul) {
4195 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004196 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004197 } else
4198#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004199 if (tp_features.wan &&
4200 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4201 /* no wan hardware present in system */
4202 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004203 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004204 "wan hardware not installed\n");
4205 }
4206
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004207 if (!tp_features.wan)
4208 return 1;
4209
Johannes Berg19d337d2009-06-02 13:01:37 +02004210 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4211 &wan_tprfk_ops,
4212 RFKILL_TYPE_WWAN,
4213 TPACPI_RFK_WWAN_SW_NAME,
4214 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004215 if (res)
4216 return res;
4217
Johannes Berg19d337d2009-06-02 13:01:37 +02004218 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4219 &wan_attr_group);
4220
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004221 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004222 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004223 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004224 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004225
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004226 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004227}
4228
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004229/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004230static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004231{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004232 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004233}
4234
4235static int wan_write(char *buf)
4236{
Johannes Berg19d337d2009-06-02 13:01:37 +02004237 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004238}
4239
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004240static struct ibm_struct wan_driver_data = {
4241 .name = "wan",
4242 .read = wan_read,
4243 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004244 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004245 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004246};
4247
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004248/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004249 * UWB subdriver
4250 */
4251
4252enum {
4253 /* ACPI GUWB/SUWB bits */
4254 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4255 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4256};
4257
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004258#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4259
Johannes Berg19d337d2009-06-02 13:01:37 +02004260static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004261{
4262 int status;
4263
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004264#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4265 if (dbg_uwbemul)
4266 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004267 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004268#endif
4269
4270 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4271 return -EIO;
4272
4273 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004275}
4276
Johannes Berg19d337d2009-06-02 13:01:37 +02004277static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004278{
4279 int status;
4280
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004281 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004282 "will attempt to %s UWB\n",
4283 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004284
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004285#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4286 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004287 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004288 return 0;
4289 }
4290#endif
4291
Johannes Berg19d337d2009-06-02 13:01:37 +02004292 if (state == TPACPI_RFK_RADIO_ON)
4293 status = TP_ACPI_UWB_RADIOSSW;
4294 else
4295 status = 0;
4296
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004297 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4298 return -EIO;
4299
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004300 return 0;
4301}
4302
4303/* --------------------------------------------------------------------- */
4304
Johannes Berg19d337d2009-06-02 13:01:37 +02004305static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4306 .get_status = uwb_get_status,
4307 .set_status = uwb_set_status,
4308};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309
4310static void uwb_exit(void)
4311{
Johannes Berg19d337d2009-06-02 13:01:37 +02004312 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004313}
4314
4315static int __init uwb_init(struct ibm_init_struct *iibm)
4316{
4317 int res;
4318 int status = 0;
4319
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004320 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4321 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004322
4323 TPACPI_ACPIHANDLE_INIT(hkey);
4324
4325 tp_features.uwb = hkey_handle &&
4326 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4327
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004328 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4329 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004330 str_supported(tp_features.uwb),
4331 status);
4332
4333#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4334 if (dbg_uwbemul) {
4335 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004336 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004337 } else
4338#endif
4339 if (tp_features.uwb &&
4340 !(status & TP_ACPI_UWB_HWPRESENT)) {
4341 /* no uwb hardware present in system */
4342 tp_features.uwb = 0;
4343 dbg_printk(TPACPI_DBG_INIT,
4344 "uwb hardware not installed\n");
4345 }
4346
4347 if (!tp_features.uwb)
4348 return 1;
4349
4350 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004351 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004352 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd92009-04-04 04:25:50 +00004353 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004354 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004355 return res;
4356}
4357
4358static struct ibm_struct uwb_driver_data = {
4359 .name = "uwb",
4360 .exit = uwb_exit,
4361 .flags.experimental = 1,
4362};
4363
4364/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004365 * Video subdriver
4366 */
4367
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004368#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4369
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004370enum video_access_mode {
4371 TPACPI_VIDEO_NONE = 0,
4372 TPACPI_VIDEO_570, /* 570 */
4373 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4374 TPACPI_VIDEO_NEW, /* all others */
4375};
4376
4377enum { /* video status flags, based on VIDEO_570 */
4378 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4379 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4380 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4381};
4382
4383enum { /* TPACPI_VIDEO_570 constants */
4384 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4385 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4386 * video_status_flags */
4387 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4389};
4390
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004391static enum video_access_mode video_supported;
4392static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004393
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004394static int video_autosw_get(void);
4395static int video_autosw_set(int enable);
4396
Joe Perches112a6ee2011-04-04 10:06:24 -07004397TPACPI_HANDLE(vid, root,
4398 "\\_SB.PCI.AGP.VGA", /* 570 */
4399 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4400 "\\_SB.PCI0.VID0", /* 770e */
4401 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4402 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4403 "\\_SB.PCI0.AGP.VID", /* all others */
4404 ); /* R30, R31 */
4405
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004406TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004407
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004408static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004410 int ivga;
4411
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004412 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4413
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004414 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004415 if (tpacpi_is_ibm())
4416 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004417
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004418 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4419 /* G41, assume IVGA doesn't change */
4420 vid_handle = vid2_handle;
4421
4422 if (!vid_handle)
4423 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004424 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004425 else if (tpacpi_is_ibm() &&
4426 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004427 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004428 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004429 else if (tpacpi_is_ibm() &&
4430 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004431 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004432 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004433 else
4434 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004437 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4438 str_supported(video_supported != TPACPI_VIDEO_NONE),
4439 video_supported);
4440
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004441 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004444static void video_exit(void)
4445{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004446 dbg_printk(TPACPI_DBG_EXIT,
4447 "restoring original video autoswitch mode\n");
4448 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004449 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004450 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004451}
4452
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004453static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 int status = 0;
4456 int i;
4457
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004458 switch (video_supported) {
4459 case TPACPI_VIDEO_570:
4460 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4461 TP_ACPI_VIDEO_570_PHSCMD))
4462 return -EIO;
4463 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4464 break;
4465 case TPACPI_VIDEO_770:
4466 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_LCD;
4470 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4471 return -EIO;
4472 if (i)
4473 status |= TP_ACPI_VIDEO_S_CRT;
4474 break;
4475 case TPACPI_VIDEO_NEW:
4476 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4477 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4478 return -EIO;
4479 if (i)
4480 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004482 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4483 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_LCD;
4487 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4488 return -EIO;
4489 if (i)
4490 status |= TP_ACPI_VIDEO_S_DVI;
4491 break;
4492 default:
4493 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
4496 return status;
4497}
4498
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004499static int video_outputsw_set(int status)
4500{
4501 int autosw;
4502 int res = 0;
4503
4504 switch (video_supported) {
4505 case TPACPI_VIDEO_570:
4506 res = acpi_evalf(NULL, NULL,
4507 "\\_SB.PHS2", "vdd",
4508 TP_ACPI_VIDEO_570_PHS2CMD,
4509 status | TP_ACPI_VIDEO_570_PHS2SET);
4510 break;
4511 case TPACPI_VIDEO_770:
4512 autosw = video_autosw_get();
4513 if (autosw < 0)
4514 return autosw;
4515
4516 res = video_autosw_set(1);
4517 if (res)
4518 return res;
4519 res = acpi_evalf(vid_handle, NULL,
4520 "ASWT", "vdd", status * 0x100, 0);
4521 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004522 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004523 return -EIO;
4524 }
4525 break;
4526 case TPACPI_VIDEO_NEW:
4527 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004528 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004529 break;
4530 default:
4531 return -ENOSYS;
4532 }
4533
4534 return (res)? 0 : -EIO;
4535}
4536
4537static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004539 int autosw = 0;
4540
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004541 switch (video_supported) {
4542 case TPACPI_VIDEO_570:
4543 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4544 return -EIO;
4545 break;
4546 case TPACPI_VIDEO_770:
4547 case TPACPI_VIDEO_NEW:
4548 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4549 return -EIO;
4550 break;
4551 default:
4552 return -ENOSYS;
4553 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004554
4555 return autosw & 1;
4556}
4557
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004558static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004560 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004562 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563}
4564
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004565static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004567 int autosw = video_autosw_get();
4568 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 if (autosw < 0)
4571 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004573 switch (video_supported) {
4574 case TPACPI_VIDEO_570:
4575 res = video_autosw_set(1);
4576 if (res)
4577 return res;
4578 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4579 break;
4580 case TPACPI_VIDEO_770:
4581 case TPACPI_VIDEO_NEW:
4582 res = video_autosw_set(1);
4583 if (res)
4584 return res;
4585 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4586 break;
4587 default:
4588 return -ENOSYS;
4589 }
4590 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004591 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004592 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004593 }
4594
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004595 return (res)? 0 : -EIO;
4596}
4597
4598static int video_expand_toggle(void)
4599{
4600 switch (video_supported) {
4601 case TPACPI_VIDEO_570:
4602 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4603 0 : -EIO;
4604 case TPACPI_VIDEO_770:
4605 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_NEW:
4608 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4609 0 : -EIO;
4610 default:
4611 return -ENOSYS;
4612 }
4613 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004614}
4615
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004616static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004618 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004620 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004621 seq_printf(m, "status:\t\tnot supported\n");
4622 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004623 }
4624
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004625 /* Even reads can crash X.org, so... */
4626 if (!capable(CAP_SYS_ADMIN))
4627 return -EPERM;
4628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004629 status = video_outputsw_get();
4630 if (status < 0)
4631 return status;
4632
4633 autosw = video_autosw_get();
4634 if (autosw < 0)
4635 return autosw;
4636
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004637 seq_printf(m, "status:\t\tsupported\n");
4638 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4639 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004640 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004641 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4642 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4643 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4644 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004645 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004646 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4647 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4648 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004649
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004650 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004651}
4652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004653static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
4655 char *cmd;
4656 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004657 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004659 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004660 return -ENODEV;
4661
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004662 /* Even reads can crash X.org, let alone writes... */
4663 if (!capable(CAP_SYS_ADMIN))
4664 return -EPERM;
4665
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004666 enable = 0;
4667 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 while ((cmd = next_cmd(&buf))) {
4670 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004671 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004673 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004675 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004677 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004678 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004679 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004680 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004681 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004683 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004685 res = video_autosw_set(1);
4686 if (res)
4687 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004689 res = video_autosw_set(0);
4690 if (res)
4691 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004693 res = video_outputsw_cycle();
4694 if (res)
4695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004697 res = video_expand_toggle();
4698 if (res)
4699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 } else
4701 return -EINVAL;
4702 }
4703
4704 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004705 status = video_outputsw_get();
4706 if (status < 0)
4707 return status;
4708 res = video_outputsw_set((status & ~disable) | enable);
4709 if (res)
4710 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 }
4712
4713 return 0;
4714}
4715
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004716static struct ibm_struct video_driver_data = {
4717 .name = "video",
4718 .read = video_read,
4719 .write = video_write,
4720 .exit = video_exit,
4721};
4722
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004723#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004725/*************************************************************************
4726 * Light (thinklight) subdriver
4727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004729TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4730TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004731
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004732static int light_get_status(void)
4733{
4734 int status = 0;
4735
4736 if (tp_features.light_status) {
4737 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4738 return -EIO;
4739 return (!!status);
4740 }
4741
4742 return -ENXIO;
4743}
4744
4745static int light_set_status(int status)
4746{
4747 int rc;
4748
4749 if (tp_features.light) {
4750 if (cmos_handle) {
4751 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4752 (status)?
4753 TP_CMOS_THINKLIGHT_ON :
4754 TP_CMOS_THINKLIGHT_OFF);
4755 } else {
4756 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4757 (status)? 1 : 0);
4758 }
4759 return (rc)? 0 : -EIO;
4760 }
4761
4762 return -ENXIO;
4763}
4764
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004765static void light_set_status_worker(struct work_struct *work)
4766{
4767 struct tpacpi_led_classdev *data =
4768 container_of(work, struct tpacpi_led_classdev, work);
4769
4770 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004771 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004772}
4773
4774static void light_sysfs_set(struct led_classdev *led_cdev,
4775 enum led_brightness brightness)
4776{
4777 struct tpacpi_led_classdev *data =
4778 container_of(led_cdev,
4779 struct tpacpi_led_classdev,
4780 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004781 data->new_state = (brightness != LED_OFF) ?
4782 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004783 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004784}
4785
4786static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4787{
4788 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4789}
4790
4791static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4792 .led_classdev = {
4793 .name = "tpacpi::thinklight",
4794 .brightness_set = &light_sysfs_set,
4795 .brightness_get = &light_sysfs_get,
4796 }
4797};
4798
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004799static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004801 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004802
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004803 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4804
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004805 if (tpacpi_is_ibm()) {
4806 TPACPI_ACPIHANDLE_INIT(ledb);
4807 TPACPI_ACPIHANDLE_INIT(lght);
4808 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004809 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004810 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004811
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004812 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004813 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004814
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004815 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004816 /* light status not supported on
4817 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004818 tp_features.light_status =
4819 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004821 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4822 str_supported(tp_features.light),
4823 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004824
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004825 if (!tp_features.light)
4826 return 1;
4827
4828 rc = led_classdev_register(&tpacpi_pdev->dev,
4829 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004830
4831 if (rc < 0) {
4832 tp_features.light = 0;
4833 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004834 } else {
4835 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004836 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004837
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004838 return rc;
4839}
4840
4841static void light_exit(void)
4842{
4843 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08004844 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845}
4846
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004847static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004849 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004851 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004852 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004853 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004854 seq_printf(m, "status:\t\tunknown\n");
4855 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004856 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004857 status = light_get_status();
4858 if (status < 0)
4859 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004860 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4861 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004867static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004870 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004872 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004873 return -ENODEV;
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 while ((cmd = next_cmd(&buf))) {
4876 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004877 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004879 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 } else
4881 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 }
4883
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004884 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004887static struct ibm_struct light_driver_data = {
4888 .name = "light",
4889 .read = light_read,
4890 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004891 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004892};
4893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004894/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004895 * CMOS subdriver
4896 */
4897
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004898/* sysfs cmos_command -------------------------------------------------- */
4899static ssize_t cmos_command_store(struct device *dev,
4900 struct device_attribute *attr,
4901 const char *buf, size_t count)
4902{
4903 unsigned long cmos_cmd;
4904 int res;
4905
4906 if (parse_strtoul(buf, 21, &cmos_cmd))
4907 return -EINVAL;
4908
4909 res = issue_thinkpad_cmos_command(cmos_cmd);
4910 return (res)? res : count;
4911}
4912
4913static struct device_attribute dev_attr_cmos_command =
4914 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4915
4916/* --------------------------------------------------------------------- */
4917
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004918static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004919{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004920 int res;
4921
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004922 vdbg_printk(TPACPI_DBG_INIT,
4923 "initializing cmos commands subdriver\n");
4924
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004925 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004926
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004927 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4928 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004929
4930 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4931 if (res)
4932 return res;
4933
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004934 return (cmos_handle)? 0 : 1;
4935}
4936
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004937static void cmos_exit(void)
4938{
4939 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940}
4941
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004942static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4945 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004947 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004949 seq_printf(m, "status:\t\tsupported\n");
4950 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004956static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
4958 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004959 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004962 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4963 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 /* cmos_cmd set */
4965 } else
4966 return -EINVAL;
4967
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004968 res = issue_thinkpad_cmos_command(cmos_cmd);
4969 if (res)
4970 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
4972
4973 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004974}
4975
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004976static struct ibm_struct cmos_driver_data = {
4977 .name = "cmos",
4978 .read = cmos_read,
4979 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004980 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004981};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004982
4983/*************************************************************************
4984 * LED subdriver
4985 */
4986
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004987enum led_access_mode {
4988 TPACPI_LED_NONE = 0,
4989 TPACPI_LED_570, /* 570 */
4990 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4991 TPACPI_LED_NEW, /* all others */
4992};
4993
4994enum { /* For TPACPI_LED_OLD */
4995 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4996 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4997 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4998};
4999
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005000static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005001
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005002static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005003
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005004#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005005static struct tpacpi_led_classdev *tpacpi_leds;
5006static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005007static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005008 /* there's a limit of 19 chars + NULL before 2.6.26 */
5009 "tpacpi::power",
5010 "tpacpi:orange:batt",
5011 "tpacpi:green:batt",
5012 "tpacpi::dock_active",
5013 "tpacpi::bay_active",
5014 "tpacpi::dock_batt",
5015 "tpacpi::unknown_led",
5016 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005017 "tpacpi::dock_status1",
5018 "tpacpi::dock_status2",
5019 "tpacpi::unknown_led2",
5020 "tpacpi::unknown_led3",
5021 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005022};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005023#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005024
5025static inline bool tpacpi_is_led_restricted(const unsigned int led)
5026{
5027#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5028 return false;
5029#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005030 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005031#endif
5032}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005033
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005034static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005035{
5036 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005037 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005038
5039 switch (led_supported) {
5040 case TPACPI_LED_570:
5041 if (!acpi_evalf(ec_handle,
5042 &status, "GLED", "dd", 1 << led))
5043 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005044 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005045 TPACPI_LED_OFF :
5046 ((status == 1)?
5047 TPACPI_LED_ON :
5048 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005049 tpacpi_led_state_cache[led] = led_s;
5050 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005051 default:
5052 return -ENXIO;
5053 }
5054
5055 /* not reached */
5056}
5057
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005058static int led_set_status(const unsigned int led,
5059 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005060{
5061 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005062 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5063 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005064
5065 int rc = 0;
5066
5067 switch (led_supported) {
5068 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005069 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005070 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005071 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005072 if (unlikely(tpacpi_is_led_restricted(led)))
5073 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005074 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5075 (1 << led), led_sled_arg1[ledstatus]))
5076 rc = -EIO;
5077 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005078 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005079 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005080 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005081 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005084 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5085 if (rc >= 0)
5086 rc = ec_write(TPACPI_LED_EC_HLBL,
5087 (ledstatus == TPACPI_LED_BLINK) << led);
5088 if (rc >= 0)
5089 rc = ec_write(TPACPI_LED_EC_HLCL,
5090 (ledstatus != TPACPI_LED_OFF) << led);
5091 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005092 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005093 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005094 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5095 return -EINVAL;
5096 if (unlikely(tpacpi_is_led_restricted(led)))
5097 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005098 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5099 led, led_led_arg1[ledstatus]))
5100 rc = -EIO;
5101 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005102 default:
5103 rc = -ENXIO;
5104 }
5105
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005106 if (!rc)
5107 tpacpi_led_state_cache[led] = ledstatus;
5108
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005109 return rc;
5110}
5111
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005112static void led_set_status_worker(struct work_struct *work)
5113{
5114 struct tpacpi_led_classdev *data =
5115 container_of(work, struct tpacpi_led_classdev, work);
5116
5117 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005118 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005119}
5120
5121static void led_sysfs_set(struct led_classdev *led_cdev,
5122 enum led_brightness brightness)
5123{
5124 struct tpacpi_led_classdev *data = container_of(led_cdev,
5125 struct tpacpi_led_classdev, led_classdev);
5126
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005127 if (brightness == LED_OFF)
5128 data->new_state = TPACPI_LED_OFF;
5129 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5130 data->new_state = TPACPI_LED_ON;
5131 else
5132 data->new_state = TPACPI_LED_BLINK;
5133
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005134 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005135}
5136
5137static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5138 unsigned long *delay_on, unsigned long *delay_off)
5139{
5140 struct tpacpi_led_classdev *data = container_of(led_cdev,
5141 struct tpacpi_led_classdev, led_classdev);
5142
5143 /* Can we choose the flash rate? */
5144 if (*delay_on == 0 && *delay_off == 0) {
5145 /* yes. set them to the hardware blink rate (1 Hz) */
5146 *delay_on = 500; /* ms */
5147 *delay_off = 500; /* ms */
5148 } else if ((*delay_on != 500) || (*delay_off != 500))
5149 return -EINVAL;
5150
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005151 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005152 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005153
5154 return 0;
5155}
5156
5157static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5158{
5159 int rc;
5160
5161 struct tpacpi_led_classdev *data = container_of(led_cdev,
5162 struct tpacpi_led_classdev, led_classdev);
5163
5164 rc = led_get_status(data->led);
5165
5166 if (rc == TPACPI_LED_OFF || rc < 0)
5167 rc = LED_OFF; /* no error handling in led class :( */
5168 else
5169 rc = LED_FULL;
5170
5171 return rc;
5172}
5173
5174static void led_exit(void)
5175{
5176 unsigned int i;
5177
5178 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5179 if (tpacpi_leds[i].led_classdev.name)
5180 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5181 }
5182
Li Dongyange03e3892012-07-25 10:45:07 +10005183 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005184 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005185}
5186
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005187static int __init tpacpi_init_led(unsigned int led)
5188{
5189 int rc;
5190
5191 tpacpi_leds[led].led = led;
5192
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005193 /* LEDs with no name don't get registered */
5194 if (!tpacpi_led_names[led])
5195 return 0;
5196
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005197 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5198 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5199 if (led_supported == TPACPI_LED_570)
5200 tpacpi_leds[led].led_classdev.brightness_get =
5201 &led_sysfs_get;
5202
5203 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5204
5205 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5206
5207 rc = led_classdev_register(&tpacpi_pdev->dev,
5208 &tpacpi_leds[led].led_classdev);
5209 if (rc < 0)
5210 tpacpi_leds[led].led_classdev.name = NULL;
5211
5212 return rc;
5213}
5214
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005215static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5216 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5217 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5218 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5219
5220 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5221 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5222 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5223 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5224 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5225 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5226 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5227 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5228
5229 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5230 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5231 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5232 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5233 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5234
5235 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5236 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5237 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5238 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5239
5240 /* (1) - may have excess leds enabled on MSB */
5241
5242 /* Defaults (order matters, keep last, don't reorder!) */
5243 { /* Lenovo */
5244 .vendor = PCI_VENDOR_ID_LENOVO,
5245 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5246 .quirks = 0x1fffU,
5247 },
5248 { /* IBM ThinkPads with no EC version string */
5249 .vendor = PCI_VENDOR_ID_IBM,
5250 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5251 .quirks = 0x00ffU,
5252 },
5253 { /* IBM ThinkPads with EC version string */
5254 .vendor = PCI_VENDOR_ID_IBM,
5255 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5256 .quirks = 0x00bfU,
5257 },
5258};
5259
5260#undef TPACPI_LEDQ_IBM
5261#undef TPACPI_LEDQ_LNV
5262
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005263static enum led_access_mode __init led_init_detect_mode(void)
5264{
5265 acpi_status status;
5266
5267 if (tpacpi_is_ibm()) {
5268 /* 570 */
5269 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5270 if (ACPI_SUCCESS(status))
5271 return TPACPI_LED_570;
5272
5273 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5274 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5275 if (ACPI_SUCCESS(status))
5276 return TPACPI_LED_OLD;
5277 }
5278
5279 /* most others */
5280 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5281 if (ACPI_SUCCESS(status))
5282 return TPACPI_LED_NEW;
5283
5284 /* R30, R31, and unknown firmwares */
5285 led_handle = NULL;
5286 return TPACPI_LED_NONE;
5287}
5288
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005289static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005290{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005291 unsigned int i;
5292 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005293 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005295 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5296
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005297 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005298
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005299 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5300 str_supported(led_supported), led_supported);
5301
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005302 if (led_supported == TPACPI_LED_NONE)
5303 return 1;
5304
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005305 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5306 GFP_KERNEL);
5307 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005308 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005309 return -ENOMEM;
5310 }
5311
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005312 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5313 ARRAY_SIZE(led_useful_qtable));
5314
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005315 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005316 tpacpi_leds[i].led = -1;
5317
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005318 if (!tpacpi_is_led_restricted(i) &&
5319 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005320 rc = tpacpi_init_led(i);
5321 if (rc < 0) {
5322 led_exit();
5323 return rc;
5324 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005325 }
5326 }
5327
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005328#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005329 pr_notice("warning: userspace override of important "
5330 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005331#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005332 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005333}
5334
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005335#define str_led_status(s) \
5336 ((s) == TPACPI_LED_OFF ? "off" : \
5337 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005338
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005339static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005342 seq_printf(m, "status:\t\tnot supported\n");
5343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005344 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005345 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005346
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005347 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005348 /* 570 */
5349 int i, status;
5350 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005351 status = led_get_status(i);
5352 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005353 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005354 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005355 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005356 }
5357 }
5358
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005359 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005360 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363}
5364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005365static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
5367 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005368 int led, rc;
5369 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005370
5371 if (!led_supported)
5372 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
5374 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005375 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 return -EINVAL;
5377
Adam Leeedf2d772013-06-08 16:51:15 +08005378 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5379 tpacpi_leds[led].led < 0)
5380 return -ENODEV;
5381
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005382 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005383 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005385 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005386 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005387 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005388 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005389 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005390 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005391
5392 rc = led_set_status(led, s);
5393 if (rc < 0)
5394 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 }
5396
5397 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005398}
5399
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005400static struct ibm_struct led_driver_data = {
5401 .name = "led",
5402 .read = led_read,
5403 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005404 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005405};
5406
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005407/*************************************************************************
5408 * Beep subdriver
5409 */
5410
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005411TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005412
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005413#define TPACPI_BEEP_Q1 0x0001
5414
5415static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5416 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5417 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5418};
5419
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005420static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005421{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005422 unsigned long quirks;
5423
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005424 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5425
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005426 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005427
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005428 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5429 str_supported(beep_handle != NULL));
5430
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005431 quirks = tpacpi_check_quirks(beep_quirk_table,
5432 ARRAY_SIZE(beep_quirk_table));
5433
5434 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5435
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005436 return (beep_handle)? 0 : 1;
5437}
5438
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005439static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005441 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005442 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005443 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005444 seq_printf(m, "status:\t\tsupported\n");
5445 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449}
5450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005451static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
5453 char *cmd;
5454 int beep_cmd;
5455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005456 if (!beep_handle)
5457 return -ENODEV;
5458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005460 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5461 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 /* beep_cmd set */
5463 } else
5464 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005465 if (tp_features.beep_needs_two_args) {
5466 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5467 beep_cmd, 0))
5468 return -EIO;
5469 } else {
5470 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5471 beep_cmd))
5472 return -EIO;
5473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
5475
5476 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005477}
5478
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005479static struct ibm_struct beep_driver_data = {
5480 .name = "beep",
5481 .read = beep_read,
5482 .write = beep_write,
5483};
5484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005485/*************************************************************************
5486 * Thermal subdriver
5487 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005488
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005489enum thermal_access_mode {
5490 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5491 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5492 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5493 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5494 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5495};
5496
5497enum { /* TPACPI_THERMAL_TPEC_* */
5498 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5499 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5500 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005501
5502 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005503};
5504
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005505
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005506#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5507struct ibm_thermal_sensors_struct {
5508 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5509};
5510
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005511static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005512
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005513/* idx is zero-based */
5514static int thermal_get_sensor(int idx, s32 *value)
5515{
5516 int t;
5517 s8 tmp;
5518 char tmpi[5];
5519
5520 t = TP_EC_THERMAL_TMP0;
5521
5522 switch (thermal_read_mode) {
5523#if TPACPI_MAX_THERMAL_SENSORS >= 16
5524 case TPACPI_THERMAL_TPEC_16:
5525 if (idx >= 8 && idx <= 15) {
5526 t = TP_EC_THERMAL_TMP8;
5527 idx -= 8;
5528 }
5529 /* fallthrough */
5530#endif
5531 case TPACPI_THERMAL_TPEC_8:
5532 if (idx <= 7) {
5533 if (!acpi_ec_read(t + idx, &tmp))
5534 return -EIO;
5535 *value = tmp * 1000;
5536 return 0;
5537 }
5538 break;
5539
5540 case TPACPI_THERMAL_ACPI_UPDT:
5541 if (idx <= 7) {
5542 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5543 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5544 return -EIO;
5545 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5546 return -EIO;
5547 *value = (t - 2732) * 100;
5548 return 0;
5549 }
5550 break;
5551
5552 case TPACPI_THERMAL_ACPI_TMP07:
5553 if (idx <= 7) {
5554 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5555 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5556 return -EIO;
5557 if (t > 127 || t < -127)
5558 t = TP_EC_THERMAL_TMP_NA;
5559 *value = t * 1000;
5560 return 0;
5561 }
5562 break;
5563
5564 case TPACPI_THERMAL_NONE:
5565 default:
5566 return -ENOSYS;
5567 }
5568
5569 return -EINVAL;
5570}
5571
5572static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5573{
5574 int res, i;
5575 int n;
5576
5577 n = 8;
5578 i = 0;
5579
5580 if (!s)
5581 return -EINVAL;
5582
5583 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5584 n = 16;
5585
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005586 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005587 res = thermal_get_sensor(i, &s->temp[i]);
5588 if (res)
5589 return res;
5590 }
5591
5592 return n;
5593}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005594
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005595static void thermal_dump_all_sensors(void)
5596{
5597 int n, i;
5598 struct ibm_thermal_sensors_struct t;
5599
5600 n = thermal_get_sensors(&t);
5601 if (n <= 0)
5602 return;
5603
Joe Perches0978e012011-04-04 10:06:25 -07005604 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005605
5606 for (i = 0; i < n; i++) {
5607 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005608 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005609 else
Joe Perches0978e012011-04-04 10:06:25 -07005610 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005611 }
5612
Joe Perches0978e012011-04-04 10:06:25 -07005613 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005614}
5615
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005616/* sysfs temp##_input -------------------------------------------------- */
5617
5618static ssize_t thermal_temp_input_show(struct device *dev,
5619 struct device_attribute *attr,
5620 char *buf)
5621{
5622 struct sensor_device_attribute *sensor_attr =
5623 to_sensor_dev_attr(attr);
5624 int idx = sensor_attr->index;
5625 s32 value;
5626 int res;
5627
5628 res = thermal_get_sensor(idx, &value);
5629 if (res)
5630 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005631 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005632 return -ENXIO;
5633
5634 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5635}
5636
5637#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005638 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5639 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005640
5641static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5642 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5643 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5644 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5645 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5646 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5647 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5648 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5649 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5650 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5651 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5652 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5653 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5654 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5655 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5656 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5657 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5658};
5659
5660#define THERMAL_ATTRS(X) \
5661 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5662
5663static struct attribute *thermal_temp_input_attr[] = {
5664 THERMAL_ATTRS(8),
5665 THERMAL_ATTRS(9),
5666 THERMAL_ATTRS(10),
5667 THERMAL_ATTRS(11),
5668 THERMAL_ATTRS(12),
5669 THERMAL_ATTRS(13),
5670 THERMAL_ATTRS(14),
5671 THERMAL_ATTRS(15),
5672 THERMAL_ATTRS(0),
5673 THERMAL_ATTRS(1),
5674 THERMAL_ATTRS(2),
5675 THERMAL_ATTRS(3),
5676 THERMAL_ATTRS(4),
5677 THERMAL_ATTRS(5),
5678 THERMAL_ATTRS(6),
5679 THERMAL_ATTRS(7),
5680 NULL
5681};
5682
5683static const struct attribute_group thermal_temp_input16_group = {
5684 .attrs = thermal_temp_input_attr
5685};
5686
5687static const struct attribute_group thermal_temp_input8_group = {
5688 .attrs = &thermal_temp_input_attr[8]
5689};
5690
5691#undef THERMAL_SENSOR_ATTR_TEMP
5692#undef THERMAL_ATTRS
5693
5694/* --------------------------------------------------------------------- */
5695
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005696static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005697{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005698 u8 t, ta1, ta2;
5699 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005700 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005701 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005702
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005703 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5704
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005705 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005706
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005707 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005708 /*
5709 * Direct EC access mode: sensors at registers
5710 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5711 * non-implemented, thermal sensors return 0x80 when
5712 * not available
5713 */
5714
5715 ta1 = ta2 = 0;
5716 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005717 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005718 ta1 |= t;
5719 } else {
5720 ta1 = 0;
5721 break;
5722 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005723 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005724 ta2 |= t;
5725 } else {
5726 ta1 = 0;
5727 break;
5728 }
5729 }
5730 if (ta1 == 0) {
5731 /* This is sheer paranoia, but we handle it anyway */
5732 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005733 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005734 "falling back to ACPI TMPx access "
5735 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005736 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005737 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005738 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005739 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005740 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005741 }
5742 } else {
5743 thermal_read_mode =
5744 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005745 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005746 }
5747 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005748 if (tpacpi_is_ibm() &&
5749 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005750 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005751 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005752 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005753 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005754 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005755 }
5756 } else {
5757 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005758 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005759 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005760
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005761 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5762 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5763 thermal_read_mode);
5764
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005765 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005766 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005767 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005768 &thermal_temp_input16_group);
5769 if (res)
5770 return res;
5771 break;
5772 case TPACPI_THERMAL_TPEC_8:
5773 case TPACPI_THERMAL_ACPI_TMP07:
5774 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005775 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005776 &thermal_temp_input8_group);
5777 if (res)
5778 return res;
5779 break;
5780 case TPACPI_THERMAL_NONE:
5781 default:
5782 return 1;
5783 }
5784
5785 return 0;
5786}
5787
5788static void thermal_exit(void)
5789{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005790 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005791 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005792 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005793 &thermal_temp_input16_group);
5794 break;
5795 case TPACPI_THERMAL_TPEC_8:
5796 case TPACPI_THERMAL_ACPI_TMP07:
5797 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005798 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005799 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005800 break;
5801 case TPACPI_THERMAL_NONE:
5802 default:
5803 break;
5804 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005805}
5806
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005807static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005808{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005809 int n, i;
5810 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005811
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005812 n = thermal_get_sensors(&t);
5813 if (unlikely(n < 0))
5814 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005815
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005816 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005817
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005818 if (n > 0) {
5819 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005820 seq_printf(m, "%d ", t.temp[i] / 1000);
5821 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005822 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005823 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005824
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005825 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005826}
5827
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005828static struct ibm_struct thermal_driver_data = {
5829 .name = "thermal",
5830 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005831 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005832};
5833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005834/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005835 * Backlight/brightness subdriver
5836 */
Holger Macht8acb0252006-10-20 14:30:28 -07005837
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005838#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5839
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005840/*
5841 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5842 * CMOS NVRAM byte 0x5E, bits 0-3.
5843 *
5844 * EC HBRV (0x31) has the following layout
5845 * Bit 7: unknown function
5846 * Bit 6: unknown function
5847 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5848 * Bit 4: must be set to zero to avoid problems
5849 * Bit 3-0: backlight brightness level
5850 *
5851 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005852 *
5853 * WARNING: The X61 has been verified to use HBRV for something else, so
5854 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5855 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005856 */
5857
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005858enum {
5859 TP_EC_BACKLIGHT = 0x31,
5860
5861 /* TP_EC_BACKLIGHT bitmasks */
5862 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5863 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5864 TP_EC_BACKLIGHT_MAPSW = 0x20,
5865};
5866
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005867enum tpacpi_brightness_access_mode {
5868 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5869 TPACPI_BRGHT_MODE_EC, /* EC control */
5870 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5871 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5872 TPACPI_BRGHT_MODE_MAX
5873};
5874
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005875static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005876
5877static enum tpacpi_brightness_access_mode brightness_mode =
5878 TPACPI_BRGHT_MODE_MAX;
5879
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005880static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5881
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005882static struct mutex brightness_mutex;
5883
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005884/* NVRAM brightness access,
5885 * call with brightness_mutex held! */
5886static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005887{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005888 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005889
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005890 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5891 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5892 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005893 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005894
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005895 return lnvram;
5896}
5897
5898static void tpacpi_brightness_checkpoint_nvram(void)
5899{
5900 u8 lec = 0;
5901 u8 b_nvram;
5902
5903 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5904 return;
5905
5906 vdbg_printk(TPACPI_DBG_BRGHT,
5907 "trying to checkpoint backlight level to NVRAM...\n");
5908
5909 if (mutex_lock_killable(&brightness_mutex) < 0)
5910 return;
5911
5912 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5913 goto unlock;
5914 lec &= TP_EC_BACKLIGHT_LVLMSK;
5915 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5916
5917 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5918 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5919 /* NVRAM needs update */
5920 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5921 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5922 b_nvram |= lec;
5923 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5924 dbg_printk(TPACPI_DBG_BRGHT,
5925 "updated NVRAM backlight level to %u (0x%02x)\n",
5926 (unsigned int) lec, (unsigned int) b_nvram);
5927 } else
5928 vdbg_printk(TPACPI_DBG_BRGHT,
5929 "NVRAM backlight level already is %u (0x%02x)\n",
5930 (unsigned int) lec, (unsigned int) b_nvram);
5931
5932unlock:
5933 mutex_unlock(&brightness_mutex);
5934}
5935
5936
5937/* call with brightness_mutex held! */
5938static int tpacpi_brightness_get_raw(int *status)
5939{
5940 u8 lec = 0;
5941
5942 switch (brightness_mode) {
5943 case TPACPI_BRGHT_MODE_UCMS_STEP:
5944 *status = tpacpi_brightness_nvram_get();
5945 return 0;
5946 case TPACPI_BRGHT_MODE_EC:
5947 case TPACPI_BRGHT_MODE_ECNVRAM:
5948 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005949 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005950 *status = lec;
5951 return 0;
5952 default:
5953 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005954 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005955}
5956
5957/* call with brightness_mutex held! */
5958/* do NOT call with illegal backlight level value */
5959static int tpacpi_brightness_set_ec(unsigned int value)
5960{
5961 u8 lec = 0;
5962
5963 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5964 return -EIO;
5965
5966 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5967 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5968 (value & TP_EC_BACKLIGHT_LVLMSK))))
5969 return -EIO;
5970
5971 return 0;
5972}
5973
5974/* call with brightness_mutex held! */
5975static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5976{
5977 int cmos_cmd, inc;
5978 unsigned int current_value, i;
5979
5980 current_value = tpacpi_brightness_nvram_get();
5981
5982 if (value == current_value)
5983 return 0;
5984
5985 cmos_cmd = (value > current_value) ?
5986 TP_CMOS_BRIGHTNESS_UP :
5987 TP_CMOS_BRIGHTNESS_DOWN;
5988 inc = (value > current_value) ? 1 : -1;
5989
5990 for (i = current_value; i != value; i += inc)
5991 if (issue_thinkpad_cmos_command(cmos_cmd))
5992 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005993
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005994 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005995}
5996
5997/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005998static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005999{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006000 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006001
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006002 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006003 return -EINVAL;
6004
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006005 vdbg_printk(TPACPI_DBG_BRGHT,
6006 "set backlight level to %d\n", value);
6007
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006008 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006009 if (res < 0)
6010 return res;
6011
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006012 switch (brightness_mode) {
6013 case TPACPI_BRGHT_MODE_EC:
6014 case TPACPI_BRGHT_MODE_ECNVRAM:
6015 res = tpacpi_brightness_set_ec(value);
6016 break;
6017 case TPACPI_BRGHT_MODE_UCMS_STEP:
6018 res = tpacpi_brightness_set_ucmsstep(value);
6019 break;
6020 default:
6021 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006022 }
6023
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006024 mutex_unlock(&brightness_mutex);
6025 return res;
6026}
6027
6028/* sysfs backlight class ----------------------------------------------- */
6029
6030static int brightness_update_status(struct backlight_device *bd)
6031{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006032 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006033 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6034 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006035 bd->props.brightness : 0;
6036
6037 dbg_printk(TPACPI_DBG_BRGHT,
6038 "backlight: attempt to set level to %d\n",
6039 level);
6040
6041 /* it is the backlight class's job (caller) to handle
6042 * EINTR and other errors properly */
6043 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006044}
Holger Macht8acb0252006-10-20 14:30:28 -07006045
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006046static int brightness_get(struct backlight_device *bd)
6047{
6048 int status, res;
6049
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006050 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006051 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006052 return 0;
6053
6054 res = tpacpi_brightness_get_raw(&status);
6055
6056 mutex_unlock(&brightness_mutex);
6057
6058 if (res < 0)
6059 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006060
6061 return status & TP_EC_BACKLIGHT_LVLMSK;
6062}
6063
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006064static void tpacpi_brightness_notify_change(void)
6065{
6066 backlight_force_update(ibm_backlight_device,
6067 BACKLIGHT_UPDATE_HOTKEY);
6068}
6069
Lionel Debrouxacc24722010-11-16 14:14:02 +01006070static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006071 .get_brightness = brightness_get,
6072 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006073};
6074
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006075/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006076
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006077/*
6078 * Call _BCL method of video device. On some ThinkPads this will
6079 * switch the firmware to the ACPI brightness control mode.
6080 */
6081
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006082static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6083{
6084 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6085 union acpi_object *obj;
6086 int rc;
6087
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006088 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006089 obj = (union acpi_object *)buffer.pointer;
6090 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006091 pr_err("Unknown _BCL data, please report this to %s\n",
6092 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006093 rc = 0;
6094 } else {
6095 rc = obj->package.count;
6096 }
6097 } else {
6098 return 0;
6099 }
6100
6101 kfree(buffer.pointer);
6102 return rc;
6103}
6104
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006105
6106/*
6107 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6108 */
6109static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6110{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006111 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006112 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006113
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006114 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6115 if (video_device)
6116 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006117
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006118 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006119
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006120 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006121}
6122
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006123/*
6124 * These are only useful for models that have only one possibility
6125 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6126 * these quirks.
6127 */
6128#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6129#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6130#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6131
6132static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6133 /* Models with ATI GPUs known to require ECNVRAM mode */
6134 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6135
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006136 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006137 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006138 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006139 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006140 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6141
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006142 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006143 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006144 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6145 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006146
6147 /* Models with Intel GMA900 */
6148 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6149 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6150 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6151};
6152
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006153/*
6154 * Returns < 0 for error, otherwise sets tp_features.bright_*
6155 * and bright_maxlvl.
6156 */
6157static void __init tpacpi_detect_brightness_capabilities(void)
6158{
6159 unsigned int b;
6160
6161 vdbg_printk(TPACPI_DBG_INIT,
6162 "detecting firmware brightness interface capabilities\n");
6163
6164 /* we could run a quirks check here (same table used by
6165 * brightness_init) if needed */
6166
6167 /*
6168 * We always attempt to detect acpi support, so as to switch
6169 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6170 * going to publish a backlight interface
6171 */
6172 b = tpacpi_check_std_acpi_brightness_support();
6173 switch (b) {
6174 case 16:
6175 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006176 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006177 break;
6178 case 8:
6179 case 0:
6180 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006181 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006182 break;
6183 default:
Joe Perches0978e012011-04-04 10:06:25 -07006184 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006185 "please contact %s\n", TPACPI_MAIL);
6186 tp_features.bright_unkfw = 1;
6187 bright_maxlvl = b - 1;
6188 }
6189}
6190
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006191static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006192{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006193 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006194 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006195 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006196
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006197 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6198
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006199 mutex_init(&brightness_mutex);
6200
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006201 quirks = tpacpi_check_quirks(brightness_quirk_table,
6202 ARRAY_SIZE(brightness_quirk_table));
6203
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006204 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006205
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006206 /* if it is unknown, we don't handle it: it wouldn't be safe */
6207 if (tp_features.bright_unkfw)
6208 return 1;
6209
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006210 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006211 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006212 "brightness support disabled by "
6213 "module parameter\n");
6214 return 1;
6215 }
6216
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006217 if (acpi_video_backlight_support()) {
6218 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006219 pr_info("Standard ACPI backlight interface "
6220 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006221 return 1;
6222 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006223 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006224 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006225 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006226 return 1;
6227 }
6228 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006229 pr_notice("Standard ACPI backlight interface not "
6230 "available, thinkpad_acpi native "
6231 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006232 }
6233
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006234 /*
6235 * Check for module parameter bogosity, note that we
6236 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6237 * able to detect "unspecified"
6238 */
6239 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006240 return -EINVAL;
6241
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006242 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6243 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6244 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006245 if (quirks & TPACPI_BRGHT_Q_EC)
6246 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6247 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006248 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6249
6250 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006251 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006252 brightness_mode);
6253 }
6254
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006255 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006256 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006257 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6258 brightness_mode == TPACPI_BRGHT_MODE_EC))
6259 return -EINVAL;
6260
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006261 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006262 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006263
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006264 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006265 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006266 props.max_brightness = bright_maxlvl;
6267 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006268 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6269 NULL, NULL,
6270 &ibm_backlight_data,
6271 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006272 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006273 int rc = PTR_ERR(ibm_backlight_device);
6274 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006275 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006276 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006277 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006278 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6279 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006280
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006281 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006282 pr_notice("brightness: will use unverified default: "
6283 "brightness_mode=%d\n", brightness_mode);
6284 pr_notice("brightness: please report to %s whether it works well "
6285 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006286 }
6287
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006288 /* Added by mistake in early 2007. Probably useless, but it could
6289 * be working around some unknown firmware problem where the value
6290 * read at startup doesn't match the real hardware state... so leave
6291 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006292 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006293
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006294 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6295 "brightness: registering brightness hotkeys "
6296 "as change notification\n");
6297 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6298 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006299 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006300 return 0;
6301}
6302
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006303static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006304{
6305 tpacpi_brightness_checkpoint_nvram();
6306}
6307
6308static void brightness_shutdown(void)
6309{
6310 tpacpi_brightness_checkpoint_nvram();
6311}
6312
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006313static void brightness_exit(void)
6314{
6315 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006316 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006317 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006318 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006319 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006320
6321 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006322}
6323
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006324static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006325{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006326 int level;
6327
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006328 level = brightness_get(NULL);
6329 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006330 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006331 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006332 seq_printf(m, "level:\t\t%d\n", level);
6333 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006334 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6335 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336 }
6337
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006338 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339}
6340
6341static int brightness_write(char *buf)
6342{
6343 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006344 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006345 char *cmd;
6346
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006347 level = brightness_get(NULL);
6348 if (level < 0)
6349 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006350
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006351 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006352 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006353 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006354 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006355 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006356 if (level > 0)
6357 level--;
6358 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006359 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006360 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006361 } else
6362 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006363 }
6364
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006365 tpacpi_disclose_usertask("procfs brightness",
6366 "set level to %d\n", level);
6367
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006368 /*
6369 * Now we know what the final level should be, so we try to set it.
6370 * Doing it this way makes the syscall restartable in case of EINTR
6371 */
6372 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006373 if (!rc && ibm_backlight_device)
6374 backlight_force_update(ibm_backlight_device,
6375 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006376 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006377}
6378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006379static struct ibm_struct brightness_driver_data = {
6380 .name = "brightness",
6381 .read = brightness_read,
6382 .write = brightness_write,
6383 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006384 .suspend = brightness_suspend,
6385 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006386};
6387
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006388/*************************************************************************
6389 * Volume subdriver
6390 */
6391
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006392/*
6393 * IBM ThinkPads have a simple volume controller with MUTE gating.
6394 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6395 *
6396 * Since the *61 series (and probably also the later *60 series), Lenovo
6397 * ThinkPads only implement the MUTE gate.
6398 *
6399 * EC register 0x30
6400 * Bit 6: MUTE (1 mutes sound)
6401 * Bit 3-0: Volume
6402 * Other bits should be zero as far as we know.
6403 *
6404 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6405 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6406 * such as bit 7 which is used to detect repeated presses of MUTE,
6407 * and we leave them unchanged.
6408 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006409
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006410#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6411
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006412#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6413#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6414#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6415
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006416static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006417static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306418static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006419
6420struct tpacpi_alsa_data {
6421 struct snd_card *card;
6422 struct snd_ctl_elem_id *ctl_mute_id;
6423 struct snd_ctl_elem_id *ctl_vol_id;
6424};
6425
6426static struct snd_card *alsa_card;
6427
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006428enum {
6429 TP_EC_AUDIO = 0x30,
6430
6431 /* TP_EC_AUDIO bits */
6432 TP_EC_AUDIO_MUTESW = 6,
6433
6434 /* TP_EC_AUDIO bitmasks */
6435 TP_EC_AUDIO_LVL_MSK = 0x0F,
6436 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6437
6438 /* Maximum volume */
6439 TP_EC_VOLUME_MAX = 14,
6440};
6441
6442enum tpacpi_volume_access_mode {
6443 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6444 TPACPI_VOL_MODE_EC, /* Pure EC control */
6445 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6446 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6447 TPACPI_VOL_MODE_MAX
6448};
6449
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006450enum tpacpi_volume_capabilities {
6451 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6452 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6453 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6454 TPACPI_VOL_CAP_MAX
6455};
6456
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006457static enum tpacpi_volume_access_mode volume_mode =
6458 TPACPI_VOL_MODE_MAX;
6459
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006460static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306461static bool volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006462
6463/*
6464 * Used to syncronize writers to TP_EC_AUDIO and
6465 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6466 */
6467static struct mutex volume_mutex;
6468
6469static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006470{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006471 u8 lec = 0;
6472 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006473 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006474
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006475 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6476 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006477 if (!volume_control_allowed)
6478 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006479
6480 vdbg_printk(TPACPI_DBG_MIXER,
6481 "trying to checkpoint mixer state to NVRAM...\n");
6482
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006483 if (tp_features.mixer_no_level_control)
6484 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6485 else
6486 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6487
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006488 if (mutex_lock_killable(&volume_mutex) < 0)
6489 return;
6490
6491 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6492 goto unlock;
6493 lec &= ec_mask;
6494 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6495
6496 if (lec != (b_nvram & ec_mask)) {
6497 /* NVRAM needs update */
6498 b_nvram &= ~ec_mask;
6499 b_nvram |= lec;
6500 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6501 dbg_printk(TPACPI_DBG_MIXER,
6502 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6503 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006504 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006505 vdbg_printk(TPACPI_DBG_MIXER,
6506 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6507 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006508 }
6509
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006510unlock:
6511 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006512}
6513
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006514static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006515{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006516 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006517
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006518 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6519 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006520
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006521 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006522
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006523 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006524
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006525 return 0;
6526}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006527
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006528static int volume_get_status(u8 *status)
6529{
6530 return volume_get_status_ec(status);
6531}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006532
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006533static int volume_set_status_ec(const u8 status)
6534{
6535 if (!acpi_ec_write(TP_EC_AUDIO, status))
6536 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006537
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006538 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6539
6540 return 0;
6541}
6542
6543static int volume_set_status(const u8 status)
6544{
6545 return volume_set_status_ec(status);
6546}
6547
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006548/* returns < 0 on error, 0 on no change, 1 on change */
6549static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006550{
6551 int rc;
6552 u8 s, n;
6553
6554 if (mutex_lock_killable(&volume_mutex) < 0)
6555 return -EINTR;
6556
6557 rc = volume_get_status_ec(&s);
6558 if (rc)
6559 goto unlock;
6560
6561 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6562 s & ~TP_EC_AUDIO_MUTESW_MSK;
6563
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006564 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006565 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006566 if (!rc)
6567 rc = 1;
6568 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006569
6570unlock:
6571 mutex_unlock(&volume_mutex);
6572 return rc;
6573}
6574
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006575static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006576{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006577 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006578 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006579 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006580}
6581
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006582static int volume_set_mute(const bool mute)
6583{
6584 int rc;
6585
6586 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6587 (mute) ? "" : "un");
6588
6589 rc = __volume_set_mute_ec(mute);
6590 return (rc < 0) ? rc : 0;
6591}
6592
6593/* returns < 0 on error, 0 on no change, 1 on change */
6594static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006595{
6596 int rc;
6597 u8 s, n;
6598
6599 if (vol > TP_EC_VOLUME_MAX)
6600 return -EINVAL;
6601
6602 if (mutex_lock_killable(&volume_mutex) < 0)
6603 return -EINTR;
6604
6605 rc = volume_get_status_ec(&s);
6606 if (rc)
6607 goto unlock;
6608
6609 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6610
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006611 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006612 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006613 if (!rc)
6614 rc = 1;
6615 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006616
6617unlock:
6618 mutex_unlock(&volume_mutex);
6619 return rc;
6620}
6621
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006622static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006623{
6624 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006625 "ALSA: trying to set volume level to %hu\n", vol);
6626 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006627}
6628
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006629static void volume_alsa_notify_change(void)
6630{
6631 struct tpacpi_alsa_data *d;
6632
6633 if (alsa_card && alsa_card->private_data) {
6634 d = alsa_card->private_data;
6635 if (d->ctl_mute_id)
6636 snd_ctl_notify(alsa_card,
6637 SNDRV_CTL_EVENT_MASK_VALUE,
6638 d->ctl_mute_id);
6639 if (d->ctl_vol_id)
6640 snd_ctl_notify(alsa_card,
6641 SNDRV_CTL_EVENT_MASK_VALUE,
6642 d->ctl_vol_id);
6643 }
6644}
6645
6646static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6647 struct snd_ctl_elem_info *uinfo)
6648{
6649 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6650 uinfo->count = 1;
6651 uinfo->value.integer.min = 0;
6652 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6653 return 0;
6654}
6655
6656static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6657 struct snd_ctl_elem_value *ucontrol)
6658{
6659 u8 s;
6660 int rc;
6661
6662 rc = volume_get_status(&s);
6663 if (rc < 0)
6664 return rc;
6665
6666 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6667 return 0;
6668}
6669
6670static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6671 struct snd_ctl_elem_value *ucontrol)
6672{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006673 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6674 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006675 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006676}
6677
6678#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6679
6680static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6681 struct snd_ctl_elem_value *ucontrol)
6682{
6683 u8 s;
6684 int rc;
6685
6686 rc = volume_get_status(&s);
6687 if (rc < 0)
6688 return rc;
6689
6690 ucontrol->value.integer.value[0] =
6691 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6692 return 0;
6693}
6694
6695static int volume_alsa_mute_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", "%smute\n",
6699 ucontrol->value.integer.value[0] ?
6700 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006701 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006702}
6703
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006704static struct snd_kcontrol_new volume_alsa_control_vol = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006705 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6706 .name = "Console Playback Volume",
6707 .index = 0,
6708 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6709 .info = volume_alsa_vol_info,
6710 .get = volume_alsa_vol_get,
6711};
6712
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006713static struct snd_kcontrol_new volume_alsa_control_mute = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006714 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6715 .name = "Console Playback Switch",
6716 .index = 0,
6717 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6718 .info = volume_alsa_mute_info,
6719 .get = volume_alsa_mute_get,
6720};
6721
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006722static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006723{
6724 tpacpi_volume_checkpoint_nvram();
6725}
6726
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006727static void volume_resume(void)
6728{
6729 volume_alsa_notify_change();
6730}
6731
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006732static void volume_shutdown(void)
6733{
6734 tpacpi_volume_checkpoint_nvram();
6735}
6736
6737static void volume_exit(void)
6738{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006739 if (alsa_card) {
6740 snd_card_free(alsa_card);
6741 alsa_card = NULL;
6742 }
6743
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006744 tpacpi_volume_checkpoint_nvram();
6745}
6746
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006747static int __init volume_create_alsa_mixer(void)
6748{
6749 struct snd_card *card;
6750 struct tpacpi_alsa_data *data;
6751 struct snd_kcontrol *ctl_vol;
6752 struct snd_kcontrol *ctl_mute;
6753 int rc;
6754
6755 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6756 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006757 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006758 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006759 return 1;
6760 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006761
6762 BUG_ON(!card->private_data);
6763 data = card->private_data;
6764 data->card = card;
6765
6766 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6767 sizeof(card->driver));
6768 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6769 sizeof(card->shortname));
6770 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6771 (thinkpad_id.ec_version_str) ?
6772 thinkpad_id.ec_version_str : "(unknown)");
6773 snprintf(card->longname, sizeof(card->longname),
6774 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6775 (thinkpad_id.ec_version_str) ?
6776 thinkpad_id.ec_version_str : "unknown");
6777
6778 if (volume_control_allowed) {
6779 volume_alsa_control_vol.put = volume_alsa_vol_put;
6780 volume_alsa_control_vol.access =
6781 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6782
6783 volume_alsa_control_mute.put = volume_alsa_mute_put;
6784 volume_alsa_control_mute.access =
6785 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6786 }
6787
6788 if (!tp_features.mixer_no_level_control) {
6789 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6790 rc = snd_ctl_add(card, ctl_vol);
6791 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006792 pr_err("Failed to create ALSA volume control: %d\n",
6793 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006794 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006795 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006796 data->ctl_vol_id = &ctl_vol->id;
6797 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006798
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006799 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6800 rc = snd_ctl_add(card, ctl_mute);
6801 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006802 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006803 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006804 }
6805 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006806
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006807 snd_card_set_dev(card, &tpacpi_pdev->dev);
6808 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006809 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006810 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006811 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006812 }
6813
6814 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006815 return 0;
6816
6817err_exit:
6818 snd_card_free(card);
6819 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006820}
6821
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006822#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6823#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6824
6825static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6826 /* Whitelist volume level on all IBM by default */
6827 { .vendor = PCI_VENDOR_ID_IBM,
6828 .bios = TPACPI_MATCH_ANY,
6829 .ec = TPACPI_MATCH_ANY,
6830 .quirks = TPACPI_VOL_Q_LEVEL },
6831
6832 /* Lenovo models with volume control (needs confirmation) */
6833 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6834 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6835 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6836 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6837 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6838 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6839 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6840
6841 /* Whitelist mute-only on all Lenovo by default */
6842 { .vendor = PCI_VENDOR_ID_LENOVO,
6843 .bios = TPACPI_MATCH_ANY,
6844 .ec = TPACPI_MATCH_ANY,
6845 .quirks = TPACPI_VOL_Q_MUTEONLY }
6846};
6847
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006848static int __init volume_init(struct ibm_init_struct *iibm)
6849{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006850 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006851 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006852
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006853 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6854
6855 mutex_init(&volume_mutex);
6856
6857 /*
6858 * Check for module parameter bogosity, note that we
6859 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6860 * able to detect "unspecified"
6861 */
6862 if (volume_mode > TPACPI_VOL_MODE_MAX)
6863 return -EINVAL;
6864
6865 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006866 pr_err("UCMS step volume mode not implemented, "
6867 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006868 return 1;
6869 }
6870
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006871 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6872 return -EINVAL;
6873
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006874 /*
6875 * The ALSA mixer is our primary interface.
6876 * When disabled, don't install the subdriver at all
6877 */
6878 if (!alsa_enable) {
6879 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6880 "ALSA mixer disabled by parameter, "
6881 "not loading volume subdriver...\n");
6882 return 1;
6883 }
6884
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006885 quirks = tpacpi_check_quirks(volume_quirk_table,
6886 ARRAY_SIZE(volume_quirk_table));
6887
6888 switch (volume_capabilities) {
6889 case TPACPI_VOL_CAP_AUTO:
6890 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6891 tp_features.mixer_no_level_control = 1;
6892 else if (quirks & TPACPI_VOL_Q_LEVEL)
6893 tp_features.mixer_no_level_control = 0;
6894 else
6895 return 1; /* no mixer */
6896 break;
6897 case TPACPI_VOL_CAP_VOLMUTE:
6898 tp_features.mixer_no_level_control = 0;
6899 break;
6900 case TPACPI_VOL_CAP_MUTEONLY:
6901 tp_features.mixer_no_level_control = 1;
6902 break;
6903 default:
6904 return 1;
6905 }
6906
6907 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6908 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6909 "using user-supplied volume_capabilities=%d\n",
6910 volume_capabilities);
6911
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006912 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6913 volume_mode == TPACPI_VOL_MODE_MAX) {
6914 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6915
6916 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6917 "driver auto-selected volume_mode=%d\n",
6918 volume_mode);
6919 } else {
6920 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6921 "using user-supplied volume_mode=%d\n",
6922 volume_mode);
6923 }
6924
6925 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006926 "mute is supported, volume control is %s\n",
6927 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006928
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006929 rc = volume_create_alsa_mixer();
6930 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006931 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006932 return rc;
6933 }
6934
Joe Perches0978e012011-04-04 10:06:25 -07006935 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006936 (volume_control_allowed) ?
6937 "override (read/write)" :
6938 "monitor (read only)");
6939
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006940 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6941 "registering volume hotkeys as change notification\n");
6942 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6943 | TP_ACPI_HKEY_VOLUP_MASK
6944 | TP_ACPI_HKEY_VOLDWN_MASK
6945 | TP_ACPI_HKEY_MUTE_MASK);
6946
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006947 return 0;
6948}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006949
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006950static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006951{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006952 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006953
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006954 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006955 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006956 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006957 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006958 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006959 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006960 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006961 status & TP_EC_AUDIO_LVL_MSK);
6962
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006963 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006964 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006965
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006966 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006967 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006968 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006969 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006970 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006971 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006972 "commands:\tlevel <level>"
6973 " (<level> is 0-%d)\n",
6974 TP_EC_VOLUME_MAX);
6975 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006976 }
6977 }
6978
6979 return 0;
6980}
6981
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006982static int volume_write(char *buf)
6983{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006984 u8 s;
6985 u8 new_level, new_mute;
6986 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006987 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006988 int rc;
6989
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006990 /*
6991 * We do allow volume control at driver startup, so that the
6992 * user can set initial state through the volume=... parameter hack.
6993 */
6994 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6995 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6996 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07006997 pr_notice("Console audio control in monitor mode, "
6998 "changes are not allowed\n");
6999 pr_notice("Use the volume_control=1 module parameter "
7000 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007001 }
7002 return -EPERM;
7003 }
7004
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007005 rc = volume_get_status(&s);
7006 if (rc < 0)
7007 return rc;
7008
7009 new_level = s & TP_EC_AUDIO_LVL_MSK;
7010 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007011
7012 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007013 if (!tp_features.mixer_no_level_control) {
7014 if (strlencmp(cmd, "up") == 0) {
7015 if (new_mute)
7016 new_mute = 0;
7017 else if (new_level < TP_EC_VOLUME_MAX)
7018 new_level++;
7019 continue;
7020 } else if (strlencmp(cmd, "down") == 0) {
7021 if (new_mute)
7022 new_mute = 0;
7023 else if (new_level > 0)
7024 new_level--;
7025 continue;
7026 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7027 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7028 new_level = l;
7029 continue;
7030 }
7031 }
7032 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007033 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007034 else if (strlencmp(cmd, "unmute") == 0)
7035 new_mute = 0;
7036 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007037 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007038 }
7039
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007040 if (tp_features.mixer_no_level_control) {
7041 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7042 new_mute ? "" : "un");
7043 rc = volume_set_mute(!!new_mute);
7044 } else {
7045 tpacpi_disclose_usertask("procfs volume",
7046 "%smute and set level to %d\n",
7047 new_mute ? "" : "un", new_level);
7048 rc = volume_set_status(new_mute | new_level);
7049 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007050 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007051
7052 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007053}
7054
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007055static struct ibm_struct volume_driver_data = {
7056 .name = "volume",
7057 .read = volume_read,
7058 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007059 .exit = volume_exit,
7060 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007061 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007062 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007063};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007064
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007065#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7066
7067#define alsa_card NULL
7068
7069static void inline volume_alsa_notify_change(void)
7070{
7071}
7072
7073static int __init volume_init(struct ibm_init_struct *iibm)
7074{
Joe Perches0978e012011-04-04 10:06:25 -07007075 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007076
7077 return 1;
7078}
7079
7080static struct ibm_struct volume_driver_data = {
7081 .name = "volume",
7082};
7083
7084#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7085
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007086/*************************************************************************
7087 * Fan subdriver
7088 */
7089
7090/*
7091 * FAN ACCESS MODES
7092 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007093 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007094 * ACPI GFAN method: returns fan level
7095 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007096 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007097 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007098 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007099 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007100 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7101 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007102 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7103 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007104 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007105 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007106 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7107 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007108 *
7109 * Fan speed changes of any sort (including those caused by the
7110 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007111 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007112 * limited.
7113 *
7114 * Reading is not available if GFAN exists.
7115 * Writing is not available if SFAN exists.
7116 *
7117 * Bits
7118 * 7 automatic mode engaged;
7119 * (default operation mode of the ThinkPad)
7120 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007121 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007122 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007123 * the tachometer while the fan controller ramps up
7124 * the speed (which can take up to a few *minutes*).
7125 * Speeds up fan to 100% duty-cycle, which is far above
7126 * the standard RPM levels. It is not impossible that
7127 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007128 * 5-3 unused in some models. Extra bits for fan level
7129 * in others, but still useless as all values above
7130 * 7 map to the same speed as level 7 in these models.
7131 * 2-0 fan level (0..7 usually)
7132 * 0x00 = stop
7133 * 0x07 = max (set when temperatures critical)
7134 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007135 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007136 *
7137 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007138 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007139 * does so, its initial value is meaningless (0x07).
7140 *
7141 * For firmware bugs, refer to:
7142 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7143 *
7144 * ----
7145 *
7146 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7147 * Main fan tachometer reading (in RPM)
7148 *
7149 * This register is present on all ThinkPads with a new-style EC, and
7150 * it is known not to be present on the A21m/e, and T22, as there is
7151 * something else in offset 0x84 according to the ACPI DSDT. Other
7152 * ThinkPads from this same time period (and earlier) probably lack the
7153 * tachometer as well.
7154 *
Nick Andrew877d0312009-01-26 11:06:57 +01007155 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007156 * was never fixed by IBM to report the EC firmware version string
7157 * probably support the tachometer (like the early X models), so
7158 * detecting it is quite hard. We need more data to know for sure.
7159 *
7160 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7161 * might result.
7162 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007163 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7164 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007165 *
7166 * For firmware bugs, refer to:
7167 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7168 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007169 * ----
7170 *
7171 * ThinkPad EC register 0x31 bit 0 (only on select models)
7172 *
7173 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7174 * show the speed of the main fan. When bit 0 of EC register 0x31
7175 * is one, the tachometer registers show the speed of the auxiliary
7176 * fan.
7177 *
7178 * Fan control seems to affect both fans, regardless of the state
7179 * of this bit.
7180 *
7181 * So far, only the firmware for the X60/X61 non-tablet versions
7182 * seem to support this (firmware TP-7M).
7183 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007184 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007185 * ThinkPad X31, X40, X41. Not available in the X60.
7186 *
7187 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7188 * high speed. ACPI DSDT seems to map these three speeds to levels
7189 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7190 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7191 *
7192 * The speeds are stored on handles
7193 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7194 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007195 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007196 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7197 *
7198 * ACPI DSDT switches which set is in use depending on various
7199 * factors.
7200 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007201 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007202 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7203 * but the ACPI tables just mention level 7.
7204 */
7205
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007206enum { /* Fan control constants */
7207 fan_status_offset = 0x2f, /* EC register 0x2f */
7208 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7209 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007210 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7211 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007212
7213 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7214 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7215
7216 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7217};
7218
7219enum fan_status_access_mode {
7220 TPACPI_FAN_NONE = 0, /* No fan status or control */
7221 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7222 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7223};
7224
7225enum fan_control_access_mode {
7226 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7227 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7228 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7229 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7230};
7231
7232enum fan_control_commands {
7233 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7234 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7235 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7236 * and also watchdog cmd */
7237};
7238
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307239static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007240
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007241static enum fan_status_access_mode fan_status_access_mode;
7242static enum fan_control_access_mode fan_control_access_mode;
7243static enum fan_control_commands fan_control_commands;
7244
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007245static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007246static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007247static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007248static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007249
7250static struct mutex fan_mutex;
7251
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007252static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007253static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007254
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007255TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7256TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007257 "\\FSPD", /* 600e/x, 770e, 770x */
7258 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007259TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007260 "JFNS", /* 770x-JL */
7261 ); /* all others */
7262
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007263/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007264 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7265 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7266 * be in auto mode (0x80).
7267 *
7268 * This is corrected by any write to HFSP either by the driver, or
7269 * by the firmware.
7270 *
7271 * We assume 0x07 really means auto mode while this quirk is active,
7272 * as this is far more likely than the ThinkPad being in level 7,
7273 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007274 *
7275 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7276 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007277 */
7278
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007279static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007280{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007281 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007282 pr_notice("fan_init: initial fan status is unknown, "
7283 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007284 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007285 }
7286}
7287
7288static void fan_quirk1_handle(u8 *fan_status)
7289{
7290 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7291 if (*fan_status != fan_control_initial_status) {
7292 /* something changed the HFSP regisnter since
7293 * driver init time, so it is not undefined
7294 * anymore */
7295 tp_features.fan_ctrl_status_undef = 0;
7296 } else {
7297 /* Return most likely status. In fact, it
7298 * might be the only possible status */
7299 *fan_status = TP_EC_FAN_AUTO;
7300 }
7301 }
7302}
7303
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007304/* Select main fan on X60/X61, NOOP on others */
7305static bool fan_select_fan1(void)
7306{
7307 if (tp_features.second_fan) {
7308 u8 val;
7309
7310 if (ec_read(fan_select_offset, &val) < 0)
7311 return false;
7312 val &= 0xFEU;
7313 if (ec_write(fan_select_offset, val) < 0)
7314 return false;
7315 }
7316 return true;
7317}
7318
7319/* Select secondary fan on X60/X61 */
7320static bool fan_select_fan2(void)
7321{
7322 u8 val;
7323
7324 if (!tp_features.second_fan)
7325 return false;
7326
7327 if (ec_read(fan_select_offset, &val) < 0)
7328 return false;
7329 val |= 0x01U;
7330 if (ec_write(fan_select_offset, val) < 0)
7331 return false;
7332
7333 return true;
7334}
7335
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007336/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007337 * Call with fan_mutex held
7338 */
7339static void fan_update_desired_level(u8 status)
7340{
7341 if ((status &
7342 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7343 if (status > 7)
7344 fan_control_desired_level = 7;
7345 else
7346 fan_control_desired_level = status;
7347 }
7348}
7349
7350static int fan_get_status(u8 *status)
7351{
7352 u8 s;
7353
7354 /* TODO:
7355 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7356
7357 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007358 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007359 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007360 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007361
Dan Carpentereceeb432012-09-01 12:54:07 -07007362 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007363 return -EIO;
7364
7365 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007366 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007367
7368 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007369 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007370 case TPACPI_FAN_RD_TPEC:
7371 /* all except 570, 600e/x, 770e, 770x */
7372 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7373 return -EIO;
7374
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007375 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007376 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007377 fan_quirk1_handle(status);
7378 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007379
7380 break;
7381
7382 default:
7383 return -ENXIO;
7384 }
7385
7386 return 0;
7387}
7388
7389static int fan_get_status_safe(u8 *status)
7390{
7391 int rc;
7392 u8 s;
7393
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007394 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007395 return -ERESTARTSYS;
7396 rc = fan_get_status(&s);
7397 if (!rc)
7398 fan_update_desired_level(s);
7399 mutex_unlock(&fan_mutex);
7400
7401 if (status)
7402 *status = s;
7403
7404 return rc;
7405}
7406
7407static int fan_get_speed(unsigned int *speed)
7408{
7409 u8 hi, lo;
7410
7411 switch (fan_status_access_mode) {
7412 case TPACPI_FAN_RD_TPEC:
7413 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007414 if (unlikely(!fan_select_fan1()))
7415 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007416 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7417 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7418 return -EIO;
7419
7420 if (likely(speed))
7421 *speed = (hi << 8) | lo;
7422
7423 break;
7424
7425 default:
7426 return -ENXIO;
7427 }
7428
7429 return 0;
7430}
7431
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007432static int fan2_get_speed(unsigned int *speed)
7433{
7434 u8 hi, lo;
7435 bool rc;
7436
7437 switch (fan_status_access_mode) {
7438 case TPACPI_FAN_RD_TPEC:
7439 /* all except 570, 600e/x, 770e, 770x */
7440 if (unlikely(!fan_select_fan2()))
7441 return -EIO;
7442 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7443 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7444 fan_select_fan1(); /* play it safe */
7445 if (rc)
7446 return -EIO;
7447
7448 if (likely(speed))
7449 *speed = (hi << 8) | lo;
7450
7451 break;
7452
7453 default:
7454 return -ENXIO;
7455 }
7456
7457 return 0;
7458}
7459
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007460static int fan_set_level(int level)
7461{
7462 if (!fan_control_allowed)
7463 return -EPERM;
7464
7465 switch (fan_control_access_mode) {
7466 case TPACPI_FAN_WR_ACPI_SFAN:
7467 if (level >= 0 && level <= 7) {
7468 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7469 return -EIO;
7470 } else
7471 return -EINVAL;
7472 break;
7473
7474 case TPACPI_FAN_WR_ACPI_FANS:
7475 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007476 if (!(level & TP_EC_FAN_AUTO) &&
7477 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007478 ((level < 0) || (level > 7)))
7479 return -EINVAL;
7480
7481 /* safety net should the EC not support AUTO
7482 * or FULLSPEED mode bits and just ignore them */
7483 if (level & TP_EC_FAN_FULLSPEED)
7484 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007485 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007486 level |= 4; /* safety min speed 4 */
7487
7488 if (!acpi_ec_write(fan_status_offset, level))
7489 return -EIO;
7490 else
7491 tp_features.fan_ctrl_status_undef = 0;
7492 break;
7493
7494 default:
7495 return -ENXIO;
7496 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007497
7498 vdbg_printk(TPACPI_DBG_FAN,
7499 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007500 return 0;
7501}
7502
7503static int fan_set_level_safe(int level)
7504{
7505 int rc;
7506
7507 if (!fan_control_allowed)
7508 return -EPERM;
7509
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007510 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007511 return -ERESTARTSYS;
7512
7513 if (level == TPACPI_FAN_LAST_LEVEL)
7514 level = fan_control_desired_level;
7515
7516 rc = fan_set_level(level);
7517 if (!rc)
7518 fan_update_desired_level(level);
7519
7520 mutex_unlock(&fan_mutex);
7521 return rc;
7522}
7523
7524static int fan_set_enable(void)
7525{
7526 u8 s;
7527 int rc;
7528
7529 if (!fan_control_allowed)
7530 return -EPERM;
7531
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007532 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007533 return -ERESTARTSYS;
7534
7535 switch (fan_control_access_mode) {
7536 case TPACPI_FAN_WR_ACPI_FANS:
7537 case TPACPI_FAN_WR_TPEC:
7538 rc = fan_get_status(&s);
7539 if (rc < 0)
7540 break;
7541
7542 /* Don't go out of emergency fan mode */
7543 if (s != 7) {
7544 s &= 0x07;
7545 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7546 }
7547
7548 if (!acpi_ec_write(fan_status_offset, s))
7549 rc = -EIO;
7550 else {
7551 tp_features.fan_ctrl_status_undef = 0;
7552 rc = 0;
7553 }
7554 break;
7555
7556 case TPACPI_FAN_WR_ACPI_SFAN:
7557 rc = fan_get_status(&s);
7558 if (rc < 0)
7559 break;
7560
7561 s &= 0x07;
7562
7563 /* Set fan to at least level 4 */
7564 s |= 4;
7565
7566 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007567 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007568 else
7569 rc = 0;
7570 break;
7571
7572 default:
7573 rc = -ENXIO;
7574 }
7575
7576 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007577
7578 if (!rc)
7579 vdbg_printk(TPACPI_DBG_FAN,
7580 "fan control: set fan control register to 0x%02x\n",
7581 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007582 return rc;
7583}
7584
7585static int fan_set_disable(void)
7586{
7587 int rc;
7588
7589 if (!fan_control_allowed)
7590 return -EPERM;
7591
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007592 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007593 return -ERESTARTSYS;
7594
7595 rc = 0;
7596 switch (fan_control_access_mode) {
7597 case TPACPI_FAN_WR_ACPI_FANS:
7598 case TPACPI_FAN_WR_TPEC:
7599 if (!acpi_ec_write(fan_status_offset, 0x00))
7600 rc = -EIO;
7601 else {
7602 fan_control_desired_level = 0;
7603 tp_features.fan_ctrl_status_undef = 0;
7604 }
7605 break;
7606
7607 case TPACPI_FAN_WR_ACPI_SFAN:
7608 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7609 rc = -EIO;
7610 else
7611 fan_control_desired_level = 0;
7612 break;
7613
7614 default:
7615 rc = -ENXIO;
7616 }
7617
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007618 if (!rc)
7619 vdbg_printk(TPACPI_DBG_FAN,
7620 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007621
7622 mutex_unlock(&fan_mutex);
7623 return rc;
7624}
7625
7626static int fan_set_speed(int speed)
7627{
7628 int rc;
7629
7630 if (!fan_control_allowed)
7631 return -EPERM;
7632
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007633 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007634 return -ERESTARTSYS;
7635
7636 rc = 0;
7637 switch (fan_control_access_mode) {
7638 case TPACPI_FAN_WR_ACPI_FANS:
7639 if (speed >= 0 && speed <= 65535) {
7640 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7641 speed, speed, speed))
7642 rc = -EIO;
7643 } else
7644 rc = -EINVAL;
7645 break;
7646
7647 default:
7648 rc = -ENXIO;
7649 }
7650
7651 mutex_unlock(&fan_mutex);
7652 return rc;
7653}
7654
7655static void fan_watchdog_reset(void)
7656{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007657 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7658 return;
7659
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007660 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07007661 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7662 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7663 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7664 else
7665 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007666}
7667
7668static void fan_watchdog_fire(struct work_struct *ignored)
7669{
7670 int rc;
7671
7672 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7673 return;
7674
Joe Perches0978e012011-04-04 10:06:25 -07007675 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007676 rc = fan_set_enable();
7677 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007678 pr_err("fan watchdog: error %d while enabling fan, "
7679 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007680 /* reschedule for later */
7681 fan_watchdog_reset();
7682 }
7683}
7684
7685/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007686 * SYSFS fan layout: hwmon compatible (device)
7687 *
7688 * pwm*_enable:
7689 * 0: "disengaged" mode
7690 * 1: manual mode
7691 * 2: native EC "auto" mode (recommended, hardware default)
7692 *
7693 * pwm*: set speed in manual mode, ignored otherwise.
7694 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7695 * interpolation.
7696 *
7697 * fan*_input: tachometer reading, RPM
7698 *
7699 *
7700 * SYSFS fan layout: extensions
7701 *
7702 * fan_watchdog (driver):
7703 * fan watchdog interval in seconds, 0 disables (default), max 120
7704 */
7705
7706/* sysfs fan pwm1_enable ----------------------------------------------- */
7707static ssize_t fan_pwm1_enable_show(struct device *dev,
7708 struct device_attribute *attr,
7709 char *buf)
7710{
7711 int res, mode;
7712 u8 status;
7713
7714 res = fan_get_status_safe(&status);
7715 if (res)
7716 return res;
7717
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007718 if (status & TP_EC_FAN_FULLSPEED) {
7719 mode = 0;
7720 } else if (status & TP_EC_FAN_AUTO) {
7721 mode = 2;
7722 } else
7723 mode = 1;
7724
7725 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7726}
7727
7728static ssize_t fan_pwm1_enable_store(struct device *dev,
7729 struct device_attribute *attr,
7730 const char *buf, size_t count)
7731{
7732 unsigned long t;
7733 int res, level;
7734
7735 if (parse_strtoul(buf, 2, &t))
7736 return -EINVAL;
7737
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007738 tpacpi_disclose_usertask("hwmon pwm1_enable",
7739 "set fan mode to %lu\n", t);
7740
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007741 switch (t) {
7742 case 0:
7743 level = TP_EC_FAN_FULLSPEED;
7744 break;
7745 case 1:
7746 level = TPACPI_FAN_LAST_LEVEL;
7747 break;
7748 case 2:
7749 level = TP_EC_FAN_AUTO;
7750 break;
7751 case 3:
7752 /* reserved for software-controlled auto mode */
7753 return -ENOSYS;
7754 default:
7755 return -EINVAL;
7756 }
7757
7758 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007759 if (res == -ENXIO)
7760 return -EINVAL;
7761 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007762 return res;
7763
7764 fan_watchdog_reset();
7765
7766 return count;
7767}
7768
7769static struct device_attribute dev_attr_fan_pwm1_enable =
7770 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7771 fan_pwm1_enable_show, fan_pwm1_enable_store);
7772
7773/* sysfs fan pwm1 ------------------------------------------------------ */
7774static ssize_t fan_pwm1_show(struct device *dev,
7775 struct device_attribute *attr,
7776 char *buf)
7777{
7778 int res;
7779 u8 status;
7780
7781 res = fan_get_status_safe(&status);
7782 if (res)
7783 return res;
7784
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007785 if ((status &
7786 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7787 status = fan_control_desired_level;
7788
7789 if (status > 7)
7790 status = 7;
7791
7792 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7793}
7794
7795static ssize_t fan_pwm1_store(struct device *dev,
7796 struct device_attribute *attr,
7797 const char *buf, size_t count)
7798{
7799 unsigned long s;
7800 int rc;
7801 u8 status, newlevel;
7802
7803 if (parse_strtoul(buf, 255, &s))
7804 return -EINVAL;
7805
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007806 tpacpi_disclose_usertask("hwmon pwm1",
7807 "set fan speed to %lu\n", s);
7808
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007809 /* scale down from 0-255 to 0-7 */
7810 newlevel = (s >> 5) & 0x07;
7811
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007812 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007813 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007814
7815 rc = fan_get_status(&status);
7816 if (!rc && (status &
7817 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7818 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007819 if (rc == -ENXIO)
7820 rc = -EINVAL;
7821 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007822 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007823 fan_watchdog_reset();
7824 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007825 }
7826
7827 mutex_unlock(&fan_mutex);
7828 return (rc)? rc : count;
7829}
7830
7831static struct device_attribute dev_attr_fan_pwm1 =
7832 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7833 fan_pwm1_show, fan_pwm1_store);
7834
7835/* sysfs fan fan1_input ------------------------------------------------ */
7836static ssize_t fan_fan1_input_show(struct device *dev,
7837 struct device_attribute *attr,
7838 char *buf)
7839{
7840 int res;
7841 unsigned int speed;
7842
7843 res = fan_get_speed(&speed);
7844 if (res < 0)
7845 return res;
7846
7847 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7848}
7849
7850static struct device_attribute dev_attr_fan_fan1_input =
7851 __ATTR(fan1_input, S_IRUGO,
7852 fan_fan1_input_show, NULL);
7853
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007854/* sysfs fan fan2_input ------------------------------------------------ */
7855static ssize_t fan_fan2_input_show(struct device *dev,
7856 struct device_attribute *attr,
7857 char *buf)
7858{
7859 int res;
7860 unsigned int speed;
7861
7862 res = fan2_get_speed(&speed);
7863 if (res < 0)
7864 return res;
7865
7866 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7867}
7868
7869static struct device_attribute dev_attr_fan_fan2_input =
7870 __ATTR(fan2_input, S_IRUGO,
7871 fan_fan2_input_show, NULL);
7872
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007873/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007874static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7875 char *buf)
7876{
7877 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7878}
7879
7880static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7881 const char *buf, size_t count)
7882{
7883 unsigned long t;
7884
7885 if (parse_strtoul(buf, 120, &t))
7886 return -EINVAL;
7887
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007888 if (!fan_control_allowed)
7889 return -EPERM;
7890
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007891 fan_watchdog_maxinterval = t;
7892 fan_watchdog_reset();
7893
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007894 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7895
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007896 return count;
7897}
7898
7899static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7900 fan_fan_watchdog_show, fan_fan_watchdog_store);
7901
7902/* --------------------------------------------------------------------- */
7903static struct attribute *fan_attributes[] = {
7904 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7905 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007906 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007907 NULL
7908};
7909
7910static const struct attribute_group fan_attr_group = {
7911 .attrs = fan_attributes,
7912};
7913
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007914#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7915#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007916
7917#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7918 { .vendor = PCI_VENDOR_ID_IBM, \
7919 .bios = TPACPI_MATCH_ANY, \
7920 .ec = TPID(__id1, __id2), \
7921 .quirks = __quirks }
7922
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007923#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7924 { .vendor = PCI_VENDOR_ID_LENOVO, \
7925 .bios = TPACPI_MATCH_ANY, \
7926 .ec = TPID(__id1, __id2), \
7927 .quirks = __quirks }
7928
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007929static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7930 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7931 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7932 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7933 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007934 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007935};
7936
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007937#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007938#undef TPACPI_FAN_QI
7939
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007940static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007941{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007942 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007943 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007944
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007945 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7946 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007947
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007948 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007949 fan_status_access_mode = TPACPI_FAN_NONE;
7950 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007951 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007952 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007953 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007954 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007955 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007956
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007957 if (tpacpi_is_ibm()) {
7958 TPACPI_ACPIHANDLE_INIT(fans);
7959 TPACPI_ACPIHANDLE_INIT(gfan);
7960 TPACPI_ACPIHANDLE_INIT(sfan);
7961 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007962
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007963 quirks = tpacpi_check_quirks(fan_quirk_table,
7964 ARRAY_SIZE(fan_quirk_table));
7965
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007966 if (gfan_handle) {
7967 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007968 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007969 } else {
7970 /* all other ThinkPads: note that even old-style
7971 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007972 if (likely(acpi_ec_read(fan_status_offset,
7973 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007974 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007975 if (quirks & TPACPI_FAN_Q1)
7976 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007977 if (quirks & TPACPI_FAN_2FAN) {
7978 tp_features.second_fan = 1;
7979 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7980 "secondary fan support enabled\n");
7981 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007982 } else {
Joe Perches0978e012011-04-04 10:06:25 -07007983 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007984 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007985 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007986 }
7987 }
7988
7989 if (sfan_handle) {
7990 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007991 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02007992 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007993 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007994 } else {
7995 if (!gfan_handle) {
7996 /* gfan without sfan means no fan control */
7997 /* all other models implement TP EC 0x2f control */
7998
7999 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008000 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008001 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008002 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008003 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008004 TPACPI_FAN_CMD_SPEED |
8005 TPACPI_FAN_CMD_LEVEL |
8006 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008007 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008008 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008009 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008010 TPACPI_FAN_CMD_LEVEL |
8011 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008012 }
8013 }
8014 }
8015
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008016 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8017 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008018 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8019 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8020 fan_status_access_mode, fan_control_access_mode);
8021
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008022 /* fan control master switch */
8023 if (!fan_control_allowed) {
8024 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8025 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008026 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008027 "fan control features disabled by parameter\n");
8028 }
8029
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008030 /* update fan_control_desired_level */
8031 if (fan_status_access_mode != TPACPI_FAN_NONE)
8032 fan_get_status_safe(NULL);
8033
8034 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8035 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008036 if (tp_features.second_fan) {
8037 /* attach second fan tachometer */
8038 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8039 &dev_attr_fan_fan2_input.attr;
8040 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008041 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008042 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008043 if (rc < 0)
8044 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008045
8046 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8047 &driver_attr_fan_watchdog);
8048 if (rc < 0) {
8049 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8050 &fan_attr_group);
8051 return rc;
8052 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008053 return 0;
8054 } else
8055 return 1;
8056}
8057
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008058static void fan_exit(void)
8059{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008060 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008061 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008062
8063 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008064 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008065 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8066 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008067
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008068 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008069 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008070}
8071
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008072static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008073{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008074 int rc;
8075
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008076 if (!fan_control_allowed)
8077 return;
8078
8079 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008080 fan_control_resume_level = 0;
8081 rc = fan_get_status_safe(&fan_control_resume_level);
8082 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008083 pr_notice("failed to read fan level for later "
8084 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008085
8086 /* if it is undefined, don't attempt to restore it.
8087 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008088 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008089 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008090}
8091
8092static void fan_resume(void)
8093{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008094 u8 current_level = 7;
8095 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008096 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008097
8098 /* DSDT *always* updates status on resume */
8099 tp_features.fan_ctrl_status_undef = 0;
8100
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008101 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008102 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008103 (fan_get_status_safe(&current_level) < 0))
8104 return;
8105
8106 switch (fan_control_access_mode) {
8107 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008108 /* never decrease fan level */
8109 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008110 break;
8111 case TPACPI_FAN_WR_ACPI_FANS:
8112 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008113 /* never decrease fan level, scale is:
8114 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8115 *
8116 * We expect the firmware to set either 7 or AUTO, but we
8117 * handle FULLSPEED out of paranoia.
8118 *
8119 * So, we can safely only restore FULLSPEED or 7, anything
8120 * else could slow the fan. Restoring AUTO is useless, at
8121 * best that's exactly what the DSDT already set (it is the
8122 * slower it uses).
8123 *
8124 * Always keep in mind that the DSDT *will* have set the
8125 * fans to what the vendor supposes is the best level. We
8126 * muck with it only to speed the fan up.
8127 */
8128 if (fan_control_resume_level != 7 &&
8129 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8130 return;
8131 else
8132 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8133 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008134 break;
8135 default:
8136 return;
8137 }
8138 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008139 pr_notice("restoring fan level to 0x%02x\n",
8140 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008141 rc = fan_set_level_safe(fan_control_resume_level);
8142 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008143 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008144 }
8145}
8146
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008147static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008148{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008149 int rc;
8150 u8 status;
8151 unsigned int speed = 0;
8152
8153 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008154 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008155 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008156 rc = fan_get_status_safe(&status);
8157 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008158 return rc;
8159
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008160 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008161 "level:\t\t%d\n",
8162 (status != 0) ? "enabled" : "disabled", status);
8163 break;
8164
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008165 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008167 rc = fan_get_status_safe(&status);
8168 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008169 return rc;
8170
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008171 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008172 (status != 0) ? "enabled" : "disabled");
8173
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008174 rc = fan_get_speed(&speed);
8175 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008176 return rc;
8177
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008178 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008179
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008180 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008181 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008182 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008183 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008184 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008185 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008186 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008187 break;
8188
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008189 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008190 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008191 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008192 }
8193
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008194 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008195 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008196
8197 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008198 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008199 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008200 break;
8201
8202 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008203 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008204 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008205 break;
8206 }
8207 }
8208
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008209 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008210 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008211 "commands:\twatchdog <timeout> (<timeout> "
8212 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008213
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008214 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008215 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008216 " (<speed> is 0-65535)\n");
8217
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008218 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008219}
8220
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008221static int fan_write_cmd_level(const char *cmd, int *rc)
8222{
8223 int level;
8224
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008225 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008226 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008227 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008228 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008229 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008230 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008231 return 0;
8232
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008233 *rc = fan_set_level_safe(level);
8234 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008235 pr_err("level command accepted for unsupported access mode %d\n",
8236 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008237 else if (!*rc)
8238 tpacpi_disclose_usertask("procfs fan",
8239 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008240
8241 return 1;
8242}
8243
8244static int fan_write_cmd_enable(const char *cmd, int *rc)
8245{
8246 if (strlencmp(cmd, "enable") != 0)
8247 return 0;
8248
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008249 *rc = fan_set_enable();
8250 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008251 pr_err("enable command accepted for unsupported access mode %d\n",
8252 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008253 else if (!*rc)
8254 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008255
8256 return 1;
8257}
8258
8259static int fan_write_cmd_disable(const char *cmd, int *rc)
8260{
8261 if (strlencmp(cmd, "disable") != 0)
8262 return 0;
8263
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008264 *rc = fan_set_disable();
8265 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008266 pr_err("disable command accepted for unsupported access mode %d\n",
8267 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008268 else if (!*rc)
8269 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008270
8271 return 1;
8272}
8273
8274static int fan_write_cmd_speed(const char *cmd, int *rc)
8275{
8276 int speed;
8277
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008278 /* TODO:
8279 * Support speed <low> <medium> <high> ? */
8280
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008281 if (sscanf(cmd, "speed %d", &speed) != 1)
8282 return 0;
8283
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008284 *rc = fan_set_speed(speed);
8285 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008286 pr_err("speed command accepted for unsupported access mode %d\n",
8287 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008288 else if (!*rc)
8289 tpacpi_disclose_usertask("procfs fan",
8290 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008291
8292 return 1;
8293}
8294
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008295static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8296{
8297 int interval;
8298
8299 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8300 return 0;
8301
8302 if (interval < 0 || interval > 120)
8303 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008304 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008305 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008306 tpacpi_disclose_usertask("procfs fan",
8307 "set watchdog timer to %d\n",
8308 interval);
8309 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008310
8311 return 1;
8312}
8313
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008314static int fan_write(char *buf)
8315{
8316 char *cmd;
8317 int rc = 0;
8318
8319 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008320 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008321 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008322 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008323 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008324 fan_write_cmd_disable(cmd, &rc) ||
8325 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008326 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008327 fan_write_cmd_speed(cmd, &rc))
8328 )
8329 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008330 else if (!rc)
8331 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008332 }
8333
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008334 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008335}
8336
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008337static struct ibm_struct fan_driver_data = {
8338 .name = "fan",
8339 .read = fan_read,
8340 .write = fan_write,
8341 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008342 .suspend = fan_suspend,
8343 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008344};
8345
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008346/****************************************************************************
8347 ****************************************************************************
8348 *
8349 * Infrastructure
8350 *
8351 ****************************************************************************
8352 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008354/*
8355 * HKEY event callout for other subdrivers go here
8356 * (yes, it is ugly, but it is quick, safe, and gets the job done
8357 */
8358static void tpacpi_driver_event(const unsigned int hkey_event)
8359{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008360 if (ibm_backlight_device) {
8361 switch (hkey_event) {
8362 case TP_HKEY_EV_BRGHT_UP:
8363 case TP_HKEY_EV_BRGHT_DOWN:
8364 tpacpi_brightness_notify_change();
8365 }
8366 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008367 if (alsa_card) {
8368 switch (hkey_event) {
8369 case TP_HKEY_EV_VOL_UP:
8370 case TP_HKEY_EV_VOL_DOWN:
8371 case TP_HKEY_EV_VOL_MUTE:
8372 volume_alsa_notify_change();
8373 }
8374 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008375}
8376
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008377static void hotkey_driver_event(const unsigned int scancode)
8378{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008379 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008380}
8381
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008382/* sysfs name ---------------------------------------------------------- */
8383static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8384 struct device_attribute *attr,
8385 char *buf)
8386{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008387 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008388}
8389
8390static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8391 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8392
8393/* --------------------------------------------------------------------- */
8394
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008395/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03008396static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008397
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008398/*
8399 * Module and infrastructure proble, init and exit handling
8400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308402static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008403
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008404#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008405static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008406{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008407 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008409 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008410}
8411#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8412
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008413static void ibm_exit(struct ibm_struct *ibm)
8414{
8415 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8416
8417 list_del_init(&ibm->all_drivers);
8418
8419 if (ibm->flags.acpi_notify_installed) {
8420 dbg_printk(TPACPI_DBG_EXIT,
8421 "%s: acpi_remove_notify_handler\n", ibm->name);
8422 BUG_ON(!ibm->acpi);
8423 acpi_remove_notify_handler(*ibm->acpi->handle,
8424 ibm->acpi->type,
8425 dispatch_acpi_notify);
8426 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008427 }
8428
8429 if (ibm->flags.proc_created) {
8430 dbg_printk(TPACPI_DBG_EXIT,
8431 "%s: remove_proc_entry\n", ibm->name);
8432 remove_proc_entry(ibm->name, proc_dir);
8433 ibm->flags.proc_created = 0;
8434 }
8435
8436 if (ibm->flags.acpi_driver_registered) {
8437 dbg_printk(TPACPI_DBG_EXIT,
8438 "%s: acpi_bus_unregister_driver\n", ibm->name);
8439 BUG_ON(!ibm->acpi);
8440 acpi_bus_unregister_driver(ibm->acpi->driver);
8441 kfree(ibm->acpi->driver);
8442 ibm->acpi->driver = NULL;
8443 ibm->flags.acpi_driver_registered = 0;
8444 }
8445
8446 if (ibm->flags.init_called && ibm->exit) {
8447 ibm->exit();
8448 ibm->flags.init_called = 0;
8449 }
8450
8451 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8452}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008453
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008454static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455{
8456 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008457 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458 struct proc_dir_entry *entry;
8459
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008460 BUG_ON(ibm == NULL);
8461
8462 INIT_LIST_HEAD(&ibm->all_drivers);
8463
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008464 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 return 0;
8466
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008467 dbg_printk(TPACPI_DBG_INIT,
8468 "probing for %s\n", ibm->name);
8469
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008470 if (iibm->init) {
8471 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008472 if (ret > 0)
8473 return 0; /* probe failed */
8474 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008476
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008477 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 }
8479
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008480 if (ibm->acpi) {
8481 if (ibm->acpi->hid) {
8482 ret = register_tpacpi_subdriver(ibm);
8483 if (ret)
8484 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008485 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008486
8487 if (ibm->acpi->notify) {
8488 ret = setup_acpi_notify(ibm);
8489 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008490 pr_notice("disabling subdriver %s\n",
8491 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008492 ret = 0;
8493 goto err_out;
8494 }
8495 if (ret < 0)
8496 goto err_out;
8497 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008498 }
8499
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008500 dbg_printk(TPACPI_DBG_INIT,
8501 "%s installed\n", ibm->name);
8502
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008503 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008504 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008505
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008506 if (!mode)
8507 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008508 if (ibm->write)
8509 mode |= S_IWUSR;
8510 entry = proc_create_data(ibm->name, mode, proc_dir,
8511 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008512 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008513 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008514 ret = -ENODEV;
8515 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008516 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008517 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008520 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8521
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008523
8524err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008525 dbg_printk(TPACPI_DBG_INIT,
8526 "%s: at error exit path with result %d\n",
8527 ibm->name, ret);
8528
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008529 ibm_exit(ibm);
8530 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531}
8532
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008533/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008535static bool __pure __init tpacpi_is_fw_digit(const char c)
8536{
8537 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8538}
8539
8540/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8541static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8542 const char t)
8543{
8544 return s && strlen(s) >= 8 &&
8545 tpacpi_is_fw_digit(s[0]) &&
8546 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008547 s[2] == t &&
8548 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008549 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008550 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008551}
8552
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008553/* returns 0 - probe ok, or < 0 - probe error.
8554 * Probe ok doesn't mean thinkpad found.
8555 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8556static int __must_check __init get_thinkpad_model_data(
8557 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008558{
Jeff Garzik18552562007-10-03 15:15:40 -04008559 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008560 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008561 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008562
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008563 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008564 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008565
8566 memset(tp, 0, sizeof(*tp));
8567
8568 if (dmi_name_in_vendors("IBM"))
8569 tp->vendor = PCI_VENDOR_ID_IBM;
8570 else if (dmi_name_in_vendors("LENOVO"))
8571 tp->vendor = PCI_VENDOR_ID_LENOVO;
8572 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008573 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008574
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008575 s = dmi_get_system_info(DMI_BIOS_VERSION);
8576 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8577 if (s && !tp->bios_version_str)
8578 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008579
8580 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008581 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8582 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008583 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008584
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008585 tp->bios_model = tp->bios_version_str[0]
8586 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008587 tp->bios_release = (tp->bios_version_str[4] << 8)
8588 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008589
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008590 /*
8591 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8592 * X32 or newer, all Z series; Some models must have an
8593 * up-to-date BIOS or they will not be detected.
8594 *
8595 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8596 */
8597 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008598 if (sscanf(dev->name,
8599 "IBM ThinkPad Embedded Controller -[%17c",
8600 ec_fw_string) == 1) {
8601 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8602 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008603
8604 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008605 if (!tp->ec_version_str)
8606 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008607
8608 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8609 tp->ec_model = ec_fw_string[0]
8610 | (ec_fw_string[1] << 8);
8611 tp->ec_release = (ec_fw_string[4] << 8)
8612 | ec_fw_string[5];
8613 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008614 pr_notice("ThinkPad firmware release %s "
8615 "doesn't match the known patterns\n",
8616 ec_fw_string);
8617 pr_notice("please report this to %s\n",
8618 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008619 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008620 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008621 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008622 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008623
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008624 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Manoj Iyer23b05312012-03-09 17:32:24 -06008625 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008626 tp->model_str = kstrdup(s, GFP_KERNEL);
8627 if (!tp->model_str)
8628 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05008629 } else {
8630 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8631 if (s && !(strnicmp(s, "Lenovo", 6))) {
8632 tp->model_str = kstrdup(s, GFP_KERNEL);
8633 if (!tp->model_str)
8634 return -ENOMEM;
8635 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008636 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008637
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008638 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8639 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8640 if (s && !tp->nummodel_str)
8641 return -ENOMEM;
8642
8643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644}
8645
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008646static int __init probe_for_thinkpad(void)
8647{
8648 int is_thinkpad;
8649
8650 if (acpi_disabled)
8651 return -ENODEV;
8652
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008653 /* It would be dangerous to run the driver in this case */
8654 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8655 return -ENODEV;
8656
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008657 /*
8658 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008659 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008660 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008661 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8662 (thinkpad_id.ec_model != 0) ||
8663 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008664
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008665 /* The EC handler is required */
8666 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008667 if (!ec_handle) {
8668 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008669 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008670 return -ENODEV;
8671 }
8672
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008673 if (!is_thinkpad && !force_load)
8674 return -ENODEV;
8675
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008676 return 0;
8677}
8678
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008679static void __init thinkpad_acpi_init_banner(void)
8680{
Joe Perches0978e012011-04-04 10:06:25 -07008681 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8682 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008683
Joe Perches0978e012011-04-04 10:06:25 -07008684 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008685 (thinkpad_id.bios_version_str) ?
8686 thinkpad_id.bios_version_str : "unknown",
8687 (thinkpad_id.ec_version_str) ?
8688 thinkpad_id.ec_version_str : "unknown");
8689
8690 BUG_ON(!thinkpad_id.vendor);
8691
8692 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008693 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008694 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8695 "IBM" : ((thinkpad_id.vendor ==
8696 PCI_VENDOR_ID_LENOVO) ?
8697 "Lenovo" : "Unknown vendor"),
8698 thinkpad_id.model_str,
8699 (thinkpad_id.nummodel_str) ?
8700 thinkpad_id.nummodel_str : "unknown");
8701}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008702
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008703/* Module init, exit, parameters */
8704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008705static struct ibm_init_struct ibms_init[] __initdata = {
8706 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008707 .data = &thinkpad_acpi_driver_data,
8708 },
8709 {
8710 .init = hotkey_init,
8711 .data = &hotkey_driver_data,
8712 },
8713 {
8714 .init = bluetooth_init,
8715 .data = &bluetooth_driver_data,
8716 },
8717 {
8718 .init = wan_init,
8719 .data = &wan_driver_data,
8720 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008721 {
8722 .init = uwb_init,
8723 .data = &uwb_driver_data,
8724 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008725#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008726 {
8727 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008728 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008729 .data = &video_driver_data,
8730 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008731#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008732 {
8733 .init = light_init,
8734 .data = &light_driver_data,
8735 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008736 {
8737 .init = cmos_init,
8738 .data = &cmos_driver_data,
8739 },
8740 {
8741 .init = led_init,
8742 .data = &led_driver_data,
8743 },
8744 {
8745 .init = beep_init,
8746 .data = &beep_driver_data,
8747 },
8748 {
8749 .init = thermal_init,
8750 .data = &thermal_driver_data,
8751 },
8752 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008753 .init = brightness_init,
8754 .data = &brightness_driver_data,
8755 },
8756 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008757 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008758 .data = &volume_driver_data,
8759 },
8760 {
8761 .init = fan_init,
8762 .data = &fan_driver_data,
8763 },
8764};
8765
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008766static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8767{
8768 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008769 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008770
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008771 if (!kp || !kp->name || !val)
8772 return -EINVAL;
8773
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008774 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8775 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008776 WARN_ON(ibm == NULL);
8777
8778 if (!ibm || !ibm->name)
8779 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008780
8781 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8782 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008783 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008784 strcpy(ibms_init[i].param, val);
8785 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008786 return 0;
8787 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008788 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008789
8790 return -EINVAL;
8791}
8792
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008793module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008794MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008795 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008796
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008797module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008798MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008799
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008800module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008801MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008802 "Attempts to load the driver even on a "
8803 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008804
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008805module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008806MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008807 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008808
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008809module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008810MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008811 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008812 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008813
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008814module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008815MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008816 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008817
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008818#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008819module_param_named(volume_mode, volume_mode, uint, 0444);
8820MODULE_PARM_DESC(volume_mode,
8821 "Selects volume control strategy: "
8822 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8823
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008824module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8825MODULE_PARM_DESC(volume_capabilities,
8826 "Selects the mixer capabilites: "
8827 "0=auto, 1=volume and mute, 2=mute only");
8828
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008829module_param_named(volume_control, volume_control_allowed, bool, 0444);
8830MODULE_PARM_DESC(volume_control,
8831 "Enables software override for the console audio "
8832 "control when true");
8833
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008834/* ALSA module API parameters */
8835module_param_named(index, alsa_index, int, 0444);
8836MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8837module_param_named(id, alsa_id, charp, 0444);
8838MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8839module_param_named(enable, alsa_enable, bool, 0444);
8840MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008841#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008842
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008843#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008844 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008845 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008846 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008847
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008848TPACPI_PARAM(hotkey);
8849TPACPI_PARAM(bluetooth);
8850TPACPI_PARAM(video);
8851TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008852TPACPI_PARAM(cmos);
8853TPACPI_PARAM(led);
8854TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008855TPACPI_PARAM(brightness);
8856TPACPI_PARAM(volume);
8857TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008858
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008859#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008860module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008861MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8862module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8863MODULE_PARM_DESC(wlsw_state,
8864 "Initial state of the emulated WLSW switch");
8865
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008866module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008867MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8868module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8869MODULE_PARM_DESC(bluetooth_state,
8870 "Initial state of the emulated bluetooth switch");
8871
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008872module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008873MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8874module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8875MODULE_PARM_DESC(wwan_state,
8876 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008877
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008878module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008879MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8880module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8881MODULE_PARM_DESC(uwb_state,
8882 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008883#endif
8884
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008885static void thinkpad_acpi_module_exit(void)
8886{
8887 struct ibm_struct *ibm, *itmp;
8888
8889 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8890
8891 list_for_each_entry_safe_reverse(ibm, itmp,
8892 &tpacpi_all_drivers,
8893 all_drivers) {
8894 ibm_exit(ibm);
8895 }
8896
8897 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8898
8899 if (tpacpi_inputdev) {
8900 if (tp_features.input_device_registered)
8901 input_unregister_device(tpacpi_inputdev);
8902 else
8903 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10008904 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008905 }
8906
8907 if (tpacpi_hwmon)
8908 hwmon_device_unregister(tpacpi_hwmon);
8909
8910 if (tp_features.sensors_pdev_attrs_registered)
8911 device_remove_file(&tpacpi_sensors_pdev->dev,
8912 &dev_attr_thinkpad_acpi_pdev_name);
8913 if (tpacpi_sensors_pdev)
8914 platform_device_unregister(tpacpi_sensors_pdev);
8915 if (tpacpi_pdev)
8916 platform_device_unregister(tpacpi_pdev);
8917
8918 if (tp_features.sensors_pdrv_attrs_registered)
8919 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8920 if (tp_features.platform_drv_attrs_registered)
8921 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8922
8923 if (tp_features.sensors_pdrv_registered)
8924 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8925
8926 if (tp_features.platform_drv_registered)
8927 platform_driver_unregister(&tpacpi_pdriver);
8928
8929 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008930 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008931
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008932 if (tpacpi_wq)
8933 destroy_workqueue(tpacpi_wq);
8934
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008935 kfree(thinkpad_id.bios_version_str);
8936 kfree(thinkpad_id.ec_version_str);
8937 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10008938 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008939}
8940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008941
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008942static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943{
8944 int ret, i;
8945
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008946 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8947
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008948 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008949
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008950 ret = get_thinkpad_model_data(&thinkpad_id);
8951 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008952 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008953 thinkpad_acpi_module_exit();
8954 return ret;
8955 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008956 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008957 if (ret) {
8958 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008959 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008962 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008963
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008964 thinkpad_acpi_init_banner();
8965 tpacpi_check_outdated_fw();
8966
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008967 TPACPI_ACPIHANDLE_INIT(ecrd);
8968 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008970 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8971 if (!tpacpi_wq) {
8972 thinkpad_acpi_module_exit();
8973 return -ENOMEM;
8974 }
8975
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008976 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07008978 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008979 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980 return -ENODEV;
8981 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008982
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008983 ret = platform_driver_register(&tpacpi_pdriver);
8984 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008985 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008986 thinkpad_acpi_module_exit();
8987 return ret;
8988 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03008989 tp_features.platform_drv_registered = 1;
8990
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008991 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8992 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008993 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008994 thinkpad_acpi_module_exit();
8995 return ret;
8996 }
8997 tp_features.sensors_pdrv_registered = 1;
8998
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03008999 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009000 if (!ret) {
9001 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009002 ret = tpacpi_create_driver_attributes(
9003 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009004 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009005 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009006 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009007 thinkpad_acpi_module_exit();
9008 return ret;
9009 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009010 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009011
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009012
9013 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009014 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009015 NULL, 0);
9016 if (IS_ERR(tpacpi_pdev)) {
9017 ret = PTR_ERR(tpacpi_pdev);
9018 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009019 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009020 thinkpad_acpi_module_exit();
9021 return ret;
9022 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009023 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009024 TPACPI_HWMON_DRVR_NAME,
9025 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009026 if (IS_ERR(tpacpi_sensors_pdev)) {
9027 ret = PTR_ERR(tpacpi_sensors_pdev);
9028 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009029 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009030 thinkpad_acpi_module_exit();
9031 return ret;
9032 }
9033 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9034 &dev_attr_thinkpad_acpi_pdev_name);
9035 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009036 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009037 thinkpad_acpi_module_exit();
9038 return ret;
9039 }
9040 tp_features.sensors_pdev_attrs_registered = 1;
9041 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009042 if (IS_ERR(tpacpi_hwmon)) {
9043 ret = PTR_ERR(tpacpi_hwmon);
9044 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009045 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009046 thinkpad_acpi_module_exit();
9047 return ret;
9048 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009049 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009050 tpacpi_inputdev = input_allocate_device();
9051 if (!tpacpi_inputdev) {
Joe Perches0978e012011-04-04 10:06:25 -07009052 pr_err("unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009053 thinkpad_acpi_module_exit();
9054 return -ENOMEM;
9055 } else {
9056 /* Prepare input device, but don't register */
9057 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009058 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009059 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009060 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009061 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9062 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009063 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009064 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009065
9066 /* Init subdriver dependencies */
9067 tpacpi_detect_brightness_capabilities();
9068
9069 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009070 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9071 ret = ibm_init(&ibms_init[i]);
9072 if (ret >= 0 && *ibms_init[i].param)
9073 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009074 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009075 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076 return ret;
9077 }
9078 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009079
9080 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9081
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009082 ret = input_register_device(tpacpi_inputdev);
9083 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009084 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009085 thinkpad_acpi_module_exit();
9086 return ret;
9087 } else {
9088 tp_features.input_device_registered = 1;
9089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090
9091 return 0;
9092}
9093
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009094MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9095
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009096/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009097 * This will autoload the driver in almost every ThinkPad
9098 * in widespread use.
9099 *
9100 * Only _VERY_ old models, like the 240, 240x and 570 lack
9101 * the HKEY event interface.
9102 */
9103MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9104
9105/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009106 * DMI matching for module autoloading
9107 *
9108 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9109 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9110 *
9111 * Only models listed in thinkwiki will be supported, so add yours
9112 * if it is not there yet.
9113 */
9114#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009115 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009116
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009117/* Ancient thinkpad BIOSes have to be identified by
9118 * BIOS type or model number, and there are far less
9119 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009120IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009121
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009122MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9123MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009124MODULE_DESCRIPTION(TPACPI_DESC);
9125MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009126MODULE_LICENSE("GPL");
9127
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009128module_init(thinkpad_acpi_module_init);
9129module_exit(thinkpad_acpi_module_exit);