blob: 0b7efb269cf1ae565f52ced999340d45c19c914c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh1c762ca2009-04-04 04:25:42 +00006 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Joe Perches0978e012011-04-04 10:06:25 -070024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
David Henningsson420f9732013-10-16 23:10:31 +020026#define TPACPI_VERSION "0.25"
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -020027#define TPACPI_SYSFS_VERSION 0x020700
Borislav Deianov78f81cc2005-08-17 00:00:00 -040028
29/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020031 * 2007-10-20 changelog trimmed down
32 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030033 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020035 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030039 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040040 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020042 *
43 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040044 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
51
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020052#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/types.h>
56#include <linux/string.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +000059#include <linux/sched.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020060#include <linux/kthread.h>
61#include <linux/freezer.h>
62#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020064
65#include <linux/nvram.h>
66#include <linux/proc_fs.h>
Alexey Dobriyan887965e2009-12-15 21:51:12 -020067#include <linux/seq_file.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020068#include <linux/sysfs.h>
69#include <linux/backlight.h>
70#include <linux/fb.h>
71#include <linux/platform_device.h>
72#include <linux/hwmon.h>
73#include <linux/hwmon-sysfs.h>
74#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030075#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030076#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020077#include <asm/uaccess.h>
78
79#include <linux/dmi.h>
80#include <linux/jiffies.h>
81#include <linux/workqueue.h>
82
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -020083#include <sound/core.h>
84#include <sound/control.h>
85#include <sound/initval.h>
86
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020087#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020088
89#include <linux/pci_ids.h>
90
David Henningsson420f9732013-10-16 23:10:31 +020091#include <linux/thinkpad_acpi.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* ThinkPad CMOS commands */
94#define TP_CMOS_VOLUME_DOWN 0
95#define TP_CMOS_VOLUME_UP 1
96#define TP_CMOS_VOLUME_MUTE 2
97#define TP_CMOS_BRIGHTNESS_UP 4
98#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030099#define TP_CMOS_THINKLIGHT_ON 12
100#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200101
102/* NVRAM Addresses */
103enum tp_nvram_addr {
104 TP_NVRAM_ADDR_HK2 = 0x57,
105 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
106 TP_NVRAM_ADDR_VIDEO = 0x59,
107 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
108 TP_NVRAM_ADDR_MIXER = 0x60,
109};
110
111/* NVRAM bit masks */
112enum {
113 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
114 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
115 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
116 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
117 TP_NVRAM_MASK_THINKLIGHT = 0x10,
118 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
119 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
120 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
121 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
122 TP_NVRAM_MASK_MUTE = 0x40,
123 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
124 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
125 TP_NVRAM_POS_LEVEL_VOLUME = 0,
126};
127
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -0300128/* Misc NVRAM-related */
129enum {
130 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
131};
132
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200133/* ACPI HIDs */
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -0400134#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
135#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300136#define TPACPI_ACPI_EC_HID "PNP0C09"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
138/* Input IDs */
139#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
140#define TPACPI_HKEY_INPUT_VERSION 0x4101
141
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200142/* ACPI \WGSV commands */
143enum {
144 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
145 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
146 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
147 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
148};
149
150/* TP_ACPI_WGSV_GET_STATE bits */
151enum {
152 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
153 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
154 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
155 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
156 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
157 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
158 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
159 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
160 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
161 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
162};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200163
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300164/* HKEY events */
165enum tpacpi_hkey_event_t {
166 /* Hotkey-related */
167 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
168 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
169 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -0300188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300192
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
205
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300206 /* Thermal events */
207 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
208 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
209 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
210 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
211 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
212
Richard Hartmann6f62bc32012-12-29 22:51:49 +0100213 /* AC-related events */
214 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -0300215
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -0300216 /* Misc */
217 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218};
219
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200220/****************************************************************************
221 * Main driver
222 */
223
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200224#define TPACPI_NAME "thinkpad"
225#define TPACPI_DESC "ThinkPad ACPI Extras"
226#define TPACPI_FILE TPACPI_NAME "_acpi"
227#define TPACPI_URL "http://ibm-acpi.sf.net/"
228#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200229
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200230#define TPACPI_PROC_DIR "ibm"
231#define TPACPI_ACPI_EVENT_PREFIX "ibm"
232#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300233#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200234#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200235
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300236#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300237#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300238
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200239#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200240
Henrique de Moraes Holschuh7ff8d622009-04-04 04:25:46 +0000241/* Debugging printk groups */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000243#define TPACPI_DBG_DISCLOSETASK 0x8000
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244#define TPACPI_DBG_INIT 0x0001
245#define TPACPI_DBG_EXIT 0x0002
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +0000246#define TPACPI_DBG_RFKILL 0x0004
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +0000247#define TPACPI_DBG_HKEY 0x0008
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +0000248#define TPACPI_DBG_FAN 0x0010
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +0000249#define TPACPI_DBG_BRGHT 0x0020
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -0200250#define TPACPI_DBG_MIXER 0x0040
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200252#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
253#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
254#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200255
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256
257/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259 */
260
261struct ibm_struct;
262
263struct tp_acpi_drv_struct {
264 const struct acpi_device_id *hid;
265 struct acpi_driver *driver;
266
267 void (*notify) (struct ibm_struct *, u32);
268 acpi_handle *handle;
269 u32 type;
270 struct acpi_device *device;
271};
272
273struct ibm_struct {
274 char *name;
275
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200276 int (*read) (struct seq_file *);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200277 int (*write) (char *);
278 void (*exit) (void);
279 void (*resume) (void);
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200280 void (*suspend) (void);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200281 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200282
283 struct list_head all_drivers;
284
285 struct tp_acpi_drv_struct *acpi;
286
287 struct {
288 u8 acpi_driver_registered:1;
289 u8 acpi_notify_installed:1;
290 u8 proc_created:1;
291 u8 init_called:1;
292 u8 experimental:1;
293 } flags;
294};
295
296struct ibm_init_struct {
297 char param[32];
298
299 int (*init) (struct ibm_init_struct *);
Al Virod161a132011-07-24 03:36:29 -0400300 umode_t base_procfs_mode;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200301 struct ibm_struct *data;
302};
303
304static struct {
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200305 u32 bluetooth:1;
306 u32 hotkey:1;
307 u32 hotkey_mask:1;
308 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200309 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200310 u32 light:1;
311 u32 light_status:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300312 u32 bright_acpimode:1;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300313 u32 bright_unkfw:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200314 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200315 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200316 u32 fan_ctrl_status_undef:1;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -0300317 u32 second_fan:1;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -0300318 u32 beep_needs_two_args:1;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200319 u32 mixer_no_level_control:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200320 u32 input_device_registered:1;
321 u32 platform_drv_registered:1;
322 u32 platform_drv_attrs_registered:1;
323 u32 sensors_pdrv_registered:1;
324 u32 sensors_pdrv_attrs_registered:1;
325 u32 sensors_pdev_attrs_registered:1;
326 u32 hotkey_poll_active:1;
327} tp_features;
328
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300329static struct {
330 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -0200331 u16 volume_ctrl_forbidden:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300332} tp_warned;
333
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200334struct thinkpad_id_data {
335 unsigned int vendor; /* ThinkPad vendor:
336 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
337
338 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
339 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
340
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300341 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200342 u16 ec_model;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -0300343 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
344 u16 ec_release;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200345
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300346 char *model_str; /* ThinkPad T43 */
347 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200348};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200349static struct thinkpad_id_data thinkpad_id;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351static enum {
352 TPACPI_LIFE_INIT = 0,
353 TPACPI_LIFE_RUNNING,
354 TPACPI_LIFE_EXITING,
355} tpacpi_lifecycle;
356
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200357static int experimental;
358static u32 dbg_level;
359
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300360static struct workqueue_struct *tpacpi_wq;
361
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000362enum led_status_t {
363 TPACPI_LED_OFF = 0,
364 TPACPI_LED_ON,
365 TPACPI_LED_BLINK,
366};
367
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300368/* Special LED class that can defer work */
369struct tpacpi_led_classdev {
370 struct led_classdev led_classdev;
371 struct work_struct work;
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +0000372 enum led_status_t new_state;
Adam Leeedf2d772013-06-08 16:51:15 +0800373 int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300374};
375
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -0300376/* brightness level capabilities */
377static unsigned int bright_maxlvl; /* 0 = unknown */
378
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200379#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
380static int dbg_wlswemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030381static bool tpacpi_wlsw_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200382static int dbg_bluetoothemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030383static bool tpacpi_bluetooth_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200384static int dbg_wwanemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030385static bool tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200386static int dbg_uwbemul;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030387static bool tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200388#endif
389
390
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000391/*************************************************************************
392 * Debugging helpers
393 */
394
Joe Perches0978e012011-04-04 10:06:25 -0700395#define dbg_printk(a_dbg_level, format, arg...) \
396do { \
397 if (dbg_level & (a_dbg_level)) \
398 printk(KERN_DEBUG pr_fmt("%s: " format), \
399 __func__, ##arg); \
400} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000401
402#ifdef CONFIG_THINKPAD_ACPI_DEBUG
403#define vdbg_printk dbg_printk
404static const char *str_supported(int is_supported);
405#else
Joe Perches0978e012011-04-04 10:06:25 -0700406static inline const char *str_supported(int is_supported) { return ""; }
407#define vdbg_printk(a_dbg_level, format, arg...) \
408 no_printk(format, ##arg)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000409#endif
410
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000411static void tpacpi_log_usertask(const char * const what)
412{
Joe Perches0978e012011-04-04 10:06:25 -0700413 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
414 what, task_tgid_vnr(current));
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +0000415}
416
Joe Perches0978e012011-04-04 10:06:25 -0700417#define tpacpi_disclose_usertask(what, format, arg...) \
418do { \
419 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
420 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
421 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
422 what, task_tgid_vnr(current), ## arg); \
423 } \
424} while (0)
Henrique de Moraes Holschuh3dcc2c32009-04-04 04:25:45 +0000425
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300426/*
427 * Quirk handling helpers
428 *
429 * ThinkPad IDs and versions seen in the field so far
430 * are two-characters from the set [0-9A-Z], i.e. base 36.
431 *
432 * We use values well outside that range as specials.
433 */
434
435#define TPACPI_MATCH_ANY 0xffffU
436#define TPACPI_MATCH_UNKNOWN 0U
437
438/* TPID('1', 'Y') == 0x5931 */
439#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
440
441#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_IBM, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
446
447#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -0200453#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPACPI_MATCH_ANY, \
456 .ec = TPID(__id1, __id2), \
457 .quirks = (__quirk) }
458
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300459struct tpacpi_quirk {
460 unsigned int vendor;
461 u16 bios;
462 u16 ec;
463 unsigned long quirks;
464};
465
466/**
467 * tpacpi_check_quirks() - search BIOS/EC version on a list
468 * @qlist: array of &struct tpacpi_quirk
469 * @qlist_size: number of elements in @qlist
470 *
471 * Iterates over a quirks list until one is found that matches the
472 * ThinkPad's vendor, BIOS and EC model.
473 *
474 * Returns 0 if nothing matches, otherwise returns the quirks field of
475 * the matching &struct tpacpi_quirk entry.
476 *
477 * The match criteria is: vendor, ec and bios much match.
478 */
479static unsigned long __init tpacpi_check_quirks(
480 const struct tpacpi_quirk *qlist,
481 unsigned int qlist_size)
482{
483 while (qlist_size) {
484 if ((qlist->vendor == thinkpad_id.vendor ||
485 qlist->vendor == TPACPI_MATCH_ANY) &&
486 (qlist->bios == thinkpad_id.bios_model ||
487 qlist->bios == TPACPI_MATCH_ANY) &&
488 (qlist->ec == thinkpad_id.ec_model ||
489 qlist->ec == TPACPI_MATCH_ANY))
490 return qlist->quirks;
491
492 qlist_size--;
493 qlist++;
494 }
495 return 0;
496}
497
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -0300498static inline bool __pure __init tpacpi_is_lenovo(void)
499{
500 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
501}
502
503static inline bool __pure __init tpacpi_is_ibm(void)
504{
505 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
506}
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -0300507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508/****************************************************************************
509 ****************************************************************************
510 *
511 * ACPI Helpers and device model
512 *
513 ****************************************************************************
514 ****************************************************************************/
515
516/*************************************************************************
517 * ACPI basic handles
518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300520static acpi_handle root_handle;
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300521static acpi_handle ec_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200523#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 static acpi_handle object##_handle; \
Andi Kleen3bd01892012-10-04 17:12:01 -0700525 static const acpi_handle * const object##_parent __initconst = \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300526 &parent##_handle; \
527 static char *object##_paths[] __initdata = { paths }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200529TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
530TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200532TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
533 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400534 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
535 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200538TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539 "^HKEY", /* R30, R31 */
540 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543/*************************************************************************
544 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200545 */
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int acpi_evalf(acpi_handle handle,
Dan Carpentereceeb432012-09-01 12:54:07 -0700548 int *res, char *method, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400551 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200552 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 struct acpi_buffer result, *resultp;
554 union acpi_object out_obj;
555 acpi_status status;
556 va_list ap;
557 char res_type;
558 int success;
559 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!*fmt) {
Joe Perches0978e012011-04-04 10:06:25 -0700562 pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564 }
565
566 if (*fmt == 'q') {
567 quiet = 1;
568 fmt++;
569 } else
570 quiet = 0;
571
572 res_type = *(fmt++);
573
574 params.count = 0;
575 params.pointer = &in_objs[0];
576
577 va_start(ap, fmt);
578 while (*fmt) {
579 char c = *(fmt++);
580 switch (c) {
581 case 'd': /* int */
582 in_objs[params.count].integer.value = va_arg(ap, int);
583 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
584 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 default:
Joe Perches0978e012011-04-04 10:06:25 -0700587 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 "with invalid format character '%c'\n", c);
Jesper Juhl21365852010-12-24 19:56:28 +0100589 va_end(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591 }
592 }
593 va_end(ap);
594
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400595 if (res_type != 'v') {
596 result.length = sizeof(out_obj);
597 result.pointer = &out_obj;
598 resultp = &result;
599 } else
600 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400605 case 'd': /* int */
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300606 success = (status == AE_OK &&
607 out_obj.type == ACPI_TYPE_INTEGER);
608 if (success && res)
Dan Carpentereceeb432012-09-01 12:54:07 -0700609 *res = out_obj.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 success = status == AE_OK;
613 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 default:
Joe Perches0978e012011-04-04 10:06:25 -0700616 pr_err("acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 "with invalid format character '%c'\n", res_type);
618 return 0;
619 }
620
621 if (!success && !quiet)
Joe Perches0978e012011-04-04 10:06:25 -0700622 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
Henrique de Moraes Holschuh263f4a32010-05-16 19:45:45 -0300623 method, fmt0, acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return success;
626}
627
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200628static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300629{
630 int v;
631
632 if (ecrd_handle) {
633 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
634 return 0;
635 *p = v;
636 } else {
637 if (ec_read(i, p) < 0)
638 return 0;
639 }
640
641 return 1;
642}
643
644static int acpi_ec_write(int i, u8 v)
645{
646 if (ecwr_handle) {
647 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
648 return 0;
649 } else {
650 if (ec_write(i, v) < 0)
651 return 0;
652 }
653
654 return 1;
655}
656
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300657static int issue_thinkpad_cmos_command(int cmos_cmd)
658{
659 if (!cmos_handle)
660 return -ENXIO;
661
662 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
663 return -EIO;
664
665 return 0;
666}
667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300668/*************************************************************************
669 * ACPI device model
670 */
671
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200672#define TPACPI_ACPIHANDLE_INIT(object) \
673 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300674 object##_paths, ARRAY_SIZE(object##_paths))
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200675
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300676static void __init drv_acpi_handle_init(const char *name,
677 acpi_handle *handle, const acpi_handle parent,
678 char **paths, const int num_paths)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300679{
680 int i;
681 acpi_status status;
682
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300683 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
684 name);
685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686 for (i = 0; i < num_paths; i++) {
687 status = acpi_get_handle(parent, paths[i], handle);
688 if (ACPI_SUCCESS(status)) {
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300689 dbg_printk(TPACPI_DBG_INIT,
690 "Found ACPI handle %s for %s\n",
Henrique de Moraes Holschuhef07a5ab2010-05-16 19:45:54 -0300691 paths[i], name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692 return;
693 }
694 }
695
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300696 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
697 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300698 *handle = NULL;
699}
700
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -0300701static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
702 u32 level, void *context, void **return_value)
703{
704 *(acpi_handle *)return_value = handle;
705
706 return AE_CTRL_TERMINATE;
707}
708
709static void __init tpacpi_acpi_handle_locate(const char *name,
710 const char *hid,
711 acpi_handle *handle)
712{
713 acpi_status status;
714 acpi_handle device_found;
715
716 BUG_ON(!name || !hid || !handle);
717 vdbg_printk(TPACPI_DBG_INIT,
718 "trying to locate ACPI handle for %s, using HID %s\n",
719 name, hid);
720
721 memset(&device_found, 0, sizeof(device_found));
722 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
723 (void *)name, &device_found);
724
725 *handle = NULL;
726
727 if (ACPI_SUCCESS(status)) {
728 *handle = device_found;
729 dbg_printk(TPACPI_DBG_INIT,
730 "Found ACPI handle for %s\n", name);
731 } else {
732 vdbg_printk(TPACPI_DBG_INIT,
733 "Could not locate an ACPI handle for %s: %s\n",
734 name, acpi_format_exception(status));
735 }
736}
737
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300738static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300739{
740 struct ibm_struct *ibm = data;
741
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300742 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
743 return;
744
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300745 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300746 return;
747
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300748 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300749}
750
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300751static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752{
753 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300754 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300755
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300756 BUG_ON(!ibm->acpi);
757
758 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300759 return 0;
760
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300761 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300762 "setting up ACPI notify for %s\n", ibm->name);
763
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300764 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
765 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700766 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300767 return -ENODEV;
768 }
769
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700770 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300771 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200772 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300773 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775 status = acpi_install_notify_handler(*ibm->acpi->handle,
776 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300777 if (ACPI_FAILURE(status)) {
778 if (status == AE_ALREADY_EXISTS) {
Joe Perches0978e012011-04-04 10:06:25 -0700779 pr_notice("another device driver is already "
780 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300781 } else {
Joe Perches0978e012011-04-04 10:06:25 -0700782 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
Henrique de Moraes Holschuh72f19922010-05-16 19:45:48 -0300783 ibm->name, acpi_format_exception(status));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300784 }
785 return -ENODEV;
786 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300787 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300788 return 0;
789}
790
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300791static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792{
793 return 0;
794}
795
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300796static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300797{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300798 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300799
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300800 dbg_printk(TPACPI_DBG_INIT,
801 "registering %s as an ACPI driver\n", ibm->name);
802
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300803 BUG_ON(!ibm->acpi);
804
805 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
806 if (!ibm->acpi->driver) {
Joe Perches0978e012011-04-04 10:06:25 -0700807 pr_err("failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300808 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300809 }
810
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200811 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300812 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200813
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300814 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300815
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300816 rc = acpi_bus_register_driver(ibm->acpi->driver);
817 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -0700818 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200819 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300820 kfree(ibm->acpi->driver);
821 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300822 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300823 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300824
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300825 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300826}
827
828
829/****************************************************************************
830 ****************************************************************************
831 *
832 * Procfs Helpers
833 *
834 ****************************************************************************
835 ****************************************************************************/
836
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200837static int dispatch_proc_show(struct seq_file *m, void *v)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300838{
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200839 struct ibm_struct *ibm = m->private;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300840
841 if (!ibm || !ibm->read)
842 return -EINVAL;
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200843 return ibm->read(m);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844}
845
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200846static int dispatch_proc_open(struct inode *inode, struct file *file)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300847{
Al Virod9dda782013-03-31 18:16:14 -0400848 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200849}
850
851static ssize_t dispatch_proc_write(struct file *file,
852 const char __user *userbuf,
853 size_t count, loff_t *pos)
854{
Al Virod9dda782013-03-31 18:16:14 -0400855 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300856 char *kernbuf;
857 int ret;
858
859 if (!ibm || !ibm->write)
860 return -EINVAL;
Michael Buesch5b05d462009-08-01 12:04:19 -0300861 if (count > PAGE_SIZE - 2)
862 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300863
864 kernbuf = kmalloc(count + 2, GFP_KERNEL);
865 if (!kernbuf)
866 return -ENOMEM;
867
868 if (copy_from_user(kernbuf, userbuf, count)) {
869 kfree(kernbuf);
870 return -EFAULT;
871 }
872
873 kernbuf[count] = 0;
874 strcat(kernbuf, ",");
875 ret = ibm->write(kernbuf);
876 if (ret == 0)
877 ret = count;
878
879 kfree(kernbuf);
880
881 return ret;
882}
883
Alexey Dobriyan887965e2009-12-15 21:51:12 -0200884static const struct file_operations dispatch_proc_fops = {
885 .owner = THIS_MODULE,
886 .open = dispatch_proc_open,
887 .read = seq_read,
888 .llseek = seq_lseek,
889 .release = single_release,
890 .write = dispatch_proc_write,
891};
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static char *next_cmd(char **cmds)
894{
895 char *start = *cmds;
896 char *end;
897
898 while ((end = strchr(start, ',')) && end == start)
899 start = end + 1;
900
901 if (!end)
902 return NULL;
903
904 *end = 0;
905 *cmds = end + 1;
906 return start;
907}
908
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300909
910/****************************************************************************
911 ****************************************************************************
912 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300913 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300914 *
915 ****************************************************************************
916 ****************************************************************************/
917
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300918static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300919static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700920static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300921static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300922static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200923static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300924
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200925#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200926static int tpacpi_suspend_handler(struct device *dev)
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200927{
928 struct ibm_struct *ibm, *itmp;
929
930 list_for_each_entry_safe(ibm, itmp,
931 &tpacpi_all_drivers,
932 all_drivers) {
933 if (ibm->suspend)
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +0200934 (ibm->suspend)();
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200935 }
936
937 return 0;
938}
939
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200940static int tpacpi_resume_handler(struct device *dev)
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300941{
942 struct ibm_struct *ibm, *itmp;
943
944 list_for_each_entry_safe(ibm, itmp,
945 &tpacpi_all_drivers,
946 all_drivers) {
947 if (ibm->resume)
948 (ibm->resume)();
949 }
950
951 return 0;
952}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200953#endif
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300954
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200955static SIMPLE_DEV_PM_OPS(tpacpi_pm,
956 tpacpi_suspend_handler, tpacpi_resume_handler);
957
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200958static void tpacpi_shutdown_handler(struct platform_device *pdev)
959{
960 struct ibm_struct *ibm, *itmp;
961
962 list_for_each_entry_safe(ibm, itmp,
963 &tpacpi_all_drivers,
964 all_drivers) {
965 if (ibm->shutdown)
966 (ibm->shutdown)();
967 }
968}
969
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300970static struct platform_driver tpacpi_pdriver = {
971 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200972 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300973 .owner = THIS_MODULE,
Rafael J. Wysocki4959a782012-06-27 23:19:01 +0200974 .pm = &tpacpi_pm,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300975 },
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200976 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300977};
978
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300979static struct platform_driver tpacpi_hwmon_pdriver = {
980 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200981 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300982 .owner = THIS_MODULE,
983 },
984};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300985
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300986/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300987 * sysfs support helpers
988 */
989
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200990struct attribute_set {
991 unsigned int members, max_members;
992 struct attribute_group group;
993};
994
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300995struct attribute_set_obj {
996 struct attribute_set s;
997 struct attribute *a;
998} __attribute__((packed));
999
1000static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001001 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001002{
1003 struct attribute_set_obj *sobj;
1004
1005 if (max_members == 0)
1006 return NULL;
1007
1008 /* Allocates space for implicit NULL at the end too */
1009 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1010 max_members * sizeof(struct attribute *),
1011 GFP_KERNEL);
1012 if (!sobj)
1013 return NULL;
1014 sobj->s.max_members = max_members;
1015 sobj->s.group.attrs = &sobj->a;
1016 sobj->s.group.name = name;
1017
1018 return &sobj->s;
1019}
1020
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001021#define destroy_attr_set(_set) \
1022 kfree(_set);
1023
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001024/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001025static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001026{
1027 if (!s || !attr)
1028 return -EINVAL;
1029
1030 if (s->members >= s->max_members)
1031 return -ENOMEM;
1032
1033 s->group.attrs[s->members] = attr;
1034 s->members++;
1035
1036 return 0;
1037}
1038
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001039static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001040 struct attribute **attr,
1041 unsigned int count)
1042{
1043 int i, res;
1044
1045 for (i = 0; i < count; i++) {
1046 res = add_to_attr_set(s, attr[i]);
1047 if (res)
1048 return res;
1049 }
1050
1051 return 0;
1052}
1053
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001054static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001055{
1056 sysfs_remove_group(kobj, &s->group);
1057 destroy_attr_set(s);
1058}
1059
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001060#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1061 sysfs_create_group(_kobj, &_attr_set->group)
1062
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001063static int parse_strtoul(const char *buf,
1064 unsigned long max, unsigned long *value)
1065{
1066 char *endp;
1067
André Goddard Rosae7d28602009-12-14 18:01:06 -08001068 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1069 endp = skip_spaces(endp);
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -03001070 if (*endp || *value > max)
1071 return -EINVAL;
1072
1073 return 0;
1074}
1075
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001076static void tpacpi_disable_brightness_delay(void)
1077{
1078 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
Joe Perches0978e012011-04-04 10:06:25 -07001079 pr_notice("ACPI backlight control delay disabled\n");
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03001080}
1081
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001082static void printk_deprecated_attribute(const char * const what,
1083 const char * const details)
1084{
1085 tpacpi_log_usertask("deprecated sysfs attribute");
Joe Perches0978e012011-04-04 10:06:25 -07001086 pr_warn("WARNING: sysfs attribute %s is deprecated and "
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001087 "will be removed. %s\n",
1088 what, details);
1089}
1090
Johannes Berg19d337d2009-06-02 13:01:37 +02001091/*************************************************************************
1092 * rfkill and radio control support helpers
1093 */
1094
1095/*
1096 * ThinkPad-ACPI firmware handling model:
1097 *
1098 * WLSW (master wireless switch) is event-driven, and is common to all
1099 * firmware-controlled radios. It cannot be controlled, just monitored,
1100 * as expected. It overrides all radio state in firmware
1101 *
1102 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1103 * (TODO: verify how WLSW interacts with the returned radio state).
1104 *
1105 * The only time there are shadow radio state changes, is when
1106 * masked-off hotkeys are used.
1107 */
1108
1109/*
1110 * Internal driver API for radio state:
1111 *
1112 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1113 * bool: true means radio blocked (off)
1114 */
1115enum tpacpi_rfkill_state {
1116 TPACPI_RFK_RADIO_OFF = 0,
1117 TPACPI_RFK_RADIO_ON
1118};
1119
1120/* rfkill switches */
1121enum tpacpi_rfk_id {
1122 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1123 TPACPI_RFK_WWAN_SW_ID,
1124 TPACPI_RFK_UWB_SW_ID,
1125 TPACPI_RFK_SW_MAX
1126};
1127
1128static const char *tpacpi_rfkill_names[] = {
1129 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1130 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1131 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1132 [TPACPI_RFK_SW_MAX] = NULL
1133};
1134
1135/* ThinkPad-ACPI rfkill subdriver */
1136struct tpacpi_rfk {
1137 struct rfkill *rfkill;
1138 enum tpacpi_rfk_id id;
1139 const struct tpacpi_rfk_ops *ops;
1140};
1141
1142struct tpacpi_rfk_ops {
1143 /* firmware interface */
1144 int (*get_status)(void);
1145 int (*set_status)(const enum tpacpi_rfkill_state);
1146};
1147
1148static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1149
1150/* Query FW and update rfkill sw state for a given rfkill switch */
1151static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1152{
1153 int status;
1154
1155 if (!tp_rfk)
1156 return -ENODEV;
1157
1158 status = (tp_rfk->ops->get_status)();
1159 if (status < 0)
1160 return status;
1161
1162 rfkill_set_sw_state(tp_rfk->rfkill,
1163 (status == TPACPI_RFK_RADIO_OFF));
1164
1165 return status;
1166}
1167
1168/* Query FW and update rfkill sw state for all rfkill switches */
1169static void tpacpi_rfk_update_swstate_all(void)
1170{
1171 unsigned int i;
1172
1173 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1174 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1175}
1176
1177/*
1178 * Sync the HW-blocking state of all rfkill switches,
1179 * do notice it causes the rfkill core to schedule uevents
1180 */
1181static void tpacpi_rfk_update_hwblock_state(bool blocked)
1182{
1183 unsigned int i;
1184 struct tpacpi_rfk *tp_rfk;
1185
1186 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1187 tp_rfk = tpacpi_rfkill_switches[i];
1188 if (tp_rfk) {
1189 if (rfkill_set_hw_state(tp_rfk->rfkill,
1190 blocked)) {
1191 /* ignore -- we track sw block */
1192 }
1193 }
1194 }
1195}
1196
1197/* Call to get the WLSW state from the firmware */
1198static int hotkey_get_wlsw(void);
1199
1200/* Call to query WLSW state and update all rfkill switches */
1201static bool tpacpi_rfk_check_hwblock_state(void)
1202{
1203 int res = hotkey_get_wlsw();
1204 int hw_blocked;
1205
1206 /* When unknown or unsupported, we have to assume it is unblocked */
1207 if (res < 0)
1208 return false;
1209
1210 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1211 tpacpi_rfk_update_hwblock_state(hw_blocked);
1212
1213 return hw_blocked;
1214}
1215
1216static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1217{
1218 struct tpacpi_rfk *tp_rfk = data;
1219 int res;
1220
1221 dbg_printk(TPACPI_DBG_RFKILL,
1222 "request to change radio state to %s\n",
1223 blocked ? "blocked" : "unblocked");
1224
1225 /* try to set radio state */
1226 res = (tp_rfk->ops->set_status)(blocked ?
1227 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1228
1229 /* and update the rfkill core with whatever the FW really did */
1230 tpacpi_rfk_update_swstate(tp_rfk);
1231
1232 return (res < 0) ? res : 0;
1233}
1234
1235static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1236 .set_block = tpacpi_rfk_hook_set_block,
1237};
1238
1239static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1240 const struct tpacpi_rfk_ops *tp_rfkops,
1241 const enum rfkill_type rfktype,
1242 const char *name,
1243 const bool set_default)
1244{
1245 struct tpacpi_rfk *atp_rfk;
1246 int res;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001247 bool sw_state = false;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001248 bool hw_state;
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001249 int sw_status;
Johannes Berg19d337d2009-06-02 13:01:37 +02001250
1251 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1252
Johannes Berg19d337d2009-06-02 13:01:37 +02001253 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1254 if (atp_rfk)
1255 atp_rfk->rfkill = rfkill_alloc(name,
1256 &tpacpi_pdev->dev,
1257 rfktype,
1258 &tpacpi_rfk_rfkill_ops,
1259 atp_rfk);
1260 if (!atp_rfk || !atp_rfk->rfkill) {
Joe Perches0978e012011-04-04 10:06:25 -07001261 pr_err("failed to allocate memory for rfkill class\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001262 kfree(atp_rfk);
1263 return -ENOMEM;
1264 }
1265
1266 atp_rfk->id = id;
1267 atp_rfk->ops = tp_rfkops;
1268
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001269 sw_status = (tp_rfkops->get_status)();
1270 if (sw_status < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001271 pr_err("failed to read initial state for %s, error %d\n",
1272 name, sw_status);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001273 } else {
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001274 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001275 if (set_default) {
1276 /* try to keep the initial state, since we ask the
1277 * firmware to preserve it across S5 in NVRAM */
Alan Jenkins06d5caf2009-06-16 15:39:51 +01001278 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
Alan Jenkinsb3fa13292009-06-08 13:27:27 +01001279 }
1280 }
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001281 hw_state = tpacpi_rfk_check_hwblock_state();
1282 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
Johannes Berg19d337d2009-06-02 13:01:37 +02001283
1284 res = rfkill_register(atp_rfk->rfkill);
1285 if (res < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07001286 pr_err("failed to register %s rfkill switch: %d\n", name, res);
Johannes Berg19d337d2009-06-02 13:01:37 +02001287 rfkill_destroy(atp_rfk->rfkill);
1288 kfree(atp_rfk);
1289 return res;
1290 }
1291
1292 tpacpi_rfkill_switches[id] = atp_rfk;
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001293
Joe Perches0978e012011-04-04 10:06:25 -07001294 pr_info("rfkill switch %s: radio is %sblocked\n",
Henrique de Moraes Holschuh5451a922009-12-15 21:51:07 -02001295 name, (sw_state || hw_state) ? "" : "un");
Johannes Berg19d337d2009-06-02 13:01:37 +02001296 return 0;
1297}
1298
1299static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1300{
1301 struct tpacpi_rfk *tp_rfk;
1302
1303 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1304
1305 tp_rfk = tpacpi_rfkill_switches[id];
1306 if (tp_rfk) {
1307 rfkill_unregister(tp_rfk->rfkill);
Corentin Chary5f0dadb2009-09-14 12:43:52 +02001308 rfkill_destroy(tp_rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +02001309 tpacpi_rfkill_switches[id] = NULL;
1310 kfree(tp_rfk);
1311 }
1312}
1313
Henrique de Moraes Holschuh73a94d82009-04-04 04:25:47 +00001314static void printk_deprecated_rfkill_attribute(const char * const what)
1315{
1316 printk_deprecated_attribute(what,
1317 "Please switch to generic rfkill before year 2010");
1318}
1319
Johannes Berg19d337d2009-06-02 13:01:37 +02001320/* sysfs <radio> enable ------------------------------------------------ */
1321static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1322 struct device_attribute *attr,
1323 char *buf)
1324{
1325 int status;
1326
1327 printk_deprecated_rfkill_attribute(attr->attr.name);
1328
1329 /* This is in the ABI... */
1330 if (tpacpi_rfk_check_hwblock_state()) {
1331 status = TPACPI_RFK_RADIO_OFF;
1332 } else {
1333 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1334 if (status < 0)
1335 return status;
1336 }
1337
1338 return snprintf(buf, PAGE_SIZE, "%d\n",
1339 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1340}
1341
1342static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1343 struct device_attribute *attr,
1344 const char *buf, size_t count)
1345{
1346 unsigned long t;
1347 int res;
1348
1349 printk_deprecated_rfkill_attribute(attr->attr.name);
1350
1351 if (parse_strtoul(buf, 1, &t))
1352 return -EINVAL;
1353
1354 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1355
1356 /* This is in the ABI... */
1357 if (tpacpi_rfk_check_hwblock_state() && !!t)
1358 return -EPERM;
1359
1360 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1361 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1362 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1363
1364 return (res < 0) ? res : count;
1365}
1366
1367/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001368static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
Johannes Berg19d337d2009-06-02 13:01:37 +02001369{
Johannes Berg19d337d2009-06-02 13:01:37 +02001370 if (id >= TPACPI_RFK_SW_MAX)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001371 seq_printf(m, "status:\t\tnot supported\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001372 else {
1373 int status;
1374
1375 /* This is in the ABI... */
1376 if (tpacpi_rfk_check_hwblock_state()) {
1377 status = TPACPI_RFK_RADIO_OFF;
1378 } else {
1379 status = tpacpi_rfk_update_swstate(
1380 tpacpi_rfkill_switches[id]);
1381 if (status < 0)
1382 return status;
1383 }
1384
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001385 seq_printf(m, "status:\t\t%s\n",
Johannes Berg19d337d2009-06-02 13:01:37 +02001386 (status == TPACPI_RFK_RADIO_ON) ?
1387 "enabled" : "disabled");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001388 seq_printf(m, "commands:\tenable, disable\n");
Johannes Berg19d337d2009-06-02 13:01:37 +02001389 }
1390
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001391 return 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02001392}
1393
1394static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1395{
1396 char *cmd;
1397 int status = -1;
1398 int res = 0;
1399
1400 if (id >= TPACPI_RFK_SW_MAX)
1401 return -ENODEV;
1402
1403 while ((cmd = next_cmd(&buf))) {
1404 if (strlencmp(cmd, "enable") == 0)
1405 status = TPACPI_RFK_RADIO_ON;
1406 else if (strlencmp(cmd, "disable") == 0)
1407 status = TPACPI_RFK_RADIO_OFF;
1408 else
1409 return -EINVAL;
1410 }
1411
1412 if (status != -1) {
1413 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1414 (status == TPACPI_RFK_RADIO_ON) ?
1415 "enable" : "disable",
1416 tpacpi_rfkill_names[id]);
1417 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1418 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1419 }
1420
1421 return res;
1422}
1423
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001424/*************************************************************************
1425 * thinkpad-acpi driver attributes
1426 */
1427
1428/* interface_version --------------------------------------------------- */
1429static ssize_t tpacpi_driver_interface_version_show(
1430 struct device_driver *drv,
1431 char *buf)
1432{
1433 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1434}
1435
1436static DRIVER_ATTR(interface_version, S_IRUGO,
1437 tpacpi_driver_interface_version_show, NULL);
1438
1439/* debug_level --------------------------------------------------------- */
1440static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1441 char *buf)
1442{
1443 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1444}
1445
1446static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1447 const char *buf, size_t count)
1448{
1449 unsigned long t;
1450
1451 if (parse_strtoul(buf, 0xffff, &t))
1452 return -EINVAL;
1453
1454 dbg_level = t;
1455
1456 return count;
1457}
1458
1459static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1460 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1461
1462/* version ------------------------------------------------------------- */
1463static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1464 char *buf)
1465{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001466 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1467 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001468}
1469
1470static DRIVER_ATTR(version, S_IRUGO,
1471 tpacpi_driver_version_show, NULL);
1472
1473/* --------------------------------------------------------------------- */
1474
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001475#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1476
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001477/* wlsw_emulstate ------------------------------------------------------ */
1478static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1479 char *buf)
1480{
1481 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1482}
1483
1484static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1485 const char *buf, size_t count)
1486{
1487 unsigned long t;
1488
1489 if (parse_strtoul(buf, 1, &t))
1490 return -EINVAL;
1491
Johannes Berg19d337d2009-06-02 13:01:37 +02001492 if (tpacpi_wlsw_emulstate != !!t) {
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001493 tpacpi_wlsw_emulstate = !!t;
Johannes Berg19d337d2009-06-02 13:01:37 +02001494 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1495 }
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001496
1497 return count;
1498}
1499
1500static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_wlsw_emulstate_show,
1502 tpacpi_driver_wlsw_emulstate_store);
1503
1504/* bluetooth_emulstate ------------------------------------------------- */
1505static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1506 struct device_driver *drv,
1507 char *buf)
1508{
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1510}
1511
1512static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1515{
1516 unsigned long t;
1517
1518 if (parse_strtoul(buf, 1, &t))
1519 return -EINVAL;
1520
1521 tpacpi_bluetooth_emulstate = !!t;
1522
1523 return count;
1524}
1525
1526static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_bluetooth_emulstate_show,
1528 tpacpi_driver_bluetooth_emulstate_store);
1529
1530/* wwan_emulstate ------------------------------------------------- */
1531static ssize_t tpacpi_driver_wwan_emulstate_show(
1532 struct device_driver *drv,
1533 char *buf)
1534{
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1536}
1537
1538static ssize_t tpacpi_driver_wwan_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1541{
1542 unsigned long t;
1543
1544 if (parse_strtoul(buf, 1, &t))
1545 return -EINVAL;
1546
1547 tpacpi_wwan_emulstate = !!t;
1548
1549 return count;
1550}
1551
1552static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_wwan_emulstate_show,
1554 tpacpi_driver_wwan_emulstate_store);
1555
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001556/* uwb_emulstate ------------------------------------------------- */
1557static ssize_t tpacpi_driver_uwb_emulstate_show(
1558 struct device_driver *drv,
1559 char *buf)
1560{
1561 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1562}
1563
1564static ssize_t tpacpi_driver_uwb_emulstate_store(
1565 struct device_driver *drv,
1566 const char *buf, size_t count)
1567{
1568 unsigned long t;
1569
1570 if (parse_strtoul(buf, 1, &t))
1571 return -EINVAL;
1572
1573 tpacpi_uwb_emulstate = !!t;
1574
1575 return count;
1576}
1577
1578static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1579 tpacpi_driver_uwb_emulstate_show,
1580 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001581#endif
1582
1583/* --------------------------------------------------------------------- */
1584
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001585static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001586 &driver_attr_debug_level, &driver_attr_version,
1587 &driver_attr_interface_version,
1588};
1589
1590static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1591{
1592 int i, res;
1593
1594 i = 0;
1595 res = 0;
1596 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1597 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1598 i++;
1599 }
1600
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001601#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1602 if (!res && dbg_wlswemul)
1603 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1604 if (!res && dbg_bluetoothemul)
1605 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1606 if (!res && dbg_wwanemul)
1607 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001608 if (!res && dbg_uwbemul)
1609 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001610#endif
1611
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001612 return res;
1613}
1614
1615static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1616{
1617 int i;
1618
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001619 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001621
1622#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1623 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1624 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1625 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001626 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001627#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001628}
1629
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001630/*************************************************************************
1631 * Firmware Data
1632 */
1633
1634/*
1635 * Table of recommended minimum BIOS versions
1636 *
1637 * Reasons for listing:
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001638 * 1. Stable BIOS, listed because the unknown amount of
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001639 * bugs and bad ACPI behaviour on older versions
1640 *
1641 * 2. BIOS or EC fw with known bugs that trigger on Linux
1642 *
1643 * 3. BIOS with known reduced functionality in older versions
1644 *
1645 * We recommend the latest BIOS and EC version.
1646 * We only support the latest BIOS and EC fw version as a rule.
1647 *
1648 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1649 * Information from users in ThinkWiki
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001650 *
1651 * WARNING: we use this table also to detect that the machine is
1652 * a ThinkPad in some cases, so don't remove entries lightly.
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001653 */
1654
1655#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1656 { .vendor = (__v), \
1657 .bios = TPID(__id1, __id2), \
1658 .ec = TPACPI_MATCH_ANY, \
1659 .quirks = TPACPI_MATCH_ANY << 16 \
1660 | (__bv1) << 8 | (__bv2) }
1661
1662#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001663 __eid, __ev1, __ev2) \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001664 { .vendor = (__v), \
1665 .bios = TPID(__bid1, __bid2), \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001666 .ec = __eid, \
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001667 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1668 | (__bv1) << 8 | (__bv2) }
1669
1670#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1671 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1672
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001673/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001674#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001676 __bv1, __bv2, TPID(__id1, __id2), \
1677 __ev1, __ev2), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1680 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001681
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001682/* Outdated IBM BIOSes often lack the EC id string */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001683#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1684 __eid1, __eid2, __ev1, __ev2) \
1685 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001686 __bv1, __bv2, TPID(__eid1, __eid2), \
1687 __ev1, __ev2), \
1688 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1689 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1690 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001691
1692#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1693 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1694
1695#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1696 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001697 __bv1, __bv2, TPID(__id1, __id2), \
1698 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001699
1700#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1701 __eid1, __eid2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
Henrique de Moraes Holschuh275014a2009-11-17 14:07:22 -08001703 __bv1, __bv2, TPID(__eid1, __eid2), \
1704 __ev1, __ev2)
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001705
1706static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1707 /* Numeric models ------------------ */
1708 /* FW MODEL BIOS VERS */
1709 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1710 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1711 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1712 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1713 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1714 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1715 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1716 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1717 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1718
1719 /* A-series ------------------------- */
1720 /* FW MODEL BIOS VERS EC VERS */
1721 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1722 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1723 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1724 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1725 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1726 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1727 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1728 TPV_QI0('1', '3', '2', '0'), /* A22m */
1729 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1730 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1731 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1732
1733 /* G-series ------------------------- */
1734 /* FW MODEL BIOS VERS */
1735 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1736 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1737
1738 /* R-series, T-series --------------- */
1739 /* FW MODEL BIOS VERS EC VERS */
1740 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1741 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1742 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1743 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1744 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1745 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1746 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1747 T40/p, T41/p, T42/p (1) */
1748 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1749 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1750 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1751 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1752
1753 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1754 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1755 TPV_QI0('1', '6', '3', '2'), /* T22 */
1756 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1757 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1758 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1759
1760 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1761 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001762 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001763
1764 /* BIOS FW BIOS VERS EC FW EC VERS */
1765 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1766 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1767
1768 /* X-series ------------------------- */
1769 /* FW MODEL BIOS VERS EC VERS */
1770 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1771 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1772 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1773 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1774 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1775 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1776 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1777
Henrique de Moraes Holschuh90765c62009-12-09 01:36:23 +00001778 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1779 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001780
1781 /* (0) - older versions lack DMI EC fw string and functionality */
1782 /* (1) - older versions known to lack functionality */
1783};
1784
1785#undef TPV_QL1
1786#undef TPV_QL0
1787#undef TPV_QI2
1788#undef TPV_QI1
1789#undef TPV_QI0
1790#undef TPV_Q_X
1791#undef TPV_Q
1792
1793static void __init tpacpi_check_outdated_fw(void)
1794{
1795 unsigned long fwvers;
1796 u16 ec_version, bios_version;
1797
1798 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1799 ARRAY_SIZE(tpacpi_bios_version_qtable));
1800
1801 if (!fwvers)
1802 return;
1803
1804 bios_version = fwvers & 0xffffU;
1805 ec_version = (fwvers >> 16) & 0xffffU;
1806
1807 /* note that unknown versions are set to 0x0000 and we use that */
1808 if ((bios_version > thinkpad_id.bios_release) ||
1809 (ec_version > thinkpad_id.ec_release &&
1810 ec_version != TPACPI_MATCH_ANY)) {
1811 /*
1812 * The changelogs would let us track down the exact
1813 * reason, but it is just too much of a pain to track
1814 * it. We only list BIOSes that are either really
1815 * broken, or really stable to begin with, so it is
1816 * best if the user upgrades the firmware anyway.
1817 */
Joe Perches0978e012011-04-04 10:06:25 -07001818 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1819 pr_warn("WARNING: This firmware may be missing critical bug "
Henrique de Moraes Holschuh600a99f2009-09-12 15:22:12 -03001820 "fixes and/or important features\n");
1821 }
1822}
1823
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03001824static bool __init tpacpi_is_fw_known(void)
1825{
1826 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1827 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1828}
1829
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001830/****************************************************************************
1831 ****************************************************************************
1832 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001833 * Subdrivers
1834 *
1835 ****************************************************************************
1836 ****************************************************************************/
1837
1838/*************************************************************************
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03001839 * thinkpad-acpi metadata subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001840 */
1841
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001842static int thinkpad_acpi_driver_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02001844 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1845 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001849static struct ibm_struct thinkpad_acpi_driver_data = {
1850 .name = "driver",
1851 .read = thinkpad_acpi_driver_read,
1852};
1853
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001854/*************************************************************************
1855 * Hotkey subdriver
1856 */
1857
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001858/*
1859 * ThinkPad firmware event model
1860 *
1861 * The ThinkPad firmware has two main event interfaces: normal ACPI
1862 * notifications (which follow the ACPI standard), and a private event
1863 * interface.
1864 *
1865 * The private event interface also issues events for the hotkeys. As
1866 * the driver gained features, the event handling code ended up being
1867 * built around the hotkey subdriver. This will need to be refactored
1868 * to a more formal event API eventually.
1869 *
1870 * Some "hotkeys" are actually supposed to be used as event reports,
1871 * such as "brightness has changed", "volume has changed", depending on
1872 * the ThinkPad model and how the firmware is operating.
1873 *
1874 * Unlike other classes, hotkey-class events have mask/unmask control on
1875 * non-ancient firmware. However, how it behaves changes a lot with the
1876 * firmware model and version.
1877 */
1878
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001879enum { /* hot key scan codes (derived from ACPI DSDT) */
1880 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1881 TP_ACPI_HOTKEYSCAN_FNF2,
1882 TP_ACPI_HOTKEYSCAN_FNF3,
1883 TP_ACPI_HOTKEYSCAN_FNF4,
1884 TP_ACPI_HOTKEYSCAN_FNF5,
1885 TP_ACPI_HOTKEYSCAN_FNF6,
1886 TP_ACPI_HOTKEYSCAN_FNF7,
1887 TP_ACPI_HOTKEYSCAN_FNF8,
1888 TP_ACPI_HOTKEYSCAN_FNF9,
1889 TP_ACPI_HOTKEYSCAN_FNF10,
1890 TP_ACPI_HOTKEYSCAN_FNF11,
1891 TP_ACPI_HOTKEYSCAN_FNF12,
1892 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1893 TP_ACPI_HOTKEYSCAN_FNINSERT,
1894 TP_ACPI_HOTKEYSCAN_FNDELETE,
1895 TP_ACPI_HOTKEYSCAN_FNHOME,
1896 TP_ACPI_HOTKEYSCAN_FNEND,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1898 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1899 TP_ACPI_HOTKEYSCAN_FNSPACE,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1901 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1902 TP_ACPI_HOTKEYSCAN_MUTE,
1903 TP_ACPI_HOTKEYSCAN_THINKPAD,
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03001904 TP_ACPI_HOTKEYSCAN_UNK1,
1905 TP_ACPI_HOTKEYSCAN_UNK2,
1906 TP_ACPI_HOTKEYSCAN_UNK3,
1907 TP_ACPI_HOTKEYSCAN_UNK4,
1908 TP_ACPI_HOTKEYSCAN_UNK5,
1909 TP_ACPI_HOTKEYSCAN_UNK6,
1910 TP_ACPI_HOTKEYSCAN_UNK7,
1911 TP_ACPI_HOTKEYSCAN_UNK8,
1912
1913 /* Hotkey keymap size */
1914 TPACPI_HOTKEY_MAP_LEN
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001915};
1916
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001917enum { /* Keys/events available through NVRAM polling */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001918 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1919 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1920};
1921
1922enum { /* Positions of some of the keys in hotkey masks */
1923 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1924 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1925 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1926 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1927 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1928 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1929 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1930 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1931 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1932 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1933 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1934};
1935
1936enum { /* NVRAM to ACPI HKEY group map */
1937 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1938 TP_ACPI_HKEY_ZOOM_MASK |
1939 TP_ACPI_HKEY_DISPSWTCH_MASK |
1940 TP_ACPI_HKEY_HIBERNATE_MASK,
1941 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1942 TP_ACPI_HKEY_BRGHTDWN_MASK,
1943 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1944 TP_ACPI_HKEY_VOLDWN_MASK |
1945 TP_ACPI_HKEY_MUTE_MASK,
1946};
1947
1948#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1949struct tp_nvram_state {
1950 u16 thinkpad_toggle:1;
1951 u16 zoom_toggle:1;
1952 u16 display_toggle:1;
1953 u16 thinklight_toggle:1;
1954 u16 hibernate_toggle:1;
1955 u16 displayexp_toggle:1;
1956 u16 display_state:1;
1957 u16 brightness_toggle:1;
1958 u16 volume_toggle:1;
1959 u16 mute:1;
1960
1961 u8 brightness_level;
1962 u8 volume_level;
1963};
1964
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001965/* kthread for the hotkey poller */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966static struct task_struct *tpacpi_hotkey_task;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001967
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001968/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001969 * Acquire mutex to write poller control variables as an
1970 * atomic block.
1971 *
1972 * Increment hotkey_config_change when changing them if you
1973 * want the kthread to forget old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001974 *
1975 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1976 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001977static struct mutex hotkey_thread_data_mutex;
1978static unsigned int hotkey_config_change;
1979
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001980/*
1981 * hotkey poller control variables
1982 *
1983 * Must be atomic or readers will also need to acquire mutex
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03001984 *
1985 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1986 * should be used only when the changes need to be taken as
1987 * a block, OR when one needs to force the kthread to forget
1988 * old state.
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03001989 */
1990static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1991static unsigned int hotkey_poll_freq = 10; /* Hz */
1992
1993#define HOTKEY_CONFIG_CRITICAL_START \
1994 do { \
1995 mutex_lock(&hotkey_thread_data_mutex); \
1996 hotkey_config_change++; \
1997 } while (0);
1998#define HOTKEY_CONFIG_CRITICAL_END \
1999 mutex_unlock(&hotkey_thread_data_mutex);
2000
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002001#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2002
2003#define hotkey_source_mask 0U
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002004#define HOTKEY_CONFIG_CRITICAL_START
2005#define HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002006
2007#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2008
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002009static struct mutex hotkey_mutex;
2010
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002011static enum { /* Reasons for waking up */
2012 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2013 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2014 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2015} hotkey_wakeup_reason;
2016
2017static int hotkey_autosleep_ack;
2018
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002019static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2020static u32 hotkey_all_mask; /* all events supported in fw */
2021static u32 hotkey_reserved_mask; /* events better left disabled */
2022static u32 hotkey_driver_mask; /* events needed by the driver */
2023static u32 hotkey_user_mask; /* events visible to userspace */
2024static u32 hotkey_acpi_mask; /* events enabled in firmware */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002025
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002026static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002028static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002029
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002030static void tpacpi_driver_event(const unsigned int hkey_event);
2031static void hotkey_driver_event(const unsigned int scancode);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002032static void hotkey_poll_setup(const bool may_warn);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002033
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002034/* HKEY.MHKG() return bits */
2035#define TP_HOTKEY_TABLET_MASK (1 << 3)
2036
Johannes Berg19d337d2009-06-02 13:01:37 +02002037static int hotkey_get_wlsw(void)
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002038{
Johannes Berg19d337d2009-06-02 13:01:37 +02002039 int status;
2040
2041 if (!tp_features.hotkey_wlsw)
2042 return -ENODEV;
2043
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002044#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Johannes Berg19d337d2009-06-02 13:01:37 +02002045 if (dbg_wlswemul)
2046 return (tpacpi_wlsw_emulstate) ?
2047 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002048#endif
Johannes Berg19d337d2009-06-02 13:01:37 +02002049
2050 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002051 return -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02002052
2053 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002054}
2055
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002056static int hotkey_get_tablet_mode(int *status)
2057{
2058 int s;
2059
2060 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2061 return -EIO;
2062
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08002063 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2064 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002065}
2066
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002067/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002068 * Reads current event mask from firmware, and updates
2069 * hotkey_acpi_mask accordingly. Also resets any bits
2070 * from hotkey_user_mask that are unavailable to be
2071 * delivered (shadow requirement of the userspace ABI).
2072 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002073 * Call with hotkey_mutex held
2074 */
2075static int hotkey_mask_get(void)
2076{
2077 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002078 u32 m = 0;
2079
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002080 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002081 return -EIO;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002082
2083 hotkey_acpi_mask = m;
2084 } else {
2085 /* no mask support doesn't mean no event support... */
2086 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002087 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002088
2089 /* sync userspace-visible mask */
2090 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002091
2092 return 0;
2093}
2094
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002095void static hotkey_mask_warn_incomplete_mask(void)
2096{
2097 /* log only what the user can fix... */
2098 const u32 wantedmask = hotkey_driver_mask &
2099 ~(hotkey_acpi_mask | hotkey_source_mask) &
2100 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2101
2102 if (wantedmask)
Joe Perches0978e012011-04-04 10:06:25 -07002103 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002104}
2105
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002106/*
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002107 * Set the firmware mask when supported
2108 *
2109 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2110 *
2111 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2112 *
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002113 * Call with hotkey_mutex held
2114 */
2115static int hotkey_mask_set(u32 mask)
2116{
2117 int i;
2118 int rc = 0;
2119
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002120 const u32 fwmask = mask & ~hotkey_source_mask;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03002121
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002122 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002123 for (i = 0; i < 32; i++) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002124 if (!acpi_evalf(hkey_handle,
2125 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002126 !!(mask & (1 << i)))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002127 rc = -EIO;
2128 break;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 }
2130 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002131 }
2132
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002133 /*
2134 * We *must* make an inconditional call to hotkey_mask_get to
2135 * refresh hotkey_acpi_mask and update hotkey_user_mask
2136 *
2137 * Take the opportunity to also log when we cannot _enable_
2138 * a given event.
2139 */
2140 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Joe Perches0978e012011-04-04 10:06:25 -07002141 pr_notice("asked for hotkey mask 0x%08x, but "
2142 "firmware forced it to 0x%08x\n",
2143 fwmask, hotkey_acpi_mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002144 }
2145
Henrique de Moraes Holschuh6b30eb72009-12-09 01:36:25 +00002146 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2147 hotkey_mask_warn_incomplete_mask();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002148
2149 return rc;
2150}
2151
2152/*
2153 * Sets hotkey_user_mask and tries to set the firmware mask
2154 *
2155 * Call with hotkey_mutex held
2156 */
2157static int hotkey_user_mask_set(const u32 mask)
2158{
2159 int rc;
2160
2161 /* Give people a chance to notice they are doing something that
2162 * is bound to go boom on their users sooner or later */
2163 if (!tp_warned.hotkey_mask_ff &&
2164 (mask == 0xffff || mask == 0xffffff ||
2165 mask == 0xffffffff)) {
2166 tp_warned.hotkey_mask_ff = 1;
Joe Perches0978e012011-04-04 10:06:25 -07002167 pr_notice("setting the hotkey mask to 0x%08x is likely "
2168 "not the best way to go about it\n", mask);
2169 pr_notice("please consider using the driver defaults, "
2170 "and refer to up-to-date thinkpad-acpi "
2171 "documentation\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002172 }
2173
2174 /* Try to enable what the user asked for, plus whatever we need.
2175 * this syncs everything but won't enable bits in hotkey_user_mask */
2176 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2177
2178 /* Enable the available bits in hotkey_user_mask */
2179 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2180
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 return rc;
2182}
2183
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002184/*
2185 * Sets the driver hotkey mask.
2186 *
2187 * Can be called even if the hotkey subdriver is inactive
2188 */
2189static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2190{
2191 int rc;
2192
2193 /* Do the right thing if hotkey_init has not been called yet */
2194 if (!tp_features.hotkey) {
2195 hotkey_driver_mask = mask;
2196 return 0;
2197 }
2198
2199 mutex_lock(&hotkey_mutex);
2200
2201 HOTKEY_CONFIG_CRITICAL_START
2202 hotkey_driver_mask = mask;
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002203#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002204 hotkey_source_mask |= (mask & ~hotkey_all_mask);
Henrique de Moraes Holschuhb684a362009-09-26 21:42:49 -03002205#endif
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002206 HOTKEY_CONFIG_CRITICAL_END
2207
2208 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2209 ~hotkey_source_mask);
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002210 hotkey_poll_setup(true);
2211
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002212 mutex_unlock(&hotkey_mutex);
2213
2214 return rc;
2215}
2216
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002217static int hotkey_status_get(int *status)
2218{
2219 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2220 return -EIO;
2221
2222 return 0;
2223}
2224
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002225static int hotkey_status_set(bool enable)
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002226{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002227 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002228 return -EIO;
2229
2230 return 0;
2231}
2232
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002233static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002234{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002235 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002236
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002237 if (tp_features.hotkey_tablet &&
2238 !hotkey_get_tablet_mode(&state)) {
2239 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002240
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002241 input_report_switch(tpacpi_inputdev,
2242 SW_TABLET_MODE, !!state);
2243 input_sync(tpacpi_inputdev);
2244
2245 mutex_unlock(&tpacpi_inputdev_send_mutex);
2246 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002247}
2248
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002249/* Do NOT call without validating scancode first */
2250static void tpacpi_input_send_key(const unsigned int scancode)
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002251{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002252 const unsigned int keycode = hotkey_keycode_map[scancode];
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002253
2254 if (keycode != KEY_RESERVED) {
2255 mutex_lock(&tpacpi_inputdev_send_mutex);
2256
Seth Forshee5ffba7e2011-01-14 15:54:39 -06002257 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002258 input_report_key(tpacpi_inputdev, keycode, 1);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002259 input_sync(tpacpi_inputdev);
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, 0);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002263 input_sync(tpacpi_inputdev);
2264
2265 mutex_unlock(&tpacpi_inputdev_send_mutex);
2266 }
2267}
2268
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002269/* Do NOT call without validating scancode first */
2270static void tpacpi_input_send_key_masked(const unsigned int scancode)
2271{
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03002272 hotkey_driver_event(scancode);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002273 if (hotkey_user_mask & (1 << scancode))
2274 tpacpi_input_send_key(scancode);
2275}
2276
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002277#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2278static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2279
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002280/* Do NOT call without validating scancode first */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002281static void tpacpi_hotkey_send_key(unsigned int scancode)
2282{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002283 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002284}
2285
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002286static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002287{
2288 u8 d;
2289
2290 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2291 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2292 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2293 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2294 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2295 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2296 }
2297 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2298 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2299 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2300 }
2301 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2302 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2303 n->displayexp_toggle =
2304 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2305 }
2306 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2308 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2309 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2310 n->brightness_toggle =
2311 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2312 }
2313 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2315 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2316 >> TP_NVRAM_POS_LEVEL_VOLUME;
2317 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2318 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2319 }
2320}
2321
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002322static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2323 struct tp_nvram_state *newn,
2324 const u32 event_mask)
2325{
2326
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002327#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002328 do { \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002329 if ((event_mask & (1 << __scancode)) && \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002330 oldn->__member != newn->__member) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002331 tpacpi_hotkey_send_key(__scancode); \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002332 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002333
2334#define TPACPI_MAY_SEND_KEY(__scancode) \
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002335 do { \
2336 if (event_mask & (1 << __scancode)) \
2337 tpacpi_hotkey_send_key(__scancode); \
2338 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002339
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002340 void issue_volchange(const unsigned int oldvol,
2341 const unsigned int newvol)
2342 {
2343 unsigned int i = oldvol;
2344
2345 while (i > newvol) {
2346 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2347 i--;
2348 }
2349 while (i < newvol) {
2350 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2351 i++;
2352 }
2353 }
2354
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002355 void issue_brightnesschange(const unsigned int oldbrt,
2356 const unsigned int newbrt)
2357 {
2358 unsigned int i = oldbrt;
2359
2360 while (i > newbrt) {
2361 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2362 i--;
2363 }
2364 while (i < newbrt) {
2365 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2366 i++;
2367 }
2368 }
2369
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002370 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2374
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2376
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2378
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002379 /*
2380 * Handle volume
2381 *
2382 * This code is supposed to duplicate the IBM firmware behaviour:
2383 * - Pressing MUTE issues mute hotkey message, even when already mute
2384 * - Pressing Volume up/down issues volume up/down hotkey messages,
Lucas De Marchic8440332011-03-17 17:18:22 -03002385 * even when already at maximum or minimum volume
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002386 * - The act of unmuting issues volume up/down notification,
2387 * depending which key was used to unmute
2388 *
2389 * We are constrained to what the NVRAM can tell us, which is not much
2390 * and certainly not enough if more than one volume hotkey was pressed
2391 * since the last poll cycle.
2392 *
2393 * Just to make our life interesting, some newer Lenovo ThinkPads have
2394 * bugs in the BIOS and may fail to update volume_toggle properly.
2395 */
2396 if (newn->mute) {
2397 /* muted */
2398 if (!oldn->mute ||
2399 oldn->volume_toggle != newn->volume_toggle ||
2400 oldn->volume_level != newn->volume_level) {
2401 /* recently muted, or repeated mute keypress, or
2402 * multiple presses ending in mute */
2403 issue_volchange(oldn->volume_level, newn->volume_level);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2405 }
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002406 } else {
2407 /* unmute */
2408 if (oldn->mute) {
2409 /* recently unmuted, issue 'unmute' keypress */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002411 }
2412 if (oldn->volume_level != newn->volume_level) {
2413 issue_volchange(oldn->volume_level, newn->volume_level);
2414 } else if (oldn->volume_toggle != newn->volume_toggle) {
2415 /* repeated vol up/down keypress at end of scale ? */
2416 if (newn->volume_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
Henrique de Moraes Holschuh5d756db2010-05-16 19:45:28 -03002418 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002420 }
2421 }
2422
2423 /* handle brightness */
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002424 if (oldn->brightness_level != newn->brightness_level) {
2425 issue_brightnesschange(oldn->brightness_level,
2426 newn->brightness_level);
2427 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2428 /* repeated key presses that didn't change state */
2429 if (newn->brightness_level == 0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuh28999022010-05-16 19:45:36 -03002431 else if (newn->brightness_level >= bright_maxlvl
2432 && !tp_features.bright_unkfw)
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002434 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002435
2436#undef TPACPI_COMPARE_KEY
2437#undef TPACPI_MAY_SEND_KEY
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002438}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002439
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002440/*
2441 * Polling driver
2442 *
2443 * We track all events in hotkey_source_mask all the time, since
2444 * most of them are edge-based. We only issue those requested by
2445 * hotkey_user_mask or hotkey_driver_mask, though.
2446 */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002447static int hotkey_kthread(void *data)
2448{
2449 struct tp_nvram_state s[2];
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002450 u32 poll_mask, event_mask;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451 unsigned int si, so;
2452 unsigned long t;
Tejun Heo8a32c442011-11-21 12:32:23 -08002453 unsigned int change_detector;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002454 unsigned int poll_freq;
Tejun Heo8a32c442011-11-21 12:32:23 -08002455 bool was_frozen;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002456
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002457 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2458 goto exit;
2459
2460 set_freezable();
2461
2462 so = 0;
2463 si = 1;
2464 t = 0;
2465
2466 /* Initial state for compares */
2467 mutex_lock(&hotkey_thread_data_mutex);
2468 change_detector = hotkey_config_change;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002469 poll_mask = hotkey_source_mask;
2470 event_mask = hotkey_source_mask &
2471 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002472 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002473 mutex_unlock(&hotkey_thread_data_mutex);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002474 hotkey_read_nvram(&s[so], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002475
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002476 while (!kthread_should_stop()) {
2477 if (t == 0) {
2478 if (likely(poll_freq))
2479 t = 1000/poll_freq;
2480 else
2481 t = 100; /* should never happen... */
2482 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002483 t = msleep_interruptible(t);
Tejun Heo8a32c442011-11-21 12:32:23 -08002484 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002485 break;
Tejun Heo8a32c442011-11-21 12:32:23 -08002486
2487 if (t > 0 && !was_frozen)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002488 continue;
2489
2490 mutex_lock(&hotkey_thread_data_mutex);
Tejun Heo8a32c442011-11-21 12:32:23 -08002491 if (was_frozen || hotkey_config_change != change_detector) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002492 /* forget old state on thaw or config change */
2493 si = so;
2494 t = 0;
2495 change_detector = hotkey_config_change;
2496 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002497 poll_mask = hotkey_source_mask;
2498 event_mask = hotkey_source_mask &
2499 (hotkey_driver_mask | hotkey_user_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002500 poll_freq = hotkey_poll_freq;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002501 mutex_unlock(&hotkey_thread_data_mutex);
2502
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002503 if (likely(poll_mask)) {
2504 hotkey_read_nvram(&s[si], poll_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002505 if (likely(si != so)) {
2506 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002507 event_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002508 }
2509 }
2510
2511 so = si;
2512 si ^= 1;
2513 }
2514
2515exit:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002516 return 0;
2517}
2518
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002519/* call with hotkey_mutex held */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002520static void hotkey_poll_stop_sync(void)
2521{
2522 if (tpacpi_hotkey_task) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002523 kthread_stop(tpacpi_hotkey_task);
2524 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002525 }
2526}
2527
2528/* call with hotkey_mutex held */
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002529static void hotkey_poll_setup(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002530{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002531 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2532 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002533
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002534 if (hotkey_poll_freq > 0 &&
2535 (poll_driver_mask ||
2536 (poll_user_mask && tpacpi_inputdev->users > 0))) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002537 if (!tpacpi_hotkey_task) {
2538 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03002539 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002540 if (IS_ERR(tpacpi_hotkey_task)) {
2541 tpacpi_hotkey_task = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07002542 pr_err("could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002543 "for hotkey polling\n");
2544 }
2545 }
2546 } else {
2547 hotkey_poll_stop_sync();
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002548 if (may_warn && (poll_driver_mask || poll_user_mask) &&
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002549 hotkey_poll_freq == 0) {
Joe Perches0978e012011-04-04 10:06:25 -07002550 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2551 "require polling, which is currently "
2552 "disabled\n",
2553 poll_user_mask, poll_driver_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002554 }
2555 }
2556}
2557
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002558static void hotkey_poll_setup_safe(const bool may_warn)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002559{
2560 mutex_lock(&hotkey_mutex);
2561 hotkey_poll_setup(may_warn);
2562 mutex_unlock(&hotkey_mutex);
2563}
2564
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002565/* call with hotkey_mutex held */
2566static void hotkey_poll_set_freq(unsigned int freq)
2567{
2568 if (!freq)
2569 hotkey_poll_stop_sync();
2570
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002571 hotkey_poll_freq = freq;
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002572}
2573
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002574#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2575
Henrique de Moraes Holschuh7f0cf712010-02-25 21:29:00 -03002576static void hotkey_poll_setup(const bool __unused)
2577{
2578}
2579
2580static void hotkey_poll_setup_safe(const bool __unused)
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02002581{
2582}
2583
2584#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2585
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002586static int hotkey_inputdev_open(struct input_dev *dev)
2587{
2588 switch (tpacpi_lifecycle) {
2589 case TPACPI_LIFE_INIT:
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002590 case TPACPI_LIFE_RUNNING:
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002591 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002592 return 0;
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002593 case TPACPI_LIFE_EXITING:
2594 return -EBUSY;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002595 }
2596
2597 /* Should only happen if tpacpi_lifecycle is corrupt */
2598 BUG();
2599 return -EBUSY;
2600}
2601
2602static void hotkey_inputdev_close(struct input_dev *dev)
2603{
2604 /* disable hotkey polling when possible */
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03002605 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002606 !(hotkey_source_mask & hotkey_driver_mask))
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002607 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002608}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002609
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002610/* sysfs hotkey enable ------------------------------------------------- */
2611static ssize_t hotkey_enable_show(struct device *dev,
2612 struct device_attribute *attr,
2613 char *buf)
2614{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002615 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002616
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002617 printk_deprecated_attribute("hotkey_enable",
2618 "Hotkey reporting is always enabled");
2619
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002620 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002621 if (res)
2622 return res;
2623
2624 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2625}
2626
2627static ssize_t hotkey_enable_store(struct device *dev,
2628 struct device_attribute *attr,
2629 const char *buf, size_t count)
2630{
2631 unsigned long t;
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002632
2633 printk_deprecated_attribute("hotkey_enable",
2634 "Hotkeys can be disabled through hotkey_mask");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002635
2636 if (parse_strtoul(buf, 1, &t))
2637 return -EINVAL;
2638
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002639 if (t == 0)
2640 return -EPERM;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002641
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002642 return count;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002643}
2644
2645static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002646 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002647 hotkey_enable_show, hotkey_enable_store);
2648
2649/* sysfs hotkey mask --------------------------------------------------- */
2650static ssize_t hotkey_mask_show(struct device *dev,
2651 struct device_attribute *attr,
2652 char *buf)
2653{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002654 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002655}
2656
2657static ssize_t hotkey_mask_store(struct device *dev,
2658 struct device_attribute *attr,
2659 const char *buf, size_t count)
2660{
2661 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002662 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002663
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002664 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002665 return -EINVAL;
2666
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002667 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002668 return -ERESTARTSYS;
2669
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002670 res = hotkey_user_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002671
2672#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002673 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002674#endif
2675
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002676 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002677
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002678 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2679
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002680 return (res) ? res : count;
2681}
2682
2683static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002684 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002685 hotkey_mask_show, hotkey_mask_store);
2686
2687/* sysfs hotkey bios_enabled ------------------------------------------- */
2688static ssize_t hotkey_bios_enabled_show(struct device *dev,
2689 struct device_attribute *attr,
2690 char *buf)
2691{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00002692 return sprintf(buf, "0\n");
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002693}
2694
2695static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002696 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002697
2698/* sysfs hotkey bios_mask ---------------------------------------------- */
2699static ssize_t hotkey_bios_mask_show(struct device *dev,
2700 struct device_attribute *attr,
2701 char *buf)
2702{
Henrique de Moraes Holschuh06777be2009-09-12 15:22:15 -03002703 printk_deprecated_attribute("hotkey_bios_mask",
2704 "This attribute is useless.");
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002705 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002706}
2707
2708static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002709 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002710
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002711/* sysfs hotkey all_mask ----------------------------------------------- */
2712static ssize_t hotkey_all_mask_show(struct device *dev,
2713 struct device_attribute *attr,
2714 char *buf)
2715{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002716 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2717 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002718}
2719
2720static struct device_attribute dev_attr_hotkey_all_mask =
2721 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2722
2723/* sysfs hotkey recommended_mask --------------------------------------- */
2724static ssize_t hotkey_recommended_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
2728 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002729 (hotkey_all_mask | hotkey_source_mask)
2730 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002731}
2732
2733static struct device_attribute dev_attr_hotkey_recommended_mask =
2734 __ATTR(hotkey_recommended_mask, S_IRUGO,
2735 hotkey_recommended_mask_show, NULL);
2736
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002737#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2738
2739/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2740static ssize_t hotkey_source_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2743{
2744 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2745}
2746
2747static ssize_t hotkey_source_mask_store(struct device *dev,
2748 struct device_attribute *attr,
2749 const char *buf, size_t count)
2750{
2751 unsigned long t;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002752 u32 r_ev;
2753 int rc;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002754
2755 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2756 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2757 return -EINVAL;
2758
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002759 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002760 return -ERESTARTSYS;
2761
2762 HOTKEY_CONFIG_CRITICAL_START
2763 hotkey_source_mask = t;
2764 HOTKEY_CONFIG_CRITICAL_END
2765
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002766 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2767 ~hotkey_source_mask);
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002768 hotkey_poll_setup(true);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002769
2770 /* check if events needed by the driver got disabled */
2771 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2772 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002773
2774 mutex_unlock(&hotkey_mutex);
2775
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002776 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07002777 pr_err("hotkey_source_mask: "
2778 "failed to update the firmware event mask!\n");
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002779
2780 if (r_ev)
Joe Perches0978e012011-04-04 10:06:25 -07002781 pr_notice("hotkey_source_mask: "
2782 "some important events were disabled: 0x%04x\n",
2783 r_ev);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002784
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002785 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2786
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002787 return (rc < 0) ? rc : count;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002788}
2789
2790static struct device_attribute dev_attr_hotkey_source_mask =
2791 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2792 hotkey_source_mask_show, hotkey_source_mask_store);
2793
2794/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2795static ssize_t hotkey_poll_freq_show(struct device *dev,
2796 struct device_attribute *attr,
2797 char *buf)
2798{
2799 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2800}
2801
2802static ssize_t hotkey_poll_freq_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2805{
2806 unsigned long t;
2807
2808 if (parse_strtoul(buf, 25, &t))
2809 return -EINVAL;
2810
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02002811 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002812 return -ERESTARTSYS;
2813
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002814 hotkey_poll_set_freq(t);
2815 hotkey_poll_setup(true);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002816
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002817 mutex_unlock(&hotkey_mutex);
2818
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00002819 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2820
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002821 return count;
2822}
2823
2824static struct device_attribute dev_attr_hotkey_poll_freq =
2825 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2826 hotkey_poll_freq_show, hotkey_poll_freq_store);
2827
2828#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2829
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002830/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002831static ssize_t hotkey_radio_sw_show(struct device *dev,
2832 struct device_attribute *attr,
2833 char *buf)
2834{
Johannes Berg19d337d2009-06-02 13:01:37 +02002835 int res;
2836 res = hotkey_get_wlsw();
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002837 if (res < 0)
2838 return res;
2839
Johannes Berg19d337d2009-06-02 13:01:37 +02002840 /* Opportunistic update */
2841 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2842
2843 return snprintf(buf, PAGE_SIZE, "%d\n",
2844 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002845}
2846
2847static struct device_attribute dev_attr_hotkey_radio_sw =
2848 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2849
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002850static void hotkey_radio_sw_notify_change(void)
2851{
2852 if (tp_features.hotkey_wlsw)
2853 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2854 "hotkey_radio_sw");
2855}
2856
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002857/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2858static ssize_t hotkey_tablet_mode_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2861{
2862 int res, s;
2863 res = hotkey_get_tablet_mode(&s);
2864 if (res < 0)
2865 return res;
2866
2867 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2868}
2869
2870static struct device_attribute dev_attr_hotkey_tablet_mode =
2871 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2872
2873static void hotkey_tablet_mode_notify_change(void)
2874{
2875 if (tp_features.hotkey_tablet)
2876 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2877 "hotkey_tablet_mode");
2878}
2879
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002880/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002881static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2882 struct device_attribute *attr,
2883 char *buf)
2884{
2885 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2886}
2887
2888static struct device_attribute dev_attr_hotkey_wakeup_reason =
2889 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2890
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002891static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002892{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002893 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2894 "wakeup_reason");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002895}
2896
2897/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002898static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2899 struct device_attribute *attr,
2900 char *buf)
2901{
2902 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2903}
2904
2905static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2906 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2907 hotkey_wakeup_hotunplug_complete_show, NULL);
2908
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002909static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002910{
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002911 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2912 "wakeup_hotunplug_complete");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002913}
2914
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002915/* --------------------------------------------------------------------- */
2916
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002917static struct attribute *hotkey_attributes[] __initdata = {
2918 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002919 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002920 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002921 &dev_attr_hotkey_wakeup_reason.attr,
2922 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002923 &dev_attr_hotkey_mask.attr,
2924 &dev_attr_hotkey_all_mask.attr,
2925 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002926#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002927 &dev_attr_hotkey_source_mask.attr,
2928 &dev_attr_hotkey_poll_freq.attr,
2929#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002930};
2931
Johannes Berg19d337d2009-06-02 13:01:37 +02002932/*
2933 * Sync both the hw and sw blocking state of all switches
2934 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002935static void tpacpi_send_radiosw_update(void)
2936{
2937 int wlsw;
2938
Johannes Berg19d337d2009-06-02 13:01:37 +02002939 /*
2940 * We must sync all rfkill controllers *before* issuing any
2941 * rfkill input events, or we will race the rfkill core input
2942 * handler.
2943 *
Lucas De Marchic8440332011-03-17 17:18:22 -03002944 * tpacpi_inputdev_send_mutex works as a synchronization point
Johannes Berg19d337d2009-06-02 13:01:37 +02002945 * for the above.
2946 *
2947 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2948 */
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002949
Johannes Berg19d337d2009-06-02 13:01:37 +02002950 wlsw = hotkey_get_wlsw();
2951
2952 /* Sync hw blocking state first if it is hw-blocked */
2953 if (wlsw == TPACPI_RFK_RADIO_OFF)
2954 tpacpi_rfk_update_hwblock_state(true);
2955
2956 /* Sync sw blocking state */
2957 tpacpi_rfk_update_swstate_all();
2958
2959 /* Sync hw blocking state last if it is hw-unblocked */
2960 if (wlsw == TPACPI_RFK_RADIO_ON)
2961 tpacpi_rfk_update_hwblock_state(false);
2962
2963 /* Issue rfkill input event for WLSW switch */
2964 if (!(wlsw < 0)) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002965 mutex_lock(&tpacpi_inputdev_send_mutex);
2966
2967 input_report_switch(tpacpi_inputdev,
Johannes Berg19d337d2009-06-02 13:01:37 +02002968 SW_RFKILL_ALL, (wlsw > 0));
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002969 input_sync(tpacpi_inputdev);
2970
2971 mutex_unlock(&tpacpi_inputdev_send_mutex);
2972 }
Johannes Berg19d337d2009-06-02 13:01:37 +02002973
2974 /*
2975 * this can be unconditional, as we will poll state again
2976 * if userspace uses the notify to read data
2977 */
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002978 hotkey_radio_sw_notify_change();
2979}
2980
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002981static void hotkey_exit(void)
2982{
2983#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002984 mutex_lock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002985 hotkey_poll_stop_sync();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03002986 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002987#endif
2988
2989 if (hotkey_dev_attributes)
2990 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2991
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03002992 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03002993 "restoring original HKEY status and mask\n");
2994 /* yes, there is a bitwise or below, we want the
2995 * functions to be called even if one of them fail */
2996 if (((tp_features.hotkey_mask &&
2997 hotkey_mask_set(hotkey_orig_mask)) |
2998 hotkey_status_set(false)) != 0)
Joe Perches0978e012011-04-04 10:06:25 -07002999 pr_err("failed to restore hot key mask "
Henrique de Moraes Holschuh4be73002009-09-20 14:09:23 -03003000 "to BIOS defaults\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003001}
3002
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003003static void __init hotkey_unmap(const unsigned int scancode)
3004{
3005 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3006 clear_bit(hotkey_keycode_map[scancode],
3007 tpacpi_inputdev->keybit);
3008 hotkey_keycode_map[scancode] = KEY_RESERVED;
3009 }
3010}
3011
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003012/*
3013 * HKEY quirks:
3014 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3015 */
3016
3017#define TPACPI_HK_Q_INIMASK 0x0001
3018
3019static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3020 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3021 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3022 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3023 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3024 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3025 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3026 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3027 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3028 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3029 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3030 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3031 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3032 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3033 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3034 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3035 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3036 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3037 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3038 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3039};
3040
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003041typedef u16 tpacpi_keymap_entry_t;
3042typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003043
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003044static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003045{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003046 /* Requirements for changing the default keymaps:
3047 *
3048 * 1. Many of the keys are mapped to KEY_RESERVED for very
3049 * good reasons. Do not change them unless you have deep
3050 * knowledge on the IBM and Lenovo ThinkPad firmware for
3051 * the various ThinkPad models. The driver behaves
3052 * differently for KEY_RESERVED: such keys have their
3053 * hot key mask *unset* in mask_recommended, and also
3054 * in the initial hot key mask programmed into the
3055 * firmware at driver load time, which means the firm-
3056 * ware may react very differently if you change them to
3057 * something else;
3058 *
3059 * 2. You must be subscribed to the linux-thinkpad and
3060 * ibm-acpi-devel mailing lists, and you should read the
3061 * list archives since 2007 if you want to change the
3062 * keymaps. This requirement exists so that you will
3063 * know the past history of problems with the thinkpad-
3064 * acpi driver keymaps, and also that you will be
3065 * listening to any bug reports;
3066 *
3067 * 3. Do not send thinkpad-acpi specific patches directly to
3068 * for merging, *ever*. Send them to the linux-acpi
3069 * mailinglist for comments. Merging is to be done only
3070 * through acpi-test and the ACPI maintainer.
3071 *
3072 * If the above is too much to ask, don't change the keymap.
3073 * Ask the thinkpad-acpi maintainer to do it, instead.
3074 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003075
3076 enum keymap_index {
3077 TPACPI_KEYMAP_IBM_GENERIC = 0,
3078 TPACPI_KEYMAP_LENOVO_GENERIC,
3079 };
3080
3081 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3082 /* Generic keymap for IBM ThinkPads */
3083 [TPACPI_KEYMAP_IBM_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003084 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003085 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003086 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3087 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003088
3089 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003090 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3091 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3092 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003093
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003094 /* brightness: firmware always reacts to them */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003095 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003096 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003097
3098 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003099 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003100
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003101 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3102 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003103
3104 /* Volume: firmware always react to it and reprograms
3105 * the built-in *extra* mixer. Never map it to control
3106 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003107 KEY_RESERVED, /* 0x14: VOLUME UP */
3108 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3109 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003110
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003111 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003112
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003113 /* (assignments unknown, please report if found) */
3114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3115 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003116 },
3117
3118 /* Generic keymap for Lenovo ThinkPads */
3119 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003120 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3121 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
Jens Taprogge2b754262010-08-09 23:48:22 -03003122 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003123 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003124
3125 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003126 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3127 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3128 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003129
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003130 /* These should be enabled --only-- when ACPI video
3131 * is disabled (i.e. in "vendor" mode), and are handled
3132 * in a special way by the init code */
3133 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3134 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003135
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003136 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003137
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003138 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3139 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003140
3141 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3142 * react to it and reprograms the built-in *extra* mixer.
3143 * Never map it to control another mixer by default.
3144 *
3145 * T60?, T61, R60?, R61: firmware and EC tries to send
3146 * these over the regular keyboard, so these are no-ops,
3147 * but there are still weird bugs re. MUTE, so do not
3148 * change unless you get test reports from all Lenovo
3149 * models. May cause the BIOS to interfere with the
3150 * HDA mixer.
3151 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02003152 KEY_RESERVED, /* 0x14: VOLUME UP */
3153 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3154 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003155
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003156 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02003157
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003158 /* (assignments unknown, please report if found) */
Andy Lutomirski33009552011-05-24 15:16:43 -04003159 KEY_UNKNOWN, KEY_UNKNOWN,
3160
3161 /*
3162 * The mic mute button only sends 0x1a. It does not
3163 * automatically mute the mic or change the mute light.
3164 */
3165 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3166
3167 /* (assignments unknown, please report if found) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003168 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Andy Lutomirski33009552011-05-24 15:16:43 -04003169 KEY_UNKNOWN,
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003170 },
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003171 };
3172
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003173 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3174 /* Generic maps (fallback) */
3175 {
3176 .vendor = PCI_VENDOR_ID_IBM,
3177 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3178 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3179 },
3180 {
3181 .vendor = PCI_VENDOR_ID_LENOVO,
3182 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3183 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3184 },
3185 };
3186
3187#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
Henrique de Moraes Holschuhfc6e7562010-09-17 21:53:41 -03003188#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003189
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003190 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003191 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03003192 int hkeyv;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003193 bool radiosw_state = false;
3194 bool tabletsw_state = false;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003195
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003196 unsigned long quirks;
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003197 unsigned long keymap_id;
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003198
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003199 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3200 "initializing hotkey subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003201
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003202 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003203 BUG_ON(tpacpi_inputdev->open != NULL ||
3204 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003205
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003206 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003207 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003208
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003209#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02003210 mutex_init(&hotkey_thread_data_mutex);
3211#endif
3212
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003213 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003214 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003215
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3217 "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003218 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003219
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003220 if (!tp_features.hotkey)
3221 return 1;
3222
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003223 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3224 ARRAY_SIZE(tpacpi_hotkey_qtable));
3225
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003226 tpacpi_disable_brightness_delay();
3227
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003228 /* MUST have enough space for all attributes to be added to
3229 * hotkey_dev_attributes */
3230 hotkey_dev_attributes = create_attr_set(
3231 ARRAY_SIZE(hotkey_attributes) + 2,
3232 NULL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003233 if (!hotkey_dev_attributes)
3234 return -ENOMEM;
3235 res = add_many_to_attr_set(hotkey_dev_attributes,
3236 hotkey_attributes,
3237 ARRAY_SIZE(hotkey_attributes));
3238 if (res)
3239 goto err_exit;
3240
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003241 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003242 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3243 for HKEY interface version 0x100 */
3244 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3245 if ((hkeyv >> 8) != 1) {
Joe Perches0978e012011-04-04 10:06:25 -07003246 pr_err("unknown version of the HKEY interface: 0x%x\n",
3247 hkeyv);
3248 pr_err("please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003249 } else {
3250 /*
3251 * MHKV 0x100 in A31, R40, R40e,
3252 * T4x, X31, and later
3253 */
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003254 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3255 "firmware HKEY interface version: 0x%x\n",
3256 hkeyv);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003257
3258 /* Paranoia check AND init hotkey_all_mask */
3259 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3260 "MHKA", "qd")) {
Joe Perches0978e012011-04-04 10:06:25 -07003261 pr_err("missing MHKA handler, "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003262 "please report this to %s\n",
3263 TPACPI_MAIL);
3264 /* Fallback: pre-init for FN+F3,F4,F12 */
3265 hotkey_all_mask = 0x080cU;
3266 } else {
3267 tp_features.hotkey_mask = 1;
3268 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003269 }
3270 }
3271
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3273 "hotkey masks are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003274 str_supported(tp_features.hotkey_mask));
3275
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003276 /* Init hotkey_all_mask if not initialized yet */
3277 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3278 (quirks & TPACPI_HK_Q_INIMASK))
3279 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003280
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003281 /* Init hotkey_acpi_mask and hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003282 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003283 /* hotkey_source_mask *must* be zero for
3284 * the first hotkey_mask_get to return hotkey_orig_mask */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003285 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003286 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003287 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003288
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003289 hotkey_orig_mask = hotkey_acpi_mask;
3290 } else {
3291 hotkey_orig_mask = hotkey_all_mask;
3292 hotkey_acpi_mask = hotkey_all_mask;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003293 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003294
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003295#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3296 if (dbg_wlswemul) {
3297 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003298 radiosw_state = !!tpacpi_wlsw_emulstate;
Joe Perches0978e012011-04-04 10:06:25 -07003299 pr_info("radio switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003300 } else
3301#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003302 /* Not all thinkpads have a hardware radio switch */
3303 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3304 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003305 radiosw_state = !!status;
Joe Perches0978e012011-04-04 10:06:25 -07003306 pr_info("radio switch found; radios are %s\n",
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003307 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003308 }
3309 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003310 res = add_to_attr_set(hotkey_dev_attributes,
3311 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03003312
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003313 /* For X41t, X60t, X61t Tablets... */
3314 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3315 tp_features.hotkey_tablet = 1;
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003316 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
Joe Perches0978e012011-04-04 10:06:25 -07003317 pr_info("possible tablet mode switch found; "
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003318 "ThinkPad in %s mode\n",
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003319 (tabletsw_state) ? "tablet" : "laptop");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003320 res = add_to_attr_set(hotkey_dev_attributes,
3321 &dev_attr_hotkey_tablet_mode.attr);
3322 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003323
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003324 if (!res)
3325 res = register_attr_set_with_sysfs(
3326 hotkey_dev_attributes,
3327 &tpacpi_pdev->dev.kobj);
3328 if (res)
3329 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003330
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003331 /* Set up key map */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003332 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3333 GFP_KERNEL);
3334 if (!hotkey_keycode_map) {
Joe Perches0978e012011-04-04 10:06:25 -07003335 pr_err("failed to allocate memory for key map\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003336 res = -ENOMEM;
3337 goto err_exit;
3338 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003339
Henrique de Moraes Holschuhd1e14dc2010-08-09 23:48:21 -03003340 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3341 ARRAY_SIZE(tpacpi_keymap_qtable));
3342 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3343 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3344 "using keymap number %lu\n", keymap_id);
3345
3346 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3347 TPACPI_HOTKEY_MAP_SIZE);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003348
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003349 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003350 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3351 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3352 tpacpi_inputdev->keycode = hotkey_keycode_map;
3353 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3354 if (hotkey_keycode_map[i] != KEY_RESERVED) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003355 input_set_capability(tpacpi_inputdev, EV_KEY,
3356 hotkey_keycode_map[i]);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003357 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003358 if (i < sizeof(hotkey_reserved_mask)*8)
3359 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03003360 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 }
3362
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003363 if (tp_features.hotkey_wlsw) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003364 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003365 input_report_switch(tpacpi_inputdev,
3366 SW_RFKILL_ALL, radiosw_state);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003367 }
3368 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuh792979c2009-12-09 01:36:29 +00003369 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
Henrique de Moraes Holschuhd89a7272009-12-15 21:51:06 -02003370 input_report_switch(tpacpi_inputdev,
3371 SW_TABLET_MODE, tabletsw_state);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003372 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003373
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003374 /* Do not issue duplicate brightness change events to
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03003375 * userspace. tpacpi_detect_brightness_capabilities() must have
3376 * been called before this point */
Alex Hungff413192012-04-24 16:40:52 +08003377 if (acpi_video_backlight_support()) {
Joe Perches0978e012011-04-04 10:06:25 -07003378 pr_info("This ThinkPad has standard ACPI backlight "
3379 "brightness control, supported by the ACPI "
3380 "video driver\n");
3381 pr_notice("Disabling thinkpad-acpi brightness events "
3382 "by default...\n");
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003383
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003384 /* Disable brightness up/down on Lenovo thinkpads when
3385 * ACPI is handling them, otherwise it is plain impossible
3386 * for userspace to do something even remotely sane */
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003387 hotkey_reserved_mask |=
3388 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3389 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuhde4c8cc2009-09-12 15:22:18 -03003390 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3391 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003392 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003393
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003394#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003395 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3396 & ~hotkey_all_mask
3397 & ~hotkey_reserved_mask;
Henrique de Moraes Holschuh230d8cf252009-09-12 15:22:17 -03003398
3399 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3400 "hotkey source mask 0x%08x, polling freq %u\n",
3401 hotkey_source_mask, hotkey_poll_freq);
3402#endif
3403
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003404 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3405 "enabling firmware HKEY event interface...\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003406 res = hotkey_status_set(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003407 if (res) {
3408 hotkey_exit();
3409 return res;
3410 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003411 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3412 | hotkey_driver_mask)
3413 & ~hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003414 if (res < 0 && res != -ENXIO) {
3415 hotkey_exit();
3416 return res;
3417 }
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003418 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3419 & ~hotkey_reserved_mask;
3420 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3421 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3422 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003423
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003424 tpacpi_inputdev->open = &hotkey_inputdev_open;
3425 tpacpi_inputdev->close = &hotkey_inputdev_close;
3426
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003427 hotkey_poll_setup_safe(true);
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003428
3429 return 0;
3430
3431err_exit:
3432 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3433 hotkey_dev_attributes = NULL;
3434
3435 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003436}
3437
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003438static bool hotkey_notify_hotkey(const u32 hkey,
3439 bool *send_acpi_ev,
3440 bool *ignore_acpi_ev)
3441{
3442 /* 0x1000-0x1FFF: key presses */
3443 unsigned int scancode = hkey & 0xfff;
3444 *send_acpi_ev = true;
3445 *ignore_acpi_ev = false;
3446
Henrique de Moraes Holschuh34a656d22010-08-09 23:48:20 -03003447 /* HKEY event 0x1001 is scancode 0x00 */
3448 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003449 scancode--;
3450 if (!(hotkey_source_mask & (1 << scancode))) {
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003451 tpacpi_input_send_key_masked(scancode);
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003452 *send_acpi_ev = false;
3453 } else {
3454 *ignore_acpi_ev = true;
3455 }
3456 return true;
3457 }
3458 return false;
3459}
3460
3461static bool hotkey_notify_wakeup(const u32 hkey,
3462 bool *send_acpi_ev,
3463 bool *ignore_acpi_ev)
3464{
3465 /* 0x2000-0x2FFF: Wakeup reason */
3466 *send_acpi_ev = true;
3467 *ignore_acpi_ev = false;
3468
3469 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003470 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3471 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003472 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3473 *ignore_acpi_ev = true;
3474 break;
3475
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003476 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3477 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003478 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3479 *ignore_acpi_ev = true;
3480 break;
3481
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003482 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3483 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
Joe Perches0978e012011-04-04 10:06:25 -07003484 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003485 /* how to auto-heal: */
3486 /* 2313: woke up from S3, go to S4/S5 */
3487 /* 2413: woke up from S4, go to S5 */
3488 break;
3489
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003490 default:
3491 return false;
3492 }
3493
3494 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
Joe Perches0978e012011-04-04 10:06:25 -07003495 pr_info("woke up due to a hot-unplug request...\n");
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003496 hotkey_wakeup_reason_notify_change();
3497 }
3498 return true;
3499}
3500
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003501static bool hotkey_notify_dockevent(const u32 hkey,
3502 bool *send_acpi_ev,
3503 bool *ignore_acpi_ev)
3504{
3505 /* 0x4000-0x4FFF: dock-related events */
3506 *send_acpi_ev = true;
3507 *ignore_acpi_ev = false;
3508
3509 switch (hkey) {
3510 case TP_HKEY_EV_UNDOCK_ACK:
3511 /* ACPI undock operation completed after wakeup */
3512 hotkey_autosleep_ack = 1;
3513 pr_info("undocked\n");
3514 hotkey_wakeup_hotunplug_complete_notify_change();
3515 return true;
3516
3517 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3518 pr_info("docked into hotplug port replicator\n");
3519 return true;
3520 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3521 pr_info("undocked from hotplug port replicator\n");
3522 return true;
3523
3524 default:
3525 return false;
3526 }
3527}
3528
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003529static bool hotkey_notify_usrevent(const u32 hkey,
3530 bool *send_acpi_ev,
3531 bool *ignore_acpi_ev)
3532{
3533 /* 0x5000-0x5FFF: human interface helpers */
3534 *send_acpi_ev = true;
3535 *ignore_acpi_ev = false;
3536
3537 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003538 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3539 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003540 return true;
3541
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003542 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3543 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003544 tpacpi_input_send_tabletsw();
3545 hotkey_tablet_mode_notify_change();
3546 *send_acpi_ev = false;
3547 return true;
3548
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003549 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3550 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3551 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
Henrique de Moraes Holschuh176dd982009-09-20 14:09:24 -03003552 /* do not propagate these events */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003553 *ignore_acpi_ev = true;
3554 return true;
3555
3556 default:
3557 return false;
3558 }
3559}
3560
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003561static void thermal_dump_all_sensors(void);
3562
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003563static bool hotkey_notify_6xxx(const u32 hkey,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003564 bool *send_acpi_ev,
3565 bool *ignore_acpi_ev)
3566{
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003567 bool known = true;
3568
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003569 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003570 *send_acpi_ev = true;
3571 *ignore_acpi_ev = false;
3572
3573 switch (hkey) {
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003574 case TP_HKEY_EV_THM_TABLE_CHANGED:
Joe Perches0978e012011-04-04 10:06:25 -07003575 pr_info("EC reports that Thermal Table has changed\n");
Henrique de Moraes Holschuh54926ce2009-01-11 03:01:09 -02003576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003579 case TP_HKEY_EV_ALARM_BAT_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003580 pr_crit("THERMAL ALARM: battery is too hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003581 /* recommended action: warn user through gui */
3582 break;
3583 case TP_HKEY_EV_ALARM_BAT_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003584 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003585 /* recommended action: immediate sleep/hibernate */
3586 break;
3587 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Joe Perches0978e012011-04-04 10:06:25 -07003588 pr_crit("THERMAL ALARM: "
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003589 "a sensor reports something is too hot!\n");
3590 /* recommended action: warn user through gui, that */
3591 /* some internal component is too hot */
3592 break;
3593 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Joe Perches0978e012011-04-04 10:06:25 -07003594 pr_alert("THERMAL EMERGENCY: "
3595 "a sensor reports something is extremely hot!\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003596 /* recommended action: immediate sleep/hibernate */
3597 break;
Richard Hartmann6f62bc32012-12-29 22:51:49 +01003598 case TP_HKEY_EV_AC_CHANGED:
3599 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3600 * AC status changed; can be triggered by plugging or
3601 * unplugging AC adapter, docking or undocking. */
3602
3603 /* fallthrough */
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003604
3605 case TP_HKEY_EV_KEY_NUMLOCK:
3606 case TP_HKEY_EV_KEY_FN:
3607 /* key press events, we just ignore them as long as the EC
3608 * is still reporting them in the normal keyboard stream */
3609 *send_acpi_ev = false;
3610 *ignore_acpi_ev = true;
3611 return true;
3612
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003613 default:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003614 pr_warn("unknown possible thermal alarm or keyboard event received\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003615 known = false;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003616 }
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00003617
3618 thermal_dump_all_sensors();
3619
3620 return known;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003621}
3622
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003623static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3624{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003625 u32 hkey;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003626 bool send_acpi_ev;
3627 bool ignore_acpi_ev;
3628 bool known_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003629
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003630 if (event != 0x80) {
Joe Perches0978e012011-04-04 10:06:25 -07003631 pr_err("unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003632 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003633 acpi_bus_generate_netlink_event(
3634 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003635 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003636 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003637 return;
3638 }
3639
3640 while (1) {
3641 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Joe Perches0978e012011-04-04 10:06:25 -07003642 pr_err("failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003643 return;
3644 }
3645
3646 if (hkey == 0) {
3647 /* queue empty */
3648 return;
3649 }
3650
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003651 send_acpi_ev = true;
3652 ignore_acpi_ev = false;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003653
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003654 switch (hkey >> 12) {
3655 case 1:
3656 /* 0x1000-0x1FFF: key presses */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003657 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3658 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003659 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003660 case 2:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003661 /* 0x2000-0x2FFF: Wakeup reason */
3662 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3663 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003664 break;
3665 case 3:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003666 /* 0x3000-0x3FFF: bay-related wakeups */
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003667 switch (hkey) {
3668 case TP_HKEY_EV_BAYEJ_ACK:
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003669 hotkey_autosleep_ack = 1;
Joe Perches0978e012011-04-04 10:06:25 -07003670 pr_info("bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003671 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003672 known_ev = true;
Henrique de Moraes Holschuhbf8b29c2010-02-25 21:28:56 -03003673 break;
3674 case TP_HKEY_EV_OPTDRV_EJ:
3675 /* FIXME: kick libata if SATA link offline */
3676 known_ev = true;
3677 break;
3678 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003679 known_ev = false;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003680 }
3681 break;
3682 case 4:
Henrique de Moraes Holschuha50245a2011-06-05 16:22:35 -03003683 /* 0x4000-0x4FFF: dock-related events */
3684 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3685 &ignore_acpi_ev);
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003686 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003687 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02003688 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003689 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3690 &ignore_acpi_ev);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003691 break;
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003692 case 6:
Henrique de Moraes Holschuh2d43f672011-06-05 16:22:34 -03003693 /* 0x6000-0x6FFF: thermal alarms/notices and
3694 * keyboard events */
3695 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
Henrique de Moraes Holschuh106b4e62009-01-11 03:01:06 -02003696 &ignore_acpi_ev);
3697 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003698 case 7:
3699 /* 0x7000-0x7FFF: misc */
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03003700 if (tp_features.hotkey_wlsw &&
3701 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003702 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003703 send_acpi_ev = 0;
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003704 known_ev = true;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003705 break;
3706 }
3707 /* fallthrough to default */
3708 default:
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003709 known_ev = false;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02003710 }
Henrique de Moraes Holschuh3827e7a2009-01-11 03:01:05 -02003711 if (!known_ev) {
Joe Perches0978e012011-04-04 10:06:25 -07003712 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3713 pr_notice("please report the conditions when this "
3714 "event happened to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03003715 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03003716
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003717 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03003718 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003719 acpi_bus_generate_netlink_event(
3720 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003721 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003722 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03003723 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003724 }
3725}
3726
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02003727static void hotkey_suspend(void)
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003728{
3729 /* Do these on suspend, we get the events on early resume! */
3730 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3731 hotkey_autosleep_ack = 0;
3732}
3733
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003734static void hotkey_resume(void)
3735{
Henrique de Moraes Holschuhd64c81c42008-10-18 14:23:55 -03003736 tpacpi_disable_brightness_delay();
3737
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003738 if (hotkey_status_set(true) < 0 ||
3739 hotkey_mask_set(hotkey_acpi_mask) < 0)
Joe Perches0978e012011-04-04 10:06:25 -07003740 pr_err("error while attempting to reset the event "
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003741 "firmware interface\n");
3742
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03003743 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02003744 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02003745 hotkey_wakeup_reason_notify_change();
3746 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuhdb25f162009-09-12 15:22:14 -03003747 hotkey_poll_setup_safe(false);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003748}
3749
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03003750/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003751static int hotkey_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003753 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003755 if (!tp_features.hotkey) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003756 seq_printf(m, "status:\t\tnot supported\n");
3757 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003758 }
3759
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003760 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003761 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02003762 res = hotkey_status_get(&status);
3763 if (!res)
3764 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003765 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03003766 if (res)
3767 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003769 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003770 if (hotkey_all_mask) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003771 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3772 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003774 seq_printf(m, "mask:\t\tnot supported\n");
3775 seq_printf(m, "commands:\tenable, disable, reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777
Alexey Dobriyan887965e2009-12-15 21:51:12 -02003778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
3780
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003781static void hotkey_enabledisable_warn(bool enable)
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003782{
3783 tpacpi_log_usertask("procfs hotkey enable/disable");
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003784 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Joe Perches0978e012011-04-04 10:06:25 -07003785 pr_fmt("hotkey enable/disable functionality has been "
3786 "removed from the driver. "
3787 "Hotkeys are always enabled.\n")))
3788 pr_err("Please remove the hotkey=enable module "
3789 "parameter, it is deprecated. "
3790 "Hotkeys are always enabled.\n");
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003791}
3792
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003793static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003795 int res;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03003796 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003799 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return -ENODEV;
3801
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02003802 if (mutex_lock_killable(&hotkey_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003803 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003804
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003805 mask = hotkey_user_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003806
3807 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 while ((cmd = next_cmd(&buf))) {
3809 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003810 hotkey_enabledisable_warn(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh406e9882009-04-14 02:44:10 +00003812 hotkey_enabledisable_warn(0);
Henrique de Moraes Holschuh2586d562009-04-04 04:25:48 +00003813 res = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 } else if (strlencmp(cmd, "reset") == 0) {
Henrique de Moraes Holschuh20c9aa462009-09-12 15:22:16 -03003815 mask = (hotkey_all_mask | hotkey_source_mask)
3816 & ~hotkey_reserved_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3818 /* mask set */
3819 } else if (sscanf(cmd, "%x", &mask) == 1) {
3820 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003821 } else {
3822 res = -EINVAL;
3823 goto errexit;
3824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003826
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003827 if (!res) {
Henrique de Moraes Holschuh56e2c202009-04-04 04:25:51 +00003828 tpacpi_disclose_usertask("procfs hotkey",
3829 "set mask to 0x%08x\n", mask);
Henrique de Moraes Holschuh0d922e32009-09-20 14:09:25 -03003830 res = hotkey_user_mask_set(mask);
3831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003833errexit:
3834 mutex_unlock(&hotkey_mutex);
3835 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003838static const struct acpi_device_id ibm_htk_device_ids[] = {
Manoj Iyer9fbdaeb2011-05-08 18:04:29 -04003839 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3840 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003841 {"", 0},
3842};
3843
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003844static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003845 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003846 .notify = hotkey_notify,
3847 .handle = &hkey_handle,
3848 .type = ACPI_DEVICE_NOTIFY,
3849};
3850
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003851static struct ibm_struct hotkey_driver_data = {
3852 .name = "hotkey",
3853 .read = hotkey_read,
3854 .write = hotkey_write,
3855 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03003856 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02003857 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003858 .acpi = &ibm_hotkey_acpidriver,
3859};
3860
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003861/*************************************************************************
3862 * Bluetooth subdriver
3863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003865enum {
3866 /* ACPI GBDC/SBDC bits */
3867 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3868 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003869 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003870 0 = disable, 1 = enable */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003871};
3872
3873enum {
3874 /* ACPI \BLTH commands */
3875 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3876 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3877 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3878 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3879 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003880};
3881
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003882#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3883
Johannes Berg19d337d2009-06-02 13:01:37 +02003884static int bluetooth_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003885{
3886 int status;
3887
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003888#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3889 if (dbg_bluetoothemul)
3890 return (tpacpi_bluetooth_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003891 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003892#endif
3893
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003894 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3895 return -EIO;
3896
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003897 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02003898 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003899}
3900
Johannes Berg19d337d2009-06-02 13:01:37 +02003901static int bluetooth_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003902{
3903 int status;
3904
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003905 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02003906 "will attempt to %s bluetooth\n",
3907 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003908
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003909#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3910 if (dbg_bluetoothemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003911 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003912 return 0;
3913 }
3914#endif
3915
Johannes Berg19d337d2009-06-02 13:01:37 +02003916 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03003917 status = TP_ACPI_BLUETOOTH_RADIOSSW
3918 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3919 else
3920 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02003921
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003922 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3923 return -EIO;
3924
3925 return 0;
3926}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003927
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003928/* sysfs bluetooth enable ---------------------------------------------- */
3929static ssize_t bluetooth_enable_show(struct device *dev,
3930 struct device_attribute *attr,
3931 char *buf)
3932{
Johannes Berg19d337d2009-06-02 13:01:37 +02003933 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3934 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003935}
3936
3937static ssize_t bluetooth_enable_store(struct device *dev,
3938 struct device_attribute *attr,
3939 const char *buf, size_t count)
3940{
Johannes Berg19d337d2009-06-02 13:01:37 +02003941 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3942 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003943}
3944
3945static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003946 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003947 bluetooth_enable_show, bluetooth_enable_store);
3948
3949/* --------------------------------------------------------------------- */
3950
3951static struct attribute *bluetooth_attributes[] = {
3952 &dev_attr_bluetooth_enable.attr,
3953 NULL
3954};
3955
3956static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003957 .attrs = bluetooth_attributes,
3958};
3959
Johannes Berg19d337d2009-06-02 13:01:37 +02003960static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3961 .get_status = bluetooth_get_status,
3962 .set_status = bluetooth_set_status,
3963};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003964
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003965static void bluetooth_shutdown(void)
3966{
3967 /* Order firmware to save current state to NVRAM */
3968 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3969 TP_ACPI_BLTH_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07003970 pr_notice("failed to save bluetooth state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003971 else
3972 vdbg_printk(TPACPI_DBG_RFKILL,
Paul Bolle1f013582011-10-06 22:57:56 +02003973 "bluetooth state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003974}
3975
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003976static void bluetooth_exit(void)
3977{
3978 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3979 &bluetooth_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02003980
3981 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3982
3983 bluetooth_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003984}
3985
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003986static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003988 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03003989 int status = 0;
3990
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00003991 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3992 "initializing bluetooth subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003993
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003994 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003995
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003996 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3997 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003998 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03003999 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004001 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4002 "bluetooth is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004003 str_supported(tp_features.bluetooth),
4004 status);
4005
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004006#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4007 if (dbg_bluetoothemul) {
4008 tp_features.bluetooth = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004009 pr_info("bluetooth switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004010 } else
4011#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004012 if (tp_features.bluetooth &&
4013 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4014 /* no bluetooth hardware present in system */
4015 tp_features.bluetooth = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004016 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004017 "bluetooth hardware not installed\n");
4018 }
4019
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004020 if (!tp_features.bluetooth)
4021 return 1;
4022
Johannes Berg19d337d2009-06-02 13:01:37 +02004023 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4024 &bluetooth_tprfk_ops,
4025 RFKILL_TYPE_BLUETOOTH,
4026 TPACPI_RFK_BLUETOOTH_SW_NAME,
4027 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004028 if (res)
4029 return res;
4030
Johannes Berg19d337d2009-06-02 13:01:37 +02004031 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4032 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004033 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004034 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004035 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004036 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004037
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004041/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004042static int bluetooth_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004044 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004047static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Johannes Berg19d337d2009-06-02 13:01:37 +02004049 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004052static struct ibm_struct bluetooth_driver_data = {
4053 .name = "bluetooth",
4054 .read = bluetooth_read,
4055 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004056 .exit = bluetooth_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004057 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004058};
4059
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004060/*************************************************************************
4061 * Wan subdriver
4062 */
4063
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004064enum {
4065 /* ACPI GWAN/SWAN bits */
4066 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4067 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02004068 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004069 0 = disable, 1 = enable */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004070};
4071
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004072#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4073
Johannes Berg19d337d2009-06-02 13:01:37 +02004074static int wan_get_status(void)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004075{
4076 int status;
4077
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004078#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4079 if (dbg_wwanemul)
4080 return (tpacpi_wwan_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004081 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004082#endif
4083
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004084 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4085 return -EIO;
4086
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004087 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004088 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004089}
4090
Johannes Berg19d337d2009-06-02 13:01:37 +02004091static int wan_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004092{
4093 int status;
4094
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004095 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004096 "will attempt to %s wwan\n",
4097 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004098
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004099#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4100 if (dbg_wwanemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004101 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004102 return 0;
4103 }
4104#endif
4105
Johannes Berg19d337d2009-06-02 13:01:37 +02004106 if (state == TPACPI_RFK_RADIO_ON)
Henrique de Moraes Holschuh08fedfc2010-02-25 22:22:07 -03004107 status = TP_ACPI_WANCARD_RADIOSSW
4108 | TP_ACPI_WANCARD_RESUMECTRL;
4109 else
4110 status = 0;
Johannes Berg19d337d2009-06-02 13:01:37 +02004111
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004112 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4113 return -EIO;
4114
4115 return 0;
4116}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004117
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004118/* sysfs wan enable ---------------------------------------------------- */
4119static ssize_t wan_enable_show(struct device *dev,
4120 struct device_attribute *attr,
4121 char *buf)
4122{
Johannes Berg19d337d2009-06-02 13:01:37 +02004123 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4124 attr, buf);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004125}
4126
4127static ssize_t wan_enable_store(struct device *dev,
4128 struct device_attribute *attr,
4129 const char *buf, size_t count)
4130{
Johannes Berg19d337d2009-06-02 13:01:37 +02004131 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4132 attr, buf, count);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004133}
4134
4135static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03004136 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004137 wan_enable_show, wan_enable_store);
4138
4139/* --------------------------------------------------------------------- */
4140
4141static struct attribute *wan_attributes[] = {
4142 &dev_attr_wan_enable.attr,
4143 NULL
4144};
4145
4146static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004147 .attrs = wan_attributes,
4148};
4149
Johannes Berg19d337d2009-06-02 13:01:37 +02004150static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4151 .get_status = wan_get_status,
4152 .set_status = wan_set_status,
4153};
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004154
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004155static void wan_shutdown(void)
4156{
4157 /* Order firmware to save current state to NVRAM */
4158 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4159 TP_ACPI_WGSV_SAVE_STATE))
Joe Perches0978e012011-04-04 10:06:25 -07004160 pr_notice("failed to save WWAN state to NVRAM\n");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004161 else
4162 vdbg_printk(TPACPI_DBG_RFKILL,
4163 "WWAN state saved to NVRAM\n");
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004164}
4165
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004166static void wan_exit(void)
4167{
4168 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4169 &wan_attr_group);
Johannes Berg19d337d2009-06-02 13:01:37 +02004170
4171 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4172
4173 wan_shutdown();
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03004174}
4175
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004176static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004177{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004178 int res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004179 int status = 0;
4180
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004181 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4182 "initializing wan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004183
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004184 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004185
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004186 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004187 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004188
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004189 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4190 "wan is %s, status 0x%02x\n",
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004191 str_supported(tp_features.wan),
4192 status);
4193
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004194#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4195 if (dbg_wwanemul) {
4196 tp_features.wan = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004197 pr_info("wwan switch emulation enabled\n");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02004198 } else
4199#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004200 if (tp_features.wan &&
4201 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4202 /* no wan hardware present in system */
4203 tp_features.wan = 0;
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004204 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03004205 "wan hardware not installed\n");
4206 }
4207
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004208 if (!tp_features.wan)
4209 return 1;
4210
Johannes Berg19d337d2009-06-02 13:01:37 +02004211 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4212 &wan_tprfk_ops,
4213 RFKILL_TYPE_WWAN,
4214 TPACPI_RFK_WWAN_SW_NAME,
4215 true);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004216 if (res)
4217 return res;
4218
Johannes Berg19d337d2009-06-02 13:01:37 +02004219 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4220 &wan_attr_group);
4221
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004222 if (res) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004223 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004224 return res;
Henrique de Moraes Holschuhd6fdd1e92007-04-21 11:08:40 -03004225 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004226
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03004227 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004228}
4229
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004230/* procfs -------------------------------------------------------------- */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004231static int wan_read(struct seq_file *m)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004232{
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004233 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004234}
4235
4236static int wan_write(char *buf)
4237{
Johannes Berg19d337d2009-06-02 13:01:37 +02004238 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04004239}
4240
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004241static struct ibm_struct wan_driver_data = {
4242 .name = "wan",
4243 .read = wan_read,
4244 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03004245 .exit = wan_exit,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02004246 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004247};
4248
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004249/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004250 * UWB subdriver
4251 */
4252
4253enum {
4254 /* ACPI GUWB/SUWB bits */
4255 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4256 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4257};
4258
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004259#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4260
Johannes Berg19d337d2009-06-02 13:01:37 +02004261static int uwb_get_status(void)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004262{
4263 int status;
4264
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004265#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4266 if (dbg_uwbemul)
4267 return (tpacpi_uwb_emulstate) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004268 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004269#endif
4270
4271 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4272 return -EIO;
4273
4274 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
Johannes Berg19d337d2009-06-02 13:01:37 +02004275 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004276}
4277
Johannes Berg19d337d2009-06-02 13:01:37 +02004278static int uwb_set_status(enum tpacpi_rfkill_state state)
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004279{
4280 int status;
4281
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004282 vdbg_printk(TPACPI_DBG_RFKILL,
Johannes Berg19d337d2009-06-02 13:01:37 +02004283 "will attempt to %s UWB\n",
4284 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004285
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004286#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4287 if (dbg_uwbemul) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004288 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004289 return 0;
4290 }
4291#endif
4292
Johannes Berg19d337d2009-06-02 13:01:37 +02004293 if (state == TPACPI_RFK_RADIO_ON)
4294 status = TP_ACPI_UWB_RADIOSSW;
4295 else
4296 status = 0;
4297
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004298 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4299 return -EIO;
4300
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004301 return 0;
4302}
4303
4304/* --------------------------------------------------------------------- */
4305
Johannes Berg19d337d2009-06-02 13:01:37 +02004306static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4307 .get_status = uwb_get_status,
4308 .set_status = uwb_set_status,
4309};
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004310
4311static void uwb_exit(void)
4312{
Johannes Berg19d337d2009-06-02 13:01:37 +02004313 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004314}
4315
4316static int __init uwb_init(struct ibm_init_struct *iibm)
4317{
4318 int res;
4319 int status = 0;
4320
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004321 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4322 "initializing uwb subdriver\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004323
4324 TPACPI_ACPIHANDLE_INIT(hkey);
4325
4326 tp_features.uwb = hkey_handle &&
4327 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4328
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004329 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4330 "uwb is %s, status 0x%02x\n",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004331 str_supported(tp_features.uwb),
4332 status);
4333
4334#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4335 if (dbg_uwbemul) {
4336 tp_features.uwb = 1;
Joe Perches0978e012011-04-04 10:06:25 -07004337 pr_info("uwb switch emulation enabled\n");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004338 } else
4339#endif
4340 if (tp_features.uwb &&
4341 !(status & TP_ACPI_UWB_HWPRESENT)) {
4342 /* no uwb hardware present in system */
4343 tp_features.uwb = 0;
4344 dbg_printk(TPACPI_DBG_INIT,
4345 "uwb hardware not installed\n");
4346 }
4347
4348 if (!tp_features.uwb)
4349 return 1;
4350
4351 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
Johannes Berg19d337d2009-06-02 13:01:37 +02004352 &uwb_tprfk_ops,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004353 RFKILL_TYPE_UWB,
Henrique de Moraes Holschuhbee4cd9b2009-04-04 04:25:50 +00004354 TPACPI_RFK_UWB_SW_NAME,
Johannes Berg19d337d2009-06-02 13:01:37 +02004355 false);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02004356 return res;
4357}
4358
4359static struct ibm_struct uwb_driver_data = {
4360 .name = "uwb",
4361 .exit = uwb_exit,
4362 .flags.experimental = 1,
4363};
4364
4365/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004366 * Video subdriver
4367 */
4368
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004369#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4370
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004371enum video_access_mode {
4372 TPACPI_VIDEO_NONE = 0,
4373 TPACPI_VIDEO_570, /* 570 */
4374 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4375 TPACPI_VIDEO_NEW, /* all others */
4376};
4377
4378enum { /* video status flags, based on VIDEO_570 */
4379 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4380 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4381 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4382};
4383
4384enum { /* TPACPI_VIDEO_570 constants */
4385 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4386 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4387 * video_status_flags */
4388 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4389 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4390};
4391
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004392static enum video_access_mode video_supported;
4393static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004394
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004395static int video_autosw_get(void);
4396static int video_autosw_set(int enable);
4397
Joe Perches112a6ee2011-04-04 10:06:24 -07004398TPACPI_HANDLE(vid, root,
4399 "\\_SB.PCI.AGP.VGA", /* 570 */
4400 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4401 "\\_SB.PCI0.VID0", /* 770e */
4402 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4403 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4404 "\\_SB.PCI0.AGP.VID", /* all others */
4405 ); /* R30, R31 */
4406
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004407TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004409static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004411 int ivga;
4412
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004413 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4414
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004415 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004416 if (tpacpi_is_ibm())
4417 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004418
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4420 /* G41, assume IVGA doesn't change */
4421 vid_handle = vid2_handle;
4422
4423 if (!vid_handle)
4424 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004425 video_supported = TPACPI_VIDEO_NONE;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004426 else if (tpacpi_is_ibm() &&
4427 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004429 video_supported = TPACPI_VIDEO_570;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004430 else if (tpacpi_is_ibm() &&
4431 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004432 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004433 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004434 else
4435 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004436 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004438 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4439 str_supported(video_supported != TPACPI_VIDEO_NONE),
4440 video_supported);
4441
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004442 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445static void video_exit(void)
4446{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004447 dbg_printk(TPACPI_DBG_EXIT,
4448 "restoring original video autoswitch mode\n");
4449 if (video_autosw_set(video_orig_autosw))
Joe Perches0978e012011-04-04 10:06:25 -07004450 pr_err("error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004451 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004452}
4453
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004454static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int status = 0;
4457 int i;
4458
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004459 switch (video_supported) {
4460 case TPACPI_VIDEO_570:
4461 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4462 TP_ACPI_VIDEO_570_PHSCMD))
4463 return -EIO;
4464 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4465 break;
4466 case TPACPI_VIDEO_770:
4467 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4468 return -EIO;
4469 if (i)
4470 status |= TP_ACPI_VIDEO_S_LCD;
4471 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_CRT;
4475 break;
4476 case TPACPI_VIDEO_NEW:
4477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4478 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4484 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_LCD;
4488 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_DVI;
4492 break;
4493 default:
4494 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 return status;
4498}
4499
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004500static int video_outputsw_set(int status)
4501{
4502 int autosw;
4503 int res = 0;
4504
4505 switch (video_supported) {
4506 case TPACPI_VIDEO_570:
4507 res = acpi_evalf(NULL, NULL,
4508 "\\_SB.PHS2", "vdd",
4509 TP_ACPI_VIDEO_570_PHS2CMD,
4510 status | TP_ACPI_VIDEO_570_PHS2SET);
4511 break;
4512 case TPACPI_VIDEO_770:
4513 autosw = video_autosw_get();
4514 if (autosw < 0)
4515 return autosw;
4516
4517 res = video_autosw_set(1);
4518 if (res)
4519 return res;
4520 res = acpi_evalf(vid_handle, NULL,
4521 "ASWT", "vdd", status * 0x100, 0);
4522 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004523 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004524 return -EIO;
4525 }
4526 break;
4527 case TPACPI_VIDEO_NEW:
4528 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004529 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004530 break;
4531 default:
4532 return -ENOSYS;
4533 }
4534
4535 return (res)? 0 : -EIO;
4536}
4537
4538static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004540 int autosw = 0;
4541
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004542 switch (video_supported) {
4543 case TPACPI_VIDEO_570:
4544 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4545 return -EIO;
4546 break;
4547 case TPACPI_VIDEO_770:
4548 case TPACPI_VIDEO_NEW:
4549 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4550 return -EIO;
4551 break;
4552 default:
4553 return -ENOSYS;
4554 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004555
4556 return autosw & 1;
4557}
4558
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004559static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004561 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004563 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004564}
4565
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004566static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004567{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004568 int autosw = video_autosw_get();
4569 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004571 if (autosw < 0)
4572 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004573
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004574 switch (video_supported) {
4575 case TPACPI_VIDEO_570:
4576 res = video_autosw_set(1);
4577 if (res)
4578 return res;
4579 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4580 break;
4581 case TPACPI_VIDEO_770:
4582 case TPACPI_VIDEO_NEW:
4583 res = video_autosw_set(1);
4584 if (res)
4585 return res;
4586 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4587 break;
4588 default:
4589 return -ENOSYS;
4590 }
4591 if (!autosw && video_autosw_set(autosw)) {
Joe Perches0978e012011-04-04 10:06:25 -07004592 pr_err("video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004593 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004594 }
4595
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004596 return (res)? 0 : -EIO;
4597}
4598
4599static int video_expand_toggle(void)
4600{
4601 switch (video_supported) {
4602 case TPACPI_VIDEO_570:
4603 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_770:
4606 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4607 0 : -EIO;
4608 case TPACPI_VIDEO_NEW:
4609 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4610 0 : -EIO;
4611 default:
4612 return -ENOSYS;
4613 }
4614 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004615}
4616
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004617static int video_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004618{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004619 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004620
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004621 if (video_supported == TPACPI_VIDEO_NONE) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004622 seq_printf(m, "status:\t\tnot supported\n");
4623 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624 }
4625
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004626 /* Even reads can crash X.org, so... */
4627 if (!capable(CAP_SYS_ADMIN))
4628 return -EPERM;
4629
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004630 status = video_outputsw_get();
4631 if (status < 0)
4632 return status;
4633
4634 autosw = video_autosw_get();
4635 if (autosw < 0)
4636 return autosw;
4637
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004638 seq_printf(m, "status:\t\tsupported\n");
4639 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4640 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004641 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004642 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4643 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4644 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4645 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004646 if (video_supported == TPACPI_VIDEO_NEW)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004647 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4648 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4649 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004650
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004651 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004652}
4653
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004654static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
4656 char *cmd;
4657 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004658 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004660 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004661 return -ENODEV;
4662
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03004663 /* Even reads can crash X.org, let alone writes... */
4664 if (!capable(CAP_SYS_ADMIN))
4665 return -EPERM;
4666
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004667 enable = 0;
4668 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
4670 while ((cmd = next_cmd(&buf))) {
4671 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004672 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004674 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004676 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004678 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004679 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004680 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004681 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004682 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004683 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004684 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004686 res = video_autosw_set(1);
4687 if (res)
4688 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004690 res = video_autosw_set(0);
4691 if (res)
4692 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004694 res = video_outputsw_cycle();
4695 if (res)
4696 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004698 res = video_expand_toggle();
4699 if (res)
4700 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 } else
4702 return -EINVAL;
4703 }
4704
4705 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03004706 status = video_outputsw_get();
4707 if (status < 0)
4708 return status;
4709 res = video_outputsw_set((status & ~disable) | enable);
4710 if (res)
4711 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
4713
4714 return 0;
4715}
4716
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004717static struct ibm_struct video_driver_data = {
4718 .name = "video",
4719 .read = video_read,
4720 .write = video_write,
4721 .exit = video_exit,
4722};
4723
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02004724#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004726/*************************************************************************
4727 * Light (thinklight) subdriver
4728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004730TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4731TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004732
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004733static int light_get_status(void)
4734{
4735 int status = 0;
4736
4737 if (tp_features.light_status) {
4738 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4739 return -EIO;
4740 return (!!status);
4741 }
4742
4743 return -ENXIO;
4744}
4745
4746static int light_set_status(int status)
4747{
4748 int rc;
4749
4750 if (tp_features.light) {
4751 if (cmos_handle) {
4752 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4753 (status)?
4754 TP_CMOS_THINKLIGHT_ON :
4755 TP_CMOS_THINKLIGHT_OFF);
4756 } else {
4757 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4758 (status)? 1 : 0);
4759 }
4760 return (rc)? 0 : -EIO;
4761 }
4762
4763 return -ENXIO;
4764}
4765
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004766static void light_set_status_worker(struct work_struct *work)
4767{
4768 struct tpacpi_led_classdev *data =
4769 container_of(work, struct tpacpi_led_classdev, work);
4770
4771 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004772 light_set_status((data->new_state != TPACPI_LED_OFF));
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004773}
4774
4775static void light_sysfs_set(struct led_classdev *led_cdev,
4776 enum led_brightness brightness)
4777{
4778 struct tpacpi_led_classdev *data =
4779 container_of(led_cdev,
4780 struct tpacpi_led_classdev,
4781 led_classdev);
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00004782 data->new_state = (brightness != LED_OFF) ?
4783 TPACPI_LED_ON : TPACPI_LED_OFF;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004784 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004785}
4786
4787static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4788{
4789 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4790}
4791
4792static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4793 .led_classdev = {
4794 .name = "tpacpi::thinklight",
4795 .brightness_set = &light_sysfs_set,
4796 .brightness_get = &light_sysfs_get,
4797 }
4798};
4799
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004800static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004802 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004803
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004804 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4805
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03004806 if (tpacpi_is_ibm()) {
4807 TPACPI_ACPIHANDLE_INIT(ledb);
4808 TPACPI_ACPIHANDLE_INIT(lght);
4809 }
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004810 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004811 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004812
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004813 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004814 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004815
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004816 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004817 /* light status not supported on
4818 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004819 tp_features.light_status =
4820 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004822 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4823 str_supported(tp_features.light),
4824 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004825
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004826 if (!tp_features.light)
4827 return 1;
4828
4829 rc = led_classdev_register(&tpacpi_pdev->dev,
4830 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004831
4832 if (rc < 0) {
4833 tp_features.light = 0;
4834 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004835 } else {
4836 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004837 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004838
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004839 return rc;
4840}
4841
4842static void light_exit(void)
4843{
4844 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
Tejun Heo6d394e12012-12-21 17:56:58 -08004845 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004848static int light_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004850 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004852 if (!tp_features.light) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004853 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004854 } else if (!tp_features.light_status) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004855 seq_printf(m, "status:\t\tunknown\n");
4856 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004857 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004858 status = light_get_status();
4859 if (status < 0)
4860 return status;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004861 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4862 seq_printf(m, "commands:\ton, off\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
4867
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004868static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004871 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004872
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004873 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004874 return -ENODEV;
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 while ((cmd = next_cmd(&buf))) {
4877 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004878 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004880 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 } else
4882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
4884
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004885 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004888static struct ibm_struct light_driver_data = {
4889 .name = "light",
4890 .read = light_read,
4891 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004892 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004893};
4894
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004895/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004896 * CMOS subdriver
4897 */
4898
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004899/* sysfs cmos_command -------------------------------------------------- */
4900static ssize_t cmos_command_store(struct device *dev,
4901 struct device_attribute *attr,
4902 const char *buf, size_t count)
4903{
4904 unsigned long cmos_cmd;
4905 int res;
4906
4907 if (parse_strtoul(buf, 21, &cmos_cmd))
4908 return -EINVAL;
4909
4910 res = issue_thinkpad_cmos_command(cmos_cmd);
4911 return (res)? res : count;
4912}
4913
4914static struct device_attribute dev_attr_cmos_command =
4915 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4916
4917/* --------------------------------------------------------------------- */
4918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004919static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004920{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004921 int res;
4922
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004923 vdbg_printk(TPACPI_DBG_INIT,
4924 "initializing cmos commands subdriver\n");
4925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004926 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004927
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004928 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4929 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004930
4931 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4932 if (res)
4933 return res;
4934
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004935 return (cmos_handle)? 0 : 1;
4936}
4937
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004938static void cmos_exit(void)
4939{
4940 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4941}
4942
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004943static int cmos_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004945 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4946 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 if (!cmos_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004948 seq_printf(m, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004950 seq_printf(m, "status:\t\tsupported\n");
4951 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
4953
Alexey Dobriyan887965e2009-12-15 21:51:12 -02004954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955}
4956
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004957static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
4959 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004960 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4964 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 /* cmos_cmd set */
4966 } else
4967 return -EINVAL;
4968
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004969 res = issue_thinkpad_cmos_command(cmos_cmd);
4970 if (res)
4971 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 }
4973
4974 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004975}
4976
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004977static struct ibm_struct cmos_driver_data = {
4978 .name = "cmos",
4979 .read = cmos_read,
4980 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004981 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004982};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004983
4984/*************************************************************************
4985 * LED subdriver
4986 */
4987
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004988enum led_access_mode {
4989 TPACPI_LED_NONE = 0,
4990 TPACPI_LED_570, /* 570 */
4991 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4992 TPACPI_LED_NEW, /* all others */
4993};
4994
4995enum { /* For TPACPI_LED_OLD */
4996 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4997 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4998 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4999};
5000
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02005001static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005002
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005003static acpi_handle led_handle;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005004
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005005#define TPACPI_LED_NUMLEDS 16
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005006static struct tpacpi_led_classdev *tpacpi_leds;
5007static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07005008static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005009 /* there's a limit of 19 chars + NULL before 2.6.26 */
5010 "tpacpi::power",
5011 "tpacpi:orange:batt",
5012 "tpacpi:green:batt",
5013 "tpacpi::dock_active",
5014 "tpacpi::bay_active",
5015 "tpacpi::dock_batt",
5016 "tpacpi::unknown_led",
5017 "tpacpi::standby",
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005018 "tpacpi::dock_status1",
5019 "tpacpi::dock_status2",
5020 "tpacpi::unknown_led2",
5021 "tpacpi::unknown_led3",
5022 "tpacpi::thinkvantage",
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005023};
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005024#define TPACPI_SAFE_LEDS 0x1081U
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005025
5026static inline bool tpacpi_is_led_restricted(const unsigned int led)
5027{
5028#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5029 return false;
5030#else
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005031 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005032#endif
5033}
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005034
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005035static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005036{
5037 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005038 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005039
5040 switch (led_supported) {
5041 case TPACPI_LED_570:
5042 if (!acpi_evalf(ec_handle,
5043 &status, "GLED", "dd", 1 << led))
5044 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005045 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005046 TPACPI_LED_OFF :
5047 ((status == 1)?
5048 TPACPI_LED_ON :
5049 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005050 tpacpi_led_state_cache[led] = led_s;
5051 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005052 default:
5053 return -ENXIO;
5054 }
5055
5056 /* not reached */
5057}
5058
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005059static int led_set_status(const unsigned int led,
5060 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005061{
5062 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005063 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5064 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005065
5066 int rc = 0;
5067
5068 switch (led_supported) {
5069 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005070 /* 570 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005071 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005072 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005073 if (unlikely(tpacpi_is_led_restricted(led)))
5074 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005075 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5076 (1 << led), led_sled_arg1[ledstatus]))
5077 rc = -EIO;
5078 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005079 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005080 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005081 if (unlikely(led > 7))
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005082 return -EINVAL;
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005083 if (unlikely(tpacpi_is_led_restricted(led)))
5084 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005085 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5086 if (rc >= 0)
5087 rc = ec_write(TPACPI_LED_EC_HLBL,
5088 (ledstatus == TPACPI_LED_BLINK) << led);
5089 if (rc >= 0)
5090 rc = ec_write(TPACPI_LED_EC_HLCL,
5091 (ledstatus != TPACPI_LED_OFF) << led);
5092 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005093 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005094 /* all others */
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005095 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5096 return -EINVAL;
5097 if (unlikely(tpacpi_is_led_restricted(led)))
5098 return -EPERM;
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03005099 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5100 led, led_led_arg1[ledstatus]))
5101 rc = -EIO;
5102 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005103 default:
5104 rc = -ENXIO;
5105 }
5106
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005107 if (!rc)
5108 tpacpi_led_state_cache[led] = ledstatus;
5109
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005110 return rc;
5111}
5112
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005113static void led_set_status_worker(struct work_struct *work)
5114{
5115 struct tpacpi_led_classdev *data =
5116 container_of(work, struct tpacpi_led_classdev, work);
5117
5118 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005119 led_set_status(data->led, data->new_state);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005120}
5121
5122static void led_sysfs_set(struct led_classdev *led_cdev,
5123 enum led_brightness brightness)
5124{
5125 struct tpacpi_led_classdev *data = container_of(led_cdev,
5126 struct tpacpi_led_classdev, led_classdev);
5127
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005128 if (brightness == LED_OFF)
5129 data->new_state = TPACPI_LED_OFF;
5130 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5131 data->new_state = TPACPI_LED_ON;
5132 else
5133 data->new_state = TPACPI_LED_BLINK;
5134
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005135 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005136}
5137
5138static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5139 unsigned long *delay_on, unsigned long *delay_off)
5140{
5141 struct tpacpi_led_classdev *data = container_of(led_cdev,
5142 struct tpacpi_led_classdev, led_classdev);
5143
5144 /* Can we choose the flash rate? */
5145 if (*delay_on == 0 && *delay_off == 0) {
5146 /* yes. set them to the hardware blink rate (1 Hz) */
5147 *delay_on = 500; /* ms */
5148 *delay_off = 500; /* ms */
5149 } else if ((*delay_on != 500) || (*delay_off != 500))
5150 return -EINVAL;
5151
Henrique de Moraes Holschuh75bd3bf2009-04-14 02:44:11 +00005152 data->new_state = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005153 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005154
5155 return 0;
5156}
5157
5158static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5159{
5160 int rc;
5161
5162 struct tpacpi_led_classdev *data = container_of(led_cdev,
5163 struct tpacpi_led_classdev, led_classdev);
5164
5165 rc = led_get_status(data->led);
5166
5167 if (rc == TPACPI_LED_OFF || rc < 0)
5168 rc = LED_OFF; /* no error handling in led class :( */
5169 else
5170 rc = LED_FULL;
5171
5172 return rc;
5173}
5174
5175static void led_exit(void)
5176{
5177 unsigned int i;
5178
5179 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5180 if (tpacpi_leds[i].led_classdev.name)
5181 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5182 }
5183
Li Dongyange03e3892012-07-25 10:45:07 +10005184 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005185 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005186}
5187
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005188static int __init tpacpi_init_led(unsigned int led)
5189{
5190 int rc;
5191
5192 tpacpi_leds[led].led = led;
5193
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005194 /* LEDs with no name don't get registered */
5195 if (!tpacpi_led_names[led])
5196 return 0;
5197
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005198 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5199 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5200 if (led_supported == TPACPI_LED_570)
5201 tpacpi_leds[led].led_classdev.brightness_get =
5202 &led_sysfs_get;
5203
5204 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5205
5206 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5207
5208 rc = led_classdev_register(&tpacpi_pdev->dev,
5209 &tpacpi_leds[led].led_classdev);
5210 if (rc < 0)
5211 tpacpi_leds[led].led_classdev.name = NULL;
5212
5213 return rc;
5214}
5215
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005216static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5217 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5218 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5219 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5220
5221 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5222 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5223 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5224 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5225 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5226 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5227 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5228 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5229
5230 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5231 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5232 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5233 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5234 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5235
5236 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5237 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5238 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5239 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5240
5241 /* (1) - may have excess leds enabled on MSB */
5242
5243 /* Defaults (order matters, keep last, don't reorder!) */
5244 { /* Lenovo */
5245 .vendor = PCI_VENDOR_ID_LENOVO,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x1fffU,
5248 },
5249 { /* IBM ThinkPads with no EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5252 .quirks = 0x00ffU,
5253 },
5254 { /* IBM ThinkPads with EC version string */
5255 .vendor = PCI_VENDOR_ID_IBM,
5256 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5257 .quirks = 0x00bfU,
5258 },
5259};
5260
5261#undef TPACPI_LEDQ_IBM
5262#undef TPACPI_LEDQ_LNV
5263
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005264static enum led_access_mode __init led_init_detect_mode(void)
5265{
5266 acpi_status status;
5267
5268 if (tpacpi_is_ibm()) {
5269 /* 570 */
5270 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5271 if (ACPI_SUCCESS(status))
5272 return TPACPI_LED_570;
5273
5274 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5275 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5276 if (ACPI_SUCCESS(status))
5277 return TPACPI_LED_OLD;
5278 }
5279
5280 /* most others */
5281 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5282 if (ACPI_SUCCESS(status))
5283 return TPACPI_LED_NEW;
5284
5285 /* R30, R31, and unknown firmwares */
5286 led_handle = NULL;
5287 return TPACPI_LED_NONE;
5288}
5289
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005290static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005291{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005292 unsigned int i;
5293 int rc;
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005294 unsigned long useful_leds;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005295
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005296 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5297
Henrique de Moraes Holschuh2cbb5c82010-05-16 19:45:50 -03005298 led_supported = led_init_detect_mode();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005299
Adam Leefcb44e12013-06-07 16:20:08 +08005300 if (led_supported != TPACPI_LED_NONE) {
5301 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5302 ARRAY_SIZE(led_useful_qtable));
5303
5304 if (!useful_leds) {
5305 led_handle = NULL;
5306 led_supported = TPACPI_LED_NONE;
5307 }
5308 }
5309
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005310 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5311 str_supported(led_supported), led_supported);
5312
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005313 if (led_supported == TPACPI_LED_NONE)
5314 return 1;
5315
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005316 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5317 GFP_KERNEL);
5318 if (!tpacpi_leds) {
Joe Perches0978e012011-04-04 10:06:25 -07005319 pr_err("Out of memory for LED data\n");
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005320 return -ENOMEM;
5321 }
5322
5323 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
Adam Leeedf2d772013-06-08 16:51:15 +08005324 tpacpi_leds[i].led = -1;
5325
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005326 if (!tpacpi_is_led_restricted(i) &&
5327 test_bit(i, &useful_leds)) {
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005328 rc = tpacpi_init_led(i);
5329 if (rc < 0) {
5330 led_exit();
5331 return rc;
5332 }
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005333 }
5334 }
5335
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005336#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Joe Perches0978e012011-04-04 10:06:25 -07005337 pr_notice("warning: userspace override of important "
5338 "firmware LEDs is enabled\n");
Henrique de Moraes Holschuha4d5eff2009-04-04 04:25:49 +00005339#endif
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005340 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005341}
5342
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005343#define str_led_status(s) \
5344 ((s) == TPACPI_LED_OFF ? "off" : \
5345 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005346
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005347static int led_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005349 if (!led_supported) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005350 seq_printf(m, "status:\t\tnot supported\n");
5351 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005352 }
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005353 seq_printf(m, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005354
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005355 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005356 /* 570 */
5357 int i, status;
5358 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005359 status = led_get_status(i);
5360 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005361 return -EIO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005362 seq_printf(m, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005363 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005364 }
5365 }
5366
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005367 seq_printf(m, "commands:\t"
Henrique de Moraes Holschuhf21179a2009-05-30 13:25:08 -03005368 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005373static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
5375 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005376 int led, rc;
5377 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005378
5379 if (!led_supported)
5380 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
5382 while ((cmd = next_cmd(&buf))) {
Adam Leeedf2d772013-06-08 16:51:15 +08005383 if (sscanf(cmd, "%d", &led) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 return -EINVAL;
5385
Adam Leeedf2d772013-06-08 16:51:15 +08005386 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5387 tpacpi_leds[led].led < 0)
5388 return -ENODEV;
5389
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005390 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005391 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005393 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005394 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005395 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005396 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005397 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005398 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03005399
5400 rc = led_set_status(led, s);
5401 if (rc < 0)
5402 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 }
5404
5405 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005406}
5407
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005408static struct ibm_struct led_driver_data = {
5409 .name = "led",
5410 .read = led_read,
5411 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03005412 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005413};
5414
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005415/*************************************************************************
5416 * Beep subdriver
5417 */
5418
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005419TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005420
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005421#define TPACPI_BEEP_Q1 0x0001
5422
5423static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5424 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5425 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5426};
5427
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005428static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005429{
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005430 unsigned long quirks;
5431
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005432 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5433
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005434 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005435
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005436 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5437 str_supported(beep_handle != NULL));
5438
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005439 quirks = tpacpi_check_quirks(beep_quirk_table,
5440 ARRAY_SIZE(beep_quirk_table));
5441
5442 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5443
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005444 return (beep_handle)? 0 : 1;
5445}
5446
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005447static int beep_read(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005449 if (!beep_handle)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005450 seq_printf(m, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005451 else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005452 seq_printf(m, "status:\t\tsupported\n");
5453 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457}
5458
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005459static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
5461 char *cmd;
5462 int beep_cmd;
5463
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005464 if (!beep_handle)
5465 return -ENODEV;
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005468 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5469 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 /* beep_cmd set */
5471 } else
5472 return -EINVAL;
Henrique de Moraes Holschuh60201732009-05-30 13:25:07 -03005473 if (tp_features.beep_needs_two_args) {
5474 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5475 beep_cmd, 0))
5476 return -EIO;
5477 } else {
5478 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5479 beep_cmd))
5480 return -EIO;
5481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 }
5483
5484 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005485}
5486
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005487static struct ibm_struct beep_driver_data = {
5488 .name = "beep",
5489 .read = beep_read,
5490 .write = beep_write,
5491};
5492
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005493/*************************************************************************
5494 * Thermal subdriver
5495 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005496
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005497enum thermal_access_mode {
5498 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5499 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5500 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5501 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5502 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5503};
5504
5505enum { /* TPACPI_THERMAL_TPEC_* */
5506 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5507 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5508 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005509
5510 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005511};
5512
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005513
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005514#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5515struct ibm_thermal_sensors_struct {
5516 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5517};
5518
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005519static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005520
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005521/* idx is zero-based */
5522static int thermal_get_sensor(int idx, s32 *value)
5523{
5524 int t;
5525 s8 tmp;
5526 char tmpi[5];
5527
5528 t = TP_EC_THERMAL_TMP0;
5529
5530 switch (thermal_read_mode) {
5531#if TPACPI_MAX_THERMAL_SENSORS >= 16
5532 case TPACPI_THERMAL_TPEC_16:
5533 if (idx >= 8 && idx <= 15) {
5534 t = TP_EC_THERMAL_TMP8;
5535 idx -= 8;
5536 }
5537 /* fallthrough */
5538#endif
5539 case TPACPI_THERMAL_TPEC_8:
5540 if (idx <= 7) {
5541 if (!acpi_ec_read(t + idx, &tmp))
5542 return -EIO;
5543 *value = tmp * 1000;
5544 return 0;
5545 }
5546 break;
5547
5548 case TPACPI_THERMAL_ACPI_UPDT:
5549 if (idx <= 7) {
5550 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5551 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5552 return -EIO;
5553 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5554 return -EIO;
5555 *value = (t - 2732) * 100;
5556 return 0;
5557 }
5558 break;
5559
5560 case TPACPI_THERMAL_ACPI_TMP07:
5561 if (idx <= 7) {
5562 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5563 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5564 return -EIO;
5565 if (t > 127 || t < -127)
5566 t = TP_EC_THERMAL_TMP_NA;
5567 *value = t * 1000;
5568 return 0;
5569 }
5570 break;
5571
5572 case TPACPI_THERMAL_NONE:
5573 default:
5574 return -ENOSYS;
5575 }
5576
5577 return -EINVAL;
5578}
5579
5580static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5581{
5582 int res, i;
5583 int n;
5584
5585 n = 8;
5586 i = 0;
5587
5588 if (!s)
5589 return -EINVAL;
5590
5591 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5592 n = 16;
5593
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005594 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005595 res = thermal_get_sensor(i, &s->temp[i]);
5596 if (res)
5597 return res;
5598 }
5599
5600 return n;
5601}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005602
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005603static void thermal_dump_all_sensors(void)
5604{
5605 int n, i;
5606 struct ibm_thermal_sensors_struct t;
5607
5608 n = thermal_get_sensors(&t);
5609 if (n <= 0)
5610 return;
5611
Joe Perches0978e012011-04-04 10:06:25 -07005612 pr_notice("temperatures (Celsius):");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005613
5614 for (i = 0; i < n; i++) {
5615 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
Joe Perches0978e012011-04-04 10:06:25 -07005616 pr_cont(" %d", (int)(t.temp[i] / 1000));
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005617 else
Joe Perches0978e012011-04-04 10:06:25 -07005618 pr_cont(" N/A");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005619 }
5620
Joe Perches0978e012011-04-04 10:06:25 -07005621 pr_cont("\n");
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005622}
5623
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005624/* sysfs temp##_input -------------------------------------------------- */
5625
5626static ssize_t thermal_temp_input_show(struct device *dev,
5627 struct device_attribute *attr,
5628 char *buf)
5629{
5630 struct sensor_device_attribute *sensor_attr =
5631 to_sensor_dev_attr(attr);
5632 int idx = sensor_attr->index;
5633 s32 value;
5634 int res;
5635
5636 res = thermal_get_sensor(idx, &value);
5637 if (res)
5638 return res;
Henrique de Moraes Holschuh9ebd9e82009-12-09 01:36:28 +00005639 if (value == TPACPI_THERMAL_SENSOR_NA)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005640 return -ENXIO;
5641
5642 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5643}
5644
5645#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005646 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5647 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005648
5649static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5650 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5651 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5652 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5653 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5654 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5655 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5656 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5657 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5658 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5659 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5660 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5661 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5662 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5663 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5664 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5665 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5666};
5667
5668#define THERMAL_ATTRS(X) \
5669 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5670
5671static struct attribute *thermal_temp_input_attr[] = {
5672 THERMAL_ATTRS(8),
5673 THERMAL_ATTRS(9),
5674 THERMAL_ATTRS(10),
5675 THERMAL_ATTRS(11),
5676 THERMAL_ATTRS(12),
5677 THERMAL_ATTRS(13),
5678 THERMAL_ATTRS(14),
5679 THERMAL_ATTRS(15),
5680 THERMAL_ATTRS(0),
5681 THERMAL_ATTRS(1),
5682 THERMAL_ATTRS(2),
5683 THERMAL_ATTRS(3),
5684 THERMAL_ATTRS(4),
5685 THERMAL_ATTRS(5),
5686 THERMAL_ATTRS(6),
5687 THERMAL_ATTRS(7),
5688 NULL
5689};
5690
5691static const struct attribute_group thermal_temp_input16_group = {
5692 .attrs = thermal_temp_input_attr
5693};
5694
5695static const struct attribute_group thermal_temp_input8_group = {
5696 .attrs = &thermal_temp_input_attr[8]
5697};
5698
5699#undef THERMAL_SENSOR_ATTR_TEMP
5700#undef THERMAL_ATTRS
5701
5702/* --------------------------------------------------------------------- */
5703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005704static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005705{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005706 u8 t, ta1, ta2;
5707 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005708 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005709 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005710
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005711 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5712
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005713 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005714
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005715 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005716 /*
5717 * Direct EC access mode: sensors at registers
5718 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5719 * non-implemented, thermal sensors return 0x80 when
5720 * not available
5721 */
5722
5723 ta1 = ta2 = 0;
5724 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005725 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005726 ta1 |= t;
5727 } else {
5728 ta1 = 0;
5729 break;
5730 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005731 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005732 ta2 |= t;
5733 } else {
5734 ta1 = 0;
5735 break;
5736 }
5737 }
5738 if (ta1 == 0) {
5739 /* This is sheer paranoia, but we handle it anyway */
5740 if (acpi_tmp7) {
Joe Perches0978e012011-04-04 10:06:25 -07005741 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005742 "falling back to ACPI TMPx access "
5743 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005744 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005745 } else {
Joe Perches0978e012011-04-04 10:06:25 -07005746 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005747 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005748 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005749 }
5750 } else {
5751 thermal_read_mode =
5752 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005753 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005754 }
5755 } else if (acpi_tmp7) {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005756 if (tpacpi_is_ibm() &&
5757 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005758 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005759 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005760 } else {
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03005761 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005762 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005763 }
5764 } else {
5765 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005766 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005767 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005768
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005769 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5770 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5771 thermal_read_mode);
5772
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005773 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005774 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005775 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005776 &thermal_temp_input16_group);
5777 if (res)
5778 return res;
5779 break;
5780 case TPACPI_THERMAL_TPEC_8:
5781 case TPACPI_THERMAL_ACPI_TMP07:
5782 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005783 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005784 &thermal_temp_input8_group);
5785 if (res)
5786 return res;
5787 break;
5788 case TPACPI_THERMAL_NONE:
5789 default:
5790 return 1;
5791 }
5792
5793 return 0;
5794}
5795
5796static void thermal_exit(void)
5797{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005798 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005799 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005800 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005801 &thermal_temp_input16_group);
5802 break;
5803 case TPACPI_THERMAL_TPEC_8:
5804 case TPACPI_THERMAL_ACPI_TMP07:
5805 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005806 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Roel Kluinf04d5e02010-02-02 14:37:58 -08005807 &thermal_temp_input8_group);
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005808 break;
5809 case TPACPI_THERMAL_NONE:
5810 default:
5811 break;
5812 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005813}
5814
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005815static int thermal_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005816{
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005817 int n, i;
5818 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005819
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005820 n = thermal_get_sensors(&t);
5821 if (unlikely(n < 0))
5822 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005823
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005824 seq_printf(m, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005825
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005826 if (n > 0) {
5827 for (i = 0; i < (n - 1); i++)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005828 seq_printf(m, "%d ", t.temp[i] / 1000);
5829 seq_printf(m, "%d\n", t.temp[i] / 1000);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005830 } else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005831 seq_printf(m, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005832
Alexey Dobriyan887965e2009-12-15 21:51:12 -02005833 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005834}
5835
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005836static struct ibm_struct thermal_driver_data = {
5837 .name = "thermal",
5838 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005839 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005840};
5841
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005842/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005843 * Backlight/brightness subdriver
5844 */
Holger Macht8acb0252006-10-20 14:30:28 -07005845
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005846#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5847
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005848/*
5849 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5850 * CMOS NVRAM byte 0x5E, bits 0-3.
5851 *
5852 * EC HBRV (0x31) has the following layout
5853 * Bit 7: unknown function
5854 * Bit 6: unknown function
5855 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5856 * Bit 4: must be set to zero to avoid problems
5857 * Bit 3-0: backlight brightness level
5858 *
5859 * brightness_get_raw returns status data in the HBRV layout
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03005860 *
5861 * WARNING: The X61 has been verified to use HBRV for something else, so
5862 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5863 * testing on the very early *60 Lenovo models...
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005864 */
5865
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005866enum {
5867 TP_EC_BACKLIGHT = 0x31,
5868
5869 /* TP_EC_BACKLIGHT bitmasks */
5870 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5871 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5872 TP_EC_BACKLIGHT_MAPSW = 0x20,
5873};
5874
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005875enum tpacpi_brightness_access_mode {
5876 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5877 TPACPI_BRGHT_MODE_EC, /* EC control */
5878 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5879 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5880 TPACPI_BRGHT_MODE_MAX
5881};
5882
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005883static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005884
5885static enum tpacpi_brightness_access_mode brightness_mode =
5886 TPACPI_BRGHT_MODE_MAX;
5887
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005888static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5889
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005890static struct mutex brightness_mutex;
5891
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005892/* NVRAM brightness access,
5893 * call with brightness_mutex held! */
5894static unsigned int tpacpi_brightness_nvram_get(void)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005895{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005896 u8 lnvram;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005897
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005898 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5899 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5900 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03005901 lnvram &= bright_maxlvl;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005902
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005903 return lnvram;
5904}
5905
5906static void tpacpi_brightness_checkpoint_nvram(void)
5907{
5908 u8 lec = 0;
5909 u8 b_nvram;
5910
5911 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5912 return;
5913
5914 vdbg_printk(TPACPI_DBG_BRGHT,
5915 "trying to checkpoint backlight level to NVRAM...\n");
5916
5917 if (mutex_lock_killable(&brightness_mutex) < 0)
5918 return;
5919
5920 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5921 goto unlock;
5922 lec &= TP_EC_BACKLIGHT_LVLMSK;
5923 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5924
5925 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5926 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5927 /* NVRAM needs update */
5928 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5929 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5930 b_nvram |= lec;
5931 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5932 dbg_printk(TPACPI_DBG_BRGHT,
5933 "updated NVRAM backlight level to %u (0x%02x)\n",
5934 (unsigned int) lec, (unsigned int) b_nvram);
5935 } else
5936 vdbg_printk(TPACPI_DBG_BRGHT,
5937 "NVRAM backlight level already is %u (0x%02x)\n",
5938 (unsigned int) lec, (unsigned int) b_nvram);
5939
5940unlock:
5941 mutex_unlock(&brightness_mutex);
5942}
5943
5944
5945/* call with brightness_mutex held! */
5946static int tpacpi_brightness_get_raw(int *status)
5947{
5948 u8 lec = 0;
5949
5950 switch (brightness_mode) {
5951 case TPACPI_BRGHT_MODE_UCMS_STEP:
5952 *status = tpacpi_brightness_nvram_get();
5953 return 0;
5954 case TPACPI_BRGHT_MODE_EC:
5955 case TPACPI_BRGHT_MODE_ECNVRAM:
5956 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005957 return -EIO;
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005958 *status = lec;
5959 return 0;
5960 default:
5961 return -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005962 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00005963}
5964
5965/* call with brightness_mutex held! */
5966/* do NOT call with illegal backlight level value */
5967static int tpacpi_brightness_set_ec(unsigned int value)
5968{
5969 u8 lec = 0;
5970
5971 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5972 return -EIO;
5973
5974 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5975 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5976 (value & TP_EC_BACKLIGHT_LVLMSK))))
5977 return -EIO;
5978
5979 return 0;
5980}
5981
5982/* call with brightness_mutex held! */
5983static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5984{
5985 int cmos_cmd, inc;
5986 unsigned int current_value, i;
5987
5988 current_value = tpacpi_brightness_nvram_get();
5989
5990 if (value == current_value)
5991 return 0;
5992
5993 cmos_cmd = (value > current_value) ?
5994 TP_CMOS_BRIGHTNESS_UP :
5995 TP_CMOS_BRIGHTNESS_DOWN;
5996 inc = (value > current_value) ? 1 : -1;
5997
5998 for (i = current_value; i != value; i += inc)
5999 if (issue_thinkpad_cmos_command(cmos_cmd))
6000 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006001
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006002 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006003}
6004
6005/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006006static int brightness_set(unsigned int value)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006007{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006008 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006009
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006010 if (value > bright_maxlvl || value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006011 return -EINVAL;
6012
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006013 vdbg_printk(TPACPI_DBG_BRGHT,
6014 "set backlight level to %d\n", value);
6015
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02006016 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006017 if (res < 0)
6018 return res;
6019
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006020 switch (brightness_mode) {
6021 case TPACPI_BRGHT_MODE_EC:
6022 case TPACPI_BRGHT_MODE_ECNVRAM:
6023 res = tpacpi_brightness_set_ec(value);
6024 break;
6025 case TPACPI_BRGHT_MODE_UCMS_STEP:
6026 res = tpacpi_brightness_set_ucmsstep(value);
6027 break;
6028 default:
6029 res = -ENXIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006030 }
6031
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006032 mutex_unlock(&brightness_mutex);
6033 return res;
6034}
6035
6036/* sysfs backlight class ----------------------------------------------- */
6037
6038static int brightness_update_status(struct backlight_device *bd)
6039{
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006040 unsigned int level =
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006041 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6042 bd->props.power == FB_BLANK_UNBLANK) ?
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006043 bd->props.brightness : 0;
6044
6045 dbg_printk(TPACPI_DBG_BRGHT,
6046 "backlight: attempt to set level to %d\n",
6047 level);
6048
6049 /* it is the backlight class's job (caller) to handle
6050 * EINTR and other errors properly */
6051 return brightness_set(level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006052}
Holger Macht8acb0252006-10-20 14:30:28 -07006053
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006054static int brightness_get(struct backlight_device *bd)
6055{
6056 int status, res;
6057
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006058 res = mutex_lock_killable(&brightness_mutex);
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006059 if (res < 0)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006060 return 0;
6061
6062 res = tpacpi_brightness_get_raw(&status);
6063
6064 mutex_unlock(&brightness_mutex);
6065
6066 if (res < 0)
6067 return 0;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03006068
6069 return status & TP_EC_BACKLIGHT_LVLMSK;
6070}
6071
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006072static void tpacpi_brightness_notify_change(void)
6073{
6074 backlight_force_update(ibm_backlight_device,
6075 BACKLIGHT_UPDATE_HOTKEY);
6076}
6077
Lionel Debrouxacc24722010-11-16 14:14:02 +01006078static const struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006079 .get_brightness = brightness_get,
6080 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006081};
6082
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006083/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006084
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006085/*
6086 * Call _BCL method of video device. On some ThinkPads this will
6087 * switch the firmware to the ACPI brightness control mode.
6088 */
6089
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006090static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6091{
6092 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6093 union acpi_object *obj;
6094 int rc;
6095
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006096 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006097 obj = (union acpi_object *)buffer.pointer;
6098 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Joe Perches0978e012011-04-04 10:06:25 -07006099 pr_err("Unknown _BCL data, please report this to %s\n",
6100 TPACPI_MAIL);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006101 rc = 0;
6102 } else {
6103 rc = obj->package.count;
6104 }
6105 } else {
6106 return 0;
6107 }
6108
6109 kfree(buffer.pointer);
6110 return rc;
6111}
6112
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006113
6114/*
6115 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6116 */
6117static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6118{
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006119 acpi_handle video_device;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006120 int bcl_levels = 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006121
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006122 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6123 if (video_device)
6124 bcl_levels = tpacpi_query_bcl_levels(video_device);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006125
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006126 tp_features.bright_acpimode = (bcl_levels > 0);
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006127
Henrique de Moraes Holschuh122f2672010-08-09 23:48:18 -03006128 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006129}
6130
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006131/*
6132 * These are only useful for models that have only one possibility
6133 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6134 * these quirks.
6135 */
6136#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6137#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6138#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6139
6140static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6141 /* Models with ATI GPUs known to require ECNVRAM mode */
6142 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6143
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006144 /* Models with ATI GPUs that can use ECNVRAM */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006145 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006146 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006147 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006148 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6149
Henrique de Moraes Holschuh6da25bf2009-09-12 15:22:11 -03006150 /* Models with Intel Extreme Graphics 2 */
Henrique de Moraes Holschuh7d1894d2010-02-25 21:28:56 -03006151 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
Henrique de Moraes Holschuha9f8eac2009-12-09 01:36:21 +00006152 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6153 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006154
6155 /* Models with Intel GMA900 */
6156 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6157 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6158 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6159};
6160
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006161/*
6162 * Returns < 0 for error, otherwise sets tp_features.bright_*
6163 * and bright_maxlvl.
6164 */
6165static void __init tpacpi_detect_brightness_capabilities(void)
6166{
6167 unsigned int b;
6168
6169 vdbg_printk(TPACPI_DBG_INIT,
6170 "detecting firmware brightness interface capabilities\n");
6171
6172 /* we could run a quirks check here (same table used by
6173 * brightness_init) if needed */
6174
6175 /*
6176 * We always attempt to detect acpi support, so as to switch
6177 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6178 * going to publish a backlight interface
6179 */
6180 b = tpacpi_check_std_acpi_brightness_support();
6181 switch (b) {
6182 case 16:
6183 bright_maxlvl = 15;
Joe Perches0978e012011-04-04 10:06:25 -07006184 pr_info("detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006185 break;
6186 case 8:
6187 case 0:
6188 bright_maxlvl = 7;
Joe Perches0978e012011-04-04 10:06:25 -07006189 pr_info("detected a 8-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006190 break;
6191 default:
Joe Perches0978e012011-04-04 10:06:25 -07006192 pr_err("Unsupported brightness interface, "
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006193 "please contact %s\n", TPACPI_MAIL);
6194 tp_features.bright_unkfw = 1;
6195 bright_maxlvl = b - 1;
6196 }
6197}
6198
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006199static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006200{
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006201 struct backlight_properties props;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006202 int b;
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006203 unsigned long quirks;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006204
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006205 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6206
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03006207 mutex_init(&brightness_mutex);
6208
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006209 quirks = tpacpi_check_quirks(brightness_quirk_table,
6210 ARRAY_SIZE(brightness_quirk_table));
6211
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006212 /* tpacpi_detect_brightness_capabilities() must have run already */
Thomas Renninger2dba1b52008-08-01 17:38:03 +02006213
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006214 /* if it is unknown, we don't handle it: it wouldn't be safe */
6215 if (tp_features.bright_unkfw)
6216 return 1;
6217
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006218 if (!brightness_enable) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006219 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03006220 "brightness support disabled by "
6221 "module parameter\n");
6222 return 1;
6223 }
6224
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006225 if (acpi_video_backlight_support()) {
6226 if (brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006227 pr_info("Standard ACPI backlight interface "
6228 "available, not loading native one\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006229 return 1;
6230 } else if (brightness_enable == 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006231 pr_warn("Cannot enable backlight brightness support, "
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006232 "ACPI is already handling it. Refer to the "
Joe Perches0978e012011-04-04 10:06:25 -07006233 "acpi_backlight kernel parameter.\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006234 return 1;
6235 }
6236 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Joe Perches0978e012011-04-04 10:06:25 -07006237 pr_notice("Standard ACPI backlight interface not "
6238 "available, thinkpad_acpi native "
6239 "brightness control enabled\n");
Henrique de Moraes Holschuh217f0962010-08-09 23:48:19 -03006240 }
6241
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006242 /*
6243 * Check for module parameter bogosity, note that we
6244 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6245 * able to detect "unspecified"
6246 */
6247 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006248 return -EINVAL;
6249
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006250 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6251 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6252 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006253 if (quirks & TPACPI_BRGHT_Q_EC)
6254 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6255 else
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006256 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6257
6258 dbg_printk(TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006259 "driver auto-selected brightness_mode=%d\n",
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006260 brightness_mode);
6261 }
6262
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006263 /* Safety */
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03006264 if (!tpacpi_is_ibm() &&
Henrique de Moraes Holschuhd7880f12009-06-18 00:40:16 -03006265 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6266 brightness_mode == TPACPI_BRGHT_MODE_EC))
6267 return -EINVAL;
6268
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006269 if (tpacpi_brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006270 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006271
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006272 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07006273 props.type = BACKLIGHT_PLATFORM;
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006274 props.max_brightness = bright_maxlvl;
6275 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05006276 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6277 NULL, NULL,
6278 &ibm_backlight_data,
6279 &props);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006280 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006281 int rc = PTR_ERR(ibm_backlight_device);
6282 ibm_backlight_device = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07006283 pr_err("Could not register backlight device\n");
Henrique de Moraes Holschuh435c47e2009-09-20 14:09:22 -03006284 return rc;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006285 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006286 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6287 "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006288
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006289 if (quirks & TPACPI_BRGHT_Q_ASK) {
Joe Perches0978e012011-04-04 10:06:25 -07006290 pr_notice("brightness: will use unverified default: "
6291 "brightness_mode=%d\n", brightness_mode);
6292 pr_notice("brightness: please report to %s whether it works well "
6293 "or not on your ThinkPad\n", TPACPI_MAIL);
Henrique de Moraes Holschuh59fe4fe2009-08-01 12:04:20 -03006294 }
6295
Henrique de Moraes Holschuh7d9745c2010-05-16 19:45:57 -03006296 /* Added by mistake in early 2007. Probably useless, but it could
6297 * be working around some unknown firmware problem where the value
6298 * read at startup doesn't match the real hardware state... so leave
6299 * it in place just in case */
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02006300 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00006301
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006302 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6303 "brightness: registering brightness hotkeys "
6304 "as change notification\n");
6305 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6306 | TP_ACPI_HKEY_BRGHTUP_MASK
Joe Perches30980642010-11-14 19:04:38 -08006307 | TP_ACPI_HKEY_BRGHTDWN_MASK);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006308 return 0;
6309}
6310
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006311static void brightness_suspend(void)
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006312{
6313 tpacpi_brightness_checkpoint_nvram();
6314}
6315
6316static void brightness_shutdown(void)
6317{
6318 tpacpi_brightness_checkpoint_nvram();
6319}
6320
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006321static void brightness_exit(void)
6322{
6323 if (ibm_backlight_device) {
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006324 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006325 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006326 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006327 }
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006328
6329 tpacpi_brightness_checkpoint_nvram();
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02006330}
6331
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006332static int brightness_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006334 int level;
6335
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006336 level = brightness_get(NULL);
6337 if (level < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006338 seq_printf(m, "level:\t\tunreadable\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339 } else {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006340 seq_printf(m, "level:\t\t%d\n", level);
6341 seq_printf(m, "commands:\tup, down\n");
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006342 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6343 bright_maxlvl);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006344 }
6345
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006346 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006347}
6348
6349static int brightness_write(char *buf)
6350{
6351 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006352 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006353 char *cmd;
6354
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006355 level = brightness_get(NULL);
6356 if (level < 0)
6357 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006358
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006359 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006361 if (level < bright_maxlvl)
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006362 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006363 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006364 if (level > 0)
6365 level--;
6366 } else if (sscanf(cmd, "level %d", &level) == 1 &&
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03006367 level >= 0 && level <= bright_maxlvl) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006368 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006369 } else
6370 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006371 }
6372
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006373 tpacpi_disclose_usertask("procfs brightness",
6374 "set level to %d\n", level);
6375
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02006376 /*
6377 * Now we know what the final level should be, so we try to set it.
6378 * Doing it this way makes the syscall restartable in case of EINTR
6379 */
6380 rc = brightness_set(level);
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00006381 if (!rc && ibm_backlight_device)
6382 backlight_force_update(ibm_backlight_device,
6383 BACKLIGHT_UPDATE_SYSFS);
Roel Kluin80a8d122009-11-20 19:48:23 +01006384 return (rc == -EINTR)? -ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006385}
6386
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006387static struct ibm_struct brightness_driver_data = {
6388 .name = "brightness",
6389 .read = brightness_read,
6390 .write = brightness_write,
6391 .exit = brightness_exit,
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00006392 .suspend = brightness_suspend,
6393 .shutdown = brightness_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006394};
6395
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006396/*************************************************************************
6397 * Volume subdriver
6398 */
6399
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006400/*
6401 * IBM ThinkPads have a simple volume controller with MUTE gating.
6402 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6403 *
6404 * Since the *61 series (and probably also the later *60 series), Lenovo
6405 * ThinkPads only implement the MUTE gate.
6406 *
6407 * EC register 0x30
6408 * Bit 6: MUTE (1 mutes sound)
6409 * Bit 3-0: Volume
6410 * Other bits should be zero as far as we know.
6411 *
6412 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6413 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6414 * such as bit 7 which is used to detect repeated presses of MUTE,
6415 * and we leave them unchanged.
6416 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006417
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02006418#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6419
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006420#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6421#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6422#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6423
Henrique de Moraes Holschuhead510c2009-12-26 22:52:13 -02006424static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006425static char *alsa_id = "ThinkPadEC";
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306426static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006427
6428struct tpacpi_alsa_data {
6429 struct snd_card *card;
6430 struct snd_ctl_elem_id *ctl_mute_id;
6431 struct snd_ctl_elem_id *ctl_vol_id;
6432};
6433
6434static struct snd_card *alsa_card;
6435
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006436enum {
6437 TP_EC_AUDIO = 0x30,
6438
6439 /* TP_EC_AUDIO bits */
6440 TP_EC_AUDIO_MUTESW = 6,
6441
6442 /* TP_EC_AUDIO bitmasks */
6443 TP_EC_AUDIO_LVL_MSK = 0x0F,
6444 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6445
6446 /* Maximum volume */
6447 TP_EC_VOLUME_MAX = 14,
6448};
6449
6450enum tpacpi_volume_access_mode {
6451 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6452 TPACPI_VOL_MODE_EC, /* Pure EC control */
6453 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6454 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6455 TPACPI_VOL_MODE_MAX
6456};
6457
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006458enum tpacpi_volume_capabilities {
6459 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6460 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6461 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6462 TPACPI_VOL_CAP_MAX
6463};
6464
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006465static enum tpacpi_volume_access_mode volume_mode =
6466 TPACPI_VOL_MODE_MAX;
6467
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006468static enum tpacpi_volume_capabilities volume_capabilities;
Rusty Russell90ab5ee2012-01-13 09:32:20 +10306469static bool volume_control_allowed;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006470
6471/*
6472 * Used to syncronize writers to TP_EC_AUDIO and
6473 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6474 */
6475static struct mutex volume_mutex;
6476
6477static void tpacpi_volume_checkpoint_nvram(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006478{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006479 u8 lec = 0;
6480 u8 b_nvram;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006481 u8 ec_mask;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006482
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006483 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6484 return;
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006485 if (!volume_control_allowed)
6486 return;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006487
6488 vdbg_printk(TPACPI_DBG_MIXER,
6489 "trying to checkpoint mixer state to NVRAM...\n");
6490
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006491 if (tp_features.mixer_no_level_control)
6492 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6493 else
6494 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6495
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006496 if (mutex_lock_killable(&volume_mutex) < 0)
6497 return;
6498
6499 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6500 goto unlock;
6501 lec &= ec_mask;
6502 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6503
6504 if (lec != (b_nvram & ec_mask)) {
6505 /* NVRAM needs update */
6506 b_nvram &= ~ec_mask;
6507 b_nvram |= lec;
6508 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6509 dbg_printk(TPACPI_DBG_MIXER,
6510 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6511 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006512 } else {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006513 vdbg_printk(TPACPI_DBG_MIXER,
6514 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6515 (unsigned int) lec, (unsigned int) b_nvram);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006516 }
6517
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006518unlock:
6519 mutex_unlock(&volume_mutex);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006520}
6521
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006522static int volume_get_status_ec(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006523{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006524 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006525
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006526 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6527 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006528
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006529 *status = s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006530
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006531 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006532
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006533 return 0;
6534}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006535
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006536static int volume_get_status(u8 *status)
6537{
6538 return volume_get_status_ec(status);
6539}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006540
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006541static int volume_set_status_ec(const u8 status)
6542{
6543 if (!acpi_ec_write(TP_EC_AUDIO, status))
6544 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006545
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006546 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6547
6548 return 0;
6549}
6550
6551static int volume_set_status(const u8 status)
6552{
6553 return volume_set_status_ec(status);
6554}
6555
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006556/* returns < 0 on error, 0 on no change, 1 on change */
6557static int __volume_set_mute_ec(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006558{
6559 int rc;
6560 u8 s, n;
6561
6562 if (mutex_lock_killable(&volume_mutex) < 0)
6563 return -EINTR;
6564
6565 rc = volume_get_status_ec(&s);
6566 if (rc)
6567 goto unlock;
6568
6569 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6570 s & ~TP_EC_AUDIO_MUTESW_MSK;
6571
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006572 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006573 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006574 if (!rc)
6575 rc = 1;
6576 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006577
6578unlock:
6579 mutex_unlock(&volume_mutex);
6580 return rc;
6581}
6582
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006583static int volume_alsa_set_mute(const bool mute)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006584{
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006585 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006586 (mute) ? "" : "un");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006587 return __volume_set_mute_ec(mute);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006588}
6589
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006590static int volume_set_mute(const bool mute)
6591{
6592 int rc;
6593
6594 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6595 (mute) ? "" : "un");
6596
6597 rc = __volume_set_mute_ec(mute);
6598 return (rc < 0) ? rc : 0;
6599}
6600
6601/* returns < 0 on error, 0 on no change, 1 on change */
6602static int __volume_set_volume_ec(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006603{
6604 int rc;
6605 u8 s, n;
6606
6607 if (vol > TP_EC_VOLUME_MAX)
6608 return -EINVAL;
6609
6610 if (mutex_lock_killable(&volume_mutex) < 0)
6611 return -EINTR;
6612
6613 rc = volume_get_status_ec(&s);
6614 if (rc)
6615 goto unlock;
6616
6617 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6618
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006619 if (n != s) {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006620 rc = volume_set_status_ec(n);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006621 if (!rc)
6622 rc = 1;
6623 }
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006624
6625unlock:
6626 mutex_unlock(&volume_mutex);
6627 return rc;
6628}
6629
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006630static int volume_alsa_set_volume(const u8 vol)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006631{
6632 dbg_printk(TPACPI_DBG_MIXER,
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006633 "ALSA: trying to set volume level to %hu\n", vol);
6634 return __volume_set_volume_ec(vol);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006635}
6636
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006637static void volume_alsa_notify_change(void)
6638{
6639 struct tpacpi_alsa_data *d;
6640
6641 if (alsa_card && alsa_card->private_data) {
6642 d = alsa_card->private_data;
6643 if (d->ctl_mute_id)
6644 snd_ctl_notify(alsa_card,
6645 SNDRV_CTL_EVENT_MASK_VALUE,
6646 d->ctl_mute_id);
6647 if (d->ctl_vol_id)
6648 snd_ctl_notify(alsa_card,
6649 SNDRV_CTL_EVENT_MASK_VALUE,
6650 d->ctl_vol_id);
6651 }
6652}
6653
6654static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6655 struct snd_ctl_elem_info *uinfo)
6656{
6657 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6658 uinfo->count = 1;
6659 uinfo->value.integer.min = 0;
6660 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6661 return 0;
6662}
6663
6664static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6665 struct snd_ctl_elem_value *ucontrol)
6666{
6667 u8 s;
6668 int rc;
6669
6670 rc = volume_get_status(&s);
6671 if (rc < 0)
6672 return rc;
6673
6674 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6675 return 0;
6676}
6677
6678static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6679 struct snd_ctl_elem_value *ucontrol)
6680{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006681 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6682 ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006683 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006684}
6685
6686#define volume_alsa_mute_info snd_ctl_boolean_mono_info
6687
6688static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6689 struct snd_ctl_elem_value *ucontrol)
6690{
6691 u8 s;
6692 int rc;
6693
6694 rc = volume_get_status(&s);
6695 if (rc < 0)
6696 return rc;
6697
6698 ucontrol->value.integer.value[0] =
6699 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6700 return 0;
6701}
6702
6703static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6704 struct snd_ctl_elem_value *ucontrol)
6705{
Henrique de Moraes Holschuh38e11cd2010-05-16 19:45:40 -03006706 tpacpi_disclose_usertask("ALSA", "%smute\n",
6707 ucontrol->value.integer.value[0] ?
6708 "un" : "");
Henrique de Moraes Holschuh88cc8372010-02-27 18:45:29 -03006709 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006710}
6711
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006712static struct snd_kcontrol_new volume_alsa_control_vol = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006713 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6714 .name = "Console Playback Volume",
6715 .index = 0,
6716 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6717 .info = volume_alsa_vol_info,
6718 .get = volume_alsa_vol_get,
6719};
6720
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08006721static struct snd_kcontrol_new volume_alsa_control_mute = {
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006722 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6723 .name = "Console Playback Switch",
6724 .index = 0,
6725 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6726 .info = volume_alsa_mute_info,
6727 .get = volume_alsa_mute_get,
6728};
6729
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02006730static void volume_suspend(void)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006731{
6732 tpacpi_volume_checkpoint_nvram();
6733}
6734
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006735static void volume_resume(void)
6736{
6737 volume_alsa_notify_change();
6738}
6739
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006740static void volume_shutdown(void)
6741{
6742 tpacpi_volume_checkpoint_nvram();
6743}
6744
6745static void volume_exit(void)
6746{
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006747 if (alsa_card) {
6748 snd_card_free(alsa_card);
6749 alsa_card = NULL;
6750 }
6751
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006752 tpacpi_volume_checkpoint_nvram();
6753}
6754
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006755static int __init volume_create_alsa_mixer(void)
6756{
6757 struct snd_card *card;
6758 struct tpacpi_alsa_data *data;
6759 struct snd_kcontrol *ctl_vol;
6760 struct snd_kcontrol *ctl_mute;
6761 int rc;
6762
6763 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6764 sizeof(struct tpacpi_alsa_data), &card);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006765 if (rc < 0 || !card) {
Joe Perches0978e012011-04-04 10:06:25 -07006766 pr_err("Failed to create ALSA card structures: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006767 return 1;
6768 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006769
6770 BUG_ON(!card->private_data);
6771 data = card->private_data;
6772 data->card = card;
6773
6774 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6775 sizeof(card->driver));
6776 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6777 sizeof(card->shortname));
6778 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6779 (thinkpad_id.ec_version_str) ?
6780 thinkpad_id.ec_version_str : "(unknown)");
6781 snprintf(card->longname, sizeof(card->longname),
6782 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6783 (thinkpad_id.ec_version_str) ?
6784 thinkpad_id.ec_version_str : "unknown");
6785
6786 if (volume_control_allowed) {
6787 volume_alsa_control_vol.put = volume_alsa_vol_put;
6788 volume_alsa_control_vol.access =
6789 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6790
6791 volume_alsa_control_mute.put = volume_alsa_mute_put;
6792 volume_alsa_control_mute.access =
6793 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6794 }
6795
6796 if (!tp_features.mixer_no_level_control) {
6797 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6798 rc = snd_ctl_add(card, ctl_vol);
6799 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006800 pr_err("Failed to create ALSA volume control: %d\n",
6801 rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006802 goto err_exit;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006803 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006804 data->ctl_vol_id = &ctl_vol->id;
6805 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006806
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006807 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6808 rc = snd_ctl_add(card, ctl_mute);
6809 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006810 pr_err("Failed to create ALSA mute control: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006811 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006812 }
6813 data->ctl_mute_id = &ctl_mute->id;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006814
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006815 snd_card_set_dev(card, &tpacpi_pdev->dev);
6816 rc = snd_card_register(card);
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006817 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07006818 pr_err("Failed to register ALSA card: %d\n", rc);
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006819 goto err_exit;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006820 }
6821
6822 alsa_card = card;
Henrique de Moraes Holschuh74c75c12009-12-26 22:52:14 -02006823 return 0;
6824
6825err_exit:
6826 snd_card_free(card);
6827 return 1;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006828}
6829
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006830#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6831#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6832
6833static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6834 /* Whitelist volume level on all IBM by default */
6835 { .vendor = PCI_VENDOR_ID_IBM,
6836 .bios = TPACPI_MATCH_ANY,
6837 .ec = TPACPI_MATCH_ANY,
6838 .quirks = TPACPI_VOL_Q_LEVEL },
6839
6840 /* Lenovo models with volume control (needs confirmation) */
6841 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6842 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6843 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6844 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6845 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6846 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6847 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6848
6849 /* Whitelist mute-only on all Lenovo by default */
6850 { .vendor = PCI_VENDOR_ID_LENOVO,
6851 .bios = TPACPI_MATCH_ANY,
6852 .ec = TPACPI_MATCH_ANY,
6853 .quirks = TPACPI_VOL_Q_MUTEONLY }
6854};
6855
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006856static int __init volume_init(struct ibm_init_struct *iibm)
6857{
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006858 unsigned long quirks;
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006859 int rc;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006860
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006861 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6862
6863 mutex_init(&volume_mutex);
6864
6865 /*
6866 * Check for module parameter bogosity, note that we
6867 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6868 * able to detect "unspecified"
6869 */
6870 if (volume_mode > TPACPI_VOL_MODE_MAX)
6871 return -EINVAL;
6872
6873 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Joe Perches0978e012011-04-04 10:06:25 -07006874 pr_err("UCMS step volume mode not implemented, "
6875 "please contact %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006876 return 1;
6877 }
6878
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006879 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6880 return -EINVAL;
6881
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006882 /*
6883 * The ALSA mixer is our primary interface.
6884 * When disabled, don't install the subdriver at all
6885 */
6886 if (!alsa_enable) {
6887 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6888 "ALSA mixer disabled by parameter, "
6889 "not loading volume subdriver...\n");
6890 return 1;
6891 }
6892
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006893 quirks = tpacpi_check_quirks(volume_quirk_table,
6894 ARRAY_SIZE(volume_quirk_table));
6895
6896 switch (volume_capabilities) {
6897 case TPACPI_VOL_CAP_AUTO:
6898 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6899 tp_features.mixer_no_level_control = 1;
6900 else if (quirks & TPACPI_VOL_Q_LEVEL)
6901 tp_features.mixer_no_level_control = 0;
6902 else
6903 return 1; /* no mixer */
6904 break;
6905 case TPACPI_VOL_CAP_VOLMUTE:
6906 tp_features.mixer_no_level_control = 0;
6907 break;
6908 case TPACPI_VOL_CAP_MUTEONLY:
6909 tp_features.mixer_no_level_control = 1;
6910 break;
6911 default:
6912 return 1;
6913 }
6914
6915 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6916 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6917 "using user-supplied volume_capabilities=%d\n",
6918 volume_capabilities);
6919
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006920 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6921 volume_mode == TPACPI_VOL_MODE_MAX) {
6922 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6923
6924 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6925 "driver auto-selected volume_mode=%d\n",
6926 volume_mode);
6927 } else {
6928 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6929 "using user-supplied volume_mode=%d\n",
6930 volume_mode);
6931 }
6932
6933 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006934 "mute is supported, volume control is %s\n",
6935 str_supported(!tp_features.mixer_no_level_control));
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006936
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006937 rc = volume_create_alsa_mixer();
6938 if (rc) {
Joe Perches0978e012011-04-04 10:06:25 -07006939 pr_err("Could not create the ALSA mixer interface\n");
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006940 return rc;
6941 }
6942
Joe Perches0978e012011-04-04 10:06:25 -07006943 pr_info("Console audio control enabled, mode: %s\n",
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006944 (volume_control_allowed) ?
6945 "override (read/write)" :
6946 "monitor (read only)");
6947
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02006948 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6949 "registering volume hotkeys as change notification\n");
6950 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6951 | TP_ACPI_HKEY_VOLUP_MASK
6952 | TP_ACPI_HKEY_VOLDWN_MASK
6953 | TP_ACPI_HKEY_MUTE_MASK);
6954
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006955 return 0;
6956}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006957
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006958static int volume_read(struct seq_file *m)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006959{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006960 u8 status;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006961
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006962 if (volume_get_status(&status) < 0) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006963 seq_printf(m, "level:\t\tunreadable\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006964 } else {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006965 if (tp_features.mixer_no_level_control)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006966 seq_printf(m, "level:\t\tunsupported\n");
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006967 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006968 seq_printf(m, "level:\t\t%d\n",
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006969 status & TP_EC_AUDIO_LVL_MSK);
6970
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006971 seq_printf(m, "mute:\t\t%s\n",
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006972 onoff(status, TP_EC_AUDIO_MUTESW));
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02006973
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006974 if (volume_control_allowed) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006975 seq_printf(m, "commands:\tunmute, mute\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006976 if (!tp_features.mixer_no_level_control) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006977 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006978 "commands:\tup, down\n");
Alexey Dobriyan887965e2009-12-15 21:51:12 -02006979 seq_printf(m,
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006980 "commands:\tlevel <level>"
6981 " (<level> is 0-%d)\n",
6982 TP_EC_VOLUME_MAX);
6983 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006984 }
6985 }
6986
6987 return 0;
6988}
6989
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006990static int volume_write(char *buf)
6991{
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006992 u8 s;
6993 u8 new_level, new_mute;
6994 int l;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006995 char *cmd;
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02006996 int rc;
6997
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02006998 /*
6999 * We do allow volume control at driver startup, so that the
7000 * user can set initial state through the volume=... parameter hack.
7001 */
7002 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7003 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7004 tp_warned.volume_ctrl_forbidden = 1;
Joe Perches0978e012011-04-04 10:06:25 -07007005 pr_notice("Console audio control in monitor mode, "
7006 "changes are not allowed\n");
7007 pr_notice("Use the volume_control=1 module parameter "
7008 "to enable volume control\n");
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02007009 }
7010 return -EPERM;
7011 }
7012
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007013 rc = volume_get_status(&s);
7014 if (rc < 0)
7015 return rc;
7016
7017 new_level = s & TP_EC_AUDIO_LVL_MSK;
7018 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007019
7020 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007021 if (!tp_features.mixer_no_level_control) {
7022 if (strlencmp(cmd, "up") == 0) {
7023 if (new_mute)
7024 new_mute = 0;
7025 else if (new_level < TP_EC_VOLUME_MAX)
7026 new_level++;
7027 continue;
7028 } else if (strlencmp(cmd, "down") == 0) {
7029 if (new_mute)
7030 new_mute = 0;
7031 else if (new_level > 0)
7032 new_level--;
7033 continue;
7034 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7035 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7036 new_level = l;
7037 continue;
7038 }
7039 }
7040 if (strlencmp(cmd, "mute") == 0)
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007041 new_mute = TP_EC_AUDIO_MUTESW_MSK;
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007042 else if (strlencmp(cmd, "unmute") == 0)
7043 new_mute = 0;
7044 else
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007045 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007046 }
7047
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02007048 if (tp_features.mixer_no_level_control) {
7049 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7050 new_mute ? "" : "un");
7051 rc = volume_set_mute(!!new_mute);
7052 } else {
7053 tpacpi_disclose_usertask("procfs volume",
7054 "%smute and set level to %d\n",
7055 new_mute ? "" : "un", new_level);
7056 rc = volume_set_status(new_mute | new_level);
7057 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007058 volume_alsa_notify_change();
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007059
7060 return (rc == -EINTR) ? -ERESTARTSYS : rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007061}
7062
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007063static struct ibm_struct volume_driver_data = {
7064 .name = "volume",
7065 .read = volume_read,
7066 .write = volume_write,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007067 .exit = volume_exit,
7068 .suspend = volume_suspend,
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02007069 .resume = volume_resume,
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02007070 .shutdown = volume_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007071};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007072
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007073#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7074
7075#define alsa_card NULL
7076
7077static void inline volume_alsa_notify_change(void)
7078{
7079}
7080
7081static int __init volume_init(struct ibm_init_struct *iibm)
7082{
Joe Perches0978e012011-04-04 10:06:25 -07007083 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02007084
7085 return 1;
7086}
7087
7088static struct ibm_struct volume_driver_data = {
7089 .name = "volume",
7090};
7091
7092#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7093
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007094/*************************************************************************
7095 * Fan subdriver
7096 */
7097
7098/*
7099 * FAN ACCESS MODES
7100 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007101 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007102 * ACPI GFAN method: returns fan level
7103 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007104 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007105 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007106 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007107 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007108 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7109 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007110 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7111 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007112 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007113 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007114 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7115 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007116 *
7117 * Fan speed changes of any sort (including those caused by the
7118 * disengaged mode) are usually done slowly by the firmware as the
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01007119 * maximum amount of fan duty cycle change per second seems to be
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007120 * limited.
7121 *
7122 * Reading is not available if GFAN exists.
7123 * Writing is not available if SFAN exists.
7124 *
7125 * Bits
7126 * 7 automatic mode engaged;
7127 * (default operation mode of the ThinkPad)
7128 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007129 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007130 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007131 * the tachometer while the fan controller ramps up
7132 * the speed (which can take up to a few *minutes*).
7133 * Speeds up fan to 100% duty-cycle, which is far above
7134 * the standard RPM levels. It is not impossible that
7135 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007136 * 5-3 unused in some models. Extra bits for fan level
7137 * in others, but still useless as all values above
7138 * 7 map to the same speed as level 7 in these models.
7139 * 2-0 fan level (0..7 usually)
7140 * 0x00 = stop
7141 * 0x07 = max (set when temperatures critical)
7142 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007143 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007144 *
7145 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04007146 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007147 * does so, its initial value is meaningless (0x07).
7148 *
7149 * For firmware bugs, refer to:
7150 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7151 *
7152 * ----
7153 *
7154 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7155 * Main fan tachometer reading (in RPM)
7156 *
7157 * This register is present on all ThinkPads with a new-style EC, and
7158 * it is known not to be present on the A21m/e, and T22, as there is
7159 * something else in offset 0x84 according to the ACPI DSDT. Other
7160 * ThinkPads from this same time period (and earlier) probably lack the
7161 * tachometer as well.
7162 *
Nick Andrew877d0312009-01-26 11:06:57 +01007163 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007164 * was never fixed by IBM to report the EC firmware version string
7165 * probably support the tachometer (like the early X models), so
7166 * detecting it is quite hard. We need more data to know for sure.
7167 *
7168 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7169 * might result.
7170 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03007171 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7172 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007173 *
7174 * For firmware bugs, refer to:
7175 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7176 *
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007177 * ----
7178 *
7179 * ThinkPad EC register 0x31 bit 0 (only on select models)
7180 *
7181 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7182 * show the speed of the main fan. When bit 0 of EC register 0x31
7183 * is one, the tachometer registers show the speed of the auxiliary
7184 * fan.
7185 *
7186 * Fan control seems to affect both fans, regardless of the state
7187 * of this bit.
7188 *
7189 * So far, only the firmware for the X60/X61 non-tablet versions
7190 * seem to support this (firmware TP-7M).
7191 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007192 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007193 * ThinkPad X31, X40, X41. Not available in the X60.
7194 *
7195 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7196 * high speed. ACPI DSDT seems to map these three speeds to levels
7197 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7198 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7199 *
7200 * The speeds are stored on handles
7201 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7202 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007203 * There are three default speed sets, accessible as handles:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007204 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7205 *
7206 * ACPI DSDT switches which set is in use depending on various
7207 * factors.
7208 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007209 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007210 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7211 * but the ACPI tables just mention level 7.
7212 */
7213
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007214enum { /* Fan control constants */
7215 fan_status_offset = 0x2f, /* EC register 0x2f */
7216 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7217 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007218 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7219 bit 0 selects which fan is active */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007220
7221 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7222 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7223
7224 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7225};
7226
7227enum fan_status_access_mode {
7228 TPACPI_FAN_NONE = 0, /* No fan status or control */
7229 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7230 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7231};
7232
7233enum fan_control_access_mode {
7234 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7235 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7236 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7237 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7238};
7239
7240enum fan_control_commands {
7241 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7242 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7243 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7244 * and also watchdog cmd */
7245};
7246
Rusty Russell90ab5ee2012-01-13 09:32:20 +10307247static bool fan_control_allowed;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007248
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007249static enum fan_status_access_mode fan_status_access_mode;
7250static enum fan_control_access_mode fan_control_access_mode;
7251static enum fan_control_commands fan_control_commands;
7252
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007253static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007254static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007255static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007256static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007257
7258static struct mutex fan_mutex;
7259
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007260static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05007261static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007262
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007263TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7264TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007265 "\\FSPD", /* 600e/x, 770e, 770x */
7266 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007267TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007268 "JFNS", /* 770x-JL */
7269 ); /* all others */
7270
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007271/*
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007272 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7273 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7274 * be in auto mode (0x80).
7275 *
7276 * This is corrected by any write to HFSP either by the driver, or
7277 * by the firmware.
7278 *
7279 * We assume 0x07 really means auto mode while this quirk is active,
7280 * as this is far more likely than the ThinkPad being in level 7,
7281 * which is only used by the firmware during thermal emergencies.
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007282 *
7283 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7284 * TP-70 (T43, R52), which are known to be buggy.
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007285 */
7286
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007287static void fan_quirk1_setup(void)
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007288{
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007289 if (fan_control_initial_status == 0x07) {
Joe Perches0978e012011-04-04 10:06:25 -07007290 pr_notice("fan_init: initial fan status is unknown, "
7291 "assuming it is in auto mode\n");
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007292 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007293 }
7294}
7295
7296static void fan_quirk1_handle(u8 *fan_status)
7297{
7298 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7299 if (*fan_status != fan_control_initial_status) {
7300 /* something changed the HFSP regisnter since
7301 * driver init time, so it is not undefined
7302 * anymore */
7303 tp_features.fan_ctrl_status_undef = 0;
7304 } else {
7305 /* Return most likely status. In fact, it
7306 * might be the only possible status */
7307 *fan_status = TP_EC_FAN_AUTO;
7308 }
7309 }
7310}
7311
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007312/* Select main fan on X60/X61, NOOP on others */
7313static bool fan_select_fan1(void)
7314{
7315 if (tp_features.second_fan) {
7316 u8 val;
7317
7318 if (ec_read(fan_select_offset, &val) < 0)
7319 return false;
7320 val &= 0xFEU;
7321 if (ec_write(fan_select_offset, val) < 0)
7322 return false;
7323 }
7324 return true;
7325}
7326
7327/* Select secondary fan on X60/X61 */
7328static bool fan_select_fan2(void)
7329{
7330 u8 val;
7331
7332 if (!tp_features.second_fan)
7333 return false;
7334
7335 if (ec_read(fan_select_offset, &val) < 0)
7336 return false;
7337 val |= 0x01U;
7338 if (ec_write(fan_select_offset, val) < 0)
7339 return false;
7340
7341 return true;
7342}
7343
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007344/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007345 * Call with fan_mutex held
7346 */
7347static void fan_update_desired_level(u8 status)
7348{
7349 if ((status &
7350 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7351 if (status > 7)
7352 fan_control_desired_level = 7;
7353 else
7354 fan_control_desired_level = status;
7355 }
7356}
7357
7358static int fan_get_status(u8 *status)
7359{
7360 u8 s;
7361
7362 /* TODO:
7363 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7364
7365 switch (fan_status_access_mode) {
Dan Carpentereceeb432012-09-01 12:54:07 -07007366 case TPACPI_FAN_RD_ACPI_GFAN: {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007367 /* 570, 600e/x, 770e, 770x */
Dan Carpentereceeb432012-09-01 12:54:07 -07007368 int res;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007369
Dan Carpentereceeb432012-09-01 12:54:07 -07007370 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007371 return -EIO;
7372
7373 if (likely(status))
Dan Carpentereceeb432012-09-01 12:54:07 -07007374 *status = res & 0x07;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007375
7376 break;
Dan Carpentereceeb432012-09-01 12:54:07 -07007377 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007378 case TPACPI_FAN_RD_TPEC:
7379 /* all except 570, 600e/x, 770e, 770x */
7380 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7381 return -EIO;
7382
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007383 if (likely(status)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007384 *status = s;
Henrique de Moraes Holschuh1c2ece72009-01-11 03:01:08 -02007385 fan_quirk1_handle(status);
7386 }
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007387
7388 break;
7389
7390 default:
7391 return -ENXIO;
7392 }
7393
7394 return 0;
7395}
7396
7397static int fan_get_status_safe(u8 *status)
7398{
7399 int rc;
7400 u8 s;
7401
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007402 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007403 return -ERESTARTSYS;
7404 rc = fan_get_status(&s);
7405 if (!rc)
7406 fan_update_desired_level(s);
7407 mutex_unlock(&fan_mutex);
7408
7409 if (status)
7410 *status = s;
7411
7412 return rc;
7413}
7414
7415static int fan_get_speed(unsigned int *speed)
7416{
7417 u8 hi, lo;
7418
7419 switch (fan_status_access_mode) {
7420 case TPACPI_FAN_RD_TPEC:
7421 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007422 if (unlikely(!fan_select_fan1()))
7423 return -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007424 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7425 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7426 return -EIO;
7427
7428 if (likely(speed))
7429 *speed = (hi << 8) | lo;
7430
7431 break;
7432
7433 default:
7434 return -ENXIO;
7435 }
7436
7437 return 0;
7438}
7439
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007440static int fan2_get_speed(unsigned int *speed)
7441{
7442 u8 hi, lo;
7443 bool rc;
7444
7445 switch (fan_status_access_mode) {
7446 case TPACPI_FAN_RD_TPEC:
7447 /* all except 570, 600e/x, 770e, 770x */
7448 if (unlikely(!fan_select_fan2()))
7449 return -EIO;
7450 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7451 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7452 fan_select_fan1(); /* play it safe */
7453 if (rc)
7454 return -EIO;
7455
7456 if (likely(speed))
7457 *speed = (hi << 8) | lo;
7458
7459 break;
7460
7461 default:
7462 return -ENXIO;
7463 }
7464
7465 return 0;
7466}
7467
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007468static int fan_set_level(int level)
7469{
7470 if (!fan_control_allowed)
7471 return -EPERM;
7472
7473 switch (fan_control_access_mode) {
7474 case TPACPI_FAN_WR_ACPI_SFAN:
7475 if (level >= 0 && level <= 7) {
7476 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7477 return -EIO;
7478 } else
7479 return -EINVAL;
7480 break;
7481
7482 case TPACPI_FAN_WR_ACPI_FANS:
7483 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02007484 if (!(level & TP_EC_FAN_AUTO) &&
7485 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007486 ((level < 0) || (level > 7)))
7487 return -EINVAL;
7488
7489 /* safety net should the EC not support AUTO
7490 * or FULLSPEED mode bits and just ignore them */
7491 if (level & TP_EC_FAN_FULLSPEED)
7492 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01007493 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007494 level |= 4; /* safety min speed 4 */
7495
7496 if (!acpi_ec_write(fan_status_offset, level))
7497 return -EIO;
7498 else
7499 tp_features.fan_ctrl_status_undef = 0;
7500 break;
7501
7502 default:
7503 return -ENXIO;
7504 }
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007505
7506 vdbg_printk(TPACPI_DBG_FAN,
7507 "fan control: set fan control register to 0x%02x\n", level);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007508 return 0;
7509}
7510
7511static int fan_set_level_safe(int level)
7512{
7513 int rc;
7514
7515 if (!fan_control_allowed)
7516 return -EPERM;
7517
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007518 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007519 return -ERESTARTSYS;
7520
7521 if (level == TPACPI_FAN_LAST_LEVEL)
7522 level = fan_control_desired_level;
7523
7524 rc = fan_set_level(level);
7525 if (!rc)
7526 fan_update_desired_level(level);
7527
7528 mutex_unlock(&fan_mutex);
7529 return rc;
7530}
7531
7532static int fan_set_enable(void)
7533{
7534 u8 s;
7535 int rc;
7536
7537 if (!fan_control_allowed)
7538 return -EPERM;
7539
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007540 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007541 return -ERESTARTSYS;
7542
7543 switch (fan_control_access_mode) {
7544 case TPACPI_FAN_WR_ACPI_FANS:
7545 case TPACPI_FAN_WR_TPEC:
7546 rc = fan_get_status(&s);
7547 if (rc < 0)
7548 break;
7549
7550 /* Don't go out of emergency fan mode */
7551 if (s != 7) {
7552 s &= 0x07;
7553 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7554 }
7555
7556 if (!acpi_ec_write(fan_status_offset, s))
7557 rc = -EIO;
7558 else {
7559 tp_features.fan_ctrl_status_undef = 0;
7560 rc = 0;
7561 }
7562 break;
7563
7564 case TPACPI_FAN_WR_ACPI_SFAN:
7565 rc = fan_get_status(&s);
7566 if (rc < 0)
7567 break;
7568
7569 s &= 0x07;
7570
7571 /* Set fan to at least level 4 */
7572 s |= 4;
7573
7574 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007575 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007576 else
7577 rc = 0;
7578 break;
7579
7580 default:
7581 rc = -ENXIO;
7582 }
7583
7584 mutex_unlock(&fan_mutex);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007585
7586 if (!rc)
7587 vdbg_printk(TPACPI_DBG_FAN,
7588 "fan control: set fan control register to 0x%02x\n",
7589 s);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007590 return rc;
7591}
7592
7593static int fan_set_disable(void)
7594{
7595 int rc;
7596
7597 if (!fan_control_allowed)
7598 return -EPERM;
7599
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007600 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007601 return -ERESTARTSYS;
7602
7603 rc = 0;
7604 switch (fan_control_access_mode) {
7605 case TPACPI_FAN_WR_ACPI_FANS:
7606 case TPACPI_FAN_WR_TPEC:
7607 if (!acpi_ec_write(fan_status_offset, 0x00))
7608 rc = -EIO;
7609 else {
7610 fan_control_desired_level = 0;
7611 tp_features.fan_ctrl_status_undef = 0;
7612 }
7613 break;
7614
7615 case TPACPI_FAN_WR_ACPI_SFAN:
7616 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7617 rc = -EIO;
7618 else
7619 fan_control_desired_level = 0;
7620 break;
7621
7622 default:
7623 rc = -ENXIO;
7624 }
7625
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007626 if (!rc)
7627 vdbg_printk(TPACPI_DBG_FAN,
7628 "fan control: set fan control register to 0\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007629
7630 mutex_unlock(&fan_mutex);
7631 return rc;
7632}
7633
7634static int fan_set_speed(int speed)
7635{
7636 int rc;
7637
7638 if (!fan_control_allowed)
7639 return -EPERM;
7640
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007641 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007642 return -ERESTARTSYS;
7643
7644 rc = 0;
7645 switch (fan_control_access_mode) {
7646 case TPACPI_FAN_WR_ACPI_FANS:
7647 if (speed >= 0 && speed <= 65535) {
7648 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7649 speed, speed, speed))
7650 rc = -EIO;
7651 } else
7652 rc = -EINVAL;
7653 break;
7654
7655 default:
7656 rc = -ENXIO;
7657 }
7658
7659 mutex_unlock(&fan_mutex);
7660 return rc;
7661}
7662
7663static void fan_watchdog_reset(void)
7664{
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007665 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7666 return;
7667
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007668 if (fan_watchdog_maxinterval > 0 &&
Tejun Heo41f63c52012-08-03 10:30:47 -07007669 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7670 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7671 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7672 else
7673 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007674}
7675
7676static void fan_watchdog_fire(struct work_struct *ignored)
7677{
7678 int rc;
7679
7680 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7681 return;
7682
Joe Perches0978e012011-04-04 10:06:25 -07007683 pr_notice("fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007684 rc = fan_set_enable();
7685 if (rc < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07007686 pr_err("fan watchdog: error %d while enabling fan, "
7687 "will try again later...\n", -rc);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007688 /* reschedule for later */
7689 fan_watchdog_reset();
7690 }
7691}
7692
7693/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007694 * SYSFS fan layout: hwmon compatible (device)
7695 *
7696 * pwm*_enable:
7697 * 0: "disengaged" mode
7698 * 1: manual mode
7699 * 2: native EC "auto" mode (recommended, hardware default)
7700 *
7701 * pwm*: set speed in manual mode, ignored otherwise.
7702 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7703 * interpolation.
7704 *
7705 * fan*_input: tachometer reading, RPM
7706 *
7707 *
7708 * SYSFS fan layout: extensions
7709 *
7710 * fan_watchdog (driver):
7711 * fan watchdog interval in seconds, 0 disables (default), max 120
7712 */
7713
7714/* sysfs fan pwm1_enable ----------------------------------------------- */
7715static ssize_t fan_pwm1_enable_show(struct device *dev,
7716 struct device_attribute *attr,
7717 char *buf)
7718{
7719 int res, mode;
7720 u8 status;
7721
7722 res = fan_get_status_safe(&status);
7723 if (res)
7724 return res;
7725
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007726 if (status & TP_EC_FAN_FULLSPEED) {
7727 mode = 0;
7728 } else if (status & TP_EC_FAN_AUTO) {
7729 mode = 2;
7730 } else
7731 mode = 1;
7732
7733 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7734}
7735
7736static ssize_t fan_pwm1_enable_store(struct device *dev,
7737 struct device_attribute *attr,
7738 const char *buf, size_t count)
7739{
7740 unsigned long t;
7741 int res, level;
7742
7743 if (parse_strtoul(buf, 2, &t))
7744 return -EINVAL;
7745
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007746 tpacpi_disclose_usertask("hwmon pwm1_enable",
7747 "set fan mode to %lu\n", t);
7748
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007749 switch (t) {
7750 case 0:
7751 level = TP_EC_FAN_FULLSPEED;
7752 break;
7753 case 1:
7754 level = TPACPI_FAN_LAST_LEVEL;
7755 break;
7756 case 2:
7757 level = TP_EC_FAN_AUTO;
7758 break;
7759 case 3:
7760 /* reserved for software-controlled auto mode */
7761 return -ENOSYS;
7762 default:
7763 return -EINVAL;
7764 }
7765
7766 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007767 if (res == -ENXIO)
7768 return -EINVAL;
7769 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007770 return res;
7771
7772 fan_watchdog_reset();
7773
7774 return count;
7775}
7776
7777static struct device_attribute dev_attr_fan_pwm1_enable =
7778 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7779 fan_pwm1_enable_show, fan_pwm1_enable_store);
7780
7781/* sysfs fan pwm1 ------------------------------------------------------ */
7782static ssize_t fan_pwm1_show(struct device *dev,
7783 struct device_attribute *attr,
7784 char *buf)
7785{
7786 int res;
7787 u8 status;
7788
7789 res = fan_get_status_safe(&status);
7790 if (res)
7791 return res;
7792
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007793 if ((status &
7794 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7795 status = fan_control_desired_level;
7796
7797 if (status > 7)
7798 status = 7;
7799
7800 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7801}
7802
7803static ssize_t fan_pwm1_store(struct device *dev,
7804 struct device_attribute *attr,
7805 const char *buf, size_t count)
7806{
7807 unsigned long s;
7808 int rc;
7809 u8 status, newlevel;
7810
7811 if (parse_strtoul(buf, 255, &s))
7812 return -EINVAL;
7813
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007814 tpacpi_disclose_usertask("hwmon pwm1",
7815 "set fan speed to %lu\n", s);
7816
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007817 /* scale down from 0-255 to 0-7 */
7818 newlevel = (s >> 5) & 0x07;
7819
Henrique de Moraes Holschuh7646ea82009-01-11 03:01:04 -02007820 if (mutex_lock_killable(&fan_mutex))
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02007821 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007822
7823 rc = fan_get_status(&status);
7824 if (!rc && (status &
7825 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7826 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03007827 if (rc == -ENXIO)
7828 rc = -EINVAL;
7829 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007830 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03007831 fan_watchdog_reset();
7832 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007833 }
7834
7835 mutex_unlock(&fan_mutex);
7836 return (rc)? rc : count;
7837}
7838
7839static struct device_attribute dev_attr_fan_pwm1 =
7840 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7841 fan_pwm1_show, fan_pwm1_store);
7842
7843/* sysfs fan fan1_input ------------------------------------------------ */
7844static ssize_t fan_fan1_input_show(struct device *dev,
7845 struct device_attribute *attr,
7846 char *buf)
7847{
7848 int res;
7849 unsigned int speed;
7850
7851 res = fan_get_speed(&speed);
7852 if (res < 0)
7853 return res;
7854
7855 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7856}
7857
7858static struct device_attribute dev_attr_fan_fan1_input =
7859 __ATTR(fan1_input, S_IRUGO,
7860 fan_fan1_input_show, NULL);
7861
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007862/* sysfs fan fan2_input ------------------------------------------------ */
7863static ssize_t fan_fan2_input_show(struct device *dev,
7864 struct device_attribute *attr,
7865 char *buf)
7866{
7867 int res;
7868 unsigned int speed;
7869
7870 res = fan2_get_speed(&speed);
7871 if (res < 0)
7872 return res;
7873
7874 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7875}
7876
7877static struct device_attribute dev_attr_fan_fan2_input =
7878 __ATTR(fan2_input, S_IRUGO,
7879 fan_fan2_input_show, NULL);
7880
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007881/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007882static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7883 char *buf)
7884{
7885 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7886}
7887
7888static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7889 const char *buf, size_t count)
7890{
7891 unsigned long t;
7892
7893 if (parse_strtoul(buf, 120, &t))
7894 return -EINVAL;
7895
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007896 if (!fan_control_allowed)
7897 return -EPERM;
7898
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007899 fan_watchdog_maxinterval = t;
7900 fan_watchdog_reset();
7901
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007902 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7903
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007904 return count;
7905}
7906
7907static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7908 fan_fan_watchdog_show, fan_fan_watchdog_store);
7909
7910/* --------------------------------------------------------------------- */
7911static struct attribute *fan_attributes[] = {
7912 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7913 &dev_attr_fan_fan1_input.attr,
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007914 NULL, /* for fan2_input */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007915 NULL
7916};
7917
7918static const struct attribute_group fan_attr_group = {
7919 .attrs = fan_attributes,
7920};
7921
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007922#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7923#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007924
7925#define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7926 { .vendor = PCI_VENDOR_ID_IBM, \
7927 .bios = TPACPI_MATCH_ANY, \
7928 .ec = TPID(__id1, __id2), \
7929 .quirks = __quirks }
7930
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007931#define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7932 { .vendor = PCI_VENDOR_ID_LENOVO, \
7933 .bios = TPACPI_MATCH_ANY, \
7934 .ec = TPID(__id1, __id2), \
7935 .quirks = __quirks }
7936
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007937static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7938 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7939 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7940 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7941 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007942 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007943};
7944
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007945#undef TPACPI_FAN_QL
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007946#undef TPACPI_FAN_QI
7947
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007948static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007949{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007950 int rc;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007951 unsigned long quirks;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007952
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00007953 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7954 "initializing fan subdriver\n");
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03007955
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03007956 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007957 fan_status_access_mode = TPACPI_FAN_NONE;
7958 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007959 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02007960 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03007961 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007962 tp_features.second_fan = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03007963 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007964
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03007965 if (tpacpi_is_ibm()) {
7966 TPACPI_ACPIHANDLE_INIT(fans);
7967 TPACPI_ACPIHANDLE_INIT(gfan);
7968 TPACPI_ACPIHANDLE_INIT(sfan);
7969 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007970
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007971 quirks = tpacpi_check_quirks(fan_quirk_table,
7972 ARRAY_SIZE(fan_quirk_table));
7973
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007974 if (gfan_handle) {
7975 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007976 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007977 } else {
7978 /* all other ThinkPads: note that even old-style
7979 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02007980 if (likely(acpi_ec_read(fan_status_offset,
7981 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007982 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh7d95a3d2009-05-30 13:25:06 -03007983 if (quirks & TPACPI_FAN_Q1)
7984 fan_quirk1_setup();
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03007985 if (quirks & TPACPI_FAN_2FAN) {
7986 tp_features.second_fan = 1;
7987 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7988 "secondary fan support enabled\n");
7989 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007990 } else {
Joe Perches0978e012011-04-04 10:06:25 -07007991 pr_err("ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007992 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007993 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02007994 }
7995 }
7996
7997 if (sfan_handle) {
7998 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03007999 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02008000 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008001 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008002 } else {
8003 if (!gfan_handle) {
8004 /* gfan without sfan means no fan control */
8005 /* all other models implement TP EC 0x2f control */
8006
8007 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008008 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008009 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008010 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008011 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008012 TPACPI_FAN_CMD_SPEED |
8013 TPACPI_FAN_CMD_LEVEL |
8014 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008015 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008016 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008017 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008018 TPACPI_FAN_CMD_LEVEL |
8019 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02008020 }
8021 }
8022 }
8023
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008024 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8025 "fan is %s, modes %d, %d\n",
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008026 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8027 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8028 fan_status_access_mode, fan_control_access_mode);
8029
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008030 /* fan control master switch */
8031 if (!fan_control_allowed) {
8032 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8033 fan_control_commands = 0;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008034 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008035 "fan control features disabled by parameter\n");
8036 }
8037
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008038 /* update fan_control_desired_level */
8039 if (fan_status_access_mode != TPACPI_FAN_NONE)
8040 fan_get_status_safe(NULL);
8041
8042 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8043 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuhd7377242009-06-18 00:40:17 -03008044 if (tp_features.second_fan) {
8045 /* attach second fan tachometer */
8046 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8047 &dev_attr_fan_fan2_input.attr;
8048 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008049 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008050 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008051 if (rc < 0)
8052 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03008053
8054 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8055 &driver_attr_fan_watchdog);
8056 if (rc < 0) {
8057 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8058 &fan_attr_group);
8059 return rc;
8060 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008061 return 0;
8062 } else
8063 return 1;
8064}
8065
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008066static void fan_exit(void)
8067{
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008068 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008069 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008070
8071 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008072 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008073 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8074 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008075
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008076 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03008077 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008078}
8079
Rafael J. Wysockifd3c3a42012-06-27 23:18:44 +02008080static void fan_suspend(void)
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008081{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008082 int rc;
8083
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008084 if (!fan_control_allowed)
8085 return;
8086
8087 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008088 fan_control_resume_level = 0;
8089 rc = fan_get_status_safe(&fan_control_resume_level);
8090 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008091 pr_notice("failed to read fan level for later "
8092 "restore during resume: %d\n", rc);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008093
8094 /* if it is undefined, don't attempt to restore it.
8095 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008096 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008097 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008098}
8099
8100static void fan_resume(void)
8101{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008102 u8 current_level = 7;
8103 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008104 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008105
8106 /* DSDT *always* updates status on resume */
8107 tp_features.fan_ctrl_status_undef = 0;
8108
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008109 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008110 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008111 (fan_get_status_safe(&current_level) < 0))
8112 return;
8113
8114 switch (fan_control_access_mode) {
8115 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008116 /* never decrease fan level */
8117 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008118 break;
8119 case TPACPI_FAN_WR_ACPI_FANS:
8120 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008121 /* never decrease fan level, scale is:
8122 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8123 *
8124 * We expect the firmware to set either 7 or AUTO, but we
8125 * handle FULLSPEED out of paranoia.
8126 *
8127 * So, we can safely only restore FULLSPEED or 7, anything
8128 * else could slow the fan. Restoring AUTO is useless, at
8129 * best that's exactly what the DSDT already set (it is the
8130 * slower it uses).
8131 *
8132 * Always keep in mind that the DSDT *will* have set the
8133 * fans to what the vendor supposes is the best level. We
8134 * muck with it only to speed the fan up.
8135 */
8136 if (fan_control_resume_level != 7 &&
8137 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8138 return;
8139 else
8140 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8141 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008142 break;
8143 default:
8144 return;
8145 }
8146 if (do_set) {
Joe Perches0978e012011-04-04 10:06:25 -07008147 pr_notice("restoring fan level to 0x%02x\n",
8148 fan_control_resume_level);
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02008149 rc = fan_set_level_safe(fan_control_resume_level);
8150 if (rc < 0)
Joe Perches0978e012011-04-04 10:06:25 -07008151 pr_notice("failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008152 }
8153}
8154
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008155static int fan_read(struct seq_file *m)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008156{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008157 int rc;
8158 u8 status;
8159 unsigned int speed = 0;
8160
8161 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008162 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008163 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008164 rc = fan_get_status_safe(&status);
8165 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008166 return rc;
8167
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008168 seq_printf(m, "status:\t\t%s\n"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008169 "level:\t\t%d\n",
8170 (status != 0) ? "enabled" : "disabled", status);
8171 break;
8172
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008173 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008174 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008175 rc = fan_get_status_safe(&status);
8176 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008177 return rc;
8178
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008179 seq_printf(m, "status:\t\t%s\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008180 (status != 0) ? "enabled" : "disabled");
8181
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008182 rc = fan_get_speed(&speed);
8183 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008184 return rc;
8185
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008186 seq_printf(m, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008187
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008188 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008189 /* Disengaged mode takes precedence */
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008190 seq_printf(m, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008191 else if (status & TP_EC_FAN_AUTO)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008192 seq_printf(m, "level:\t\tauto\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008193 else
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008194 seq_printf(m, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008195 break;
8196
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008197 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008198 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008199 seq_printf(m, "status:\t\tnot supported\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008200 }
8201
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008202 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008203 seq_printf(m, "commands:\tlevel <level>");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008204
8205 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008206 case TPACPI_FAN_WR_ACPI_SFAN:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008207 seq_printf(m, " (<level> is 0-7)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008208 break;
8209
8210 default:
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008211 seq_printf(m, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008212 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008213 break;
8214 }
8215 }
8216
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008217 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008218 seq_printf(m, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008219 "commands:\twatchdog <timeout> (<timeout> "
8220 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008221
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008222 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008223 seq_printf(m, "commands:\tspeed <speed>"
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008224 " (<speed> is 0-65535)\n");
8225
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008226 return 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008227}
8228
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008229static int fan_write_cmd_level(const char *cmd, int *rc)
8230{
8231 int level;
8232
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008233 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008234 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03008235 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008236 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008237 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02008238 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008239 return 0;
8240
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008241 *rc = fan_set_level_safe(level);
8242 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008243 pr_err("level command accepted for unsupported access mode %d\n",
8244 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008245 else if (!*rc)
8246 tpacpi_disclose_usertask("procfs fan",
8247 "set level to %d\n", level);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008248
8249 return 1;
8250}
8251
8252static int fan_write_cmd_enable(const char *cmd, int *rc)
8253{
8254 if (strlencmp(cmd, "enable") != 0)
8255 return 0;
8256
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008257 *rc = fan_set_enable();
8258 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008259 pr_err("enable command accepted for unsupported access mode %d\n",
8260 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008261 else if (!*rc)
8262 tpacpi_disclose_usertask("procfs fan", "enable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008263
8264 return 1;
8265}
8266
8267static int fan_write_cmd_disable(const char *cmd, int *rc)
8268{
8269 if (strlencmp(cmd, "disable") != 0)
8270 return 0;
8271
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008272 *rc = fan_set_disable();
8273 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008274 pr_err("disable command accepted for unsupported access mode %d\n",
8275 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008276 else if (!*rc)
8277 tpacpi_disclose_usertask("procfs fan", "disable\n");
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008278
8279 return 1;
8280}
8281
8282static int fan_write_cmd_speed(const char *cmd, int *rc)
8283{
8284 int speed;
8285
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02008286 /* TODO:
8287 * Support speed <low> <medium> <high> ? */
8288
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008289 if (sscanf(cmd, "speed %d", &speed) != 1)
8290 return 0;
8291
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008292 *rc = fan_set_speed(speed);
8293 if (*rc == -ENXIO)
Joe Perches0978e012011-04-04 10:06:25 -07008294 pr_err("speed command accepted for unsupported access mode %d\n",
8295 fan_control_access_mode);
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008296 else if (!*rc)
8297 tpacpi_disclose_usertask("procfs fan",
8298 "set speed to %d\n", speed);
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008299
8300 return 1;
8301}
8302
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008303static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8304{
8305 int interval;
8306
8307 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8308 return 0;
8309
8310 if (interval < 0 || interval > 120)
8311 *rc = -EINVAL;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008312 else {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008313 fan_watchdog_maxinterval = interval;
Henrique de Moraes Holschuh74a60c02009-04-04 04:25:52 +00008314 tpacpi_disclose_usertask("procfs fan",
8315 "set watchdog timer to %d\n",
8316 interval);
8317 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008318
8319 return 1;
8320}
8321
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008322static int fan_write(char *buf)
8323{
8324 char *cmd;
8325 int rc = 0;
8326
8327 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008328 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008329 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008330 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008331 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008332 fan_write_cmd_disable(cmd, &rc) ||
8333 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03008334 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008335 fan_write_cmd_speed(cmd, &rc))
8336 )
8337 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02008338 else if (!rc)
8339 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008340 }
8341
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02008342 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008343}
8344
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008345static struct ibm_struct fan_driver_data = {
8346 .name = "fan",
8347 .read = fan_read,
8348 .write = fan_write,
8349 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03008350 .suspend = fan_suspend,
8351 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008352};
8353
David Henningsson420f9732013-10-16 23:10:31 +02008354/*************************************************************************
8355 * Mute LED subdriver
8356 */
8357
8358
8359struct tp_led_table {
8360 acpi_string name;
8361 int on_value;
8362 int off_value;
8363 int state;
8364};
8365
8366static struct tp_led_table led_tables[] = {
8367 [TPACPI_LED_MUTE] = {
8368 .name = "SSMS",
8369 .on_value = 1,
8370 .off_value = 0,
8371 },
8372 [TPACPI_LED_MICMUTE] = {
8373 .name = "MMTS",
8374 .on_value = 2,
8375 .off_value = 0,
8376 },
8377};
8378
8379static int mute_led_on_off(struct tp_led_table *t, bool state)
8380{
8381 acpi_handle temp;
8382 int output;
8383
8384 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
8385 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
8386 return -EIO;
8387 }
8388
8389 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
8390 state ? t->on_value : t->off_value))
8391 return -EIO;
8392
8393 t->state = state;
8394 return state;
8395}
8396
8397int tpacpi_led_set(int whichled, bool on)
8398{
8399 struct tp_led_table *t;
8400
8401 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
8402 return -EINVAL;
8403
8404 t = &led_tables[whichled];
8405 if (t->state < 0 || t->state == on)
8406 return t->state;
8407 return mute_led_on_off(t, on);
8408}
8409EXPORT_SYMBOL_GPL(tpacpi_led_set);
8410
8411static int mute_led_init(struct ibm_init_struct *iibm)
8412{
8413 acpi_handle temp;
8414 int i;
8415
8416 for (i = 0; i < TPACPI_LED_MAX; i++) {
8417 struct tp_led_table *t = &led_tables[i];
8418 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
8419 mute_led_on_off(t, false);
8420 else
8421 t->state = -ENODEV;
8422 }
8423 return 0;
8424}
8425
8426static void mute_led_exit(void)
8427{
8428 int i;
8429
8430 for (i = 0; i < TPACPI_LED_MAX; i++)
8431 tpacpi_led_set(i, false);
8432}
8433
8434static struct ibm_struct mute_led_driver_data = {
8435 .name = "mute_led",
8436 .exit = mute_led_exit,
8437};
8438
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008439/****************************************************************************
8440 ****************************************************************************
8441 *
8442 * Infrastructure
8443 *
8444 ****************************************************************************
8445 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008447/*
8448 * HKEY event callout for other subdrivers go here
8449 * (yes, it is ugly, but it is quick, safe, and gets the job done
8450 */
8451static void tpacpi_driver_event(const unsigned int hkey_event)
8452{
Henrique de Moraes Holschuh347a2682009-12-09 01:36:24 +00008453 if (ibm_backlight_device) {
8454 switch (hkey_event) {
8455 case TP_HKEY_EV_BRGHT_UP:
8456 case TP_HKEY_EV_BRGHT_DOWN:
8457 tpacpi_brightness_notify_change();
8458 }
8459 }
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008460 if (alsa_card) {
8461 switch (hkey_event) {
8462 case TP_HKEY_EV_VOL_UP:
8463 case TP_HKEY_EV_VOL_DOWN:
8464 case TP_HKEY_EV_VOL_MUTE:
8465 volume_alsa_notify_change();
8466 }
8467 }
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008468}
8469
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008470static void hotkey_driver_event(const unsigned int scancode)
8471{
Henrique de Moraes Holschuh67bcae62009-09-20 14:09:27 -03008472 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
Henrique de Moraes Holschuh8b468c02009-09-20 14:09:26 -03008473}
8474
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008475/* sysfs name ---------------------------------------------------------- */
8476static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8477 struct device_attribute *attr,
8478 char *buf)
8479{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008480 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03008481}
8482
8483static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8484 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8485
8486/* --------------------------------------------------------------------- */
8487
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008488/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03008489static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008490
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008491/*
8492 * Module and infrastructure proble, init and exit handling
8493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
Rusty Russell90ab5ee2012-01-13 09:32:20 +10308495static bool force_load;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008496
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008497#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008498static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008499{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008500 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008501
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008502 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008503}
8504#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8505
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008506static void ibm_exit(struct ibm_struct *ibm)
8507{
8508 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8509
8510 list_del_init(&ibm->all_drivers);
8511
8512 if (ibm->flags.acpi_notify_installed) {
8513 dbg_printk(TPACPI_DBG_EXIT,
8514 "%s: acpi_remove_notify_handler\n", ibm->name);
8515 BUG_ON(!ibm->acpi);
8516 acpi_remove_notify_handler(*ibm->acpi->handle,
8517 ibm->acpi->type,
8518 dispatch_acpi_notify);
8519 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008520 }
8521
8522 if (ibm->flags.proc_created) {
8523 dbg_printk(TPACPI_DBG_EXIT,
8524 "%s: remove_proc_entry\n", ibm->name);
8525 remove_proc_entry(ibm->name, proc_dir);
8526 ibm->flags.proc_created = 0;
8527 }
8528
8529 if (ibm->flags.acpi_driver_registered) {
8530 dbg_printk(TPACPI_DBG_EXIT,
8531 "%s: acpi_bus_unregister_driver\n", ibm->name);
8532 BUG_ON(!ibm->acpi);
8533 acpi_bus_unregister_driver(ibm->acpi->driver);
8534 kfree(ibm->acpi->driver);
8535 ibm->acpi->driver = NULL;
8536 ibm->flags.acpi_driver_registered = 0;
8537 }
8538
8539 if (ibm->flags.init_called && ibm->exit) {
8540 ibm->exit();
8541 ibm->flags.init_called = 0;
8542 }
8543
8544 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8545}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02008546
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008547static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548{
8549 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008550 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 struct proc_dir_entry *entry;
8552
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008553 BUG_ON(ibm == NULL);
8554
8555 INIT_LIST_HEAD(&ibm->all_drivers);
8556
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008557 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 return 0;
8559
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008560 dbg_printk(TPACPI_DBG_INIT,
8561 "probing for %s\n", ibm->name);
8562
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008563 if (iibm->init) {
8564 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008565 if (ret > 0)
8566 return 0; /* probe failed */
8567 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008569
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008570 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 }
8572
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008573 if (ibm->acpi) {
8574 if (ibm->acpi->hid) {
8575 ret = register_tpacpi_subdriver(ibm);
8576 if (ret)
8577 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008578 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008579
8580 if (ibm->acpi->notify) {
8581 ret = setup_acpi_notify(ibm);
8582 if (ret == -ENODEV) {
Joe Perches0978e012011-04-04 10:06:25 -07008583 pr_notice("disabling subdriver %s\n",
8584 ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03008585 ret = 0;
8586 goto err_out;
8587 }
8588 if (ret < 0)
8589 goto err_out;
8590 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008591 }
8592
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008593 dbg_printk(TPACPI_DBG_INIT,
8594 "%s installed\n", ibm->name);
8595
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008596 if (ibm->read) {
Al Virod161a132011-07-24 03:36:29 -04008597 umode_t mode = iibm->base_procfs_mode;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008598
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008599 if (!mode)
8600 mode = S_IRUGO;
Alexey Dobriyan887965e2009-12-15 21:51:12 -02008601 if (ibm->write)
8602 mode |= S_IWUSR;
8603 entry = proc_create_data(ibm->name, mode, proc_dir,
8604 &dispatch_proc_fops, ibm);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008605 if (!entry) {
Joe Perches0978e012011-04-04 10:06:25 -07008606 pr_err("unable to create proc entry %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008607 ret = -ENODEV;
8608 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008609 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03008610 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04008611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008613 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8614
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008616
8617err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03008618 dbg_printk(TPACPI_DBG_INIT,
8619 "%s: at error exit path with result %d\n",
8620 ibm->name, ret);
8621
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008622 ibm_exit(ibm);
8623 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624}
8625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008626/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008628static bool __pure __init tpacpi_is_fw_digit(const char c)
8629{
8630 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8631}
8632
8633/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8634static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8635 const char t)
8636{
8637 return s && strlen(s) >= 8 &&
8638 tpacpi_is_fw_digit(s[0]) &&
8639 tpacpi_is_fw_digit(s[1]) &&
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008640 s[2] == t &&
8641 (s[3] == 'T' || s[3] == 'N') &&
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008642 tpacpi_is_fw_digit(s[4]) &&
Keith Packardb569ab32011-03-31 15:22:33 -07008643 tpacpi_is_fw_digit(s[5]);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008644}
8645
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008646/* returns 0 - probe ok, or < 0 - probe error.
8647 * Probe ok doesn't mean thinkpad found.
8648 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8649static int __must_check __init get_thinkpad_model_data(
8650 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008651{
Jeff Garzik18552562007-10-03 15:15:40 -04008652 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008653 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008654 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008655
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008656 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008657 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008658
8659 memset(tp, 0, sizeof(*tp));
8660
8661 if (dmi_name_in_vendors("IBM"))
8662 tp->vendor = PCI_VENDOR_ID_IBM;
8663 else if (dmi_name_in_vendors("LENOVO"))
8664 tp->vendor = PCI_VENDOR_ID_LENOVO;
8665 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008666 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008667
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008668 s = dmi_get_system_info(DMI_BIOS_VERSION);
8669 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8670 if (s && !tp->bios_version_str)
8671 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008672
8673 /* Really ancient ThinkPad 240X will fail this, which is fine */
Manoj Iyer16a3d9f2012-09-24 11:57:40 -05008674 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8675 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008676 return 0;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008677
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008678 tp->bios_model = tp->bios_version_str[0]
8679 | (tp->bios_version_str[1] << 8);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008680 tp->bios_release = (tp->bios_version_str[4] << 8)
8681 | tp->bios_version_str[5];
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008682
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008683 /*
8684 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8685 * X32 or newer, all Z series; Some models must have an
8686 * up-to-date BIOS or they will not be detected.
8687 *
8688 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8689 */
8690 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008691 if (sscanf(dev->name,
8692 "IBM ThinkPad Embedded Controller -[%17c",
8693 ec_fw_string) == 1) {
8694 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8695 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008696
8697 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008698 if (!tp->ec_version_str)
8699 return -ENOMEM;
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008700
8701 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8702 tp->ec_model = ec_fw_string[0]
8703 | (ec_fw_string[1] << 8);
8704 tp->ec_release = (ec_fw_string[4] << 8)
8705 | ec_fw_string[5];
8706 } else {
Joe Perches0978e012011-04-04 10:06:25 -07008707 pr_notice("ThinkPad firmware release %s "
8708 "doesn't match the known patterns\n",
8709 ec_fw_string);
8710 pr_notice("please report this to %s\n",
8711 TPACPI_MAIL);
Henrique de Moraes Holschuh050df102009-05-30 13:25:05 -03008712 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008713 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02008714 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02008715 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008716
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008717 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Manoj Iyer23b05312012-03-09 17:32:24 -06008718 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008719 tp->model_str = kstrdup(s, GFP_KERNEL);
8720 if (!tp->model_str)
8721 return -ENOMEM;
Manoj Iyera4f46bb2012-08-06 18:15:37 -05008722 } else {
8723 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8724 if (s && !(strnicmp(s, "Lenovo", 6))) {
8725 tp->model_str = kstrdup(s, GFP_KERNEL);
8726 if (!tp->model_str)
8727 return -ENOMEM;
8728 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03008729 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03008730
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03008731 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8732 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8733 if (s && !tp->nummodel_str)
8734 return -ENOMEM;
8735
8736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737}
8738
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008739static int __init probe_for_thinkpad(void)
8740{
8741 int is_thinkpad;
8742
8743 if (acpi_disabled)
8744 return -ENODEV;
8745
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03008746 /* It would be dangerous to run the driver in this case */
8747 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8748 return -ENODEV;
8749
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008750 /*
8751 * Non-ancient models have better DMI tagging, but very old models
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008752 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008753 */
Henrique de Moraes Holschuhe675aba2009-09-12 15:22:13 -03008754 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8755 (thinkpad_id.ec_model != 0) ||
8756 tpacpi_is_fw_known();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008757
Henrique de Moraes Holschuh437e4702010-05-16 19:45:43 -03008758 /* The EC handler is required */
8759 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008760 if (!ec_handle) {
8761 if (is_thinkpad)
Joe Perches0978e012011-04-04 10:06:25 -07008762 pr_err("Not yet supported ThinkPad detected!\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008763 return -ENODEV;
8764 }
8765
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008766 if (!is_thinkpad && !force_load)
8767 return -ENODEV;
8768
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008769 return 0;
8770}
8771
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008772static void __init thinkpad_acpi_init_banner(void)
8773{
Joe Perches0978e012011-04-04 10:06:25 -07008774 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8775 pr_info("%s\n", TPACPI_URL);
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008776
Joe Perches0978e012011-04-04 10:06:25 -07008777 pr_info("ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008778 (thinkpad_id.bios_version_str) ?
8779 thinkpad_id.bios_version_str : "unknown",
8780 (thinkpad_id.ec_version_str) ?
8781 thinkpad_id.ec_version_str : "unknown");
8782
8783 BUG_ON(!thinkpad_id.vendor);
8784
8785 if (thinkpad_id.model_str)
Joe Perches0978e012011-04-04 10:06:25 -07008786 pr_info("%s %s, model %s\n",
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03008787 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8788 "IBM" : ((thinkpad_id.vendor ==
8789 PCI_VENDOR_ID_LENOVO) ?
8790 "Lenovo" : "Unknown vendor"),
8791 thinkpad_id.model_str,
8792 (thinkpad_id.nummodel_str) ?
8793 thinkpad_id.nummodel_str : "unknown");
8794}
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03008795
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008796/* Module init, exit, parameters */
8797
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008798static struct ibm_init_struct ibms_init[] __initdata = {
8799 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008800 .data = &thinkpad_acpi_driver_data,
8801 },
8802 {
8803 .init = hotkey_init,
8804 .data = &hotkey_driver_data,
8805 },
8806 {
8807 .init = bluetooth_init,
8808 .data = &bluetooth_driver_data,
8809 },
8810 {
8811 .init = wan_init,
8812 .data = &wan_driver_data,
8813 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008814 {
8815 .init = uwb_init,
8816 .data = &uwb_driver_data,
8817 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008818#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008819 {
8820 .init = video_init,
Henrique de Moraes Holschuhb525c062010-02-25 22:22:22 -03008821 .base_procfs_mode = S_IRUSR,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008822 .data = &video_driver_data,
8823 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02008824#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008825 {
8826 .init = light_init,
8827 .data = &light_driver_data,
8828 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008829 {
8830 .init = cmos_init,
8831 .data = &cmos_driver_data,
8832 },
8833 {
8834 .init = led_init,
8835 .data = &led_driver_data,
8836 },
8837 {
8838 .init = beep_init,
8839 .data = &beep_driver_data,
8840 },
8841 {
8842 .init = thermal_init,
8843 .data = &thermal_driver_data,
8844 },
8845 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008846 .init = brightness_init,
8847 .data = &brightness_driver_data,
8848 },
8849 {
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008850 .init = volume_init,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008851 .data = &volume_driver_data,
8852 },
8853 {
8854 .init = fan_init,
8855 .data = &fan_driver_data,
8856 },
David Henningsson420f9732013-10-16 23:10:31 +02008857 {
8858 .init = mute_led_init,
8859 .data = &mute_led_driver_data,
8860 },
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008861};
8862
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008863static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8864{
8865 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008866 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008867
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008868 if (!kp || !kp->name || !val)
8869 return -EINVAL;
8870
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008871 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8872 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02008873 WARN_ON(ibm == NULL);
8874
8875 if (!ibm || !ibm->name)
8876 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008877
8878 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8879 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008880 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008881 strcpy(ibms_init[i].param, val);
8882 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008883 return 0;
8884 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03008885 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008886
8887 return -EINVAL;
8888}
8889
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008890module_param(experimental, int, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008891MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008892 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008893
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008894module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008895MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03008896
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008897module_param(force_load, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008898MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008899 "Attempts to load the driver even on a "
8900 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03008901
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008902module_param_named(fan_control, fan_control_allowed, bool, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008903MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008904 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03008905
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008906module_param_named(brightness_mode, brightness_mode, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008907MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008908 "Selects brightness control strategy: "
Henrique de Moraes Holschuh0e501832009-04-04 04:25:53 +00008909 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03008910
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008911module_param(brightness_enable, uint, 0444);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008912MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008913 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02008914
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008915#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
Henrique de Moraes Holschuh329e4e12009-12-15 21:51:08 -02008916module_param_named(volume_mode, volume_mode, uint, 0444);
8917MODULE_PARM_DESC(volume_mode,
8918 "Selects volume control strategy: "
8919 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8920
Henrique de Moraes Holschuha112cee2009-12-15 21:51:09 -02008921module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8922MODULE_PARM_DESC(volume_capabilities,
8923 "Selects the mixer capabilites: "
8924 "0=auto, 1=volume and mute, 2=mute only");
8925
Henrique de Moraes Holschuhc7ac6292009-12-15 21:51:10 -02008926module_param_named(volume_control, volume_control_allowed, bool, 0444);
8927MODULE_PARM_DESC(volume_control,
8928 "Enables software override for the console audio "
8929 "control when true");
8930
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008931/* ALSA module API parameters */
8932module_param_named(index, alsa_index, int, 0444);
8933MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8934module_param_named(id, alsa_id, charp, 0444);
8935MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8936module_param_named(enable, alsa_enable, bool, 0444);
8937MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
Henrique de Moraes Holschuhff850c32009-12-26 22:52:15 -02008938#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
Henrique de Moraes Holschuh0d204c32009-12-15 21:51:11 -02008939
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008940#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02008941 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02008942 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02008943 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008944
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008945TPACPI_PARAM(hotkey);
8946TPACPI_PARAM(bluetooth);
8947TPACPI_PARAM(video);
8948TPACPI_PARAM(light);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008949TPACPI_PARAM(cmos);
8950TPACPI_PARAM(led);
8951TPACPI_PARAM(beep);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02008952TPACPI_PARAM(brightness);
8953TPACPI_PARAM(volume);
8954TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03008955
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008956#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008957module_param(dbg_wlswemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008958MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8959module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8960MODULE_PARM_DESC(wlsw_state,
8961 "Initial state of the emulated WLSW switch");
8962
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008963module_param(dbg_bluetoothemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008964MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8965module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8966MODULE_PARM_DESC(bluetooth_state,
8967 "Initial state of the emulated bluetooth switch");
8968
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008969module_param(dbg_wwanemul, uint, 0444);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008970MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8971module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8972MODULE_PARM_DESC(wwan_state,
8973 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008974
Henrique de Moraes Holschuhb09c72252009-12-09 01:36:27 +00008975module_param(dbg_uwbemul, uint, 0444);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02008976MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8977module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8978MODULE_PARM_DESC(uwb_state,
8979 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02008980#endif
8981
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02008982static void thinkpad_acpi_module_exit(void)
8983{
8984 struct ibm_struct *ibm, *itmp;
8985
8986 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8987
8988 list_for_each_entry_safe_reverse(ibm, itmp,
8989 &tpacpi_all_drivers,
8990 all_drivers) {
8991 ibm_exit(ibm);
8992 }
8993
8994 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8995
8996 if (tpacpi_inputdev) {
8997 if (tp_features.input_device_registered)
8998 input_unregister_device(tpacpi_inputdev);
8999 else
9000 input_free_device(tpacpi_inputdev);
Li Dongyang00d39592012-07-25 10:45:09 +10009001 kfree(hotkey_keycode_map);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009002 }
9003
9004 if (tpacpi_hwmon)
9005 hwmon_device_unregister(tpacpi_hwmon);
9006
9007 if (tp_features.sensors_pdev_attrs_registered)
9008 device_remove_file(&tpacpi_sensors_pdev->dev,
9009 &dev_attr_thinkpad_acpi_pdev_name);
9010 if (tpacpi_sensors_pdev)
9011 platform_device_unregister(tpacpi_sensors_pdev);
9012 if (tpacpi_pdev)
9013 platform_device_unregister(tpacpi_pdev);
9014
9015 if (tp_features.sensors_pdrv_attrs_registered)
9016 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9017 if (tp_features.platform_drv_attrs_registered)
9018 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9019
9020 if (tp_features.sensors_pdrv_registered)
9021 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9022
9023 if (tp_features.platform_drv_registered)
9024 platform_driver_unregister(&tpacpi_pdriver);
9025
9026 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009027 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009028
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009029 if (tpacpi_wq)
9030 destroy_workqueue(tpacpi_wq);
9031
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009032 kfree(thinkpad_id.bios_version_str);
9033 kfree(thinkpad_id.ec_version_str);
9034 kfree(thinkpad_id.model_str);
Li Dongyangd2be15b2012-07-25 10:45:08 +10009035 kfree(thinkpad_id.nummodel_str);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02009036}
9037
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02009038
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009039static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040{
9041 int ret, i;
9042
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03009043 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9044
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009045 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009046
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009047 ret = get_thinkpad_model_data(&thinkpad_id);
9048 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009049 pr_err("unable to get DMI data: %d\n", ret);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03009050 thinkpad_acpi_module_exit();
9051 return ret;
9052 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009053 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009054 if (ret) {
9055 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03009056 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009058
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009059 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03009060
Henrique de Moraes Holschuh7a43f782010-05-16 19:45:31 -03009061 thinkpad_acpi_init_banner();
9062 tpacpi_check_outdated_fw();
9063
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009064 TPACPI_ACPIHANDLE_INIT(ecrd);
9065 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03009067 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9068 if (!tpacpi_wq) {
9069 thinkpad_acpi_module_exit();
9070 return -ENOMEM;
9071 }
9072
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009073 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009074 if (!proc_dir) {
Joe Perches0978e012011-04-04 10:06:25 -07009075 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009076 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 return -ENODEV;
9078 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04009079
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009080 ret = platform_driver_register(&tpacpi_pdriver);
9081 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009082 pr_err("unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009083 thinkpad_acpi_module_exit();
9084 return ret;
9085 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03009086 tp_features.platform_drv_registered = 1;
9087
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009088 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9089 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009090 pr_err("unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009091 thinkpad_acpi_module_exit();
9092 return ret;
9093 }
9094 tp_features.sensors_pdrv_registered = 1;
9095
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009096 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009097 if (!ret) {
9098 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009099 ret = tpacpi_create_driver_attributes(
9100 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009101 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009102 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009103 pr_err("unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009104 thinkpad_acpi_module_exit();
9105 return ret;
9106 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03009107 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03009108
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009109
9110 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009111 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009112 NULL, 0);
9113 if (IS_ERR(tpacpi_pdev)) {
9114 ret = PTR_ERR(tpacpi_pdev);
9115 tpacpi_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009116 pr_err("unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009117 thinkpad_acpi_module_exit();
9118 return ret;
9119 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009120 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02009121 TPACPI_HWMON_DRVR_NAME,
9122 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009123 if (IS_ERR(tpacpi_sensors_pdev)) {
9124 ret = PTR_ERR(tpacpi_sensors_pdev);
9125 tpacpi_sensors_pdev = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009126 pr_err("unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009127 thinkpad_acpi_module_exit();
9128 return ret;
9129 }
9130 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9131 &dev_attr_thinkpad_acpi_pdev_name);
9132 if (ret) {
Joe Perches0978e012011-04-04 10:06:25 -07009133 pr_err("unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03009134 thinkpad_acpi_module_exit();
9135 return ret;
9136 }
9137 tp_features.sensors_pdev_attrs_registered = 1;
9138 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009139 if (IS_ERR(tpacpi_hwmon)) {
9140 ret = PTR_ERR(tpacpi_hwmon);
9141 tpacpi_hwmon = NULL;
Joe Perches0978e012011-04-04 10:06:25 -07009142 pr_err("unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03009143 thinkpad_acpi_module_exit();
9144 return ret;
9145 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03009146 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009147 tpacpi_inputdev = input_allocate_device();
9148 if (!tpacpi_inputdev) {
Joe Perches0978e012011-04-04 10:06:25 -07009149 pr_err("unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009150 thinkpad_acpi_module_exit();
9151 return -ENOMEM;
9152 } else {
9153 /* Prepare input device, but don't register */
9154 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009155 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009156 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhe28393c2010-05-16 19:45:23 -03009157 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009158 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9159 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
Henrique de Moraes Holschuhd112ef92009-12-09 01:36:26 +00009160 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009161 }
Henrique de Moraes Holschuh77775832010-05-16 19:45:33 -03009162
9163 /* Init subdriver dependencies */
9164 tpacpi_detect_brightness_capabilities();
9165
9166 /* Init subdrivers */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03009167 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9168 ret = ibm_init(&ibms_init[i]);
9169 if (ret >= 0 && *ibms_init[i].param)
9170 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009172 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173 return ret;
9174 }
9175 }
Henrique de Moraes Holschuhb589ea42010-02-25 21:28:58 -03009176
9177 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9178
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009179 ret = input_register_device(tpacpi_inputdev);
9180 if (ret < 0) {
Joe Perches0978e012011-04-04 10:06:25 -07009181 pr_err("unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03009182 thinkpad_acpi_module_exit();
9183 return ret;
9184 } else {
9185 tp_features.input_device_registered = 1;
9186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187
9188 return 0;
9189}
9190
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03009191MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9192
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009193/*
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009194 * This will autoload the driver in almost every ThinkPad
9195 * in widespread use.
9196 *
9197 * Only _VERY_ old models, like the 240, 240x and 570 lack
9198 * the HKEY event interface.
9199 */
9200MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9201
9202/*
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009203 * DMI matching for module autoloading
9204 *
9205 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9206 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9207 *
9208 * Only models listed in thinkwiki will be supported, so add yours
9209 * if it is not there yet.
9210 */
9211#define IBM_BIOS_MODULE_ALIAS(__type) \
Mathieu Chouquet-Stringerb36a50f2009-03-14 16:35:26 +01009212 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009213
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009214/* Ancient thinkpad BIOSes have to be identified by
9215 * BIOS type or model number, and there are far less
9216 * BIOS types than model numbers... */
Henrique de Moraes Holschuh922fe092009-04-14 02:44:13 +00009217IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009218
Henrique de Moraes Holschuhf68f53a2009-04-14 02:44:12 +00009219MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9220MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02009221MODULE_DESCRIPTION(TPACPI_DESC);
9222MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02009223MODULE_LICENSE("GPL");
9224
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03009225module_init(thinkpad_acpi_module_init);
9226module_exit(thinkpad_acpi_module_exit);