blob: 9a907567f41edf495f1c102d906ba63660de082e [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 Holschuhbee4cd9b2009-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;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300372 unsigned 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 Holschuh94954cc62007-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 Holschuhef07a5ab2010-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 Holschuhef07a5ab2010-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 Holschuhef07a5ab2010-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 Holschuhef07a5ab2010-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{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200847 return single_open(file, dispatch_proc_show, PDE(inode)->data);
848}
849
850static ssize_t dispatch_proc_write(struct file *file,
851 const char __user *userbuf,
852 size_t count, loff_t *pos)
853{
Al Viro496ad9a2013-01-23 17:07:38 -0500854 struct ibm_struct *ibm = PDE(file_inode(file))->data;
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 Holschuh94954cc62007-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. Wysocki3567a4e22012-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. Wysocki3567a4e22012-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 Holschuhd64c81c42008-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 Holschuhd64c81c42008-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
1967/* Acquired while the poller kthread is running, use to sync start/stop */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001968static struct mutex hotkey_thread_mutex;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001969
1970/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001971 * Acquire mutex to write poller control variables as an
1972 * atomic block.
1973 *
1974 * Increment hotkey_config_change when changing them if you
1975 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001976 *
1977 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1978 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001979static struct mutex hotkey_thread_data_mutex;
1980static unsigned int hotkey_config_change;
1981
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001982/*
1983 * hotkey poller control variables
1984 *
1985 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001986 *
1987 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 * should be used only when the changes need to be taken as
1989 * a block, OR when one needs to force the kthread to forget
1990 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001991 */
1992static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1993static unsigned int hotkey_poll_freq = 10; /* Hz */
1994
1995#define HOTKEY_CONFIG_CRITICAL_START \
1996 do { \
1997 mutex_lock(&hotkey_thread_data_mutex); \
1998 hotkey_config_change++; \
1999 } while (0);
2000#define HOTKEY_CONFIG_CRITICAL_END \
2001 mutex_unlock(&hotkey_thread_data_mutex);
2002
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002003#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004
2005#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002006#define HOTKEY_CONFIG_CRITICAL_START
2007#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002008
2009#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002011static struct mutex hotkey_mutex;
2012
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002013static enum { /* Reasons for waking up */
2014 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2015 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2016 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2017} hotkey_wakeup_reason;
2018
2019static int hotkey_autosleep_ack;
2020
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002021static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2022static u32 hotkey_all_mask; /* all events supported in fw */
2023static u32 hotkey_reserved_mask; /* events better left disabled */
2024static u32 hotkey_driver_mask; /* events needed by the driver */
2025static u32 hotkey_user_mask; /* events visible to userspace */
2026static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002027
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02002028static unsigned int hotkey_report_mode;
2029
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002030static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002031
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002032static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002033
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002034static void tpacpi_driver_event(const unsigned int hkey_event);
2035static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002036static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002037
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002038/* HKEY.MHKG() return bits */
2039#define TP_HOTKEY_TABLET_MASK (1 << 3)
2040
Johannes Berg19d337d2009-06-02 13:01:37 +02002041static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002042{
Johannes Berg19d337d2009-06-02 13:01:37 +02002043 int status;
2044
2045 if (!tp_features.hotkey_wlsw)
2046 return -ENODEV;
2047
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002048#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002049 if (dbg_wlswemul)
2050 return (tpacpi_wlsw_emulstate) ?
2051 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002052#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002053
2054 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002055 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002056
2057 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002058}
2059
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002060static int hotkey_get_tablet_mode(int *status)
2061{
2062 int s;
2063
2064 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2065 return -EIO;
2066
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002067 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2068 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002069}
2070
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002071/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002072 * Reads current event mask from firmware, and updates
2073 * hotkey_acpi_mask accordingly. Also resets any bits
2074 * from hotkey_user_mask that are unavailable to be
2075 * delivered (shadow requirement of the userspace ABI).
2076 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002077 * Call with hotkey_mutex held
2078 */
2079static int hotkey_mask_get(void)
2080{
2081 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002082 u32 m = 0;
2083
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002084 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002085 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002086
2087 hotkey_acpi_mask = m;
2088 } else {
2089 /* no mask support doesn't mean no event support... */
2090 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002091 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002092
2093 /* sync userspace-visible mask */
2094 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095
2096 return 0;
2097}
2098
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002099void static hotkey_mask_warn_incomplete_mask(void)
2100{
2101 /* log only what the user can fix... */
2102 const u32 wantedmask = hotkey_driver_mask &
2103 ~(hotkey_acpi_mask | hotkey_source_mask) &
2104 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2105
2106 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002107 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002108}
2109
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002110/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002111 * Set the firmware mask when supported
2112 *
2113 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2114 *
2115 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2116 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002117 * Call with hotkey_mutex held
2118 */
2119static int hotkey_mask_set(u32 mask)
2120{
2121 int i;
2122 int rc = 0;
2123
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002124 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002125
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002128 if (!acpi_evalf(hkey_handle,
2129 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002130 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 rc = -EIO;
2132 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002133 }
2134 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002135 }
2136
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002137 /*
2138 * We *must* make an inconditional call to hotkey_mask_get to
2139 * refresh hotkey_acpi_mask and update hotkey_user_mask
2140 *
2141 * Take the opportunity to also log when we cannot _enable_
2142 * a given event.
2143 */
2144 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002145 pr_notice("asked for hotkey mask 0x%08x, but "
2146 "firmware forced it to 0x%08x\n",
2147 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148 }
2149
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002150 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2151 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002152
2153 return rc;
2154}
2155
2156/*
2157 * Sets hotkey_user_mask and tries to set the firmware mask
2158 *
2159 * Call with hotkey_mutex held
2160 */
2161static int hotkey_user_mask_set(const u32 mask)
2162{
2163 int rc;
2164
2165 /* Give people a chance to notice they are doing something that
2166 * is bound to go boom on their users sooner or later */
2167 if (!tp_warned.hotkey_mask_ff &&
2168 (mask == 0xffff || mask == 0xffffff ||
2169 mask == 0xffffffff)) {
2170 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002171 pr_notice("setting the hotkey mask to 0x%08x is likely "
2172 "not the best way to go about it\n", mask);
2173 pr_notice("please consider using the driver defaults, "
2174 "and refer to up-to-date thinkpad-acpi "
2175 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002176 }
2177
2178 /* Try to enable what the user asked for, plus whatever we need.
2179 * this syncs everything but won't enable bits in hotkey_user_mask */
2180 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2181
2182 /* Enable the available bits in hotkey_user_mask */
2183 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2184
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002185 return rc;
2186}
2187
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002188/*
2189 * Sets the driver hotkey mask.
2190 *
2191 * Can be called even if the hotkey subdriver is inactive
2192 */
2193static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2194{
2195 int rc;
2196
2197 /* Do the right thing if hotkey_init has not been called yet */
2198 if (!tp_features.hotkey) {
2199 hotkey_driver_mask = mask;
2200 return 0;
2201 }
2202
2203 mutex_lock(&hotkey_mutex);
2204
2205 HOTKEY_CONFIG_CRITICAL_START
2206 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002207#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002208 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002209#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002210 HOTKEY_CONFIG_CRITICAL_END
2211
2212 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2213 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002214 hotkey_poll_setup(true);
2215
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002216 mutex_unlock(&hotkey_mutex);
2217
2218 return rc;
2219}
2220
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002221static int hotkey_status_get(int *status)
2222{
2223 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2224 return -EIO;
2225
2226 return 0;
2227}
2228
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002229static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002230{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002231 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002232 return -EIO;
2233
2234 return 0;
2235}
2236
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002237static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002238{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002239 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002240
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002241 if (tp_features.hotkey_tablet &&
2242 !hotkey_get_tablet_mode(&state)) {
2243 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002244
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002245 input_report_switch(tpacpi_inputdev,
2246 SW_TABLET_MODE, !!state);
2247 input_sync(tpacpi_inputdev);
2248
2249 mutex_unlock(&tpacpi_inputdev_send_mutex);
2250 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002251}
2252
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002253/* Do NOT call without validating scancode first */
2254static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002255{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002256 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002257
2258 if (keycode != KEY_RESERVED) {
2259 mutex_lock(&tpacpi_inputdev_send_mutex);
2260
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002261 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002262 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002263 input_sync(tpacpi_inputdev);
2264
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002265 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002266 input_report_key(tpacpi_inputdev, keycode, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002267 input_sync(tpacpi_inputdev);
2268
2269 mutex_unlock(&tpacpi_inputdev_send_mutex);
2270 }
2271}
2272
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002273/* Do NOT call without validating scancode first */
2274static void tpacpi_input_send_key_masked(const unsigned int scancode)
2275{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002276 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002277 if (hotkey_user_mask & (1 << scancode))
2278 tpacpi_input_send_key(scancode);
2279}
2280
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002281#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2282static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2283
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002284/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002285static void tpacpi_hotkey_send_key(unsigned int scancode)
2286{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002287 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002288 if (hotkey_report_mode < 2) {
2289 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03002290 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002291 }
2292}
2293
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002294static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002295{
2296 u8 d;
2297
2298 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2299 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2300 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2301 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2302 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2303 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2304 }
2305 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2306 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2307 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2308 }
2309 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2310 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2311 n->displayexp_toggle =
2312 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2313 }
2314 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2315 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2316 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2317 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2318 n->brightness_toggle =
2319 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2320 }
2321 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2322 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2323 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2324 >> TP_NVRAM_POS_LEVEL_VOLUME;
2325 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2326 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2327 }
2328}
2329
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002330static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2331 struct tp_nvram_state *newn,
2332 const u32 event_mask)
2333{
2334
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002335#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002336 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002337 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002338 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002339 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002340 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002341
2342#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002343 do { \
2344 if (event_mask & (1 << __scancode)) \
2345 tpacpi_hotkey_send_key(__scancode); \
2346 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002348 void issue_volchange(const unsigned int oldvol,
2349 const unsigned int newvol)
2350 {
2351 unsigned int i = oldvol;
2352
2353 while (i > newvol) {
2354 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2355 i--;
2356 }
2357 while (i < newvol) {
2358 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2359 i++;
2360 }
2361 }
2362
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002363 void issue_brightnesschange(const unsigned int oldbrt,
2364 const unsigned int newbrt)
2365 {
2366 unsigned int i = oldbrt;
2367
2368 while (i > newbrt) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2370 i--;
2371 }
2372 while (i < newbrt) {
2373 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2374 i++;
2375 }
2376 }
2377
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002378 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2381 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2382
2383 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2384
2385 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2386
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002387 /*
2388 * Handle volume
2389 *
2390 * This code is supposed to duplicate the IBM firmware behaviour:
2391 * - Pressing MUTE issues mute hotkey message, even when already mute
2392 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002393 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002394 * - The act of unmuting issues volume up/down notification,
2395 * depending which key was used to unmute
2396 *
2397 * We are constrained to what the NVRAM can tell us, which is not much
2398 * and certainly not enough if more than one volume hotkey was pressed
2399 * since the last poll cycle.
2400 *
2401 * Just to make our life interesting, some newer Lenovo ThinkPads have
2402 * bugs in the BIOS and may fail to update volume_toggle properly.
2403 */
2404 if (newn->mute) {
2405 /* muted */
2406 if (!oldn->mute ||
2407 oldn->volume_toggle != newn->volume_toggle ||
2408 oldn->volume_level != newn->volume_level) {
2409 /* recently muted, or repeated mute keypress, or
2410 * multiple presses ending in mute */
2411 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2413 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002414 } else {
2415 /* unmute */
2416 if (oldn->mute) {
2417 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002419 }
2420 if (oldn->volume_level != newn->volume_level) {
2421 issue_volchange(oldn->volume_level, newn->volume_level);
2422 } else if (oldn->volume_toggle != newn->volume_toggle) {
2423 /* repeated vol up/down keypress at end of scale ? */
2424 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002426 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002428 }
2429 }
2430
2431 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002432 if (oldn->brightness_level != newn->brightness_level) {
2433 issue_brightnesschange(oldn->brightness_level,
2434 newn->brightness_level);
2435 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2436 /* repeated key presses that didn't change state */
2437 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002439 else if (newn->brightness_level >= bright_maxlvl
2440 && !tp_features.bright_unkfw)
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002442 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002443
2444#undef TPACPI_COMPARE_KEY
2445#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002446}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002448/*
2449 * Polling driver
2450 *
2451 * We track all events in hotkey_source_mask all the time, since
2452 * most of them are edge-based. We only issue those requested by
2453 * hotkey_user_mask or hotkey_driver_mask, though.
2454 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002455static int hotkey_kthread(void *data)
2456{
2457 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002458 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002459 unsigned int si, so;
2460 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002461 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002462 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002463 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002464
2465 mutex_lock(&hotkey_thread_mutex);
2466
2467 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2468 goto exit;
2469
2470 set_freezable();
2471
2472 so = 0;
2473 si = 1;
2474 t = 0;
2475
2476 /* Initial state for compares */
2477 mutex_lock(&hotkey_thread_data_mutex);
2478 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002479 poll_mask = hotkey_source_mask;
2480 event_mask = hotkey_source_mask &
2481 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002482 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002484 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002486 while (!kthread_should_stop()) {
2487 if (t == 0) {
2488 if (likely(poll_freq))
2489 t = 1000/poll_freq;
2490 else
2491 t = 100; /* should never happen... */
2492 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002493 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002494 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002495 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002496
2497 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002498 continue;
2499
2500 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002501 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002502 /* forget old state on thaw or config change */
2503 si = so;
2504 t = 0;
2505 change_detector = hotkey_config_change;
2506 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002507 poll_mask = hotkey_source_mask;
2508 event_mask = hotkey_source_mask &
2509 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002510 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002511 mutex_unlock(&hotkey_thread_data_mutex);
2512
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002513 if (likely(poll_mask)) {
2514 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002515 if (likely(si != so)) {
2516 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002517 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002518 }
2519 }
2520
2521 so = si;
2522 si ^= 1;
2523 }
2524
2525exit:
2526 mutex_unlock(&hotkey_thread_mutex);
2527 return 0;
2528}
2529
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002530/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002531static void hotkey_poll_stop_sync(void)
2532{
2533 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002534 kthread_stop(tpacpi_hotkey_task);
2535 tpacpi_hotkey_task = NULL;
2536 mutex_lock(&hotkey_thread_mutex);
2537 /* at this point, the thread did exit */
2538 mutex_unlock(&hotkey_thread_mutex);
2539 }
2540}
2541
2542/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002543static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002544{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002545 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2546 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002547
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002548 if (hotkey_poll_freq > 0 &&
2549 (poll_driver_mask ||
2550 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002551 if (!tpacpi_hotkey_task) {
2552 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002553 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 if (IS_ERR(tpacpi_hotkey_task)) {
2555 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002556 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002557 "for hotkey polling\n");
2558 }
2559 }
2560 } else {
2561 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002562 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002563 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002564 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2565 "require polling, which is currently "
2566 "disabled\n",
2567 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002568 }
2569 }
2570}
2571
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002572static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002573{
2574 mutex_lock(&hotkey_mutex);
2575 hotkey_poll_setup(may_warn);
2576 mutex_unlock(&hotkey_mutex);
2577}
2578
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002579/* call with hotkey_mutex held */
2580static void hotkey_poll_set_freq(unsigned int freq)
2581{
2582 if (!freq)
2583 hotkey_poll_stop_sync();
2584
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002585 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002586}
2587
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002588#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2589
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002590static void hotkey_poll_setup(const bool __unused)
2591{
2592}
2593
2594static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002595{
2596}
2597
2598#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002600static int hotkey_inputdev_open(struct input_dev *dev)
2601{
2602 switch (tpacpi_lifecycle) {
2603 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002604 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002605 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002606 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002607 case TPACPI_LIFE_EXITING:
2608 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609 }
2610
2611 /* Should only happen if tpacpi_lifecycle is corrupt */
2612 BUG();
2613 return -EBUSY;
2614}
2615
2616static void hotkey_inputdev_close(struct input_dev *dev)
2617{
2618 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002619 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002620 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002621 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002622}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002623
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002624/* sysfs hotkey enable ------------------------------------------------- */
2625static ssize_t hotkey_enable_show(struct device *dev,
2626 struct device_attribute *attr,
2627 char *buf)
2628{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002629 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002630
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002631 printk_deprecated_attribute("hotkey_enable",
2632 "Hotkey reporting is always enabled");
2633
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002634 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002635 if (res)
2636 return res;
2637
2638 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2639}
2640
2641static ssize_t hotkey_enable_store(struct device *dev,
2642 struct device_attribute *attr,
2643 const char *buf, size_t count)
2644{
2645 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002646
2647 printk_deprecated_attribute("hotkey_enable",
2648 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002649
2650 if (parse_strtoul(buf, 1, &t))
2651 return -EINVAL;
2652
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002653 if (t == 0)
2654 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002655
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002656 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002657}
2658
2659static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002660 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002661 hotkey_enable_show, hotkey_enable_store);
2662
2663/* sysfs hotkey mask --------------------------------------------------- */
2664static ssize_t hotkey_mask_show(struct device *dev,
2665 struct device_attribute *attr,
2666 char *buf)
2667{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002668 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002669}
2670
2671static ssize_t hotkey_mask_store(struct device *dev,
2672 struct device_attribute *attr,
2673 const char *buf, size_t count)
2674{
2675 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002676 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002678 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002679 return -EINVAL;
2680
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002681 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002682 return -ERESTARTSYS;
2683
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002684 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002685
2686#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002687 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002688#endif
2689
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002690 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002691
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002692 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2693
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002694 return (res) ? res : count;
2695}
2696
2697static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002698 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002699 hotkey_mask_show, hotkey_mask_store);
2700
2701/* sysfs hotkey bios_enabled ------------------------------------------- */
2702static ssize_t hotkey_bios_enabled_show(struct device *dev,
2703 struct device_attribute *attr,
2704 char *buf)
2705{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002706 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002707}
2708
2709static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002710 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002711
2712/* sysfs hotkey bios_mask ---------------------------------------------- */
2713static ssize_t hotkey_bios_mask_show(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2716{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002717 printk_deprecated_attribute("hotkey_bios_mask",
2718 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002719 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002720}
2721
2722static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002723 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002724
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002725/* sysfs hotkey all_mask ----------------------------------------------- */
2726static ssize_t hotkey_all_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2729{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002730 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2731 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002732}
2733
2734static struct device_attribute dev_attr_hotkey_all_mask =
2735 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2736
2737/* sysfs hotkey recommended_mask --------------------------------------- */
2738static ssize_t hotkey_recommended_mask_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2741{
2742 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002743 (hotkey_all_mask | hotkey_source_mask)
2744 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002745}
2746
2747static struct device_attribute dev_attr_hotkey_recommended_mask =
2748 __ATTR(hotkey_recommended_mask, S_IRUGO,
2749 hotkey_recommended_mask_show, NULL);
2750
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002751#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2752
2753/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2754static ssize_t hotkey_source_mask_show(struct device *dev,
2755 struct device_attribute *attr,
2756 char *buf)
2757{
2758 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2759}
2760
2761static ssize_t hotkey_source_mask_store(struct device *dev,
2762 struct device_attribute *attr,
2763 const char *buf, size_t count)
2764{
2765 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002766 u32 r_ev;
2767 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002768
2769 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2770 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2771 return -EINVAL;
2772
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002773 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002774 return -ERESTARTSYS;
2775
2776 HOTKEY_CONFIG_CRITICAL_START
2777 hotkey_source_mask = t;
2778 HOTKEY_CONFIG_CRITICAL_END
2779
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002780 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2781 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002782 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002783
2784 /* check if events needed by the driver got disabled */
2785 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2786 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002787
2788 mutex_unlock(&hotkey_mutex);
2789
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002790 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002791 pr_err("hotkey_source_mask: "
2792 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002793
2794 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002795 pr_notice("hotkey_source_mask: "
2796 "some important events were disabled: 0x%04x\n",
2797 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002798
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002799 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2800
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002801 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002802}
2803
2804static struct device_attribute dev_attr_hotkey_source_mask =
2805 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2806 hotkey_source_mask_show, hotkey_source_mask_store);
2807
2808/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2809static ssize_t hotkey_poll_freq_show(struct device *dev,
2810 struct device_attribute *attr,
2811 char *buf)
2812{
2813 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2814}
2815
2816static ssize_t hotkey_poll_freq_store(struct device *dev,
2817 struct device_attribute *attr,
2818 const char *buf, size_t count)
2819{
2820 unsigned long t;
2821
2822 if (parse_strtoul(buf, 25, &t))
2823 return -EINVAL;
2824
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002825 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002826 return -ERESTARTSYS;
2827
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002828 hotkey_poll_set_freq(t);
2829 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002830
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002831 mutex_unlock(&hotkey_mutex);
2832
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002833 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2834
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002835 return count;
2836}
2837
2838static struct device_attribute dev_attr_hotkey_poll_freq =
2839 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2840 hotkey_poll_freq_show, hotkey_poll_freq_store);
2841
2842#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2843
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002844/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002845static ssize_t hotkey_radio_sw_show(struct device *dev,
2846 struct device_attribute *attr,
2847 char *buf)
2848{
Johannes Berg19d337d2009-06-02 13:01:37 +02002849 int res;
2850 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002851 if (res < 0)
2852 return res;
2853
Johannes Berg19d337d2009-06-02 13:01:37 +02002854 /* Opportunistic update */
2855 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2856
2857 return snprintf(buf, PAGE_SIZE, "%d\n",
2858 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002859}
2860
2861static struct device_attribute dev_attr_hotkey_radio_sw =
2862 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2863
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002864static void hotkey_radio_sw_notify_change(void)
2865{
2866 if (tp_features.hotkey_wlsw)
2867 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2868 "hotkey_radio_sw");
2869}
2870
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002871/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2872static ssize_t hotkey_tablet_mode_show(struct device *dev,
2873 struct device_attribute *attr,
2874 char *buf)
2875{
2876 int res, s;
2877 res = hotkey_get_tablet_mode(&s);
2878 if (res < 0)
2879 return res;
2880
2881 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2882}
2883
2884static struct device_attribute dev_attr_hotkey_tablet_mode =
2885 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2886
2887static void hotkey_tablet_mode_notify_change(void)
2888{
2889 if (tp_features.hotkey_tablet)
2890 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2891 "hotkey_tablet_mode");
2892}
2893
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002894/* sysfs hotkey report_mode -------------------------------------------- */
2895static ssize_t hotkey_report_mode_show(struct device *dev,
2896 struct device_attribute *attr,
2897 char *buf)
2898{
2899 return snprintf(buf, PAGE_SIZE, "%d\n",
2900 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2901}
2902
2903static struct device_attribute dev_attr_hotkey_report_mode =
2904 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2905
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002906/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002907static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2908 struct device_attribute *attr,
2909 char *buf)
2910{
2911 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2912}
2913
2914static struct device_attribute dev_attr_hotkey_wakeup_reason =
2915 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2916
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002917static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002918{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002919 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2920 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002921}
2922
2923/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002924static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2925 struct device_attribute *attr,
2926 char *buf)
2927{
2928 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2929}
2930
2931static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2932 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2933 hotkey_wakeup_hotunplug_complete_show, NULL);
2934
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002935static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002936{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002937 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2938 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002939}
2940
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002941/* --------------------------------------------------------------------- */
2942
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002943static struct attribute *hotkey_attributes[] __initdata = {
2944 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002945 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002946 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002947 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002948 &dev_attr_hotkey_wakeup_reason.attr,
2949 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002950 &dev_attr_hotkey_mask.attr,
2951 &dev_attr_hotkey_all_mask.attr,
2952 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002953#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002954 &dev_attr_hotkey_source_mask.attr,
2955 &dev_attr_hotkey_poll_freq.attr,
2956#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002957};
2958
Johannes Berg19d337d2009-06-02 13:01:37 +02002959/*
2960 * Sync both the hw and sw blocking state of all switches
2961 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002962static void tpacpi_send_radiosw_update(void)
2963{
2964 int wlsw;
2965
Johannes Berg19d337d2009-06-02 13:01:37 +02002966 /*
2967 * We must sync all rfkill controllers *before* issuing any
2968 * rfkill input events, or we will race the rfkill core input
2969 * handler.
2970 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002971 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002972 * for the above.
2973 *
2974 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2975 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002976
Johannes Berg19d337d2009-06-02 13:01:37 +02002977 wlsw = hotkey_get_wlsw();
2978
2979 /* Sync hw blocking state first if it is hw-blocked */
2980 if (wlsw == TPACPI_RFK_RADIO_OFF)
2981 tpacpi_rfk_update_hwblock_state(true);
2982
2983 /* Sync sw blocking state */
2984 tpacpi_rfk_update_swstate_all();
2985
2986 /* Sync hw blocking state last if it is hw-unblocked */
2987 if (wlsw == TPACPI_RFK_RADIO_ON)
2988 tpacpi_rfk_update_hwblock_state(false);
2989
2990 /* Issue rfkill input event for WLSW switch */
2991 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002992 mutex_lock(&tpacpi_inputdev_send_mutex);
2993
2994 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002995 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002996 input_sync(tpacpi_inputdev);
2997
2998 mutex_unlock(&tpacpi_inputdev_send_mutex);
2999 }
Johannes Berg19d337d2009-06-02 13:01:37 +02003000
3001 /*
3002 * this can be unconditional, as we will poll state again
3003 * if userspace uses the notify to read data
3004 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003005 hotkey_radio_sw_notify_change();
3006}
3007
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003008static void hotkey_exit(void)
3009{
3010#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003011 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003012 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003013 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003014#endif
3015
3016 if (hotkey_dev_attributes)
3017 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3018
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003019 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003020 "restoring original HKEY status and mask\n");
3021 /* yes, there is a bitwise or below, we want the
3022 * functions to be called even if one of them fail */
3023 if (((tp_features.hotkey_mask &&
3024 hotkey_mask_set(hotkey_orig_mask)) |
3025 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07003026 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003027 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003028}
3029
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003030static void __init hotkey_unmap(const unsigned int scancode)
3031{
3032 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3033 clear_bit(hotkey_keycode_map[scancode],
3034 tpacpi_inputdev->keybit);
3035 hotkey_keycode_map[scancode] = KEY_RESERVED;
3036 }
3037}
3038
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003039/*
3040 * HKEY quirks:
3041 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3042 */
3043
3044#define TPACPI_HK_Q_INIMASK 0x0001
3045
3046static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3047 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3048 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3049 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3050 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3051 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3052 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3053 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3054 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3055 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3056 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3057 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3058 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3059 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3060 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3061 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3062 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3063 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3064 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3065 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3066};
3067
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003068typedef u16 tpacpi_keymap_entry_t;
3069typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003070
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003071static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003072{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003073 /* Requirements for changing the default keymaps:
3074 *
3075 * 1. Many of the keys are mapped to KEY_RESERVED for very
3076 * good reasons. Do not change them unless you have deep
3077 * knowledge on the IBM and Lenovo ThinkPad firmware for
3078 * the various ThinkPad models. The driver behaves
3079 * differently for KEY_RESERVED: such keys have their
3080 * hot key mask *unset* in mask_recommended, and also
3081 * in the initial hot key mask programmed into the
3082 * firmware at driver load time, which means the firm-
3083 * ware may react very differently if you change them to
3084 * something else;
3085 *
3086 * 2. You must be subscribed to the linux-thinkpad and
3087 * ibm-acpi-devel mailing lists, and you should read the
3088 * list archives since 2007 if you want to change the
3089 * keymaps. This requirement exists so that you will
3090 * know the past history of problems with the thinkpad-
3091 * acpi driver keymaps, and also that you will be
3092 * listening to any bug reports;
3093 *
3094 * 3. Do not send thinkpad-acpi specific patches directly to
3095 * for merging, *ever*. Send them to the linux-acpi
3096 * mailinglist for comments. Merging is to be done only
3097 * through acpi-test and the ACPI maintainer.
3098 *
3099 * If the above is too much to ask, don't change the keymap.
3100 * Ask the thinkpad-acpi maintainer to do it, instead.
3101 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003102
3103 enum keymap_index {
3104 TPACPI_KEYMAP_IBM_GENERIC = 0,
3105 TPACPI_KEYMAP_LENOVO_GENERIC,
3106 };
3107
3108 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3109 /* Generic keymap for IBM ThinkPads */
3110 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003111 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003112 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003113 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3114 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003115
3116 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003117 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3118 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3119 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003120
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003121 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003122 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003123 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003124
3125 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003126 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003127
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003128 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3129 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003130
3131 /* Volume: firmware always react to it and reprograms
3132 * the built-in *extra* mixer. Never map it to control
3133 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003134 KEY_RESERVED, /* 0x14: VOLUME UP */
3135 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3136 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003138 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003139
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003140 /* (assignments unknown, please report if found) */
3141 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3142 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003143 },
3144
3145 /* Generic keymap for Lenovo ThinkPads */
3146 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003147 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3148 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003149 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003150 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003151
3152 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003153 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3154 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3155 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003156
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003157 /* These should be enabled --only-- when ACPI video
3158 * is disabled (i.e. in "vendor" mode), and are handled
3159 * in a special way by the init code */
3160 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3161 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003162
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003163 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003164
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003165 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3166 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003167
3168 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3169 * react to it and reprograms the built-in *extra* mixer.
3170 * Never map it to control another mixer by default.
3171 *
3172 * T60?, T61, R60?, R61: firmware and EC tries to send
3173 * these over the regular keyboard, so these are no-ops,
3174 * but there are still weird bugs re. MUTE, so do not
3175 * change unless you get test reports from all Lenovo
3176 * models. May cause the BIOS to interfere with the
3177 * HDA mixer.
3178 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003179 KEY_RESERVED, /* 0x14: VOLUME UP */
3180 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3181 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003182
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003183 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003184
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003185 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003186 KEY_UNKNOWN, KEY_UNKNOWN,
3187
3188 /*
3189 * The mic mute button only sends 0x1a. It does not
3190 * automatically mute the mic or change the mute light.
3191 */
3192 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3193
3194 /* (assignments unknown, please report if found) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003195 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Andy Lutomirski33009552011-05-24 15:16:43 -04003196 KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003197 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003198 };
3199
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003200 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3201 /* Generic maps (fallback) */
3202 {
3203 .vendor = PCI_VENDOR_ID_IBM,
3204 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3205 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3206 },
3207 {
3208 .vendor = PCI_VENDOR_ID_LENOVO,
3209 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3210 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3211 },
3212 };
3213
3214#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003215#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003216
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003217 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003218 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003219 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003220 bool radiosw_state = false;
3221 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003222
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003223 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003224 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003225
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003226 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3227 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003228
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003229 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003230 BUG_ON(tpacpi_inputdev->open != NULL ||
3231 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003232
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003233 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003234 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003235
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003236#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3237 mutex_init(&hotkey_thread_mutex);
3238 mutex_init(&hotkey_thread_data_mutex);
3239#endif
3240
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003241 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003242 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003243
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003244 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3245 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003246 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003247
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003248 if (!tp_features.hotkey)
3249 return 1;
3250
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003251 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3252 ARRAY_SIZE(tpacpi_hotkey_qtable));
3253
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003254 tpacpi_disable_brightness_delay();
3255
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003256 /* MUST have enough space for all attributes to be added to
3257 * hotkey_dev_attributes */
3258 hotkey_dev_attributes = create_attr_set(
3259 ARRAY_SIZE(hotkey_attributes) + 2,
3260 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003261 if (!hotkey_dev_attributes)
3262 return -ENOMEM;
3263 res = add_many_to_attr_set(hotkey_dev_attributes,
3264 hotkey_attributes,
3265 ARRAY_SIZE(hotkey_attributes));
3266 if (res)
3267 goto err_exit;
3268
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003269 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003270 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3271 for HKEY interface version 0x100 */
3272 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3273 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003274 pr_err("unknown version of the HKEY interface: 0x%x\n",
3275 hkeyv);
3276 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003277 } else {
3278 /*
3279 * MHKV 0x100 in A31, R40, R40e,
3280 * T4x, X31, and later
3281 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "firmware HKEY interface version: 0x%x\n",
3284 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003285
3286 /* Paranoia check AND init hotkey_all_mask */
3287 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3288 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003289 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003290 "please report this to %s\n",
3291 TPACPI_MAIL);
3292 /* Fallback: pre-init for FN+F3,F4,F12 */
3293 hotkey_all_mask = 0x080cU;
3294 } else {
3295 tp_features.hotkey_mask = 1;
3296 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003297 }
3298 }
3299
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003300 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3301 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003302 str_supported(tp_features.hotkey_mask));
3303
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003304 /* Init hotkey_all_mask if not initialized yet */
3305 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3306 (quirks & TPACPI_HK_Q_INIMASK))
3307 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003308
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003309 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003311 /* hotkey_source_mask *must* be zero for
3312 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003314 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003315 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003316
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003317 hotkey_orig_mask = hotkey_acpi_mask;
3318 } else {
3319 hotkey_orig_mask = hotkey_all_mask;
3320 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003321 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003322
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003323#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3324 if (dbg_wlswemul) {
3325 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003326 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003327 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003328 } else
3329#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003330 /* Not all thinkpads have a hardware radio switch */
3331 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3332 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003333 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003334 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003335 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003336 }
3337 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003338 res = add_to_attr_set(hotkey_dev_attributes,
3339 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003340
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003341 /* For X41t, X60t, X61t Tablets... */
3342 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3343 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003344 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003345 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003346 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003347 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003348 res = add_to_attr_set(hotkey_dev_attributes,
3349 &dev_attr_hotkey_tablet_mode.attr);
3350 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003351
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003352 if (!res)
3353 res = register_attr_set_with_sysfs(
3354 hotkey_dev_attributes,
3355 &tpacpi_pdev->dev.kobj);
3356 if (res)
3357 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003358
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003359 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003360 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3361 GFP_KERNEL);
3362 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003363 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003364 res = -ENOMEM;
3365 goto err_exit;
3366 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003367
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003368 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3369 ARRAY_SIZE(tpacpi_keymap_qtable));
3370 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3371 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3372 "using keymap number %lu\n", keymap_id);
3373
3374 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3375 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003376
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003377 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003378 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3379 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3380 tpacpi_inputdev->keycode = hotkey_keycode_map;
3381 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3382 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003383 input_set_capability(tpacpi_inputdev, EV_KEY,
3384 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003385 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 if (i < sizeof(hotkey_reserved_mask)*8)
3387 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003388 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003389 }
3390
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003391 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003392 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003393 input_report_switch(tpacpi_inputdev,
3394 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003395 }
3396 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003397 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003398 input_report_switch(tpacpi_inputdev,
3399 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003400 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003401
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003402 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003403 * userspace. tpacpi_detect_brightness_capabilities() must have
3404 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003405 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003406 pr_info("This ThinkPad has standard ACPI backlight "
3407 "brightness control, supported by the ACPI "
3408 "video driver\n");
3409 pr_notice("Disabling thinkpad-acpi brightness events "
3410 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003411
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003412 /* Disable brightness up/down on Lenovo thinkpads when
3413 * ACPI is handling them, otherwise it is plain impossible
3414 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003415 hotkey_reserved_mask |=
3416 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3417 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003418 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3419 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003420 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003421
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003422#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003423 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3424 & ~hotkey_all_mask
3425 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003426
3427 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3428 "hotkey source mask 0x%08x, polling freq %u\n",
3429 hotkey_source_mask, hotkey_poll_freq);
3430#endif
3431
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003432 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3433 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003434 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003435 if (res) {
3436 hotkey_exit();
3437 return res;
3438 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003439 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3440 | hotkey_driver_mask)
3441 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003442 if (res < 0 && res != -ENXIO) {
3443 hotkey_exit();
3444 return res;
3445 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003446 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3447 & ~hotkey_reserved_mask;
3448 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3449 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3450 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003451
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003452 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3453 "legacy ibm/hotkey event reporting over procfs %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003454 (hotkey_report_mode < 2) ?
3455 "enabled" : "disabled");
3456
3457 tpacpi_inputdev->open = &hotkey_inputdev_open;
3458 tpacpi_inputdev->close = &hotkey_inputdev_close;
3459
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003460 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003461
3462 return 0;
3463
3464err_exit:
3465 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3466 hotkey_dev_attributes = NULL;
3467
3468 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003469}
3470
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003471static bool hotkey_notify_hotkey(const u32 hkey,
3472 bool *send_acpi_ev,
3473 bool *ignore_acpi_ev)
3474{
3475 /* 0x1000-0x1FFF: key presses */
3476 unsigned int scancode = hkey & 0xfff;
3477 *send_acpi_ev = true;
3478 *ignore_acpi_ev = false;
3479
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003480 /* HKEY event 0x1001 is scancode 0x00 */
3481 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003482 scancode--;
3483 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003484 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003485 *send_acpi_ev = false;
3486 } else {
3487 *ignore_acpi_ev = true;
3488 }
3489 return true;
3490 }
3491 return false;
3492}
3493
3494static bool hotkey_notify_wakeup(const u32 hkey,
3495 bool *send_acpi_ev,
3496 bool *ignore_acpi_ev)
3497{
3498 /* 0x2000-0x2FFF: Wakeup reason */
3499 *send_acpi_ev = true;
3500 *ignore_acpi_ev = false;
3501
3502 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003503 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3504 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003505 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3506 *ignore_acpi_ev = true;
3507 break;
3508
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003509 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3510 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003511 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3512 *ignore_acpi_ev = true;
3513 break;
3514
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003515 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3516 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003517 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003518 /* how to auto-heal: */
3519 /* 2313: woke up from S3, go to S4/S5 */
3520 /* 2413: woke up from S4, go to S5 */
3521 break;
3522
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003523 default:
3524 return false;
3525 }
3526
3527 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003528 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003529 hotkey_wakeup_reason_notify_change();
3530 }
3531 return true;
3532}
3533
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003534static bool hotkey_notify_dockevent(const u32 hkey,
3535 bool *send_acpi_ev,
3536 bool *ignore_acpi_ev)
3537{
3538 /* 0x4000-0x4FFF: dock-related events */
3539 *send_acpi_ev = true;
3540 *ignore_acpi_ev = false;
3541
3542 switch (hkey) {
3543 case TP_HKEY_EV_UNDOCK_ACK:
3544 /* ACPI undock operation completed after wakeup */
3545 hotkey_autosleep_ack = 1;
3546 pr_info("undocked\n");
3547 hotkey_wakeup_hotunplug_complete_notify_change();
3548 return true;
3549
3550 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3551 pr_info("docked into hotplug port replicator\n");
3552 return true;
3553 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3554 pr_info("undocked from hotplug port replicator\n");
3555 return true;
3556
3557 default:
3558 return false;
3559 }
3560}
3561
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003562static bool hotkey_notify_usrevent(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3565{
3566 /* 0x5000-0x5FFF: human interface helpers */
3567 *send_acpi_ev = true;
3568 *ignore_acpi_ev = false;
3569
3570 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003571 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3572 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003573 return true;
3574
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003575 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3576 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003577 tpacpi_input_send_tabletsw();
3578 hotkey_tablet_mode_notify_change();
3579 *send_acpi_ev = false;
3580 return true;
3581
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003582 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3583 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3584 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003585 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003586 *ignore_acpi_ev = true;
3587 return true;
3588
3589 default:
3590 return false;
3591 }
3592}
3593
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003594static void thermal_dump_all_sensors(void);
3595
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003596static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003597 bool *send_acpi_ev,
3598 bool *ignore_acpi_ev)
3599{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003600 bool known = true;
3601
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003602 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003603 *send_acpi_ev = true;
3604 *ignore_acpi_ev = false;
3605
3606 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003607 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003608 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003609 /* recommended action: do nothing, we don't have
3610 * Lenovo ATM information */
3611 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003612 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003613 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003614 /* recommended action: warn user through gui */
3615 break;
3616 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003617 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003618 /* recommended action: immediate sleep/hibernate */
3619 break;
3620 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003621 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003622 "a sensor reports something is too hot!\n");
3623 /* recommended action: warn user through gui, that */
3624 /* some internal component is too hot */
3625 break;
3626 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003627 pr_alert("THERMAL EMERGENCY: "
3628 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003629 /* recommended action: immediate sleep/hibernate */
3630 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003631 case TP_HKEY_EV_AC_CHANGED:
3632 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3633 * AC status changed; can be triggered by plugging or
3634 * unplugging AC adapter, docking or undocking. */
3635
3636 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003637
3638 case TP_HKEY_EV_KEY_NUMLOCK:
3639 case TP_HKEY_EV_KEY_FN:
3640 /* key press events, we just ignore them as long as the EC
3641 * is still reporting them in the normal keyboard stream */
3642 *send_acpi_ev = false;
3643 *ignore_acpi_ev = true;
3644 return true;
3645
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003646 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003647 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003648 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003649 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003650
3651 thermal_dump_all_sensors();
3652
3653 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003654}
3655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003656static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3657{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003658 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003659 bool send_acpi_ev;
3660 bool ignore_acpi_ev;
3661 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003662
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003663 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003664 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003665 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003666 acpi_bus_generate_netlink_event(
3667 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003668 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003669 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003670 return;
3671 }
3672
3673 while (1) {
3674 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003675 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003676 return;
3677 }
3678
3679 if (hkey == 0) {
3680 /* queue empty */
3681 return;
3682 }
3683
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003684 send_acpi_ev = true;
3685 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003686
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003687 switch (hkey >> 12) {
3688 case 1:
3689 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003690 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003692 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003693 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003694 /* 0x2000-0x2FFF: Wakeup reason */
3695 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3696 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003697 break;
3698 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003699 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003700 switch (hkey) {
3701 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003702 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003703 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003704 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003705 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003706 break;
3707 case TP_HKEY_EV_OPTDRV_EJ:
3708 /* FIXME: kick libata if SATA link offline */
3709 known_ev = true;
3710 break;
3711 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003712 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003713 }
3714 break;
3715 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003716 /* 0x4000-0x4FFF: dock-related events */
3717 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3718 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003719 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003720 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003721 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003722 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3723 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003724 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003725 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003726 /* 0x6000-0x6FFF: thermal alarms/notices and
3727 * keyboard events */
3728 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003729 &ignore_acpi_ev);
3730 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003731 case 7:
3732 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003733 if (tp_features.hotkey_wlsw &&
3734 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003735 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003736 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003737 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003738 break;
3739 }
3740 /* fallthrough to default */
3741 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003742 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003743 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003744 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003745 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3746 pr_notice("please report the conditions when this "
3747 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003748 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003749
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003750 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003751 if (!ignore_acpi_ev &&
3752 (send_acpi_ev || hotkey_report_mode < 2)) {
3753 acpi_bus_generate_proc_event(ibm->acpi->device,
3754 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003755 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003756
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003757 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003758 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003759 acpi_bus_generate_netlink_event(
3760 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003761 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003762 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003763 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003764 }
3765}
3766
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003767static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003768{
3769 /* Do these on suspend, we get the events on early resume! */
3770 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3771 hotkey_autosleep_ack = 0;
3772}
3773
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003774static void hotkey_resume(void)
3775{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003776 tpacpi_disable_brightness_delay();
3777
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003778 if (hotkey_status_set(true) < 0 ||
3779 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003780 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003781 "firmware interface\n");
3782
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003783 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003784 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003785 hotkey_wakeup_reason_notify_change();
3786 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003787 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003788}
3789
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003790/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003791static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003793 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003795 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003796 seq_printf(m, "status:\t\tnot supported\n");
3797 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003798 }
3799
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003800 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003801 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003802 res = hotkey_status_get(&status);
3803 if (!res)
3804 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003805 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003806 if (res)
3807 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003809 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003810 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003811 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3812 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003814 seq_printf(m, "mask:\t\tnot supported\n");
3815 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 }
3817
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003821static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003822{
3823 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003824 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003825 pr_fmt("hotkey enable/disable functionality has been "
3826 "removed from the driver. "
3827 "Hotkeys are always enabled.\n")))
3828 pr_err("Please remove the hotkey=enable module "
3829 "parameter, it is deprecated. "
3830 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003831}
3832
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003833static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003835 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003836 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003839 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return -ENODEV;
3841
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003842 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003843 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003844
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003845 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003846
3847 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 while ((cmd = next_cmd(&buf))) {
3849 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003850 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003852 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003853 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa42009-09-12 15:22:16 -03003855 mask = (hotkey_all_mask | hotkey_source_mask)
3856 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3858 /* mask set */
3859 } else if (sscanf(cmd, "%x", &mask) == 1) {
3860 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003861 } else {
3862 res = -EINVAL;
3863 goto errexit;
3864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003866
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003867 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003868 tpacpi_disclose_usertask("procfs hotkey",
3869 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003870 res = hotkey_user_mask_set(mask);
3871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003873errexit:
3874 mutex_unlock(&hotkey_mutex);
3875 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003878static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003879 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3880 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003881 {"", 0},
3882};
3883
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003884static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003885 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003886 .notify = hotkey_notify,
3887 .handle = &hkey_handle,
3888 .type = ACPI_DEVICE_NOTIFY,
3889};
3890
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003891static struct ibm_struct hotkey_driver_data = {
3892 .name = "hotkey",
3893 .read = hotkey_read,
3894 .write = hotkey_write,
3895 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003896 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003897 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003898 .acpi = &ibm_hotkey_acpidriver,
3899};
3900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003901/*************************************************************************
3902 * Bluetooth subdriver
3903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003905enum {
3906 /* ACPI GBDC/SBDC bits */
3907 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3908 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003909 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003910 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003911};
3912
3913enum {
3914 /* ACPI \BLTH commands */
3915 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3916 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3917 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3918 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3919 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003920};
3921
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003922#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3923
Johannes Berg19d337d2009-06-02 13:01:37 +02003924static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003925{
3926 int status;
3927
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003928#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3929 if (dbg_bluetoothemul)
3930 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003931 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003932#endif
3933
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003934 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3935 return -EIO;
3936
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003937 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003938 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003939}
3940
Johannes Berg19d337d2009-06-02 13:01:37 +02003941static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003942{
3943 int status;
3944
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003945 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003946 "will attempt to %s bluetooth\n",
3947 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003948
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003949#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3950 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003951 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003952 return 0;
3953 }
3954#endif
3955
Johannes Berg19d337d2009-06-02 13:01:37 +02003956 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003957 status = TP_ACPI_BLUETOOTH_RADIOSSW
3958 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3959 else
3960 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003961
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003962 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3963 return -EIO;
3964
3965 return 0;
3966}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003967
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003968/* sysfs bluetooth enable ---------------------------------------------- */
3969static ssize_t bluetooth_enable_show(struct device *dev,
3970 struct device_attribute *attr,
3971 char *buf)
3972{
Johannes Berg19d337d2009-06-02 13:01:37 +02003973 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3974 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003975}
3976
3977static ssize_t bluetooth_enable_store(struct device *dev,
3978 struct device_attribute *attr,
3979 const char *buf, size_t count)
3980{
Johannes Berg19d337d2009-06-02 13:01:37 +02003981 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3982 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003983}
3984
3985static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003986 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003987 bluetooth_enable_show, bluetooth_enable_store);
3988
3989/* --------------------------------------------------------------------- */
3990
3991static struct attribute *bluetooth_attributes[] = {
3992 &dev_attr_bluetooth_enable.attr,
3993 NULL
3994};
3995
3996static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003997 .attrs = bluetooth_attributes,
3998};
3999
Johannes Berg19d337d2009-06-02 13:01:37 +02004000static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4001 .get_status = bluetooth_get_status,
4002 .set_status = bluetooth_set_status,
4003};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004004
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004005static void bluetooth_shutdown(void)
4006{
4007 /* Order firmware to save current state to NVRAM */
4008 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4009 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004010 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004011 else
4012 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02004013 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004014}
4015
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004016static void bluetooth_exit(void)
4017{
4018 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4019 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004020
4021 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4022
4023 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004024}
4025
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004026static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004028 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004029 int status = 0;
4030
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004031 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4032 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004033
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004034 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004035
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004036 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4037 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004038 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004039 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004041 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4042 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004043 str_supported(tp_features.bluetooth),
4044 status);
4045
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004046#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4047 if (dbg_bluetoothemul) {
4048 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004049 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004050 } else
4051#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004052 if (tp_features.bluetooth &&
4053 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4054 /* no bluetooth hardware present in system */
4055 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004056 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004057 "bluetooth hardware not installed\n");
4058 }
4059
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004060 if (!tp_features.bluetooth)
4061 return 1;
4062
Johannes Berg19d337d2009-06-02 13:01:37 +02004063 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4064 &bluetooth_tprfk_ops,
4065 RFKILL_TYPE_BLUETOOTH,
4066 TPACPI_RFK_BLUETOOTH_SW_NAME,
4067 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004068 if (res)
4069 return res;
4070
Johannes Berg19d337d2009-06-02 13:01:37 +02004071 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4072 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004073 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004074 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004075 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004076 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004077
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004081/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004082static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004084 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004087static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
Johannes Berg19d337d2009-06-02 13:01:37 +02004089 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004092static struct ibm_struct bluetooth_driver_data = {
4093 .name = "bluetooth",
4094 .read = bluetooth_read,
4095 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004096 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004097 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004098};
4099
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004100/*************************************************************************
4101 * Wan subdriver
4102 */
4103
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004104enum {
4105 /* ACPI GWAN/SWAN bits */
4106 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4107 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004108 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004109 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004110};
4111
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004112#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4113
Johannes Berg19d337d2009-06-02 13:01:37 +02004114static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004115{
4116 int status;
4117
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004118#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4119 if (dbg_wwanemul)
4120 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004121 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004122#endif
4123
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004124 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4125 return -EIO;
4126
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004127 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004128 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004129}
4130
Johannes Berg19d337d2009-06-02 13:01:37 +02004131static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004132{
4133 int status;
4134
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004135 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004136 "will attempt to %s wwan\n",
4137 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004138
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004139#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4140 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004141 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004142 return 0;
4143 }
4144#endif
4145
Johannes Berg19d337d2009-06-02 13:01:37 +02004146 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004147 status = TP_ACPI_WANCARD_RADIOSSW
4148 | TP_ACPI_WANCARD_RESUMECTRL;
4149 else
4150 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004151
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004152 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4153 return -EIO;
4154
4155 return 0;
4156}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004157
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004158/* sysfs wan enable ---------------------------------------------------- */
4159static ssize_t wan_enable_show(struct device *dev,
4160 struct device_attribute *attr,
4161 char *buf)
4162{
Johannes Berg19d337d2009-06-02 13:01:37 +02004163 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4164 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004165}
4166
4167static ssize_t wan_enable_store(struct device *dev,
4168 struct device_attribute *attr,
4169 const char *buf, size_t count)
4170{
Johannes Berg19d337d2009-06-02 13:01:37 +02004171 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4172 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004173}
4174
4175static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004176 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004177 wan_enable_show, wan_enable_store);
4178
4179/* --------------------------------------------------------------------- */
4180
4181static struct attribute *wan_attributes[] = {
4182 &dev_attr_wan_enable.attr,
4183 NULL
4184};
4185
4186static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004187 .attrs = wan_attributes,
4188};
4189
Johannes Berg19d337d2009-06-02 13:01:37 +02004190static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4191 .get_status = wan_get_status,
4192 .set_status = wan_set_status,
4193};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004194
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004195static void wan_shutdown(void)
4196{
4197 /* Order firmware to save current state to NVRAM */
4198 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4199 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004200 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004201 else
4202 vdbg_printk(TPACPI_DBG_RFKILL,
4203 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004204}
4205
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004206static void wan_exit(void)
4207{
4208 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4209 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004210
4211 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4212
4213 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004214}
4215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004216static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004217{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004218 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004219 int status = 0;
4220
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4222 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004224 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004225
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004226 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004227 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004228
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004229 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4230 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004231 str_supported(tp_features.wan),
4232 status);
4233
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004234#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4235 if (dbg_wwanemul) {
4236 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004237 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004238 } else
4239#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004240 if (tp_features.wan &&
4241 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4242 /* no wan hardware present in system */
4243 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004244 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004245 "wan hardware not installed\n");
4246 }
4247
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004248 if (!tp_features.wan)
4249 return 1;
4250
Johannes Berg19d337d2009-06-02 13:01:37 +02004251 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4252 &wan_tprfk_ops,
4253 RFKILL_TYPE_WWAN,
4254 TPACPI_RFK_WWAN_SW_NAME,
4255 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004256 if (res)
4257 return res;
4258
Johannes Berg19d337d2009-06-02 13:01:37 +02004259 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4260 &wan_attr_group);
4261
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004262 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004263 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004264 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03004265 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004266
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004267 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004268}
4269
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004270/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004271static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004272{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004273 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004274}
4275
4276static int wan_write(char *buf)
4277{
Johannes Berg19d337d2009-06-02 13:01:37 +02004278 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004279}
4280
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004281static struct ibm_struct wan_driver_data = {
4282 .name = "wan",
4283 .read = wan_read,
4284 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004285 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004286 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004287};
4288
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004289/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004290 * UWB subdriver
4291 */
4292
4293enum {
4294 /* ACPI GUWB/SUWB bits */
4295 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4296 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4297};
4298
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004299#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4300
Johannes Berg19d337d2009-06-02 13:01:37 +02004301static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004302{
4303 int status;
4304
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004305#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4306 if (dbg_uwbemul)
4307 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004308 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004309#endif
4310
4311 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4312 return -EIO;
4313
4314 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004315 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004316}
4317
Johannes Berg19d337d2009-06-02 13:01:37 +02004318static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004319{
4320 int status;
4321
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004322 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004323 "will attempt to %s UWB\n",
4324 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004325
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004326#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4327 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004328 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004329 return 0;
4330 }
4331#endif
4332
Johannes Berg19d337d2009-06-02 13:01:37 +02004333 if (state == TPACPI_RFK_RADIO_ON)
4334 status = TP_ACPI_UWB_RADIOSSW;
4335 else
4336 status = 0;
4337
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004338 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4339 return -EIO;
4340
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004341 return 0;
4342}
4343
4344/* --------------------------------------------------------------------- */
4345
Johannes Berg19d337d2009-06-02 13:01:37 +02004346static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4347 .get_status = uwb_get_status,
4348 .set_status = uwb_set_status,
4349};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004350
4351static void uwb_exit(void)
4352{
Johannes Berg19d337d2009-06-02 13:01:37 +02004353 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004354}
4355
4356static int __init uwb_init(struct ibm_init_struct *iibm)
4357{
4358 int res;
4359 int status = 0;
4360
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004361 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4362 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004363
4364 TPACPI_ACPIHANDLE_INIT(hkey);
4365
4366 tp_features.uwb = hkey_handle &&
4367 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4368
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004369 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4370 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004371 str_supported(tp_features.uwb),
4372 status);
4373
4374#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4375 if (dbg_uwbemul) {
4376 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004377 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004378 } else
4379#endif
4380 if (tp_features.uwb &&
4381 !(status & TP_ACPI_UWB_HWPRESENT)) {
4382 /* no uwb hardware present in system */
4383 tp_features.uwb = 0;
4384 dbg_printk(TPACPI_DBG_INIT,
4385 "uwb hardware not installed\n");
4386 }
4387
4388 if (!tp_features.uwb)
4389 return 1;
4390
4391 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004392 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004393 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004394 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004395 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004396 return res;
4397}
4398
4399static struct ibm_struct uwb_driver_data = {
4400 .name = "uwb",
4401 .exit = uwb_exit,
4402 .flags.experimental = 1,
4403};
4404
4405/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406 * Video subdriver
4407 */
4408
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004409#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4410
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004411enum video_access_mode {
4412 TPACPI_VIDEO_NONE = 0,
4413 TPACPI_VIDEO_570, /* 570 */
4414 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4415 TPACPI_VIDEO_NEW, /* all others */
4416};
4417
4418enum { /* video status flags, based on VIDEO_570 */
4419 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4420 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4421 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4422};
4423
4424enum { /* TPACPI_VIDEO_570 constants */
4425 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4426 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4427 * video_status_flags */
4428 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4429 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4430};
4431
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004432static enum video_access_mode video_supported;
4433static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004435static int video_autosw_get(void);
4436static int video_autosw_set(int enable);
4437
Joe Perches112a6ee2011-04-04 10:06:24 -07004438TPACPI_HANDLE(vid, root,
4439 "\\_SB.PCI.AGP.VGA", /* 570 */
4440 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4441 "\\_SB.PCI0.VID0", /* 770e */
4442 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4443 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4444 "\\_SB.PCI0.AGP.VID", /* all others */
4445 ); /* R30, R31 */
4446
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004447TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004448
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004449static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004451 int ivga;
4452
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004453 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4454
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004455 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004456 if (tpacpi_is_ibm())
4457 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004458
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004459 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4460 /* G41, assume IVGA doesn't change */
4461 vid_handle = vid2_handle;
4462
4463 if (!vid_handle)
4464 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004465 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004466 else if (tpacpi_is_ibm() &&
4467 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004468 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004469 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004470 else if (tpacpi_is_ibm() &&
4471 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004472 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004473 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004474 else
4475 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004476 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004478 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4479 str_supported(video_supported != TPACPI_VIDEO_NONE),
4480 video_supported);
4481
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004482 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
4484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004485static void video_exit(void)
4486{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004487 dbg_printk(TPACPI_DBG_EXIT,
4488 "restoring original video autoswitch mode\n");
4489 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004490 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004491 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004492}
4493
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004494static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
4496 int status = 0;
4497 int i;
4498
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004499 switch (video_supported) {
4500 case TPACPI_VIDEO_570:
4501 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4502 TP_ACPI_VIDEO_570_PHSCMD))
4503 return -EIO;
4504 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4505 break;
4506 case TPACPI_VIDEO_770:
4507 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4508 return -EIO;
4509 if (i)
4510 status |= TP_ACPI_VIDEO_S_LCD;
4511 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4512 return -EIO;
4513 if (i)
4514 status |= TP_ACPI_VIDEO_S_CRT;
4515 break;
4516 case TPACPI_VIDEO_NEW:
4517 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4518 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4519 return -EIO;
4520 if (i)
4521 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004523 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4524 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4525 return -EIO;
4526 if (i)
4527 status |= TP_ACPI_VIDEO_S_LCD;
4528 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4529 return -EIO;
4530 if (i)
4531 status |= TP_ACPI_VIDEO_S_DVI;
4532 break;
4533 default:
4534 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 return status;
4538}
4539
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004540static int video_outputsw_set(int status)
4541{
4542 int autosw;
4543 int res = 0;
4544
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 res = acpi_evalf(NULL, NULL,
4548 "\\_SB.PHS2", "vdd",
4549 TP_ACPI_VIDEO_570_PHS2CMD,
4550 status | TP_ACPI_VIDEO_570_PHS2SET);
4551 break;
4552 case TPACPI_VIDEO_770:
4553 autosw = video_autosw_get();
4554 if (autosw < 0)
4555 return autosw;
4556
4557 res = video_autosw_set(1);
4558 if (res)
4559 return res;
4560 res = acpi_evalf(vid_handle, NULL,
4561 "ASWT", "vdd", status * 0x100, 0);
4562 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004563 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004564 return -EIO;
4565 }
4566 break;
4567 case TPACPI_VIDEO_NEW:
4568 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004569 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004570 break;
4571 default:
4572 return -ENOSYS;
4573 }
4574
4575 return (res)? 0 : -EIO;
4576}
4577
4578static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004580 int autosw = 0;
4581
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004582 switch (video_supported) {
4583 case TPACPI_VIDEO_570:
4584 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4585 return -EIO;
4586 break;
4587 case TPACPI_VIDEO_770:
4588 case TPACPI_VIDEO_NEW:
4589 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4590 return -EIO;
4591 break;
4592 default:
4593 return -ENOSYS;
4594 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004595
4596 return autosw & 1;
4597}
4598
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004599static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004600{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004601 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004602 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004603 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004604}
4605
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004606static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004607{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004608 int autosw = video_autosw_get();
4609 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004610
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004611 if (autosw < 0)
4612 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004613
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004614 switch (video_supported) {
4615 case TPACPI_VIDEO_570:
4616 res = video_autosw_set(1);
4617 if (res)
4618 return res;
4619 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4620 break;
4621 case TPACPI_VIDEO_770:
4622 case TPACPI_VIDEO_NEW:
4623 res = video_autosw_set(1);
4624 if (res)
4625 return res;
4626 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4627 break;
4628 default:
4629 return -ENOSYS;
4630 }
4631 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004632 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004633 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004634 }
4635
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004636 return (res)? 0 : -EIO;
4637}
4638
4639static int video_expand_toggle(void)
4640{
4641 switch (video_supported) {
4642 case TPACPI_VIDEO_570:
4643 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4644 0 : -EIO;
4645 case TPACPI_VIDEO_770:
4646 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4647 0 : -EIO;
4648 case TPACPI_VIDEO_NEW:
4649 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4650 0 : -EIO;
4651 default:
4652 return -ENOSYS;
4653 }
4654 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004655}
4656
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004657static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004658{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004659 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004660
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004661 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004662 seq_printf(m, "status:\t\tnot supported\n");
4663 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004664 }
4665
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004666 /* Even reads can crash X.org, so... */
4667 if (!capable(CAP_SYS_ADMIN))
4668 return -EPERM;
4669
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004670 status = video_outputsw_get();
4671 if (status < 0)
4672 return status;
4673
4674 autosw = video_autosw_get();
4675 if (autosw < 0)
4676 return autosw;
4677
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004678 seq_printf(m, "status:\t\tsupported\n");
4679 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4680 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004681 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004682 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4683 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4684 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4685 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004686 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004687 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4688 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4689 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004690
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004691 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004692}
4693
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004694static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
4696 char *cmd;
4697 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004698 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004700 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004701 return -ENODEV;
4702
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004703 /* Even reads can crash X.org, let alone writes... */
4704 if (!capable(CAP_SYS_ADMIN))
4705 return -EPERM;
4706
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004707 enable = 0;
4708 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 while ((cmd = next_cmd(&buf))) {
4711 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004712 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004714 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004716 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004718 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004719 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004720 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004721 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004722 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004723 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004724 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004726 res = video_autosw_set(1);
4727 if (res)
4728 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004730 res = video_autosw_set(0);
4731 if (res)
4732 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004734 res = video_outputsw_cycle();
4735 if (res)
4736 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004738 res = video_expand_toggle();
4739 if (res)
4740 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 } else
4742 return -EINVAL;
4743 }
4744
4745 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004746 status = video_outputsw_get();
4747 if (status < 0)
4748 return status;
4749 res = video_outputsw_set((status & ~disable) | enable);
4750 if (res)
4751 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 }
4753
4754 return 0;
4755}
4756
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004757static struct ibm_struct video_driver_data = {
4758 .name = "video",
4759 .read = video_read,
4760 .write = video_write,
4761 .exit = video_exit,
4762};
4763
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004764#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4765
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004766/*************************************************************************
4767 * Light (thinklight) subdriver
4768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004770TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4771TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004772
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004773static int light_get_status(void)
4774{
4775 int status = 0;
4776
4777 if (tp_features.light_status) {
4778 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4779 return -EIO;
4780 return (!!status);
4781 }
4782
4783 return -ENXIO;
4784}
4785
4786static int light_set_status(int status)
4787{
4788 int rc;
4789
4790 if (tp_features.light) {
4791 if (cmos_handle) {
4792 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4793 (status)?
4794 TP_CMOS_THINKLIGHT_ON :
4795 TP_CMOS_THINKLIGHT_OFF);
4796 } else {
4797 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4798 (status)? 1 : 0);
4799 }
4800 return (rc)? 0 : -EIO;
4801 }
4802
4803 return -ENXIO;
4804}
4805
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004806static void light_set_status_worker(struct work_struct *work)
4807{
4808 struct tpacpi_led_classdev *data =
4809 container_of(work, struct tpacpi_led_classdev, work);
4810
4811 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004812 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004813}
4814
4815static void light_sysfs_set(struct led_classdev *led_cdev,
4816 enum led_brightness brightness)
4817{
4818 struct tpacpi_led_classdev *data =
4819 container_of(led_cdev,
4820 struct tpacpi_led_classdev,
4821 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004822 data->new_state = (brightness != LED_OFF) ?
4823 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004824 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004825}
4826
4827static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4828{
4829 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4830}
4831
4832static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4833 .led_classdev = {
4834 .name = "tpacpi::thinklight",
4835 .brightness_set = &light_sysfs_set,
4836 .brightness_get = &light_sysfs_get,
4837 }
4838};
4839
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004840static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004842 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004843
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004844 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4845
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004846 if (tpacpi_is_ibm()) {
4847 TPACPI_ACPIHANDLE_INIT(ledb);
4848 TPACPI_ACPIHANDLE_INIT(lght);
4849 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004850 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004851 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004852
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004853 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004854 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004855
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004856 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857 /* light status not supported on
4858 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004859 tp_features.light_status =
4860 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004862 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4863 str_supported(tp_features.light),
4864 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004865
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004866 if (!tp_features.light)
4867 return 1;
4868
4869 rc = led_classdev_register(&tpacpi_pdev->dev,
4870 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004871
4872 if (rc < 0) {
4873 tp_features.light = 0;
4874 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004875 } else {
4876 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004877 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004878
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004879 return rc;
4880}
4881
4882static void light_exit(void)
4883{
4884 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08004885 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004888static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004890 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004892 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004893 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004894 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004895 seq_printf(m, "status:\t\tunknown\n");
4896 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004897 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004898 status = light_get_status();
4899 if (status < 0)
4900 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004901 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4902 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004908static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004911 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004912
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004913 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004914 return -ENODEV;
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 while ((cmd = next_cmd(&buf))) {
4917 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004918 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004920 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 } else
4922 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 }
4924
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004925 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926}
4927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004928static struct ibm_struct light_driver_data = {
4929 .name = "light",
4930 .read = light_read,
4931 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004932 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004933};
4934
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004935/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004936 * CMOS subdriver
4937 */
4938
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004939/* sysfs cmos_command -------------------------------------------------- */
4940static ssize_t cmos_command_store(struct device *dev,
4941 struct device_attribute *attr,
4942 const char *buf, size_t count)
4943{
4944 unsigned long cmos_cmd;
4945 int res;
4946
4947 if (parse_strtoul(buf, 21, &cmos_cmd))
4948 return -EINVAL;
4949
4950 res = issue_thinkpad_cmos_command(cmos_cmd);
4951 return (res)? res : count;
4952}
4953
4954static struct device_attribute dev_attr_cmos_command =
4955 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4956
4957/* --------------------------------------------------------------------- */
4958
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004959static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004960{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004961 int res;
4962
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004963 vdbg_printk(TPACPI_DBG_INIT,
4964 "initializing cmos commands subdriver\n");
4965
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004966 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004967
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004968 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4969 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004970
4971 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4972 if (res)
4973 return res;
4974
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004975 return (cmos_handle)? 0 : 1;
4976}
4977
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004978static void cmos_exit(void)
4979{
4980 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4981}
4982
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004983static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004985 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4986 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004988 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004990 seq_printf(m, "status:\t\tsupported\n");
4991 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 }
4993
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
4996
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004997static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
4999 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005000 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005003 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5004 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 /* cmos_cmd set */
5006 } else
5007 return -EINVAL;
5008
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005009 res = issue_thinkpad_cmos_command(cmos_cmd);
5010 if (res)
5011 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
5013
5014 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005015}
5016
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005017static struct ibm_struct cmos_driver_data = {
5018 .name = "cmos",
5019 .read = cmos_read,
5020 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03005021 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005022};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005023
5024/*************************************************************************
5025 * LED subdriver
5026 */
5027
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005028enum led_access_mode {
5029 TPACPI_LED_NONE = 0,
5030 TPACPI_LED_570, /* 570 */
5031 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5032 TPACPI_LED_NEW, /* all others */
5033};
5034
5035enum { /* For TPACPI_LED_OLD */
5036 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5037 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5038 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5039};
5040
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005041static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005042
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005043static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005044
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005045#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005046static struct tpacpi_led_classdev *tpacpi_leds;
5047static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005048static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005049 /* there's a limit of 19 chars + NULL before 2.6.26 */
5050 "tpacpi::power",
5051 "tpacpi:orange:batt",
5052 "tpacpi:green:batt",
5053 "tpacpi::dock_active",
5054 "tpacpi::bay_active",
5055 "tpacpi::dock_batt",
5056 "tpacpi::unknown_led",
5057 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005058 "tpacpi::dock_status1",
5059 "tpacpi::dock_status2",
5060 "tpacpi::unknown_led2",
5061 "tpacpi::unknown_led3",
5062 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005063};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005064#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005065
5066static inline bool tpacpi_is_led_restricted(const unsigned int led)
5067{
5068#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5069 return false;
5070#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005071 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005072#endif
5073}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005074
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005075static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005076{
5077 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005078 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005079
5080 switch (led_supported) {
5081 case TPACPI_LED_570:
5082 if (!acpi_evalf(ec_handle,
5083 &status, "GLED", "dd", 1 << led))
5084 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005085 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005086 TPACPI_LED_OFF :
5087 ((status == 1)?
5088 TPACPI_LED_ON :
5089 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005090 tpacpi_led_state_cache[led] = led_s;
5091 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005092 default:
5093 return -ENXIO;
5094 }
5095
5096 /* not reached */
5097}
5098
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005099static int led_set_status(const unsigned int led,
5100 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005101{
5102 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005103 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5104 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005105
5106 int rc = 0;
5107
5108 switch (led_supported) {
5109 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005110 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005111 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005112 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005113 if (unlikely(tpacpi_is_led_restricted(led)))
5114 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005115 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5116 (1 << led), led_sled_arg1[ledstatus]))
5117 rc = -EIO;
5118 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005119 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005120 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005121 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005122 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005123 if (unlikely(tpacpi_is_led_restricted(led)))
5124 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005125 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5126 if (rc >= 0)
5127 rc = ec_write(TPACPI_LED_EC_HLBL,
5128 (ledstatus == TPACPI_LED_BLINK) << led);
5129 if (rc >= 0)
5130 rc = ec_write(TPACPI_LED_EC_HLCL,
5131 (ledstatus != TPACPI_LED_OFF) << led);
5132 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005133 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005134 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005135 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5136 return -EINVAL;
5137 if (unlikely(tpacpi_is_led_restricted(led)))
5138 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005139 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5140 led, led_led_arg1[ledstatus]))
5141 rc = -EIO;
5142 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005143 default:
5144 rc = -ENXIO;
5145 }
5146
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005147 if (!rc)
5148 tpacpi_led_state_cache[led] = ledstatus;
5149
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005150 return rc;
5151}
5152
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005153static void led_set_status_worker(struct work_struct *work)
5154{
5155 struct tpacpi_led_classdev *data =
5156 container_of(work, struct tpacpi_led_classdev, work);
5157
5158 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005159 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005160}
5161
5162static void led_sysfs_set(struct led_classdev *led_cdev,
5163 enum led_brightness brightness)
5164{
5165 struct tpacpi_led_classdev *data = container_of(led_cdev,
5166 struct tpacpi_led_classdev, led_classdev);
5167
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005168 if (brightness == LED_OFF)
5169 data->new_state = TPACPI_LED_OFF;
5170 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5171 data->new_state = TPACPI_LED_ON;
5172 else
5173 data->new_state = TPACPI_LED_BLINK;
5174
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005175 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005176}
5177
5178static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5179 unsigned long *delay_on, unsigned long *delay_off)
5180{
5181 struct tpacpi_led_classdev *data = container_of(led_cdev,
5182 struct tpacpi_led_classdev, led_classdev);
5183
5184 /* Can we choose the flash rate? */
5185 if (*delay_on == 0 && *delay_off == 0) {
5186 /* yes. set them to the hardware blink rate (1 Hz) */
5187 *delay_on = 500; /* ms */
5188 *delay_off = 500; /* ms */
5189 } else if ((*delay_on != 500) || (*delay_off != 500))
5190 return -EINVAL;
5191
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005192 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005193 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005194
5195 return 0;
5196}
5197
5198static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5199{
5200 int rc;
5201
5202 struct tpacpi_led_classdev *data = container_of(led_cdev,
5203 struct tpacpi_led_classdev, led_classdev);
5204
5205 rc = led_get_status(data->led);
5206
5207 if (rc == TPACPI_LED_OFF || rc < 0)
5208 rc = LED_OFF; /* no error handling in led class :( */
5209 else
5210 rc = LED_FULL;
5211
5212 return rc;
5213}
5214
5215static void led_exit(void)
5216{
5217 unsigned int i;
5218
5219 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5220 if (tpacpi_leds[i].led_classdev.name)
5221 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5222 }
5223
Li Dongyange03e3892012-07-25 10:45:07 +10005224 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005225 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005226}
5227
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005228static int __init tpacpi_init_led(unsigned int led)
5229{
5230 int rc;
5231
5232 tpacpi_leds[led].led = led;
5233
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005234 /* LEDs with no name don't get registered */
5235 if (!tpacpi_led_names[led])
5236 return 0;
5237
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005238 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5239 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5240 if (led_supported == TPACPI_LED_570)
5241 tpacpi_leds[led].led_classdev.brightness_get =
5242 &led_sysfs_get;
5243
5244 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5245
5246 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5247
5248 rc = led_classdev_register(&tpacpi_pdev->dev,
5249 &tpacpi_leds[led].led_classdev);
5250 if (rc < 0)
5251 tpacpi_leds[led].led_classdev.name = NULL;
5252
5253 return rc;
5254}
5255
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005256static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5257 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5258 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5259 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5260
5261 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5262 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5263 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5264 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5265 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5266 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5267 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5268 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5269
5270 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5271 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5272 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5273 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5274 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5275
5276 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5277 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5278 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5279 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5280
5281 /* (1) - may have excess leds enabled on MSB */
5282
5283 /* Defaults (order matters, keep last, don't reorder!) */
5284 { /* Lenovo */
5285 .vendor = PCI_VENDOR_ID_LENOVO,
5286 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5287 .quirks = 0x1fffU,
5288 },
5289 { /* IBM ThinkPads with no EC version string */
5290 .vendor = PCI_VENDOR_ID_IBM,
5291 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5292 .quirks = 0x00ffU,
5293 },
5294 { /* IBM ThinkPads with EC version string */
5295 .vendor = PCI_VENDOR_ID_IBM,
5296 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5297 .quirks = 0x00bfU,
5298 },
5299};
5300
5301#undef TPACPI_LEDQ_IBM
5302#undef TPACPI_LEDQ_LNV
5303
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005304static enum led_access_mode __init led_init_detect_mode(void)
5305{
5306 acpi_status status;
5307
5308 if (tpacpi_is_ibm()) {
5309 /* 570 */
5310 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5311 if (ACPI_SUCCESS(status))
5312 return TPACPI_LED_570;
5313
5314 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5315 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5316 if (ACPI_SUCCESS(status))
5317 return TPACPI_LED_OLD;
5318 }
5319
5320 /* most others */
5321 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5322 if (ACPI_SUCCESS(status))
5323 return TPACPI_LED_NEW;
5324
5325 /* R30, R31, and unknown firmwares */
5326 led_handle = NULL;
5327 return TPACPI_LED_NONE;
5328}
5329
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005330static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005331{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005332 unsigned int i;
5333 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005334 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005335
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005336 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5337
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005338 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005339
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005340 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5341 str_supported(led_supported), led_supported);
5342
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005343 if (led_supported == TPACPI_LED_NONE)
5344 return 1;
5345
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005346 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5347 GFP_KERNEL);
5348 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005349 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005350 return -ENOMEM;
5351 }
5352
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005353 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5354 ARRAY_SIZE(led_useful_qtable));
5355
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005356 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005357 if (!tpacpi_is_led_restricted(i) &&
5358 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005359 rc = tpacpi_init_led(i);
5360 if (rc < 0) {
5361 led_exit();
5362 return rc;
5363 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005364 }
5365 }
5366
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005367#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005368 pr_notice("warning: userspace override of important "
5369 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005370#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005371 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005372}
5373
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005374#define str_led_status(s) \
5375 ((s) == TPACPI_LED_OFF ? "off" : \
5376 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005377
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005378static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005380 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005381 seq_printf(m, "status:\t\tnot supported\n");
5382 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005383 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005384 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005385
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005386 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005387 /* 570 */
5388 int i, status;
5389 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005390 status = led_get_status(i);
5391 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005392 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005393 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005394 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005395 }
5396 }
5397
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005398 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005399 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005404static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
5406 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005407 int led, rc;
5408 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005409
5410 if (!led_supported)
5411 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
5413 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005414 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return -EINVAL;
5416
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005417 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005418 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005420 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005421 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005422 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005423 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005424 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005425 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005426
5427 rc = led_set_status(led, s);
5428 if (rc < 0)
5429 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
5431
5432 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005433}
5434
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005435static struct ibm_struct led_driver_data = {
5436 .name = "led",
5437 .read = led_read,
5438 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005439 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005440};
5441
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005442/*************************************************************************
5443 * Beep subdriver
5444 */
5445
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005446TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005447
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005448#define TPACPI_BEEP_Q1 0x0001
5449
5450static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5451 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5452 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5453};
5454
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005455static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005456{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005457 unsigned long quirks;
5458
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005459 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5460
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005461 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005462
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005463 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5464 str_supported(beep_handle != NULL));
5465
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005466 quirks = tpacpi_check_quirks(beep_quirk_table,
5467 ARRAY_SIZE(beep_quirk_table));
5468
5469 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5470
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005471 return (beep_handle)? 0 : 1;
5472}
5473
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005474static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005476 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005477 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005478 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005479 seq_printf(m, "status:\t\tsupported\n");
5480 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484}
5485
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005486static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487{
5488 char *cmd;
5489 int beep_cmd;
5490
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005491 if (!beep_handle)
5492 return -ENODEV;
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5496 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 /* beep_cmd set */
5498 } else
5499 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005500 if (tp_features.beep_needs_two_args) {
5501 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5502 beep_cmd, 0))
5503 return -EIO;
5504 } else {
5505 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5506 beep_cmd))
5507 return -EIO;
5508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 }
5510
5511 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005512}
5513
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005514static struct ibm_struct beep_driver_data = {
5515 .name = "beep",
5516 .read = beep_read,
5517 .write = beep_write,
5518};
5519
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005520/*************************************************************************
5521 * Thermal subdriver
5522 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005523
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005524enum thermal_access_mode {
5525 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5526 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5527 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5528 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5529 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5530};
5531
5532enum { /* TPACPI_THERMAL_TPEC_* */
5533 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5534 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5535 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005536
5537 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005538};
5539
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005540
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005541#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5542struct ibm_thermal_sensors_struct {
5543 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5544};
5545
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005546static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005547
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005548/* idx is zero-based */
5549static int thermal_get_sensor(int idx, s32 *value)
5550{
5551 int t;
5552 s8 tmp;
5553 char tmpi[5];
5554
5555 t = TP_EC_THERMAL_TMP0;
5556
5557 switch (thermal_read_mode) {
5558#if TPACPI_MAX_THERMAL_SENSORS >= 16
5559 case TPACPI_THERMAL_TPEC_16:
5560 if (idx >= 8 && idx <= 15) {
5561 t = TP_EC_THERMAL_TMP8;
5562 idx -= 8;
5563 }
5564 /* fallthrough */
5565#endif
5566 case TPACPI_THERMAL_TPEC_8:
5567 if (idx <= 7) {
5568 if (!acpi_ec_read(t + idx, &tmp))
5569 return -EIO;
5570 *value = tmp * 1000;
5571 return 0;
5572 }
5573 break;
5574
5575 case TPACPI_THERMAL_ACPI_UPDT:
5576 if (idx <= 7) {
5577 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5578 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5579 return -EIO;
5580 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5581 return -EIO;
5582 *value = (t - 2732) * 100;
5583 return 0;
5584 }
5585 break;
5586
5587 case TPACPI_THERMAL_ACPI_TMP07:
5588 if (idx <= 7) {
5589 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5590 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5591 return -EIO;
5592 if (t > 127 || t < -127)
5593 t = TP_EC_THERMAL_TMP_NA;
5594 *value = t * 1000;
5595 return 0;
5596 }
5597 break;
5598
5599 case TPACPI_THERMAL_NONE:
5600 default:
5601 return -ENOSYS;
5602 }
5603
5604 return -EINVAL;
5605}
5606
5607static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5608{
5609 int res, i;
5610 int n;
5611
5612 n = 8;
5613 i = 0;
5614
5615 if (!s)
5616 return -EINVAL;
5617
5618 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5619 n = 16;
5620
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005621 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005622 res = thermal_get_sensor(i, &s->temp[i]);
5623 if (res)
5624 return res;
5625 }
5626
5627 return n;
5628}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005629
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005630static void thermal_dump_all_sensors(void)
5631{
5632 int n, i;
5633 struct ibm_thermal_sensors_struct t;
5634
5635 n = thermal_get_sensors(&t);
5636 if (n <= 0)
5637 return;
5638
Joe Perches0978e012011-04-04 10:06:25 -07005639 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005640
5641 for (i = 0; i < n; i++) {
5642 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005643 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005644 else
Joe Perches0978e012011-04-04 10:06:25 -07005645 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005646 }
5647
Joe Perches0978e012011-04-04 10:06:25 -07005648 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005649}
5650
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005651/* sysfs temp##_input -------------------------------------------------- */
5652
5653static ssize_t thermal_temp_input_show(struct device *dev,
5654 struct device_attribute *attr,
5655 char *buf)
5656{
5657 struct sensor_device_attribute *sensor_attr =
5658 to_sensor_dev_attr(attr);
5659 int idx = sensor_attr->index;
5660 s32 value;
5661 int res;
5662
5663 res = thermal_get_sensor(idx, &value);
5664 if (res)
5665 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005666 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005667 return -ENXIO;
5668
5669 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5670}
5671
5672#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005673 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5674 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005675
5676static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5677 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5678 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5679 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5680 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5681 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5682 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5683 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5684 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5685 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5686 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5687 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5688 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5689 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5690 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5691 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5692 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5693};
5694
5695#define THERMAL_ATTRS(X) \
5696 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5697
5698static struct attribute *thermal_temp_input_attr[] = {
5699 THERMAL_ATTRS(8),
5700 THERMAL_ATTRS(9),
5701 THERMAL_ATTRS(10),
5702 THERMAL_ATTRS(11),
5703 THERMAL_ATTRS(12),
5704 THERMAL_ATTRS(13),
5705 THERMAL_ATTRS(14),
5706 THERMAL_ATTRS(15),
5707 THERMAL_ATTRS(0),
5708 THERMAL_ATTRS(1),
5709 THERMAL_ATTRS(2),
5710 THERMAL_ATTRS(3),
5711 THERMAL_ATTRS(4),
5712 THERMAL_ATTRS(5),
5713 THERMAL_ATTRS(6),
5714 THERMAL_ATTRS(7),
5715 NULL
5716};
5717
5718static const struct attribute_group thermal_temp_input16_group = {
5719 .attrs = thermal_temp_input_attr
5720};
5721
5722static const struct attribute_group thermal_temp_input8_group = {
5723 .attrs = &thermal_temp_input_attr[8]
5724};
5725
5726#undef THERMAL_SENSOR_ATTR_TEMP
5727#undef THERMAL_ATTRS
5728
5729/* --------------------------------------------------------------------- */
5730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005731static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005732{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005733 u8 t, ta1, ta2;
5734 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005735 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005736 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005737
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005738 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5739
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005740 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005741
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005742 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005743 /*
5744 * Direct EC access mode: sensors at registers
5745 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5746 * non-implemented, thermal sensors return 0x80 when
5747 * not available
5748 */
5749
5750 ta1 = ta2 = 0;
5751 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005752 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005753 ta1 |= t;
5754 } else {
5755 ta1 = 0;
5756 break;
5757 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005758 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005759 ta2 |= t;
5760 } else {
5761 ta1 = 0;
5762 break;
5763 }
5764 }
5765 if (ta1 == 0) {
5766 /* This is sheer paranoia, but we handle it anyway */
5767 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005768 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005769 "falling back to ACPI TMPx access "
5770 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005771 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005772 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005773 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005774 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005775 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005776 }
5777 } else {
5778 thermal_read_mode =
5779 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005780 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005781 }
5782 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005783 if (tpacpi_is_ibm() &&
5784 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005785 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005786 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005787 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005788 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005789 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005790 }
5791 } else {
5792 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005793 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005794 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005795
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005796 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5797 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5798 thermal_read_mode);
5799
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005800 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005801 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005802 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005803 &thermal_temp_input16_group);
5804 if (res)
5805 return res;
5806 break;
5807 case TPACPI_THERMAL_TPEC_8:
5808 case TPACPI_THERMAL_ACPI_TMP07:
5809 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005810 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005811 &thermal_temp_input8_group);
5812 if (res)
5813 return res;
5814 break;
5815 case TPACPI_THERMAL_NONE:
5816 default:
5817 return 1;
5818 }
5819
5820 return 0;
5821}
5822
5823static void thermal_exit(void)
5824{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005825 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005826 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005827 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005828 &thermal_temp_input16_group);
5829 break;
5830 case TPACPI_THERMAL_TPEC_8:
5831 case TPACPI_THERMAL_ACPI_TMP07:
5832 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005833 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005834 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005835 break;
5836 case TPACPI_THERMAL_NONE:
5837 default:
5838 break;
5839 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005840}
5841
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005842static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005843{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005844 int n, i;
5845 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005846
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005847 n = thermal_get_sensors(&t);
5848 if (unlikely(n < 0))
5849 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005850
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005851 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005852
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005853 if (n > 0) {
5854 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005855 seq_printf(m, "%d ", t.temp[i] / 1000);
5856 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005857 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005858 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005859
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005860 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005861}
5862
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005863static struct ibm_struct thermal_driver_data = {
5864 .name = "thermal",
5865 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005866 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005867};
5868
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005869/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005870 * Backlight/brightness subdriver
5871 */
Holger Macht8acb0252006-10-20 14:30:28 -07005872
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005873#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5874
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005875/*
5876 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5877 * CMOS NVRAM byte 0x5E, bits 0-3.
5878 *
5879 * EC HBRV (0x31) has the following layout
5880 * Bit 7: unknown function
5881 * Bit 6: unknown function
5882 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5883 * Bit 4: must be set to zero to avoid problems
5884 * Bit 3-0: backlight brightness level
5885 *
5886 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005887 *
5888 * WARNING: The X61 has been verified to use HBRV for something else, so
5889 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5890 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005891 */
5892
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005893enum {
5894 TP_EC_BACKLIGHT = 0x31,
5895
5896 /* TP_EC_BACKLIGHT bitmasks */
5897 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5898 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5899 TP_EC_BACKLIGHT_MAPSW = 0x20,
5900};
5901
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005902enum tpacpi_brightness_access_mode {
5903 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5904 TPACPI_BRGHT_MODE_EC, /* EC control */
5905 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5906 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5907 TPACPI_BRGHT_MODE_MAX
5908};
5909
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005910static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005911
5912static enum tpacpi_brightness_access_mode brightness_mode =
5913 TPACPI_BRGHT_MODE_MAX;
5914
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005915static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5916
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005917static struct mutex brightness_mutex;
5918
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005919/* NVRAM brightness access,
5920 * call with brightness_mutex held! */
5921static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005922{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005923 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005924
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005925 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5926 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5927 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005928 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005929
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005930 return lnvram;
5931}
5932
5933static void tpacpi_brightness_checkpoint_nvram(void)
5934{
5935 u8 lec = 0;
5936 u8 b_nvram;
5937
5938 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5939 return;
5940
5941 vdbg_printk(TPACPI_DBG_BRGHT,
5942 "trying to checkpoint backlight level to NVRAM...\n");
5943
5944 if (mutex_lock_killable(&brightness_mutex) < 0)
5945 return;
5946
5947 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5948 goto unlock;
5949 lec &= TP_EC_BACKLIGHT_LVLMSK;
5950 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5951
5952 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5953 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5954 /* NVRAM needs update */
5955 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5956 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5957 b_nvram |= lec;
5958 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5959 dbg_printk(TPACPI_DBG_BRGHT,
5960 "updated NVRAM backlight level to %u (0x%02x)\n",
5961 (unsigned int) lec, (unsigned int) b_nvram);
5962 } else
5963 vdbg_printk(TPACPI_DBG_BRGHT,
5964 "NVRAM backlight level already is %u (0x%02x)\n",
5965 (unsigned int) lec, (unsigned int) b_nvram);
5966
5967unlock:
5968 mutex_unlock(&brightness_mutex);
5969}
5970
5971
5972/* call with brightness_mutex held! */
5973static int tpacpi_brightness_get_raw(int *status)
5974{
5975 u8 lec = 0;
5976
5977 switch (brightness_mode) {
5978 case TPACPI_BRGHT_MODE_UCMS_STEP:
5979 *status = tpacpi_brightness_nvram_get();
5980 return 0;
5981 case TPACPI_BRGHT_MODE_EC:
5982 case TPACPI_BRGHT_MODE_ECNVRAM:
5983 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005984 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005985 *status = lec;
5986 return 0;
5987 default:
5988 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005989 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005990}
5991
5992/* call with brightness_mutex held! */
5993/* do NOT call with illegal backlight level value */
5994static int tpacpi_brightness_set_ec(unsigned int value)
5995{
5996 u8 lec = 0;
5997
5998 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5999 return -EIO;
6000
6001 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6002 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6003 (value & TP_EC_BACKLIGHT_LVLMSK))))
6004 return -EIO;
6005
6006 return 0;
6007}
6008
6009/* call with brightness_mutex held! */
6010static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6011{
6012 int cmos_cmd, inc;
6013 unsigned int current_value, i;
6014
6015 current_value = tpacpi_brightness_nvram_get();
6016
6017 if (value == current_value)
6018 return 0;
6019
6020 cmos_cmd = (value > current_value) ?
6021 TP_CMOS_BRIGHTNESS_UP :
6022 TP_CMOS_BRIGHTNESS_DOWN;
6023 inc = (value > current_value) ? 1 : -1;
6024
6025 for (i = current_value; i != value; i += inc)
6026 if (issue_thinkpad_cmos_command(cmos_cmd))
6027 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006028
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006029 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006030}
6031
6032/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006033static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006034{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006035 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006036
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006037 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006038 return -EINVAL;
6039
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006040 vdbg_printk(TPACPI_DBG_BRGHT,
6041 "set backlight level to %d\n", value);
6042
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006043 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006044 if (res < 0)
6045 return res;
6046
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006047 switch (brightness_mode) {
6048 case TPACPI_BRGHT_MODE_EC:
6049 case TPACPI_BRGHT_MODE_ECNVRAM:
6050 res = tpacpi_brightness_set_ec(value);
6051 break;
6052 case TPACPI_BRGHT_MODE_UCMS_STEP:
6053 res = tpacpi_brightness_set_ucmsstep(value);
6054 break;
6055 default:
6056 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006057 }
6058
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006059 mutex_unlock(&brightness_mutex);
6060 return res;
6061}
6062
6063/* sysfs backlight class ----------------------------------------------- */
6064
6065static int brightness_update_status(struct backlight_device *bd)
6066{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006067 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006068 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6069 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006070 bd->props.brightness : 0;
6071
6072 dbg_printk(TPACPI_DBG_BRGHT,
6073 "backlight: attempt to set level to %d\n",
6074 level);
6075
6076 /* it is the backlight class's job (caller) to handle
6077 * EINTR and other errors properly */
6078 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006079}
Holger Macht8acb0252006-10-20 14:30:28 -07006080
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006081static int brightness_get(struct backlight_device *bd)
6082{
6083 int status, res;
6084
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006085 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006086 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006087 return 0;
6088
6089 res = tpacpi_brightness_get_raw(&status);
6090
6091 mutex_unlock(&brightness_mutex);
6092
6093 if (res < 0)
6094 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006095
6096 return status & TP_EC_BACKLIGHT_LVLMSK;
6097}
6098
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006099static void tpacpi_brightness_notify_change(void)
6100{
6101 backlight_force_update(ibm_backlight_device,
6102 BACKLIGHT_UPDATE_HOTKEY);
6103}
6104
Lionel Debrouxacc24722010-11-16 14:14:02 +01006105static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006106 .get_brightness = brightness_get,
6107 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006108};
6109
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006110/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006111
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006112/*
6113 * Call _BCL method of video device. On some ThinkPads this will
6114 * switch the firmware to the ACPI brightness control mode.
6115 */
6116
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006117static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6118{
6119 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6120 union acpi_object *obj;
6121 int rc;
6122
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006123 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006124 obj = (union acpi_object *)buffer.pointer;
6125 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006126 pr_err("Unknown _BCL data, please report this to %s\n",
6127 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006128 rc = 0;
6129 } else {
6130 rc = obj->package.count;
6131 }
6132 } else {
6133 return 0;
6134 }
6135
6136 kfree(buffer.pointer);
6137 return rc;
6138}
6139
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006140
6141/*
6142 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6143 */
6144static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6145{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006146 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006147 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006148
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006149 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6150 if (video_device)
6151 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006152
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006153 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006154
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006155 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006156}
6157
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006158/*
6159 * These are only useful for models that have only one possibility
6160 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6161 * these quirks.
6162 */
6163#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6164#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6165#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6166
6167static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6168 /* Models with ATI GPUs known to require ECNVRAM mode */
6169 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6170
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006171 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006172 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006173 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006174 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006175 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6176
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006177 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006178 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006179 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6180 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006181
6182 /* Models with Intel GMA900 */
6183 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6184 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6185 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6186};
6187
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006188/*
6189 * Returns < 0 for error, otherwise sets tp_features.bright_*
6190 * and bright_maxlvl.
6191 */
6192static void __init tpacpi_detect_brightness_capabilities(void)
6193{
6194 unsigned int b;
6195
6196 vdbg_printk(TPACPI_DBG_INIT,
6197 "detecting firmware brightness interface capabilities\n");
6198
6199 /* we could run a quirks check here (same table used by
6200 * brightness_init) if needed */
6201
6202 /*
6203 * We always attempt to detect acpi support, so as to switch
6204 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6205 * going to publish a backlight interface
6206 */
6207 b = tpacpi_check_std_acpi_brightness_support();
6208 switch (b) {
6209 case 16:
6210 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006211 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006212 break;
6213 case 8:
6214 case 0:
6215 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006216 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006217 break;
6218 default:
Joe Perches0978e012011-04-04 10:06:25 -07006219 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006220 "please contact %s\n", TPACPI_MAIL);
6221 tp_features.bright_unkfw = 1;
6222 bright_maxlvl = b - 1;
6223 }
6224}
6225
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006226static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006227{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006228 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006229 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006230 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006231
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006232 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6233
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006234 mutex_init(&brightness_mutex);
6235
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006236 quirks = tpacpi_check_quirks(brightness_quirk_table,
6237 ARRAY_SIZE(brightness_quirk_table));
6238
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006239 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006240
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006241 /* if it is unknown, we don't handle it: it wouldn't be safe */
6242 if (tp_features.bright_unkfw)
6243 return 1;
6244
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006245 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006246 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006247 "brightness support disabled by "
6248 "module parameter\n");
6249 return 1;
6250 }
6251
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006252 if (acpi_video_backlight_support()) {
6253 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006254 pr_info("Standard ACPI backlight interface "
6255 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006256 return 1;
6257 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006258 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006259 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006260 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006261 return 1;
6262 }
6263 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006264 pr_notice("Standard ACPI backlight interface not "
6265 "available, thinkpad_acpi native "
6266 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006267 }
6268
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006269 /*
6270 * Check for module parameter bogosity, note that we
6271 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6272 * able to detect "unspecified"
6273 */
6274 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006275 return -EINVAL;
6276
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006277 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6278 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6279 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006280 if (quirks & TPACPI_BRGHT_Q_EC)
6281 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6282 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006283 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6284
6285 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006286 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006287 brightness_mode);
6288 }
6289
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006290 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006291 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006292 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6293 brightness_mode == TPACPI_BRGHT_MODE_EC))
6294 return -EINVAL;
6295
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006296 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006297 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006298
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006299 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006300 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006301 props.max_brightness = bright_maxlvl;
6302 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006303 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6304 NULL, NULL,
6305 &ibm_backlight_data,
6306 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006307 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006308 int rc = PTR_ERR(ibm_backlight_device);
6309 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006310 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006311 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006312 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6314 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006315
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006316 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006317 pr_notice("brightness: will use unverified default: "
6318 "brightness_mode=%d\n", brightness_mode);
6319 pr_notice("brightness: please report to %s whether it works well "
6320 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006321 }
6322
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006323 /* Added by mistake in early 2007. Probably useless, but it could
6324 * be working around some unknown firmware problem where the value
6325 * read at startup doesn't match the real hardware state... so leave
6326 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006327 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006328
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006329 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6330 "brightness: registering brightness hotkeys "
6331 "as change notification\n");
6332 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6333 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006334 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006335 return 0;
6336}
6337
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006338static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006339{
6340 tpacpi_brightness_checkpoint_nvram();
6341}
6342
6343static void brightness_shutdown(void)
6344{
6345 tpacpi_brightness_checkpoint_nvram();
6346}
6347
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006348static void brightness_exit(void)
6349{
6350 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006351 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006352 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006353 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006354 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006355
6356 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006357}
6358
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006359static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006361 int level;
6362
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006363 level = brightness_get(NULL);
6364 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006365 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006366 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006367 seq_printf(m, "level:\t\t%d\n", level);
6368 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006369 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6370 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006371 }
6372
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006373 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006374}
6375
6376static int brightness_write(char *buf)
6377{
6378 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006379 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006380 char *cmd;
6381
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006382 level = brightness_get(NULL);
6383 if (level < 0)
6384 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006385
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006386 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006387 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006388 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006389 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006390 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006391 if (level > 0)
6392 level--;
6393 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006394 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006395 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006396 } else
6397 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006398 }
6399
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006400 tpacpi_disclose_usertask("procfs brightness",
6401 "set level to %d\n", level);
6402
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006403 /*
6404 * Now we know what the final level should be, so we try to set it.
6405 * Doing it this way makes the syscall restartable in case of EINTR
6406 */
6407 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006408 if (!rc && ibm_backlight_device)
6409 backlight_force_update(ibm_backlight_device,
6410 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006411 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006412}
6413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006414static struct ibm_struct brightness_driver_data = {
6415 .name = "brightness",
6416 .read = brightness_read,
6417 .write = brightness_write,
6418 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006419 .suspend = brightness_suspend,
6420 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006421};
6422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006423/*************************************************************************
6424 * Volume subdriver
6425 */
6426
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006427/*
6428 * IBM ThinkPads have a simple volume controller with MUTE gating.
6429 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6430 *
6431 * Since the *61 series (and probably also the later *60 series), Lenovo
6432 * ThinkPads only implement the MUTE gate.
6433 *
6434 * EC register 0x30
6435 * Bit 6: MUTE (1 mutes sound)
6436 * Bit 3-0: Volume
6437 * Other bits should be zero as far as we know.
6438 *
6439 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6440 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6441 * such as bit 7 which is used to detect repeated presses of MUTE,
6442 * and we leave them unchanged.
6443 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006444
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006445#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6446
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006447#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6448#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6449#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6450
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006451static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006452static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306453static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006454
6455struct tpacpi_alsa_data {
6456 struct snd_card *card;
6457 struct snd_ctl_elem_id *ctl_mute_id;
6458 struct snd_ctl_elem_id *ctl_vol_id;
6459};
6460
6461static struct snd_card *alsa_card;
6462
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006463enum {
6464 TP_EC_AUDIO = 0x30,
6465
6466 /* TP_EC_AUDIO bits */
6467 TP_EC_AUDIO_MUTESW = 6,
6468
6469 /* TP_EC_AUDIO bitmasks */
6470 TP_EC_AUDIO_LVL_MSK = 0x0F,
6471 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6472
6473 /* Maximum volume */
6474 TP_EC_VOLUME_MAX = 14,
6475};
6476
6477enum tpacpi_volume_access_mode {
6478 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6479 TPACPI_VOL_MODE_EC, /* Pure EC control */
6480 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6481 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6482 TPACPI_VOL_MODE_MAX
6483};
6484
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006485enum tpacpi_volume_capabilities {
6486 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6487 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6488 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6489 TPACPI_VOL_CAP_MAX
6490};
6491
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006492static enum tpacpi_volume_access_mode volume_mode =
6493 TPACPI_VOL_MODE_MAX;
6494
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006495static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306496static bool volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006497
6498/*
6499 * Used to syncronize writers to TP_EC_AUDIO and
6500 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6501 */
6502static struct mutex volume_mutex;
6503
6504static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006505{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006506 u8 lec = 0;
6507 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006508 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006509
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006510 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6511 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006512 if (!volume_control_allowed)
6513 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006514
6515 vdbg_printk(TPACPI_DBG_MIXER,
6516 "trying to checkpoint mixer state to NVRAM...\n");
6517
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006518 if (tp_features.mixer_no_level_control)
6519 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6520 else
6521 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6522
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006523 if (mutex_lock_killable(&volume_mutex) < 0)
6524 return;
6525
6526 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6527 goto unlock;
6528 lec &= ec_mask;
6529 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6530
6531 if (lec != (b_nvram & ec_mask)) {
6532 /* NVRAM needs update */
6533 b_nvram &= ~ec_mask;
6534 b_nvram |= lec;
6535 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6536 dbg_printk(TPACPI_DBG_MIXER,
6537 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6538 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006539 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006540 vdbg_printk(TPACPI_DBG_MIXER,
6541 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6542 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006543 }
6544
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006545unlock:
6546 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006547}
6548
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006549static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006550{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006551 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006552
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006553 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6554 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006555
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006556 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006557
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006558 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006559
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006560 return 0;
6561}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006562
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006563static int volume_get_status(u8 *status)
6564{
6565 return volume_get_status_ec(status);
6566}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006567
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006568static int volume_set_status_ec(const u8 status)
6569{
6570 if (!acpi_ec_write(TP_EC_AUDIO, status))
6571 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006572
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006573 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6574
6575 return 0;
6576}
6577
6578static int volume_set_status(const u8 status)
6579{
6580 return volume_set_status_ec(status);
6581}
6582
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006583/* returns < 0 on error, 0 on no change, 1 on change */
6584static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006585{
6586 int rc;
6587 u8 s, n;
6588
6589 if (mutex_lock_killable(&volume_mutex) < 0)
6590 return -EINTR;
6591
6592 rc = volume_get_status_ec(&s);
6593 if (rc)
6594 goto unlock;
6595
6596 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6597 s & ~TP_EC_AUDIO_MUTESW_MSK;
6598
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006599 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006600 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006601 if (!rc)
6602 rc = 1;
6603 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006604
6605unlock:
6606 mutex_unlock(&volume_mutex);
6607 return rc;
6608}
6609
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006610static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006611{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006612 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006613 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006614 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006615}
6616
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006617static int volume_set_mute(const bool mute)
6618{
6619 int rc;
6620
6621 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6622 (mute) ? "" : "un");
6623
6624 rc = __volume_set_mute_ec(mute);
6625 return (rc < 0) ? rc : 0;
6626}
6627
6628/* returns < 0 on error, 0 on no change, 1 on change */
6629static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006630{
6631 int rc;
6632 u8 s, n;
6633
6634 if (vol > TP_EC_VOLUME_MAX)
6635 return -EINVAL;
6636
6637 if (mutex_lock_killable(&volume_mutex) < 0)
6638 return -EINTR;
6639
6640 rc = volume_get_status_ec(&s);
6641 if (rc)
6642 goto unlock;
6643
6644 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6645
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006646 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006647 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006648 if (!rc)
6649 rc = 1;
6650 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006651
6652unlock:
6653 mutex_unlock(&volume_mutex);
6654 return rc;
6655}
6656
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006657static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006658{
6659 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006660 "ALSA: trying to set volume level to %hu\n", vol);
6661 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006662}
6663
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006664static void volume_alsa_notify_change(void)
6665{
6666 struct tpacpi_alsa_data *d;
6667
6668 if (alsa_card && alsa_card->private_data) {
6669 d = alsa_card->private_data;
6670 if (d->ctl_mute_id)
6671 snd_ctl_notify(alsa_card,
6672 SNDRV_CTL_EVENT_MASK_VALUE,
6673 d->ctl_mute_id);
6674 if (d->ctl_vol_id)
6675 snd_ctl_notify(alsa_card,
6676 SNDRV_CTL_EVENT_MASK_VALUE,
6677 d->ctl_vol_id);
6678 }
6679}
6680
6681static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6682 struct snd_ctl_elem_info *uinfo)
6683{
6684 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6685 uinfo->count = 1;
6686 uinfo->value.integer.min = 0;
6687 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6688 return 0;
6689}
6690
6691static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6692 struct snd_ctl_elem_value *ucontrol)
6693{
6694 u8 s;
6695 int rc;
6696
6697 rc = volume_get_status(&s);
6698 if (rc < 0)
6699 return rc;
6700
6701 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6702 return 0;
6703}
6704
6705static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6706 struct snd_ctl_elem_value *ucontrol)
6707{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006708 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6709 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006710 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006711}
6712
6713#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6714
6715static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6716 struct snd_ctl_elem_value *ucontrol)
6717{
6718 u8 s;
6719 int rc;
6720
6721 rc = volume_get_status(&s);
6722 if (rc < 0)
6723 return rc;
6724
6725 ucontrol->value.integer.value[0] =
6726 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6727 return 0;
6728}
6729
6730static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6731 struct snd_ctl_elem_value *ucontrol)
6732{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006733 tpacpi_disclose_usertask("ALSA", "%smute\n",
6734 ucontrol->value.integer.value[0] ?
6735 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006736 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006737}
6738
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006739static struct snd_kcontrol_new volume_alsa_control_vol = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6741 .name = "Console Playback Volume",
6742 .index = 0,
6743 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6744 .info = volume_alsa_vol_info,
6745 .get = volume_alsa_vol_get,
6746};
6747
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006748static struct snd_kcontrol_new volume_alsa_control_mute = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006749 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6750 .name = "Console Playback Switch",
6751 .index = 0,
6752 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6753 .info = volume_alsa_mute_info,
6754 .get = volume_alsa_mute_get,
6755};
6756
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006757static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006758{
6759 tpacpi_volume_checkpoint_nvram();
6760}
6761
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006762static void volume_resume(void)
6763{
6764 volume_alsa_notify_change();
6765}
6766
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006767static void volume_shutdown(void)
6768{
6769 tpacpi_volume_checkpoint_nvram();
6770}
6771
6772static void volume_exit(void)
6773{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006774 if (alsa_card) {
6775 snd_card_free(alsa_card);
6776 alsa_card = NULL;
6777 }
6778
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006779 tpacpi_volume_checkpoint_nvram();
6780}
6781
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006782static int __init volume_create_alsa_mixer(void)
6783{
6784 struct snd_card *card;
6785 struct tpacpi_alsa_data *data;
6786 struct snd_kcontrol *ctl_vol;
6787 struct snd_kcontrol *ctl_mute;
6788 int rc;
6789
6790 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6791 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006792 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006793 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006794 return 1;
6795 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006796
6797 BUG_ON(!card->private_data);
6798 data = card->private_data;
6799 data->card = card;
6800
6801 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6802 sizeof(card->driver));
6803 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6804 sizeof(card->shortname));
6805 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6806 (thinkpad_id.ec_version_str) ?
6807 thinkpad_id.ec_version_str : "(unknown)");
6808 snprintf(card->longname, sizeof(card->longname),
6809 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6810 (thinkpad_id.ec_version_str) ?
6811 thinkpad_id.ec_version_str : "unknown");
6812
6813 if (volume_control_allowed) {
6814 volume_alsa_control_vol.put = volume_alsa_vol_put;
6815 volume_alsa_control_vol.access =
6816 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6817
6818 volume_alsa_control_mute.put = volume_alsa_mute_put;
6819 volume_alsa_control_mute.access =
6820 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6821 }
6822
6823 if (!tp_features.mixer_no_level_control) {
6824 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6825 rc = snd_ctl_add(card, ctl_vol);
6826 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006827 pr_err("Failed to create ALSA volume control: %d\n",
6828 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006829 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006830 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006831 data->ctl_vol_id = &ctl_vol->id;
6832 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006833
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006834 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6835 rc = snd_ctl_add(card, ctl_mute);
6836 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006837 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006838 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006839 }
6840 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006841
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006842 snd_card_set_dev(card, &tpacpi_pdev->dev);
6843 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006844 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006845 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006846 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006847 }
6848
6849 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006850 return 0;
6851
6852err_exit:
6853 snd_card_free(card);
6854 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006855}
6856
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006857#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6858#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6859
6860static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6861 /* Whitelist volume level on all IBM by default */
6862 { .vendor = PCI_VENDOR_ID_IBM,
6863 .bios = TPACPI_MATCH_ANY,
6864 .ec = TPACPI_MATCH_ANY,
6865 .quirks = TPACPI_VOL_Q_LEVEL },
6866
6867 /* Lenovo models with volume control (needs confirmation) */
6868 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6869 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6870 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6871 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6872 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6873 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6874 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6875
6876 /* Whitelist mute-only on all Lenovo by default */
6877 { .vendor = PCI_VENDOR_ID_LENOVO,
6878 .bios = TPACPI_MATCH_ANY,
6879 .ec = TPACPI_MATCH_ANY,
6880 .quirks = TPACPI_VOL_Q_MUTEONLY }
6881};
6882
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006883static int __init volume_init(struct ibm_init_struct *iibm)
6884{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006885 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006886 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006887
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006888 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6889
6890 mutex_init(&volume_mutex);
6891
6892 /*
6893 * Check for module parameter bogosity, note that we
6894 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6895 * able to detect "unspecified"
6896 */
6897 if (volume_mode > TPACPI_VOL_MODE_MAX)
6898 return -EINVAL;
6899
6900 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006901 pr_err("UCMS step volume mode not implemented, "
6902 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006903 return 1;
6904 }
6905
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006906 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6907 return -EINVAL;
6908
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006909 /*
6910 * The ALSA mixer is our primary interface.
6911 * When disabled, don't install the subdriver at all
6912 */
6913 if (!alsa_enable) {
6914 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6915 "ALSA mixer disabled by parameter, "
6916 "not loading volume subdriver...\n");
6917 return 1;
6918 }
6919
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006920 quirks = tpacpi_check_quirks(volume_quirk_table,
6921 ARRAY_SIZE(volume_quirk_table));
6922
6923 switch (volume_capabilities) {
6924 case TPACPI_VOL_CAP_AUTO:
6925 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6926 tp_features.mixer_no_level_control = 1;
6927 else if (quirks & TPACPI_VOL_Q_LEVEL)
6928 tp_features.mixer_no_level_control = 0;
6929 else
6930 return 1; /* no mixer */
6931 break;
6932 case TPACPI_VOL_CAP_VOLMUTE:
6933 tp_features.mixer_no_level_control = 0;
6934 break;
6935 case TPACPI_VOL_CAP_MUTEONLY:
6936 tp_features.mixer_no_level_control = 1;
6937 break;
6938 default:
6939 return 1;
6940 }
6941
6942 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6943 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6944 "using user-supplied volume_capabilities=%d\n",
6945 volume_capabilities);
6946
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006947 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6948 volume_mode == TPACPI_VOL_MODE_MAX) {
6949 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6950
6951 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6952 "driver auto-selected volume_mode=%d\n",
6953 volume_mode);
6954 } else {
6955 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6956 "using user-supplied volume_mode=%d\n",
6957 volume_mode);
6958 }
6959
6960 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006961 "mute is supported, volume control is %s\n",
6962 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006963
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006964 rc = volume_create_alsa_mixer();
6965 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006966 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006967 return rc;
6968 }
6969
Joe Perches0978e012011-04-04 10:06:25 -07006970 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006971 (volume_control_allowed) ?
6972 "override (read/write)" :
6973 "monitor (read only)");
6974
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006975 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6976 "registering volume hotkeys as change notification\n");
6977 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6978 | TP_ACPI_HKEY_VOLUP_MASK
6979 | TP_ACPI_HKEY_VOLDWN_MASK
6980 | TP_ACPI_HKEY_MUTE_MASK);
6981
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006982 return 0;
6983}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006984
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006985static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006986{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006987 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006988
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006989 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006990 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006991 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006992 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006993 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006994 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006995 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006996 status & TP_EC_AUDIO_LVL_MSK);
6997
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006998 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006999 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007000
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007001 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007002 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007003 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007004 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007005 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02007006 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007007 "commands:\tlevel <level>"
7008 " (<level> is 0-%d)\n",
7009 TP_EC_VOLUME_MAX);
7010 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007011 }
7012 }
7013
7014 return 0;
7015}
7016
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007017static int volume_write(char *buf)
7018{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007019 u8 s;
7020 u8 new_level, new_mute;
7021 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007022 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007023 int rc;
7024
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007025 /*
7026 * We do allow volume control at driver startup, so that the
7027 * user can set initial state through the volume=... parameter hack.
7028 */
7029 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7030 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7031 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007032 pr_notice("Console audio control in monitor mode, "
7033 "changes are not allowed\n");
7034 pr_notice("Use the volume_control=1 module parameter "
7035 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007036 }
7037 return -EPERM;
7038 }
7039
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007040 rc = volume_get_status(&s);
7041 if (rc < 0)
7042 return rc;
7043
7044 new_level = s & TP_EC_AUDIO_LVL_MSK;
7045 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007046
7047 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007048 if (!tp_features.mixer_no_level_control) {
7049 if (strlencmp(cmd, "up") == 0) {
7050 if (new_mute)
7051 new_mute = 0;
7052 else if (new_level < TP_EC_VOLUME_MAX)
7053 new_level++;
7054 continue;
7055 } else if (strlencmp(cmd, "down") == 0) {
7056 if (new_mute)
7057 new_mute = 0;
7058 else if (new_level > 0)
7059 new_level--;
7060 continue;
7061 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7062 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7063 new_level = l;
7064 continue;
7065 }
7066 }
7067 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007068 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007069 else if (strlencmp(cmd, "unmute") == 0)
7070 new_mute = 0;
7071 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007072 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007073 }
7074
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007075 if (tp_features.mixer_no_level_control) {
7076 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7077 new_mute ? "" : "un");
7078 rc = volume_set_mute(!!new_mute);
7079 } else {
7080 tpacpi_disclose_usertask("procfs volume",
7081 "%smute and set level to %d\n",
7082 new_mute ? "" : "un", new_level);
7083 rc = volume_set_status(new_mute | new_level);
7084 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007085 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007086
7087 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007088}
7089
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007090static struct ibm_struct volume_driver_data = {
7091 .name = "volume",
7092 .read = volume_read,
7093 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007094 .exit = volume_exit,
7095 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007096 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007097 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007098};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007099
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007100#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7101
7102#define alsa_card NULL
7103
7104static void inline volume_alsa_notify_change(void)
7105{
7106}
7107
7108static int __init volume_init(struct ibm_init_struct *iibm)
7109{
Joe Perches0978e012011-04-04 10:06:25 -07007110 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007111
7112 return 1;
7113}
7114
7115static struct ibm_struct volume_driver_data = {
7116 .name = "volume",
7117};
7118
7119#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7120
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007121/*************************************************************************
7122 * Fan subdriver
7123 */
7124
7125/*
7126 * FAN ACCESS MODES
7127 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007128 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007129 * ACPI GFAN method: returns fan level
7130 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007131 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007132 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007133 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007134 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007135 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7136 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007137 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7138 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007139 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007140 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007141 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7142 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007143 *
7144 * Fan speed changes of any sort (including those caused by the
7145 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007146 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007147 * limited.
7148 *
7149 * Reading is not available if GFAN exists.
7150 * Writing is not available if SFAN exists.
7151 *
7152 * Bits
7153 * 7 automatic mode engaged;
7154 * (default operation mode of the ThinkPad)
7155 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007156 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007157 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007158 * the tachometer while the fan controller ramps up
7159 * the speed (which can take up to a few *minutes*).
7160 * Speeds up fan to 100% duty-cycle, which is far above
7161 * the standard RPM levels. It is not impossible that
7162 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007163 * 5-3 unused in some models. Extra bits for fan level
7164 * in others, but still useless as all values above
7165 * 7 map to the same speed as level 7 in these models.
7166 * 2-0 fan level (0..7 usually)
7167 * 0x00 = stop
7168 * 0x07 = max (set when temperatures critical)
7169 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007170 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007171 *
7172 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007173 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007174 * does so, its initial value is meaningless (0x07).
7175 *
7176 * For firmware bugs, refer to:
7177 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7178 *
7179 * ----
7180 *
7181 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7182 * Main fan tachometer reading (in RPM)
7183 *
7184 * This register is present on all ThinkPads with a new-style EC, and
7185 * it is known not to be present on the A21m/e, and T22, as there is
7186 * something else in offset 0x84 according to the ACPI DSDT. Other
7187 * ThinkPads from this same time period (and earlier) probably lack the
7188 * tachometer as well.
7189 *
Nick Andrew877d0312009-01-26 11:06:57 +01007190 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007191 * was never fixed by IBM to report the EC firmware version string
7192 * probably support the tachometer (like the early X models), so
7193 * detecting it is quite hard. We need more data to know for sure.
7194 *
7195 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7196 * might result.
7197 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007198 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7199 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007200 *
7201 * For firmware bugs, refer to:
7202 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7203 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007204 * ----
7205 *
7206 * ThinkPad EC register 0x31 bit 0 (only on select models)
7207 *
7208 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7209 * show the speed of the main fan. When bit 0 of EC register 0x31
7210 * is one, the tachometer registers show the speed of the auxiliary
7211 * fan.
7212 *
7213 * Fan control seems to affect both fans, regardless of the state
7214 * of this bit.
7215 *
7216 * So far, only the firmware for the X60/X61 non-tablet versions
7217 * seem to support this (firmware TP-7M).
7218 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007219 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007220 * ThinkPad X31, X40, X41. Not available in the X60.
7221 *
7222 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7223 * high speed. ACPI DSDT seems to map these three speeds to levels
7224 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7225 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7226 *
7227 * The speeds are stored on handles
7228 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7229 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007230 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007231 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7232 *
7233 * ACPI DSDT switches which set is in use depending on various
7234 * factors.
7235 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007236 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007237 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7238 * but the ACPI tables just mention level 7.
7239 */
7240
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007241enum { /* Fan control constants */
7242 fan_status_offset = 0x2f, /* EC register 0x2f */
7243 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7244 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007245 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7246 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007247
7248 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7249 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7250
7251 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7252};
7253
7254enum fan_status_access_mode {
7255 TPACPI_FAN_NONE = 0, /* No fan status or control */
7256 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7257 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7258};
7259
7260enum fan_control_access_mode {
7261 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7262 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7263 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7264 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7265};
7266
7267enum fan_control_commands {
7268 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7269 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7270 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7271 * and also watchdog cmd */
7272};
7273
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307274static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007275
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007276static enum fan_status_access_mode fan_status_access_mode;
7277static enum fan_control_access_mode fan_control_access_mode;
7278static enum fan_control_commands fan_control_commands;
7279
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007280static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007281static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007282static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007283static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007284
7285static struct mutex fan_mutex;
7286
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007287static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007288static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007289
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007290TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7291TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007292 "\\FSPD", /* 600e/x, 770e, 770x */
7293 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007294TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007295 "JFNS", /* 770x-JL */
7296 ); /* all others */
7297
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007298/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007299 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7300 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7301 * be in auto mode (0x80).
7302 *
7303 * This is corrected by any write to HFSP either by the driver, or
7304 * by the firmware.
7305 *
7306 * We assume 0x07 really means auto mode while this quirk is active,
7307 * as this is far more likely than the ThinkPad being in level 7,
7308 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007309 *
7310 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7311 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007312 */
7313
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007314static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007315{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007316 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007317 pr_notice("fan_init: initial fan status is unknown, "
7318 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007319 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007320 }
7321}
7322
7323static void fan_quirk1_handle(u8 *fan_status)
7324{
7325 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7326 if (*fan_status != fan_control_initial_status) {
7327 /* something changed the HFSP regisnter since
7328 * driver init time, so it is not undefined
7329 * anymore */
7330 tp_features.fan_ctrl_status_undef = 0;
7331 } else {
7332 /* Return most likely status. In fact, it
7333 * might be the only possible status */
7334 *fan_status = TP_EC_FAN_AUTO;
7335 }
7336 }
7337}
7338
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007339/* Select main fan on X60/X61, NOOP on others */
7340static bool fan_select_fan1(void)
7341{
7342 if (tp_features.second_fan) {
7343 u8 val;
7344
7345 if (ec_read(fan_select_offset, &val) < 0)
7346 return false;
7347 val &= 0xFEU;
7348 if (ec_write(fan_select_offset, val) < 0)
7349 return false;
7350 }
7351 return true;
7352}
7353
7354/* Select secondary fan on X60/X61 */
7355static bool fan_select_fan2(void)
7356{
7357 u8 val;
7358
7359 if (!tp_features.second_fan)
7360 return false;
7361
7362 if (ec_read(fan_select_offset, &val) < 0)
7363 return false;
7364 val |= 0x01U;
7365 if (ec_write(fan_select_offset, val) < 0)
7366 return false;
7367
7368 return true;
7369}
7370
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007371/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007372 * Call with fan_mutex held
7373 */
7374static void fan_update_desired_level(u8 status)
7375{
7376 if ((status &
7377 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7378 if (status > 7)
7379 fan_control_desired_level = 7;
7380 else
7381 fan_control_desired_level = status;
7382 }
7383}
7384
7385static int fan_get_status(u8 *status)
7386{
7387 u8 s;
7388
7389 /* TODO:
7390 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7391
7392 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007393 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007394 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007395 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007396
Dan Carpentereceeb432012-09-01 12:54:07 -07007397 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007398 return -EIO;
7399
7400 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007401 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007402
7403 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007404 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007405 case TPACPI_FAN_RD_TPEC:
7406 /* all except 570, 600e/x, 770e, 770x */
7407 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7408 return -EIO;
7409
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007410 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007411 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007412 fan_quirk1_handle(status);
7413 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007414
7415 break;
7416
7417 default:
7418 return -ENXIO;
7419 }
7420
7421 return 0;
7422}
7423
7424static int fan_get_status_safe(u8 *status)
7425{
7426 int rc;
7427 u8 s;
7428
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007429 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007430 return -ERESTARTSYS;
7431 rc = fan_get_status(&s);
7432 if (!rc)
7433 fan_update_desired_level(s);
7434 mutex_unlock(&fan_mutex);
7435
7436 if (status)
7437 *status = s;
7438
7439 return rc;
7440}
7441
7442static int fan_get_speed(unsigned int *speed)
7443{
7444 u8 hi, lo;
7445
7446 switch (fan_status_access_mode) {
7447 case TPACPI_FAN_RD_TPEC:
7448 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007449 if (unlikely(!fan_select_fan1()))
7450 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007451 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7452 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7453 return -EIO;
7454
7455 if (likely(speed))
7456 *speed = (hi << 8) | lo;
7457
7458 break;
7459
7460 default:
7461 return -ENXIO;
7462 }
7463
7464 return 0;
7465}
7466
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007467static int fan2_get_speed(unsigned int *speed)
7468{
7469 u8 hi, lo;
7470 bool rc;
7471
7472 switch (fan_status_access_mode) {
7473 case TPACPI_FAN_RD_TPEC:
7474 /* all except 570, 600e/x, 770e, 770x */
7475 if (unlikely(!fan_select_fan2()))
7476 return -EIO;
7477 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7478 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7479 fan_select_fan1(); /* play it safe */
7480 if (rc)
7481 return -EIO;
7482
7483 if (likely(speed))
7484 *speed = (hi << 8) | lo;
7485
7486 break;
7487
7488 default:
7489 return -ENXIO;
7490 }
7491
7492 return 0;
7493}
7494
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007495static int fan_set_level(int level)
7496{
7497 if (!fan_control_allowed)
7498 return -EPERM;
7499
7500 switch (fan_control_access_mode) {
7501 case TPACPI_FAN_WR_ACPI_SFAN:
7502 if (level >= 0 && level <= 7) {
7503 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7504 return -EIO;
7505 } else
7506 return -EINVAL;
7507 break;
7508
7509 case TPACPI_FAN_WR_ACPI_FANS:
7510 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007511 if (!(level & TP_EC_FAN_AUTO) &&
7512 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007513 ((level < 0) || (level > 7)))
7514 return -EINVAL;
7515
7516 /* safety net should the EC not support AUTO
7517 * or FULLSPEED mode bits and just ignore them */
7518 if (level & TP_EC_FAN_FULLSPEED)
7519 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007520 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007521 level |= 4; /* safety min speed 4 */
7522
7523 if (!acpi_ec_write(fan_status_offset, level))
7524 return -EIO;
7525 else
7526 tp_features.fan_ctrl_status_undef = 0;
7527 break;
7528
7529 default:
7530 return -ENXIO;
7531 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007532
7533 vdbg_printk(TPACPI_DBG_FAN,
7534 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007535 return 0;
7536}
7537
7538static int fan_set_level_safe(int level)
7539{
7540 int rc;
7541
7542 if (!fan_control_allowed)
7543 return -EPERM;
7544
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007545 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007546 return -ERESTARTSYS;
7547
7548 if (level == TPACPI_FAN_LAST_LEVEL)
7549 level = fan_control_desired_level;
7550
7551 rc = fan_set_level(level);
7552 if (!rc)
7553 fan_update_desired_level(level);
7554
7555 mutex_unlock(&fan_mutex);
7556 return rc;
7557}
7558
7559static int fan_set_enable(void)
7560{
7561 u8 s;
7562 int rc;
7563
7564 if (!fan_control_allowed)
7565 return -EPERM;
7566
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007567 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007568 return -ERESTARTSYS;
7569
7570 switch (fan_control_access_mode) {
7571 case TPACPI_FAN_WR_ACPI_FANS:
7572 case TPACPI_FAN_WR_TPEC:
7573 rc = fan_get_status(&s);
7574 if (rc < 0)
7575 break;
7576
7577 /* Don't go out of emergency fan mode */
7578 if (s != 7) {
7579 s &= 0x07;
7580 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7581 }
7582
7583 if (!acpi_ec_write(fan_status_offset, s))
7584 rc = -EIO;
7585 else {
7586 tp_features.fan_ctrl_status_undef = 0;
7587 rc = 0;
7588 }
7589 break;
7590
7591 case TPACPI_FAN_WR_ACPI_SFAN:
7592 rc = fan_get_status(&s);
7593 if (rc < 0)
7594 break;
7595
7596 s &= 0x07;
7597
7598 /* Set fan to at least level 4 */
7599 s |= 4;
7600
7601 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007602 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007603 else
7604 rc = 0;
7605 break;
7606
7607 default:
7608 rc = -ENXIO;
7609 }
7610
7611 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007612
7613 if (!rc)
7614 vdbg_printk(TPACPI_DBG_FAN,
7615 "fan control: set fan control register to 0x%02x\n",
7616 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007617 return rc;
7618}
7619
7620static int fan_set_disable(void)
7621{
7622 int rc;
7623
7624 if (!fan_control_allowed)
7625 return -EPERM;
7626
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007627 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007628 return -ERESTARTSYS;
7629
7630 rc = 0;
7631 switch (fan_control_access_mode) {
7632 case TPACPI_FAN_WR_ACPI_FANS:
7633 case TPACPI_FAN_WR_TPEC:
7634 if (!acpi_ec_write(fan_status_offset, 0x00))
7635 rc = -EIO;
7636 else {
7637 fan_control_desired_level = 0;
7638 tp_features.fan_ctrl_status_undef = 0;
7639 }
7640 break;
7641
7642 case TPACPI_FAN_WR_ACPI_SFAN:
7643 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7644 rc = -EIO;
7645 else
7646 fan_control_desired_level = 0;
7647 break;
7648
7649 default:
7650 rc = -ENXIO;
7651 }
7652
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007653 if (!rc)
7654 vdbg_printk(TPACPI_DBG_FAN,
7655 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007656
7657 mutex_unlock(&fan_mutex);
7658 return rc;
7659}
7660
7661static int fan_set_speed(int speed)
7662{
7663 int rc;
7664
7665 if (!fan_control_allowed)
7666 return -EPERM;
7667
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007668 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007669 return -ERESTARTSYS;
7670
7671 rc = 0;
7672 switch (fan_control_access_mode) {
7673 case TPACPI_FAN_WR_ACPI_FANS:
7674 if (speed >= 0 && speed <= 65535) {
7675 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7676 speed, speed, speed))
7677 rc = -EIO;
7678 } else
7679 rc = -EINVAL;
7680 break;
7681
7682 default:
7683 rc = -ENXIO;
7684 }
7685
7686 mutex_unlock(&fan_mutex);
7687 return rc;
7688}
7689
7690static void fan_watchdog_reset(void)
7691{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007692 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7693 return;
7694
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007695 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07007696 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7697 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7698 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7699 else
7700 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007701}
7702
7703static void fan_watchdog_fire(struct work_struct *ignored)
7704{
7705 int rc;
7706
7707 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7708 return;
7709
Joe Perches0978e012011-04-04 10:06:25 -07007710 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007711 rc = fan_set_enable();
7712 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007713 pr_err("fan watchdog: error %d while enabling fan, "
7714 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007715 /* reschedule for later */
7716 fan_watchdog_reset();
7717 }
7718}
7719
7720/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007721 * SYSFS fan layout: hwmon compatible (device)
7722 *
7723 * pwm*_enable:
7724 * 0: "disengaged" mode
7725 * 1: manual mode
7726 * 2: native EC "auto" mode (recommended, hardware default)
7727 *
7728 * pwm*: set speed in manual mode, ignored otherwise.
7729 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7730 * interpolation.
7731 *
7732 * fan*_input: tachometer reading, RPM
7733 *
7734 *
7735 * SYSFS fan layout: extensions
7736 *
7737 * fan_watchdog (driver):
7738 * fan watchdog interval in seconds, 0 disables (default), max 120
7739 */
7740
7741/* sysfs fan pwm1_enable ----------------------------------------------- */
7742static ssize_t fan_pwm1_enable_show(struct device *dev,
7743 struct device_attribute *attr,
7744 char *buf)
7745{
7746 int res, mode;
7747 u8 status;
7748
7749 res = fan_get_status_safe(&status);
7750 if (res)
7751 return res;
7752
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007753 if (status & TP_EC_FAN_FULLSPEED) {
7754 mode = 0;
7755 } else if (status & TP_EC_FAN_AUTO) {
7756 mode = 2;
7757 } else
7758 mode = 1;
7759
7760 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7761}
7762
7763static ssize_t fan_pwm1_enable_store(struct device *dev,
7764 struct device_attribute *attr,
7765 const char *buf, size_t count)
7766{
7767 unsigned long t;
7768 int res, level;
7769
7770 if (parse_strtoul(buf, 2, &t))
7771 return -EINVAL;
7772
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007773 tpacpi_disclose_usertask("hwmon pwm1_enable",
7774 "set fan mode to %lu\n", t);
7775
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007776 switch (t) {
7777 case 0:
7778 level = TP_EC_FAN_FULLSPEED;
7779 break;
7780 case 1:
7781 level = TPACPI_FAN_LAST_LEVEL;
7782 break;
7783 case 2:
7784 level = TP_EC_FAN_AUTO;
7785 break;
7786 case 3:
7787 /* reserved for software-controlled auto mode */
7788 return -ENOSYS;
7789 default:
7790 return -EINVAL;
7791 }
7792
7793 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007794 if (res == -ENXIO)
7795 return -EINVAL;
7796 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007797 return res;
7798
7799 fan_watchdog_reset();
7800
7801 return count;
7802}
7803
7804static struct device_attribute dev_attr_fan_pwm1_enable =
7805 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7806 fan_pwm1_enable_show, fan_pwm1_enable_store);
7807
7808/* sysfs fan pwm1 ------------------------------------------------------ */
7809static ssize_t fan_pwm1_show(struct device *dev,
7810 struct device_attribute *attr,
7811 char *buf)
7812{
7813 int res;
7814 u8 status;
7815
7816 res = fan_get_status_safe(&status);
7817 if (res)
7818 return res;
7819
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007820 if ((status &
7821 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7822 status = fan_control_desired_level;
7823
7824 if (status > 7)
7825 status = 7;
7826
7827 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7828}
7829
7830static ssize_t fan_pwm1_store(struct device *dev,
7831 struct device_attribute *attr,
7832 const char *buf, size_t count)
7833{
7834 unsigned long s;
7835 int rc;
7836 u8 status, newlevel;
7837
7838 if (parse_strtoul(buf, 255, &s))
7839 return -EINVAL;
7840
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007841 tpacpi_disclose_usertask("hwmon pwm1",
7842 "set fan speed to %lu\n", s);
7843
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007844 /* scale down from 0-255 to 0-7 */
7845 newlevel = (s >> 5) & 0x07;
7846
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007847 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007848 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007849
7850 rc = fan_get_status(&status);
7851 if (!rc && (status &
7852 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7853 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007854 if (rc == -ENXIO)
7855 rc = -EINVAL;
7856 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007857 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007858 fan_watchdog_reset();
7859 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007860 }
7861
7862 mutex_unlock(&fan_mutex);
7863 return (rc)? rc : count;
7864}
7865
7866static struct device_attribute dev_attr_fan_pwm1 =
7867 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7868 fan_pwm1_show, fan_pwm1_store);
7869
7870/* sysfs fan fan1_input ------------------------------------------------ */
7871static ssize_t fan_fan1_input_show(struct device *dev,
7872 struct device_attribute *attr,
7873 char *buf)
7874{
7875 int res;
7876 unsigned int speed;
7877
7878 res = fan_get_speed(&speed);
7879 if (res < 0)
7880 return res;
7881
7882 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7883}
7884
7885static struct device_attribute dev_attr_fan_fan1_input =
7886 __ATTR(fan1_input, S_IRUGO,
7887 fan_fan1_input_show, NULL);
7888
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007889/* sysfs fan fan2_input ------------------------------------------------ */
7890static ssize_t fan_fan2_input_show(struct device *dev,
7891 struct device_attribute *attr,
7892 char *buf)
7893{
7894 int res;
7895 unsigned int speed;
7896
7897 res = fan2_get_speed(&speed);
7898 if (res < 0)
7899 return res;
7900
7901 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7902}
7903
7904static struct device_attribute dev_attr_fan_fan2_input =
7905 __ATTR(fan2_input, S_IRUGO,
7906 fan_fan2_input_show, NULL);
7907
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007908/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007909static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7910 char *buf)
7911{
7912 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7913}
7914
7915static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7916 const char *buf, size_t count)
7917{
7918 unsigned long t;
7919
7920 if (parse_strtoul(buf, 120, &t))
7921 return -EINVAL;
7922
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007923 if (!fan_control_allowed)
7924 return -EPERM;
7925
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007926 fan_watchdog_maxinterval = t;
7927 fan_watchdog_reset();
7928
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007929 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7930
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007931 return count;
7932}
7933
7934static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7935 fan_fan_watchdog_show, fan_fan_watchdog_store);
7936
7937/* --------------------------------------------------------------------- */
7938static struct attribute *fan_attributes[] = {
7939 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7940 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007941 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007942 NULL
7943};
7944
7945static const struct attribute_group fan_attr_group = {
7946 .attrs = fan_attributes,
7947};
7948
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007949#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7950#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007951
7952#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7953 { .vendor = PCI_VENDOR_ID_IBM, \
7954 .bios = TPACPI_MATCH_ANY, \
7955 .ec = TPID(__id1, __id2), \
7956 .quirks = __quirks }
7957
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007958#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7959 { .vendor = PCI_VENDOR_ID_LENOVO, \
7960 .bios = TPACPI_MATCH_ANY, \
7961 .ec = TPID(__id1, __id2), \
7962 .quirks = __quirks }
7963
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007964static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7965 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7966 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7967 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7968 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007969 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007970};
7971
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007972#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007973#undef TPACPI_FAN_QI
7974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007975static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007976{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007977 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007978 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007979
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007980 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7981 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007982
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007983 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007984 fan_status_access_mode = TPACPI_FAN_NONE;
7985 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007986 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007987 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007988 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007989 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007990 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007991
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007992 if (tpacpi_is_ibm()) {
7993 TPACPI_ACPIHANDLE_INIT(fans);
7994 TPACPI_ACPIHANDLE_INIT(gfan);
7995 TPACPI_ACPIHANDLE_INIT(sfan);
7996 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007997
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007998 quirks = tpacpi_check_quirks(fan_quirk_table,
7999 ARRAY_SIZE(fan_quirk_table));
8000
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008001 if (gfan_handle) {
8002 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008003 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008004 } else {
8005 /* all other ThinkPads: note that even old-style
8006 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02008007 if (likely(acpi_ec_read(fan_status_offset,
8008 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008009 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03008010 if (quirks & TPACPI_FAN_Q1)
8011 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008012 if (quirks & TPACPI_FAN_2FAN) {
8013 tp_features.second_fan = 1;
8014 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8015 "secondary fan support enabled\n");
8016 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008017 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008018 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008019 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008020 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008021 }
8022 }
8023
8024 if (sfan_handle) {
8025 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008026 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008027 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008028 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008029 } else {
8030 if (!gfan_handle) {
8031 /* gfan without sfan means no fan control */
8032 /* all other models implement TP EC 0x2f control */
8033
8034 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008035 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008036 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008037 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008038 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008039 TPACPI_FAN_CMD_SPEED |
8040 TPACPI_FAN_CMD_LEVEL |
8041 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008042 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008043 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008044 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008045 TPACPI_FAN_CMD_LEVEL |
8046 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008047 }
8048 }
8049 }
8050
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008051 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8052 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008053 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8054 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8055 fan_status_access_mode, fan_control_access_mode);
8056
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008057 /* fan control master switch */
8058 if (!fan_control_allowed) {
8059 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8060 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008061 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008062 "fan control features disabled by parameter\n");
8063 }
8064
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008065 /* update fan_control_desired_level */
8066 if (fan_status_access_mode != TPACPI_FAN_NONE)
8067 fan_get_status_safe(NULL);
8068
8069 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8070 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008071 if (tp_features.second_fan) {
8072 /* attach second fan tachometer */
8073 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8074 &dev_attr_fan_fan2_input.attr;
8075 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008076 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008077 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008078 if (rc < 0)
8079 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008080
8081 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8082 &driver_attr_fan_watchdog);
8083 if (rc < 0) {
8084 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8085 &fan_attr_group);
8086 return rc;
8087 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008088 return 0;
8089 } else
8090 return 1;
8091}
8092
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008093static void fan_exit(void)
8094{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008095 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008096 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008097
8098 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008099 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008100 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8101 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008102
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008103 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008104 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008105}
8106
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008107static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008108{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008109 int rc;
8110
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008111 if (!fan_control_allowed)
8112 return;
8113
8114 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008115 fan_control_resume_level = 0;
8116 rc = fan_get_status_safe(&fan_control_resume_level);
8117 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008118 pr_notice("failed to read fan level for later "
8119 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008120
8121 /* if it is undefined, don't attempt to restore it.
8122 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008123 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008124 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008125}
8126
8127static void fan_resume(void)
8128{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008129 u8 current_level = 7;
8130 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008131 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008132
8133 /* DSDT *always* updates status on resume */
8134 tp_features.fan_ctrl_status_undef = 0;
8135
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008136 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008137 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008138 (fan_get_status_safe(&current_level) < 0))
8139 return;
8140
8141 switch (fan_control_access_mode) {
8142 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008143 /* never decrease fan level */
8144 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008145 break;
8146 case TPACPI_FAN_WR_ACPI_FANS:
8147 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008148 /* never decrease fan level, scale is:
8149 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8150 *
8151 * We expect the firmware to set either 7 or AUTO, but we
8152 * handle FULLSPEED out of paranoia.
8153 *
8154 * So, we can safely only restore FULLSPEED or 7, anything
8155 * else could slow the fan. Restoring AUTO is useless, at
8156 * best that's exactly what the DSDT already set (it is the
8157 * slower it uses).
8158 *
8159 * Always keep in mind that the DSDT *will* have set the
8160 * fans to what the vendor supposes is the best level. We
8161 * muck with it only to speed the fan up.
8162 */
8163 if (fan_control_resume_level != 7 &&
8164 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8165 return;
8166 else
8167 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8168 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008169 break;
8170 default:
8171 return;
8172 }
8173 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008174 pr_notice("restoring fan level to 0x%02x\n",
8175 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008176 rc = fan_set_level_safe(fan_control_resume_level);
8177 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008178 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008179 }
8180}
8181
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008182static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008183{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008184 int rc;
8185 u8 status;
8186 unsigned int speed = 0;
8187
8188 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008189 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008190 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008191 rc = fan_get_status_safe(&status);
8192 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008193 return rc;
8194
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008195 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008196 "level:\t\t%d\n",
8197 (status != 0) ? "enabled" : "disabled", status);
8198 break;
8199
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008200 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008201 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008202 rc = fan_get_status_safe(&status);
8203 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008204 return rc;
8205
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008206 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008207 (status != 0) ? "enabled" : "disabled");
8208
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008209 rc = fan_get_speed(&speed);
8210 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008211 return rc;
8212
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008213 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008214
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008215 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008216 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008217 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008218 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008219 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008220 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008221 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008222 break;
8223
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008224 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008225 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008226 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008227 }
8228
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008229 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008230 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008231
8232 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008233 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008234 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008235 break;
8236
8237 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008238 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008239 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008240 break;
8241 }
8242 }
8243
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008244 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008245 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008246 "commands:\twatchdog <timeout> (<timeout> "
8247 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008248
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008249 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008250 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008251 " (<speed> is 0-65535)\n");
8252
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008253 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008254}
8255
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008256static int fan_write_cmd_level(const char *cmd, int *rc)
8257{
8258 int level;
8259
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008260 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008261 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008262 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008263 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008264 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008265 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008266 return 0;
8267
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008268 *rc = fan_set_level_safe(level);
8269 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008270 pr_err("level command accepted for unsupported access mode %d\n",
8271 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008272 else if (!*rc)
8273 tpacpi_disclose_usertask("procfs fan",
8274 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008275
8276 return 1;
8277}
8278
8279static int fan_write_cmd_enable(const char *cmd, int *rc)
8280{
8281 if (strlencmp(cmd, "enable") != 0)
8282 return 0;
8283
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008284 *rc = fan_set_enable();
8285 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008286 pr_err("enable 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", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008290
8291 return 1;
8292}
8293
8294static int fan_write_cmd_disable(const char *cmd, int *rc)
8295{
8296 if (strlencmp(cmd, "disable") != 0)
8297 return 0;
8298
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008299 *rc = fan_set_disable();
8300 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008301 pr_err("disable command accepted for unsupported access mode %d\n",
8302 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008303 else if (!*rc)
8304 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008305
8306 return 1;
8307}
8308
8309static int fan_write_cmd_speed(const char *cmd, int *rc)
8310{
8311 int speed;
8312
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008313 /* TODO:
8314 * Support speed <low> <medium> <high> ? */
8315
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008316 if (sscanf(cmd, "speed %d", &speed) != 1)
8317 return 0;
8318
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008319 *rc = fan_set_speed(speed);
8320 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008321 pr_err("speed command accepted for unsupported access mode %d\n",
8322 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008323 else if (!*rc)
8324 tpacpi_disclose_usertask("procfs fan",
8325 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008326
8327 return 1;
8328}
8329
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008330static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8331{
8332 int interval;
8333
8334 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8335 return 0;
8336
8337 if (interval < 0 || interval > 120)
8338 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008339 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008340 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008341 tpacpi_disclose_usertask("procfs fan",
8342 "set watchdog timer to %d\n",
8343 interval);
8344 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008345
8346 return 1;
8347}
8348
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008349static int fan_write(char *buf)
8350{
8351 char *cmd;
8352 int rc = 0;
8353
8354 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008355 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008356 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008357 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008358 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008359 fan_write_cmd_disable(cmd, &rc) ||
8360 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008361 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008362 fan_write_cmd_speed(cmd, &rc))
8363 )
8364 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008365 else if (!rc)
8366 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008367 }
8368
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008369 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008370}
8371
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008372static struct ibm_struct fan_driver_data = {
8373 .name = "fan",
8374 .read = fan_read,
8375 .write = fan_write,
8376 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008377 .suspend = fan_suspend,
8378 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008379};
8380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008381/****************************************************************************
8382 ****************************************************************************
8383 *
8384 * Infrastructure
8385 *
8386 ****************************************************************************
8387 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008389/*
8390 * HKEY event callout for other subdrivers go here
8391 * (yes, it is ugly, but it is quick, safe, and gets the job done
8392 */
8393static void tpacpi_driver_event(const unsigned int hkey_event)
8394{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008395 if (ibm_backlight_device) {
8396 switch (hkey_event) {
8397 case TP_HKEY_EV_BRGHT_UP:
8398 case TP_HKEY_EV_BRGHT_DOWN:
8399 tpacpi_brightness_notify_change();
8400 }
8401 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008402 if (alsa_card) {
8403 switch (hkey_event) {
8404 case TP_HKEY_EV_VOL_UP:
8405 case TP_HKEY_EV_VOL_DOWN:
8406 case TP_HKEY_EV_VOL_MUTE:
8407 volume_alsa_notify_change();
8408 }
8409 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008410}
8411
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008412static void hotkey_driver_event(const unsigned int scancode)
8413{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008414 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008415}
8416
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008417/* sysfs name ---------------------------------------------------------- */
8418static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8419 struct device_attribute *attr,
8420 char *buf)
8421{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008422 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008423}
8424
8425static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8426 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8427
8428/* --------------------------------------------------------------------- */
8429
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008430/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008431static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008433/*
8434 * Module and infrastructure proble, init and exit handling
8435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308437static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008438
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008439#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008440static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008441{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008442 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008443
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008444 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008445}
8446#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8447
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008448static void ibm_exit(struct ibm_struct *ibm)
8449{
8450 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8451
8452 list_del_init(&ibm->all_drivers);
8453
8454 if (ibm->flags.acpi_notify_installed) {
8455 dbg_printk(TPACPI_DBG_EXIT,
8456 "%s: acpi_remove_notify_handler\n", ibm->name);
8457 BUG_ON(!ibm->acpi);
8458 acpi_remove_notify_handler(*ibm->acpi->handle,
8459 ibm->acpi->type,
8460 dispatch_acpi_notify);
8461 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008462 }
8463
8464 if (ibm->flags.proc_created) {
8465 dbg_printk(TPACPI_DBG_EXIT,
8466 "%s: remove_proc_entry\n", ibm->name);
8467 remove_proc_entry(ibm->name, proc_dir);
8468 ibm->flags.proc_created = 0;
8469 }
8470
8471 if (ibm->flags.acpi_driver_registered) {
8472 dbg_printk(TPACPI_DBG_EXIT,
8473 "%s: acpi_bus_unregister_driver\n", ibm->name);
8474 BUG_ON(!ibm->acpi);
8475 acpi_bus_unregister_driver(ibm->acpi->driver);
8476 kfree(ibm->acpi->driver);
8477 ibm->acpi->driver = NULL;
8478 ibm->flags.acpi_driver_registered = 0;
8479 }
8480
8481 if (ibm->flags.init_called && ibm->exit) {
8482 ibm->exit();
8483 ibm->flags.init_called = 0;
8484 }
8485
8486 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8487}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008488
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008489static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490{
8491 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008492 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 struct proc_dir_entry *entry;
8494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008495 BUG_ON(ibm == NULL);
8496
8497 INIT_LIST_HEAD(&ibm->all_drivers);
8498
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008499 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 return 0;
8501
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008502 dbg_printk(TPACPI_DBG_INIT,
8503 "probing for %s\n", ibm->name);
8504
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008505 if (iibm->init) {
8506 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008507 if (ret > 0)
8508 return 0; /* probe failed */
8509 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008511
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008512 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 }
8514
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008515 if (ibm->acpi) {
8516 if (ibm->acpi->hid) {
8517 ret = register_tpacpi_subdriver(ibm);
8518 if (ret)
8519 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008520 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008521
8522 if (ibm->acpi->notify) {
8523 ret = setup_acpi_notify(ibm);
8524 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008525 pr_notice("disabling subdriver %s\n",
8526 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008527 ret = 0;
8528 goto err_out;
8529 }
8530 if (ret < 0)
8531 goto err_out;
8532 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008533 }
8534
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008535 dbg_printk(TPACPI_DBG_INIT,
8536 "%s installed\n", ibm->name);
8537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008538 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008539 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008540
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008541 if (!mode)
8542 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008543 if (ibm->write)
8544 mode |= S_IWUSR;
8545 entry = proc_create_data(ibm->name, mode, proc_dir,
8546 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008547 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008548 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008549 ret = -ENODEV;
8550 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008551 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008552 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008555 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008558
8559err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008560 dbg_printk(TPACPI_DBG_INIT,
8561 "%s: at error exit path with result %d\n",
8562 ibm->name, ret);
8563
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008564 ibm_exit(ibm);
8565 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566}
8567
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008568/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008570static bool __pure __init tpacpi_is_fw_digit(const char c)
8571{
8572 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8573}
8574
8575/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8576static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8577 const char t)
8578{
8579 return s && strlen(s) >= 8 &&
8580 tpacpi_is_fw_digit(s[0]) &&
8581 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008582 s[2] == t &&
8583 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008584 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008585 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008586}
8587
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008588/* returns 0 - probe ok, or < 0 - probe error.
8589 * Probe ok doesn't mean thinkpad found.
8590 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8591static int __must_check __init get_thinkpad_model_data(
8592 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008593{
Jeff Garzik18552562007-10-03 15:15:40 -04008594 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008595 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008596 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008597
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008598 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008599 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008600
8601 memset(tp, 0, sizeof(*tp));
8602
8603 if (dmi_name_in_vendors("IBM"))
8604 tp->vendor = PCI_VENDOR_ID_IBM;
8605 else if (dmi_name_in_vendors("LENOVO"))
8606 tp->vendor = PCI_VENDOR_ID_LENOVO;
8607 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008608 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008609
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008610 s = dmi_get_system_info(DMI_BIOS_VERSION);
8611 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8612 if (s && !tp->bios_version_str)
8613 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008614
8615 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008616 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8617 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008618 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008619
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008620 tp->bios_model = tp->bios_version_str[0]
8621 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008622 tp->bios_release = (tp->bios_version_str[4] << 8)
8623 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008624
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008625 /*
8626 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8627 * X32 or newer, all Z series; Some models must have an
8628 * up-to-date BIOS or they will not be detected.
8629 *
8630 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8631 */
8632 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008633 if (sscanf(dev->name,
8634 "IBM ThinkPad Embedded Controller -[%17c",
8635 ec_fw_string) == 1) {
8636 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8637 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008638
8639 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008640 if (!tp->ec_version_str)
8641 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008642
8643 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8644 tp->ec_model = ec_fw_string[0]
8645 | (ec_fw_string[1] << 8);
8646 tp->ec_release = (ec_fw_string[4] << 8)
8647 | ec_fw_string[5];
8648 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008649 pr_notice("ThinkPad firmware release %s "
8650 "doesn't match the known patterns\n",
8651 ec_fw_string);
8652 pr_notice("please report this to %s\n",
8653 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008654 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008655 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008656 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008657 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008658
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008659 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Manoj Iyer23b05312012-03-09 17:32:24 -06008660 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008661 tp->model_str = kstrdup(s, GFP_KERNEL);
8662 if (!tp->model_str)
8663 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05008664 } else {
8665 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8666 if (s && !(strnicmp(s, "Lenovo", 6))) {
8667 tp->model_str = kstrdup(s, GFP_KERNEL);
8668 if (!tp->model_str)
8669 return -ENOMEM;
8670 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008671 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008672
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008673 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8674 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8675 if (s && !tp->nummodel_str)
8676 return -ENOMEM;
8677
8678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679}
8680
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008681static int __init probe_for_thinkpad(void)
8682{
8683 int is_thinkpad;
8684
8685 if (acpi_disabled)
8686 return -ENODEV;
8687
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008688 /* It would be dangerous to run the driver in this case */
8689 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8690 return -ENODEV;
8691
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008692 /*
8693 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008694 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008695 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008696 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8697 (thinkpad_id.ec_model != 0) ||
8698 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008699
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008700 /* The EC handler is required */
8701 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008702 if (!ec_handle) {
8703 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008704 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008705 return -ENODEV;
8706 }
8707
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008708 if (!is_thinkpad && !force_load)
8709 return -ENODEV;
8710
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008711 return 0;
8712}
8713
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008714static void __init thinkpad_acpi_init_banner(void)
8715{
Joe Perches0978e012011-04-04 10:06:25 -07008716 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8717 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008718
Joe Perches0978e012011-04-04 10:06:25 -07008719 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008720 (thinkpad_id.bios_version_str) ?
8721 thinkpad_id.bios_version_str : "unknown",
8722 (thinkpad_id.ec_version_str) ?
8723 thinkpad_id.ec_version_str : "unknown");
8724
8725 BUG_ON(!thinkpad_id.vendor);
8726
8727 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008728 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008729 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8730 "IBM" : ((thinkpad_id.vendor ==
8731 PCI_VENDOR_ID_LENOVO) ?
8732 "Lenovo" : "Unknown vendor"),
8733 thinkpad_id.model_str,
8734 (thinkpad_id.nummodel_str) ?
8735 thinkpad_id.nummodel_str : "unknown");
8736}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008737
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008738/* Module init, exit, parameters */
8739
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008740static struct ibm_init_struct ibms_init[] __initdata = {
8741 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008742 .data = &thinkpad_acpi_driver_data,
8743 },
8744 {
8745 .init = hotkey_init,
8746 .data = &hotkey_driver_data,
8747 },
8748 {
8749 .init = bluetooth_init,
8750 .data = &bluetooth_driver_data,
8751 },
8752 {
8753 .init = wan_init,
8754 .data = &wan_driver_data,
8755 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008756 {
8757 .init = uwb_init,
8758 .data = &uwb_driver_data,
8759 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008760#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008761 {
8762 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008763 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008764 .data = &video_driver_data,
8765 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008766#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008767 {
8768 .init = light_init,
8769 .data = &light_driver_data,
8770 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008771 {
8772 .init = cmos_init,
8773 .data = &cmos_driver_data,
8774 },
8775 {
8776 .init = led_init,
8777 .data = &led_driver_data,
8778 },
8779 {
8780 .init = beep_init,
8781 .data = &beep_driver_data,
8782 },
8783 {
8784 .init = thermal_init,
8785 .data = &thermal_driver_data,
8786 },
8787 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008788 .init = brightness_init,
8789 .data = &brightness_driver_data,
8790 },
8791 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008792 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008793 .data = &volume_driver_data,
8794 },
8795 {
8796 .init = fan_init,
8797 .data = &fan_driver_data,
8798 },
8799};
8800
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008801static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8802{
8803 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008804 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008805
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008806 if (!kp || !kp->name || !val)
8807 return -EINVAL;
8808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008809 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8810 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008811 WARN_ON(ibm == NULL);
8812
8813 if (!ibm || !ibm->name)
8814 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008815
8816 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8817 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008818 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008819 strcpy(ibms_init[i].param, val);
8820 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008821 return 0;
8822 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008823 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008824
8825 return -EINVAL;
8826}
8827
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008828module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008829MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008830 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008831
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008832module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008833MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008834
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008835module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008836MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008837 "Attempts to load the driver even on a "
8838 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008839
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008840module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008841MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008842 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008843
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008844module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008845MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008846 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008847 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008848
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008849module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008850MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008851 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008852
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008853module_param(hotkey_report_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008854MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008855 "used for backwards compatibility with userspace, "
8856 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008857
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008858#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008859module_param_named(volume_mode, volume_mode, uint, 0444);
8860MODULE_PARM_DESC(volume_mode,
8861 "Selects volume control strategy: "
8862 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8863
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008864module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8865MODULE_PARM_DESC(volume_capabilities,
8866 "Selects the mixer capabilites: "
8867 "0=auto, 1=volume and mute, 2=mute only");
8868
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008869module_param_named(volume_control, volume_control_allowed, bool, 0444);
8870MODULE_PARM_DESC(volume_control,
8871 "Enables software override for the console audio "
8872 "control when true");
8873
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008874/* ALSA module API parameters */
8875module_param_named(index, alsa_index, int, 0444);
8876MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8877module_param_named(id, alsa_id, charp, 0444);
8878MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8879module_param_named(enable, alsa_enable, bool, 0444);
8880MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008881#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008882
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008883#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008884 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008885 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008886 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008887
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008888TPACPI_PARAM(hotkey);
8889TPACPI_PARAM(bluetooth);
8890TPACPI_PARAM(video);
8891TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008892TPACPI_PARAM(cmos);
8893TPACPI_PARAM(led);
8894TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008895TPACPI_PARAM(brightness);
8896TPACPI_PARAM(volume);
8897TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008898
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008899#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008900module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008901MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8902module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8903MODULE_PARM_DESC(wlsw_state,
8904 "Initial state of the emulated WLSW switch");
8905
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008906module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008907MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8908module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8909MODULE_PARM_DESC(bluetooth_state,
8910 "Initial state of the emulated bluetooth switch");
8911
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008912module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008913MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8914module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8915MODULE_PARM_DESC(wwan_state,
8916 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008917
Henrique de Moraes Holschuhb09c7222009-12-09 01:36:27 +00008918module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008919MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8920module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8921MODULE_PARM_DESC(uwb_state,
8922 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008923#endif
8924
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008925static void thinkpad_acpi_module_exit(void)
8926{
8927 struct ibm_struct *ibm, *itmp;
8928
8929 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8930
8931 list_for_each_entry_safe_reverse(ibm, itmp,
8932 &tpacpi_all_drivers,
8933 all_drivers) {
8934 ibm_exit(ibm);
8935 }
8936
8937 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8938
8939 if (tpacpi_inputdev) {
8940 if (tp_features.input_device_registered)
8941 input_unregister_device(tpacpi_inputdev);
8942 else
8943 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10008944 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008945 }
8946
8947 if (tpacpi_hwmon)
8948 hwmon_device_unregister(tpacpi_hwmon);
8949
8950 if (tp_features.sensors_pdev_attrs_registered)
8951 device_remove_file(&tpacpi_sensors_pdev->dev,
8952 &dev_attr_thinkpad_acpi_pdev_name);
8953 if (tpacpi_sensors_pdev)
8954 platform_device_unregister(tpacpi_sensors_pdev);
8955 if (tpacpi_pdev)
8956 platform_device_unregister(tpacpi_pdev);
8957
8958 if (tp_features.sensors_pdrv_attrs_registered)
8959 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8960 if (tp_features.platform_drv_attrs_registered)
8961 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8962
8963 if (tp_features.sensors_pdrv_registered)
8964 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8965
8966 if (tp_features.platform_drv_registered)
8967 platform_driver_unregister(&tpacpi_pdriver);
8968
8969 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008970 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008971
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008972 if (tpacpi_wq)
8973 destroy_workqueue(tpacpi_wq);
8974
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008975 kfree(thinkpad_id.bios_version_str);
8976 kfree(thinkpad_id.ec_version_str);
8977 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10008978 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008979}
8980
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008981
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03008982static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983{
8984 int ret, i;
8985
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03008986 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8987
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03008988 /* Parameter checking */
8989 if (hotkey_report_mode > 2)
8990 return -EINVAL;
8991
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03008992 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008993
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008994 ret = get_thinkpad_model_data(&thinkpad_id);
8995 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07008996 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008997 thinkpad_acpi_module_exit();
8998 return ret;
8999 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009000 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009001 if (ret) {
9002 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009003 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009006 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009007
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009008 thinkpad_acpi_init_banner();
9009 tpacpi_check_outdated_fw();
9010
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009011 TPACPI_ACPIHANDLE_INIT(ecrd);
9012 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009014 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9015 if (!tpacpi_wq) {
9016 thinkpad_acpi_module_exit();
9017 return -ENOMEM;
9018 }
9019
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009020 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009022 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009023 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024 return -ENODEV;
9025 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009026
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009027 ret = platform_driver_register(&tpacpi_pdriver);
9028 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009029 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009030 thinkpad_acpi_module_exit();
9031 return ret;
9032 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009033 tp_features.platform_drv_registered = 1;
9034
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009035 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9036 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009037 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009038 thinkpad_acpi_module_exit();
9039 return ret;
9040 }
9041 tp_features.sensors_pdrv_registered = 1;
9042
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009043 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009044 if (!ret) {
9045 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009046 ret = tpacpi_create_driver_attributes(
9047 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009048 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009049 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009050 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009051 thinkpad_acpi_module_exit();
9052 return ret;
9053 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009054 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009055
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009056
9057 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009058 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009059 NULL, 0);
9060 if (IS_ERR(tpacpi_pdev)) {
9061 ret = PTR_ERR(tpacpi_pdev);
9062 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009063 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009064 thinkpad_acpi_module_exit();
9065 return ret;
9066 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009067 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009068 TPACPI_HWMON_DRVR_NAME,
9069 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009070 if (IS_ERR(tpacpi_sensors_pdev)) {
9071 ret = PTR_ERR(tpacpi_sensors_pdev);
9072 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009073 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009074 thinkpad_acpi_module_exit();
9075 return ret;
9076 }
9077 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9078 &dev_attr_thinkpad_acpi_pdev_name);
9079 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009080 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009081 thinkpad_acpi_module_exit();
9082 return ret;
9083 }
9084 tp_features.sensors_pdev_attrs_registered = 1;
9085 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009086 if (IS_ERR(tpacpi_hwmon)) {
9087 ret = PTR_ERR(tpacpi_hwmon);
9088 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009089 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009090 thinkpad_acpi_module_exit();
9091 return ret;
9092 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009093 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009094 tpacpi_inputdev = input_allocate_device();
9095 if (!tpacpi_inputdev) {
Joe Perches0978e012011-04-04 10:06:25 -07009096 pr_err("unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009097 thinkpad_acpi_module_exit();
9098 return -ENOMEM;
9099 } else {
9100 /* Prepare input device, but don't register */
9101 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009102 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009103 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009104 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009105 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9106 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009107 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009108 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009109
9110 /* Init subdriver dependencies */
9111 tpacpi_detect_brightness_capabilities();
9112
9113 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009114 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9115 ret = ibm_init(&ibms_init[i]);
9116 if (ret >= 0 && *ibms_init[i].param)
9117 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009119 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 return ret;
9121 }
9122 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009123
9124 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9125
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009126 ret = input_register_device(tpacpi_inputdev);
9127 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009128 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009129 thinkpad_acpi_module_exit();
9130 return ret;
9131 } else {
9132 tp_features.input_device_registered = 1;
9133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134
9135 return 0;
9136}
9137
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009138MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9139
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009140/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009141 * This will autoload the driver in almost every ThinkPad
9142 * in widespread use.
9143 *
9144 * Only _VERY_ old models, like the 240, 240x and 570 lack
9145 * the HKEY event interface.
9146 */
9147MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9148
9149/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009150 * DMI matching for module autoloading
9151 *
9152 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9153 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9154 *
9155 * Only models listed in thinkwiki will be supported, so add yours
9156 * if it is not there yet.
9157 */
9158#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009159 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009160
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009161/* Ancient thinkpad BIOSes have to be identified by
9162 * BIOS type or model number, and there are far less
9163 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009164IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009165
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009166MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9167MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009168MODULE_DESCRIPTION(TPACPI_DESC);
9169MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009170MODULE_LICENSE("GPL");
9171
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009172module_init(thinkpad_acpi_module_init);
9173module_exit(thinkpad_acpi_module_exit);