blob: 8c54d3707fba333990faf3c0b2cd1ce4a136885b [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0-or-later
Googleraf606d22022-10-26 21:40:12 -07002/*
3 * thinkpad_acpi.c - ThinkPad ACPI Extras
4 *
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
7 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Googler9398cc32022-12-02 17:21:52 +080011#define TPACPI_VERSION "0.26"
12#define TPACPI_SYSFS_VERSION 0x030000
Googleraf606d22022-10-26 21:40:12 -070013
14/*
15 * Changelog:
16 * 2007-10-20 changelog trimmed down
17 *
18 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
19 * drivers/misc.
20 *
21 * 2006-11-22 0.13 new maintainer
22 * changelog now lives in git commit history, and will
23 * not be updated further in-file.
24 *
25 * 2005-03-17 0.11 support for 600e, 770x
26 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
27 *
28 * 2005-01-16 0.9 use MODULE_VERSION
29 * thanks to Henrik Brix Andersen <brix@gentoo.org>
30 * fix parameter passing on module loading
31 * thanks to Rusty Russell <rusty@rustcorp.com.au>
32 * thanks to Jim Radford <radford@blackbean.org>
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
34 * thanks to Chris Wright <chrisw@osdl.org>
35 */
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/string.h>
42#include <linux/list.h>
43#include <linux/mutex.h>
44#include <linux/sched.h>
Googler9398cc32022-12-02 17:21:52 +080045#include <linux/sched/signal.h>
Googleraf606d22022-10-26 21:40:12 -070046#include <linux/kthread.h>
47#include <linux/freezer.h>
48#include <linux/delay.h>
49#include <linux/slab.h>
50#include <linux/nvram.h>
51#include <linux/proc_fs.h>
52#include <linux/seq_file.h>
53#include <linux/sysfs.h>
54#include <linux/backlight.h>
Googler9398cc32022-12-02 17:21:52 +080055#include <linux/bitops.h>
Googleraf606d22022-10-26 21:40:12 -070056#include <linux/fb.h>
57#include <linux/platform_device.h>
58#include <linux/hwmon.h>
59#include <linux/hwmon-sysfs.h>
60#include <linux/input.h>
61#include <linux/leds.h>
62#include <linux/rfkill.h>
63#include <linux/dmi.h>
64#include <linux/jiffies.h>
65#include <linux/workqueue.h>
66#include <linux/acpi.h>
Googler9398cc32022-12-02 17:21:52 +080067#include <linux/pci.h>
68#include <linux/power_supply.h>
Googleraf606d22022-10-26 21:40:12 -070069#include <sound/core.h>
70#include <sound/control.h>
71#include <sound/initval.h>
Googler9398cc32022-12-02 17:21:52 +080072#include <linux/uaccess.h>
73#include <acpi/battery.h>
Googleraf606d22022-10-26 21:40:12 -070074#include <acpi/video.h>
75
76/* ThinkPad CMOS commands */
77#define TP_CMOS_VOLUME_DOWN 0
78#define TP_CMOS_VOLUME_UP 1
79#define TP_CMOS_VOLUME_MUTE 2
80#define TP_CMOS_BRIGHTNESS_UP 4
81#define TP_CMOS_BRIGHTNESS_DOWN 5
82#define TP_CMOS_THINKLIGHT_ON 12
83#define TP_CMOS_THINKLIGHT_OFF 13
84
85/* NVRAM Addresses */
86enum tp_nvram_addr {
87 TP_NVRAM_ADDR_HK2 = 0x57,
88 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
89 TP_NVRAM_ADDR_VIDEO = 0x59,
90 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
91 TP_NVRAM_ADDR_MIXER = 0x60,
92};
93
94/* NVRAM bit masks */
95enum {
96 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
97 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
98 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
99 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
100 TP_NVRAM_MASK_THINKLIGHT = 0x10,
101 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
102 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
103 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
104 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
105 TP_NVRAM_MASK_MUTE = 0x40,
106 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
107 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
108 TP_NVRAM_POS_LEVEL_VOLUME = 0,
109};
110
111/* Misc NVRAM-related */
112enum {
113 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
114};
115
116/* ACPI HIDs */
117#define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
118#define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
Googler9398cc32022-12-02 17:21:52 +0800119#define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
Googleraf606d22022-10-26 21:40:12 -0700120#define TPACPI_ACPI_EC_HID "PNP0C09"
121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
126/* ACPI \WGSV commands */
127enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
132};
133
134/* TP_ACPI_WGSV_GET_STATE bits */
135enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
146};
147
148/* HKEY events */
149enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
Googler9398cc32022-12-02 17:21:52 +0800154 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
Googleraf606d22022-10-26 21:40:12 -0700155 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
156 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
157 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
158
159 /* Reasons for waking up from S3/S4 */
160 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
161 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
162 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
163 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
164 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
165 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
166
167 /* Auto-sleep after eject request */
168 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
169 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
170
171 /* Misc bay events */
172 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
174 or port replicator */
175 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
176 dock or port replicator */
177
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
Googler9398cc32022-12-02 17:21:52 +0800183 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
184 * enter/leave tablet mode
185 */
Googleraf606d22022-10-26 21:40:12 -0700186 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
187 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
188 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
189
190 /* Key-related user-interface events */
191 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
192 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
193 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
194
195 /* Thermal events */
196 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
197 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
198 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
199 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
Googler9398cc32022-12-02 17:21:52 +0800200 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
201 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
202 * command completed. Related to
203 * AML DYTC */
204 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
205 * changed. Related to AML GMTS */
Googleraf606d22022-10-26 21:40:12 -0700206
207 /* AC-related events */
208 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
209
Googler9398cc32022-12-02 17:21:52 +0800210 /* Further user-interface events */
211 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
212 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
213
Googleraf606d22022-10-26 21:40:12 -0700214 /* Misc */
215 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
216};
217
218/****************************************************************************
219 * Main driver
220 */
221
222#define TPACPI_NAME "thinkpad"
223#define TPACPI_DESC "ThinkPad ACPI Extras"
224#define TPACPI_FILE TPACPI_NAME "_acpi"
225#define TPACPI_URL "http://ibm-acpi.sf.net/"
226#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
227
228#define TPACPI_PROC_DIR "ibm"
229#define TPACPI_ACPI_EVENT_PREFIX "ibm"
230#define TPACPI_DRVR_NAME TPACPI_FILE
231#define TPACPI_DRVR_SHORTNAME "tpacpi"
232#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
233
234#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
235#define TPACPI_WORKQUEUE_NAME "ktpacpid"
236
237#define TPACPI_MAX_ACPI_ARGS 3
238
239/* Debugging printk groups */
240#define TPACPI_DBG_ALL 0xffff
241#define TPACPI_DBG_DISCLOSETASK 0x8000
242#define TPACPI_DBG_INIT 0x0001
243#define TPACPI_DBG_EXIT 0x0002
244#define TPACPI_DBG_RFKILL 0x0004
245#define TPACPI_DBG_HKEY 0x0008
246#define TPACPI_DBG_FAN 0x0010
247#define TPACPI_DBG_BRGHT 0x0020
248#define TPACPI_DBG_MIXER 0x0040
249
250#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
251#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
252#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253
254
255/****************************************************************************
256 * Driver-wide structs and misc. variables
257 */
258
259struct ibm_struct;
260
261struct tp_acpi_drv_struct {
262 const struct acpi_device_id *hid;
263 struct acpi_driver *driver;
264
265 void (*notify) (struct ibm_struct *, u32);
266 acpi_handle *handle;
267 u32 type;
268 struct acpi_device *device;
269};
270
271struct ibm_struct {
272 char *name;
273
274 int (*read) (struct seq_file *);
275 int (*write) (char *);
276 void (*exit) (void);
277 void (*resume) (void);
278 void (*suspend) (void);
279 void (*shutdown) (void);
280
281 struct list_head all_drivers;
282
283 struct tp_acpi_drv_struct *acpi;
284
285 struct {
286 u8 acpi_driver_registered:1;
287 u8 acpi_notify_installed:1;
288 u8 proc_created:1;
289 u8 init_called:1;
290 u8 experimental:1;
291 } flags;
292};
293
294struct ibm_init_struct {
295 char param[32];
296
297 int (*init) (struct ibm_init_struct *);
298 umode_t base_procfs_mode;
299 struct ibm_struct *data;
300};
301
302static struct {
303 u32 bluetooth:1;
304 u32 hotkey:1;
305 u32 hotkey_mask:1;
306 u32 hotkey_wlsw:1;
Googler9398cc32022-12-02 17:21:52 +0800307 enum {
308 TP_HOTKEY_TABLET_NONE = 0,
309 TP_HOTKEY_TABLET_USES_MHKG,
310 TP_HOTKEY_TABLET_USES_GMMS,
311 } hotkey_tablet;
Googleraf606d22022-10-26 21:40:12 -0700312 u32 kbdlight:1;
313 u32 light:1;
314 u32 light_status:1;
315 u32 bright_acpimode:1;
316 u32 bright_unkfw:1;
317 u32 wan:1;
318 u32 uwb:1;
319 u32 fan_ctrl_status_undef:1;
320 u32 second_fan:1;
321 u32 beep_needs_two_args:1;
322 u32 mixer_no_level_control:1;
Googler9398cc32022-12-02 17:21:52 +0800323 u32 battery_force_primary:1;
Googleraf606d22022-10-26 21:40:12 -0700324 u32 input_device_registered:1;
325 u32 platform_drv_registered:1;
326 u32 platform_drv_attrs_registered:1;
327 u32 sensors_pdrv_registered:1;
328 u32 sensors_pdrv_attrs_registered:1;
329 u32 sensors_pdev_attrs_registered:1;
330 u32 hotkey_poll_active:1;
331 u32 has_adaptive_kbd:1;
332} tp_features;
333
334static struct {
335 u16 hotkey_mask_ff:1;
336 u16 volume_ctrl_forbidden:1;
337} tp_warned;
338
339struct thinkpad_id_data {
340 unsigned int vendor; /* ThinkPad vendor:
341 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
342
343 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
344 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
345
Googler9398cc32022-12-02 17:21:52 +0800346 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
347 u32 ec_model;
348 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
Googleraf606d22022-10-26 21:40:12 -0700349 u16 ec_release;
350
351 char *model_str; /* ThinkPad T43 */
352 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
353};
354static struct thinkpad_id_data thinkpad_id;
355
356static enum {
357 TPACPI_LIFE_INIT = 0,
358 TPACPI_LIFE_RUNNING,
359 TPACPI_LIFE_EXITING,
360} tpacpi_lifecycle;
361
362static int experimental;
363static u32 dbg_level;
364
365static struct workqueue_struct *tpacpi_wq;
366
367enum led_status_t {
368 TPACPI_LED_OFF = 0,
369 TPACPI_LED_ON,
370 TPACPI_LED_BLINK,
371};
372
Googler9398cc32022-12-02 17:21:52 +0800373/* tpacpi LED class */
Googleraf606d22022-10-26 21:40:12 -0700374struct tpacpi_led_classdev {
375 struct led_classdev led_classdev;
376 int led;
377};
378
379/* brightness level capabilities */
380static unsigned int bright_maxlvl; /* 0 = unknown */
381
382#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
383static int dbg_wlswemul;
384static bool tpacpi_wlsw_emulstate;
385static int dbg_bluetoothemul;
386static bool tpacpi_bluetooth_emulstate;
387static int dbg_wwanemul;
388static bool tpacpi_wwan_emulstate;
389static int dbg_uwbemul;
390static bool tpacpi_uwb_emulstate;
391#endif
392
393
394/*************************************************************************
395 * Debugging helpers
396 */
397
398#define dbg_printk(a_dbg_level, format, arg...) \
399do { \
400 if (dbg_level & (a_dbg_level)) \
401 printk(KERN_DEBUG pr_fmt("%s: " format), \
402 __func__, ##arg); \
403} while (0)
404
405#ifdef CONFIG_THINKPAD_ACPI_DEBUG
406#define vdbg_printk dbg_printk
407static const char *str_supported(int is_supported);
408#else
409static inline const char *str_supported(int is_supported) { return ""; }
410#define vdbg_printk(a_dbg_level, format, arg...) \
411 do { if (0) no_printk(format, ##arg); } while (0)
412#endif
413
414static void tpacpi_log_usertask(const char * const what)
415{
416 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
417 what, task_tgid_vnr(current));
418}
419
420#define tpacpi_disclose_usertask(what, format, arg...) \
421do { \
422 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
423 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
424 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
425 what, task_tgid_vnr(current), ## arg); \
426 } \
427} while (0)
428
429/*
430 * Quirk handling helpers
431 *
Googler9398cc32022-12-02 17:21:52 +0800432 * ThinkPad IDs and versions seen in the field so far are
433 * two or three characters from the set [0-9A-Z], i.e. base 36.
Googleraf606d22022-10-26 21:40:12 -0700434 *
435 * We use values well outside that range as specials.
436 */
437
Googler9398cc32022-12-02 17:21:52 +0800438#define TPACPI_MATCH_ANY 0xffffffffU
439#define TPACPI_MATCH_ANY_VERSION 0xffffU
Googleraf606d22022-10-26 21:40:12 -0700440#define TPACPI_MATCH_UNKNOWN 0U
441
Googler9398cc32022-12-02 17:21:52 +0800442/* TPID('1', 'Y') == 0x3159 */
443#define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
444#define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
445#define TPVER TPID
Googleraf606d22022-10-26 21:40:12 -0700446
447#define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453#define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
Googler9398cc32022-12-02 17:21:52 +0800459#define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPID3(__id1, __id2, __id3), \
462 .ec = TPACPI_MATCH_ANY, \
463 .quirks = (__quirk) }
464
465#define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
466 { .vendor = PCI_VENDOR_ID_IBM, \
467 .bios = TPACPI_MATCH_ANY, \
468 .ec = TPID(__id1, __id2), \
469 .quirks = (__quirk) }
470
Googleraf606d22022-10-26 21:40:12 -0700471#define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
472 { .vendor = PCI_VENDOR_ID_LENOVO, \
473 .bios = TPACPI_MATCH_ANY, \
474 .ec = TPID(__id1, __id2), \
475 .quirks = (__quirk) }
476
477struct tpacpi_quirk {
478 unsigned int vendor;
Googler9398cc32022-12-02 17:21:52 +0800479 u32 bios;
480 u32 ec;
Googleraf606d22022-10-26 21:40:12 -0700481 unsigned long quirks;
482};
483
484/**
485 * tpacpi_check_quirks() - search BIOS/EC version on a list
486 * @qlist: array of &struct tpacpi_quirk
487 * @qlist_size: number of elements in @qlist
488 *
489 * Iterates over a quirks list until one is found that matches the
490 * ThinkPad's vendor, BIOS and EC model.
491 *
492 * Returns 0 if nothing matches, otherwise returns the quirks field of
493 * the matching &struct tpacpi_quirk entry.
494 *
495 * The match criteria is: vendor, ec and bios much match.
496 */
497static unsigned long __init tpacpi_check_quirks(
498 const struct tpacpi_quirk *qlist,
499 unsigned int qlist_size)
500{
501 while (qlist_size) {
502 if ((qlist->vendor == thinkpad_id.vendor ||
503 qlist->vendor == TPACPI_MATCH_ANY) &&
504 (qlist->bios == thinkpad_id.bios_model ||
505 qlist->bios == TPACPI_MATCH_ANY) &&
506 (qlist->ec == thinkpad_id.ec_model ||
507 qlist->ec == TPACPI_MATCH_ANY))
508 return qlist->quirks;
509
510 qlist_size--;
511 qlist++;
512 }
513 return 0;
514}
515
516static inline bool __pure __init tpacpi_is_lenovo(void)
517{
518 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
519}
520
521static inline bool __pure __init tpacpi_is_ibm(void)
522{
523 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
524}
525
526/****************************************************************************
527 ****************************************************************************
528 *
529 * ACPI Helpers and device model
530 *
531 ****************************************************************************
532 ****************************************************************************/
533
534/*************************************************************************
535 * ACPI basic handles
536 */
537
538static acpi_handle root_handle;
539static acpi_handle ec_handle;
540
541#define TPACPI_HANDLE(object, parent, paths...) \
542 static acpi_handle object##_handle; \
543 static const acpi_handle * const object##_parent __initconst = \
544 &parent##_handle; \
545 static char *object##_paths[] __initdata = { paths }
546
547TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
548TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
549
550TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
551 /* T4x, X31, X40 */
552 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
553 "\\CMS", /* R40, R40e */
554 ); /* all others */
555
556TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
557 "^HKEY", /* R30, R31 */
558 "HKEY", /* all others */
559 ); /* 570 */
560
561/*************************************************************************
562 * ACPI helpers
563 */
564
565static int acpi_evalf(acpi_handle handle,
566 int *res, char *method, char *fmt, ...)
567{
568 char *fmt0 = fmt;
569 struct acpi_object_list params;
570 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
571 struct acpi_buffer result, *resultp;
572 union acpi_object out_obj;
573 acpi_status status;
574 va_list ap;
575 char res_type;
576 int success;
577 int quiet;
578
579 if (!*fmt) {
580 pr_err("acpi_evalf() called with empty format\n");
581 return 0;
582 }
583
584 if (*fmt == 'q') {
585 quiet = 1;
586 fmt++;
587 } else
588 quiet = 0;
589
590 res_type = *(fmt++);
591
592 params.count = 0;
593 params.pointer = &in_objs[0];
594
595 va_start(ap, fmt);
596 while (*fmt) {
597 char c = *(fmt++);
598 switch (c) {
599 case 'd': /* int */
600 in_objs[params.count].integer.value = va_arg(ap, int);
601 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
602 break;
603 /* add more types as needed */
604 default:
Googler9398cc32022-12-02 17:21:52 +0800605 pr_err("acpi_evalf() called with invalid format character '%c'\n",
606 c);
Googleraf606d22022-10-26 21:40:12 -0700607 va_end(ap);
608 return 0;
609 }
610 }
611 va_end(ap);
612
613 if (res_type != 'v') {
614 result.length = sizeof(out_obj);
615 result.pointer = &out_obj;
616 resultp = &result;
617 } else
618 resultp = NULL;
619
620 status = acpi_evaluate_object(handle, method, &params, resultp);
621
622 switch (res_type) {
623 case 'd': /* int */
624 success = (status == AE_OK &&
625 out_obj.type == ACPI_TYPE_INTEGER);
626 if (success && res)
627 *res = out_obj.integer.value;
628 break;
629 case 'v': /* void */
630 success = status == AE_OK;
631 break;
632 /* add more types as needed */
633 default:
Googler9398cc32022-12-02 17:21:52 +0800634 pr_err("acpi_evalf() called with invalid format character '%c'\n",
635 res_type);
Googleraf606d22022-10-26 21:40:12 -0700636 return 0;
637 }
638
639 if (!success && !quiet)
640 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
641 method, fmt0, acpi_format_exception(status));
642
643 return success;
644}
645
646static int acpi_ec_read(int i, u8 *p)
647{
648 int v;
649
650 if (ecrd_handle) {
651 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
652 return 0;
653 *p = v;
654 } else {
655 if (ec_read(i, p) < 0)
656 return 0;
657 }
658
659 return 1;
660}
661
662static int acpi_ec_write(int i, u8 v)
663{
664 if (ecwr_handle) {
665 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
666 return 0;
667 } else {
668 if (ec_write(i, v) < 0)
669 return 0;
670 }
671
672 return 1;
673}
674
675static int issue_thinkpad_cmos_command(int cmos_cmd)
676{
677 if (!cmos_handle)
678 return -ENXIO;
679
680 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
681 return -EIO;
682
683 return 0;
684}
685
686/*************************************************************************
687 * ACPI device model
688 */
689
690#define TPACPI_ACPIHANDLE_INIT(object) \
691 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
692 object##_paths, ARRAY_SIZE(object##_paths))
693
694static void __init drv_acpi_handle_init(const char *name,
695 acpi_handle *handle, const acpi_handle parent,
696 char **paths, const int num_paths)
697{
698 int i;
699 acpi_status status;
700
701 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
702 name);
703
704 for (i = 0; i < num_paths; i++) {
705 status = acpi_get_handle(parent, paths[i], handle);
706 if (ACPI_SUCCESS(status)) {
707 dbg_printk(TPACPI_DBG_INIT,
708 "Found ACPI handle %s for %s\n",
709 paths[i], name);
710 return;
711 }
712 }
713
714 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
715 name);
716 *handle = NULL;
717}
718
719static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
720 u32 level, void *context, void **return_value)
721{
722 struct acpi_device *dev;
723 if (!strcmp(context, "video")) {
724 if (acpi_bus_get_device(handle, &dev))
725 return AE_OK;
726 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
727 return AE_OK;
728 }
729
730 *(acpi_handle *)return_value = handle;
731
732 return AE_CTRL_TERMINATE;
733}
734
735static void __init tpacpi_acpi_handle_locate(const char *name,
736 const char *hid,
737 acpi_handle *handle)
738{
739 acpi_status status;
740 acpi_handle device_found;
741
742 BUG_ON(!name || !handle);
743 vdbg_printk(TPACPI_DBG_INIT,
744 "trying to locate ACPI handle for %s, using HID %s\n",
745 name, hid ? hid : "NULL");
746
747 memset(&device_found, 0, sizeof(device_found));
748 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
749 (void *)name, &device_found);
750
751 *handle = NULL;
752
753 if (ACPI_SUCCESS(status)) {
754 *handle = device_found;
755 dbg_printk(TPACPI_DBG_INIT,
756 "Found ACPI handle for %s\n", name);
757 } else {
758 vdbg_printk(TPACPI_DBG_INIT,
759 "Could not locate an ACPI handle for %s: %s\n",
760 name, acpi_format_exception(status));
761 }
762}
763
764static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
765{
766 struct ibm_struct *ibm = data;
767
768 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
769 return;
770
771 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
772 return;
773
774 ibm->acpi->notify(ibm, event);
775}
776
777static int __init setup_acpi_notify(struct ibm_struct *ibm)
778{
779 acpi_status status;
780 int rc;
781
782 BUG_ON(!ibm->acpi);
783
784 if (!*ibm->acpi->handle)
785 return 0;
786
787 vdbg_printk(TPACPI_DBG_INIT,
788 "setting up ACPI notify for %s\n", ibm->name);
789
790 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
791 if (rc < 0) {
792 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
793 return -ENODEV;
794 }
795
796 ibm->acpi->device->driver_data = ibm;
797 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
798 TPACPI_ACPI_EVENT_PREFIX,
799 ibm->name);
800
801 status = acpi_install_notify_handler(*ibm->acpi->handle,
802 ibm->acpi->type, dispatch_acpi_notify, ibm);
803 if (ACPI_FAILURE(status)) {
804 if (status == AE_ALREADY_EXISTS) {
Googler9398cc32022-12-02 17:21:52 +0800805 pr_notice("another device driver is already handling %s events\n",
806 ibm->name);
Googleraf606d22022-10-26 21:40:12 -0700807 } else {
808 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
809 ibm->name, acpi_format_exception(status));
810 }
811 return -ENODEV;
812 }
813 ibm->flags.acpi_notify_installed = 1;
814 return 0;
815}
816
817static int __init tpacpi_device_add(struct acpi_device *device)
818{
819 return 0;
820}
821
822static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
823{
824 int rc;
825
826 dbg_printk(TPACPI_DBG_INIT,
827 "registering %s as an ACPI driver\n", ibm->name);
828
829 BUG_ON(!ibm->acpi);
830
831 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
832 if (!ibm->acpi->driver) {
833 pr_err("failed to allocate memory for ibm->acpi->driver\n");
834 return -ENOMEM;
835 }
836
837 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
838 ibm->acpi->driver->ids = ibm->acpi->hid;
839
840 ibm->acpi->driver->ops.add = &tpacpi_device_add;
841
842 rc = acpi_bus_register_driver(ibm->acpi->driver);
843 if (rc < 0) {
844 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
845 ibm->name, rc);
846 kfree(ibm->acpi->driver);
847 ibm->acpi->driver = NULL;
848 } else if (!rc)
849 ibm->flags.acpi_driver_registered = 1;
850
851 return rc;
852}
853
854
855/****************************************************************************
856 ****************************************************************************
857 *
858 * Procfs Helpers
859 *
860 ****************************************************************************
861 ****************************************************************************/
862
863static int dispatch_proc_show(struct seq_file *m, void *v)
864{
865 struct ibm_struct *ibm = m->private;
866
867 if (!ibm || !ibm->read)
868 return -EINVAL;
869 return ibm->read(m);
870}
871
872static int dispatch_proc_open(struct inode *inode, struct file *file)
873{
874 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
875}
876
877static ssize_t dispatch_proc_write(struct file *file,
878 const char __user *userbuf,
879 size_t count, loff_t *pos)
880{
881 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
882 char *kernbuf;
883 int ret;
884
885 if (!ibm || !ibm->write)
886 return -EINVAL;
887 if (count > PAGE_SIZE - 2)
888 return -EINVAL;
889
890 kernbuf = kmalloc(count + 2, GFP_KERNEL);
891 if (!kernbuf)
892 return -ENOMEM;
893
894 if (copy_from_user(kernbuf, userbuf, count)) {
895 kfree(kernbuf);
896 return -EFAULT;
897 }
898
899 kernbuf[count] = 0;
900 strcat(kernbuf, ",");
901 ret = ibm->write(kernbuf);
902 if (ret == 0)
903 ret = count;
904
905 kfree(kernbuf);
906
907 return ret;
908}
909
910static const struct file_operations dispatch_proc_fops = {
911 .owner = THIS_MODULE,
912 .open = dispatch_proc_open,
913 .read = seq_read,
914 .llseek = seq_lseek,
915 .release = single_release,
916 .write = dispatch_proc_write,
917};
918
919static char *next_cmd(char **cmds)
920{
921 char *start = *cmds;
922 char *end;
923
924 while ((end = strchr(start, ',')) && end == start)
925 start = end + 1;
926
927 if (!end)
928 return NULL;
929
930 *end = 0;
931 *cmds = end + 1;
932 return start;
933}
934
935
936/****************************************************************************
937 ****************************************************************************
938 *
939 * Device model: input, hwmon and platform
940 *
941 ****************************************************************************
942 ****************************************************************************/
943
944static struct platform_device *tpacpi_pdev;
945static struct platform_device *tpacpi_sensors_pdev;
946static struct device *tpacpi_hwmon;
947static struct input_dev *tpacpi_inputdev;
948static struct mutex tpacpi_inputdev_send_mutex;
949static LIST_HEAD(tpacpi_all_drivers);
950
951#ifdef CONFIG_PM_SLEEP
952static int tpacpi_suspend_handler(struct device *dev)
953{
954 struct ibm_struct *ibm, *itmp;
955
956 list_for_each_entry_safe(ibm, itmp,
957 &tpacpi_all_drivers,
958 all_drivers) {
959 if (ibm->suspend)
960 (ibm->suspend)();
961 }
962
963 return 0;
964}
965
966static int tpacpi_resume_handler(struct device *dev)
967{
968 struct ibm_struct *ibm, *itmp;
969
970 list_for_each_entry_safe(ibm, itmp,
971 &tpacpi_all_drivers,
972 all_drivers) {
973 if (ibm->resume)
974 (ibm->resume)();
975 }
976
977 return 0;
978}
979#endif
980
981static SIMPLE_DEV_PM_OPS(tpacpi_pm,
982 tpacpi_suspend_handler, tpacpi_resume_handler);
983
984static void tpacpi_shutdown_handler(struct platform_device *pdev)
985{
986 struct ibm_struct *ibm, *itmp;
987
988 list_for_each_entry_safe(ibm, itmp,
989 &tpacpi_all_drivers,
990 all_drivers) {
991 if (ibm->shutdown)
992 (ibm->shutdown)();
993 }
994}
995
996static struct platform_driver tpacpi_pdriver = {
997 .driver = {
998 .name = TPACPI_DRVR_NAME,
999 .pm = &tpacpi_pm,
1000 },
1001 .shutdown = tpacpi_shutdown_handler,
1002};
1003
1004static struct platform_driver tpacpi_hwmon_pdriver = {
1005 .driver = {
1006 .name = TPACPI_HWMON_DRVR_NAME,
1007 },
1008};
1009
1010/*************************************************************************
1011 * sysfs support helpers
1012 */
1013
1014struct attribute_set {
1015 unsigned int members, max_members;
1016 struct attribute_group group;
1017};
1018
1019struct attribute_set_obj {
1020 struct attribute_set s;
1021 struct attribute *a;
1022} __attribute__((packed));
1023
1024static struct attribute_set *create_attr_set(unsigned int max_members,
1025 const char *name)
1026{
1027 struct attribute_set_obj *sobj;
1028
1029 if (max_members == 0)
1030 return NULL;
1031
1032 /* Allocates space for implicit NULL at the end too */
1033 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1034 max_members * sizeof(struct attribute *),
1035 GFP_KERNEL);
1036 if (!sobj)
1037 return NULL;
1038 sobj->s.max_members = max_members;
1039 sobj->s.group.attrs = &sobj->a;
1040 sobj->s.group.name = name;
1041
1042 return &sobj->s;
1043}
1044
1045#define destroy_attr_set(_set) \
1046 kfree(_set);
1047
1048/* not multi-threaded safe, use it in a single thread per set */
1049static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1050{
1051 if (!s || !attr)
1052 return -EINVAL;
1053
1054 if (s->members >= s->max_members)
1055 return -ENOMEM;
1056
1057 s->group.attrs[s->members] = attr;
1058 s->members++;
1059
1060 return 0;
1061}
1062
1063static int add_many_to_attr_set(struct attribute_set *s,
1064 struct attribute **attr,
1065 unsigned int count)
1066{
1067 int i, res;
1068
1069 for (i = 0; i < count; i++) {
1070 res = add_to_attr_set(s, attr[i]);
1071 if (res)
1072 return res;
1073 }
1074
1075 return 0;
1076}
1077
1078static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1079{
1080 sysfs_remove_group(kobj, &s->group);
1081 destroy_attr_set(s);
1082}
1083
1084#define register_attr_set_with_sysfs(_attr_set, _kobj) \
1085 sysfs_create_group(_kobj, &_attr_set->group)
1086
1087static int parse_strtoul(const char *buf,
1088 unsigned long max, unsigned long *value)
1089{
1090 char *endp;
1091
1092 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1093 endp = skip_spaces(endp);
1094 if (*endp || *value > max)
1095 return -EINVAL;
1096
1097 return 0;
1098}
1099
1100static void tpacpi_disable_brightness_delay(void)
1101{
1102 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1103 pr_notice("ACPI backlight control delay disabled\n");
1104}
1105
1106static void printk_deprecated_attribute(const char * const what,
1107 const char * const details)
1108{
1109 tpacpi_log_usertask("deprecated sysfs attribute");
Googler9398cc32022-12-02 17:21:52 +08001110 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
Googleraf606d22022-10-26 21:40:12 -07001111 what, details);
1112}
1113
1114/*************************************************************************
1115 * rfkill and radio control support helpers
1116 */
1117
1118/*
1119 * ThinkPad-ACPI firmware handling model:
1120 *
1121 * WLSW (master wireless switch) is event-driven, and is common to all
1122 * firmware-controlled radios. It cannot be controlled, just monitored,
1123 * as expected. It overrides all radio state in firmware
1124 *
1125 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1126 * (TODO: verify how WLSW interacts with the returned radio state).
1127 *
1128 * The only time there are shadow radio state changes, is when
1129 * masked-off hotkeys are used.
1130 */
1131
1132/*
1133 * Internal driver API for radio state:
1134 *
1135 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1136 * bool: true means radio blocked (off)
1137 */
1138enum tpacpi_rfkill_state {
1139 TPACPI_RFK_RADIO_OFF = 0,
1140 TPACPI_RFK_RADIO_ON
1141};
1142
1143/* rfkill switches */
1144enum tpacpi_rfk_id {
1145 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1146 TPACPI_RFK_WWAN_SW_ID,
1147 TPACPI_RFK_UWB_SW_ID,
1148 TPACPI_RFK_SW_MAX
1149};
1150
1151static const char *tpacpi_rfkill_names[] = {
1152 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1153 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1154 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1155 [TPACPI_RFK_SW_MAX] = NULL
1156};
1157
1158/* ThinkPad-ACPI rfkill subdriver */
1159struct tpacpi_rfk {
1160 struct rfkill *rfkill;
1161 enum tpacpi_rfk_id id;
1162 const struct tpacpi_rfk_ops *ops;
1163};
1164
1165struct tpacpi_rfk_ops {
1166 /* firmware interface */
1167 int (*get_status)(void);
1168 int (*set_status)(const enum tpacpi_rfkill_state);
1169};
1170
1171static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1172
1173/* Query FW and update rfkill sw state for a given rfkill switch */
1174static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1175{
1176 int status;
1177
1178 if (!tp_rfk)
1179 return -ENODEV;
1180
1181 status = (tp_rfk->ops->get_status)();
1182 if (status < 0)
1183 return status;
1184
1185 rfkill_set_sw_state(tp_rfk->rfkill,
1186 (status == TPACPI_RFK_RADIO_OFF));
1187
1188 return status;
1189}
1190
Googlerb48fa912023-03-17 12:40:29 +05301191/* Query FW and update rfkill sw state for all rfkill switches */
1192static void tpacpi_rfk_update_swstate_all(void)
1193{
1194 unsigned int i;
1195
1196 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1197 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1198}
1199
Googleraf606d22022-10-26 21:40:12 -07001200/*
1201 * Sync the HW-blocking state of all rfkill switches,
1202 * do notice it causes the rfkill core to schedule uevents
1203 */
1204static void tpacpi_rfk_update_hwblock_state(bool blocked)
1205{
1206 unsigned int i;
1207 struct tpacpi_rfk *tp_rfk;
1208
1209 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1210 tp_rfk = tpacpi_rfkill_switches[i];
1211 if (tp_rfk) {
1212 if (rfkill_set_hw_state(tp_rfk->rfkill,
1213 blocked)) {
1214 /* ignore -- we track sw block */
1215 }
1216 }
1217 }
1218}
1219
1220/* Call to get the WLSW state from the firmware */
1221static int hotkey_get_wlsw(void);
1222
1223/* Call to query WLSW state and update all rfkill switches */
1224static bool tpacpi_rfk_check_hwblock_state(void)
1225{
1226 int res = hotkey_get_wlsw();
1227 int hw_blocked;
1228
1229 /* When unknown or unsupported, we have to assume it is unblocked */
1230 if (res < 0)
1231 return false;
1232
1233 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1234 tpacpi_rfk_update_hwblock_state(hw_blocked);
1235
1236 return hw_blocked;
1237}
1238
1239static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1240{
1241 struct tpacpi_rfk *tp_rfk = data;
1242 int res;
1243
1244 dbg_printk(TPACPI_DBG_RFKILL,
1245 "request to change radio state to %s\n",
1246 blocked ? "blocked" : "unblocked");
1247
1248 /* try to set radio state */
1249 res = (tp_rfk->ops->set_status)(blocked ?
1250 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1251
1252 /* and update the rfkill core with whatever the FW really did */
1253 tpacpi_rfk_update_swstate(tp_rfk);
1254
1255 return (res < 0) ? res : 0;
1256}
1257
1258static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1259 .set_block = tpacpi_rfk_hook_set_block,
1260};
1261
1262static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1263 const struct tpacpi_rfk_ops *tp_rfkops,
1264 const enum rfkill_type rfktype,
1265 const char *name,
1266 const bool set_default)
1267{
1268 struct tpacpi_rfk *atp_rfk;
1269 int res;
1270 bool sw_state = false;
1271 bool hw_state;
1272 int sw_status;
1273
1274 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1275
1276 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1277 if (atp_rfk)
1278 atp_rfk->rfkill = rfkill_alloc(name,
1279 &tpacpi_pdev->dev,
1280 rfktype,
1281 &tpacpi_rfk_rfkill_ops,
1282 atp_rfk);
1283 if (!atp_rfk || !atp_rfk->rfkill) {
1284 pr_err("failed to allocate memory for rfkill class\n");
1285 kfree(atp_rfk);
1286 return -ENOMEM;
1287 }
1288
1289 atp_rfk->id = id;
1290 atp_rfk->ops = tp_rfkops;
1291
1292 sw_status = (tp_rfkops->get_status)();
1293 if (sw_status < 0) {
1294 pr_err("failed to read initial state for %s, error %d\n",
1295 name, sw_status);
1296 } else {
1297 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1298 if (set_default) {
1299 /* try to keep the initial state, since we ask the
1300 * firmware to preserve it across S5 in NVRAM */
1301 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1302 }
1303 }
1304 hw_state = tpacpi_rfk_check_hwblock_state();
1305 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1306
1307 res = rfkill_register(atp_rfk->rfkill);
1308 if (res < 0) {
1309 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1310 rfkill_destroy(atp_rfk->rfkill);
1311 kfree(atp_rfk);
1312 return res;
1313 }
1314
1315 tpacpi_rfkill_switches[id] = atp_rfk;
1316
1317 pr_info("rfkill switch %s: radio is %sblocked\n",
1318 name, (sw_state || hw_state) ? "" : "un");
1319 return 0;
1320}
1321
1322static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1323{
1324 struct tpacpi_rfk *tp_rfk;
1325
1326 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1327
1328 tp_rfk = tpacpi_rfkill_switches[id];
1329 if (tp_rfk) {
1330 rfkill_unregister(tp_rfk->rfkill);
1331 rfkill_destroy(tp_rfk->rfkill);
1332 tpacpi_rfkill_switches[id] = NULL;
1333 kfree(tp_rfk);
1334 }
1335}
1336
1337static void printk_deprecated_rfkill_attribute(const char * const what)
1338{
1339 printk_deprecated_attribute(what,
1340 "Please switch to generic rfkill before year 2010");
1341}
1342
1343/* sysfs <radio> enable ------------------------------------------------ */
1344static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1345 struct device_attribute *attr,
1346 char *buf)
1347{
1348 int status;
1349
1350 printk_deprecated_rfkill_attribute(attr->attr.name);
1351
1352 /* This is in the ABI... */
1353 if (tpacpi_rfk_check_hwblock_state()) {
1354 status = TPACPI_RFK_RADIO_OFF;
1355 } else {
1356 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1357 if (status < 0)
1358 return status;
1359 }
1360
1361 return snprintf(buf, PAGE_SIZE, "%d\n",
1362 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1363}
1364
1365static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1366 struct device_attribute *attr,
1367 const char *buf, size_t count)
1368{
1369 unsigned long t;
1370 int res;
1371
1372 printk_deprecated_rfkill_attribute(attr->attr.name);
1373
1374 if (parse_strtoul(buf, 1, &t))
1375 return -EINVAL;
1376
1377 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1378
1379 /* This is in the ABI... */
1380 if (tpacpi_rfk_check_hwblock_state() && !!t)
1381 return -EPERM;
1382
1383 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1384 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1385 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1386
1387 return (res < 0) ? res : count;
1388}
1389
1390/* procfs -------------------------------------------------------------- */
1391static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1392{
1393 if (id >= TPACPI_RFK_SW_MAX)
1394 seq_printf(m, "status:\t\tnot supported\n");
1395 else {
1396 int status;
1397
1398 /* This is in the ABI... */
1399 if (tpacpi_rfk_check_hwblock_state()) {
1400 status = TPACPI_RFK_RADIO_OFF;
1401 } else {
1402 status = tpacpi_rfk_update_swstate(
1403 tpacpi_rfkill_switches[id]);
1404 if (status < 0)
1405 return status;
1406 }
1407
1408 seq_printf(m, "status:\t\t%s\n",
1409 (status == TPACPI_RFK_RADIO_ON) ?
1410 "enabled" : "disabled");
1411 seq_printf(m, "commands:\tenable, disable\n");
1412 }
1413
1414 return 0;
1415}
1416
1417static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1418{
1419 char *cmd;
1420 int status = -1;
1421 int res = 0;
1422
1423 if (id >= TPACPI_RFK_SW_MAX)
1424 return -ENODEV;
1425
1426 while ((cmd = next_cmd(&buf))) {
1427 if (strlencmp(cmd, "enable") == 0)
1428 status = TPACPI_RFK_RADIO_ON;
1429 else if (strlencmp(cmd, "disable") == 0)
1430 status = TPACPI_RFK_RADIO_OFF;
1431 else
1432 return -EINVAL;
1433 }
1434
1435 if (status != -1) {
1436 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1437 (status == TPACPI_RFK_RADIO_ON) ?
1438 "enable" : "disable",
1439 tpacpi_rfkill_names[id]);
1440 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1441 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1442 }
1443
1444 return res;
1445}
1446
1447/*************************************************************************
1448 * thinkpad-acpi driver attributes
1449 */
1450
1451/* interface_version --------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001452static ssize_t interface_version_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001453{
1454 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1455}
Googler9398cc32022-12-02 17:21:52 +08001456static DRIVER_ATTR_RO(interface_version);
Googleraf606d22022-10-26 21:40:12 -07001457
1458/* debug_level --------------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001459static ssize_t debug_level_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001460{
1461 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1462}
1463
Googler9398cc32022-12-02 17:21:52 +08001464static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1465 size_t count)
Googleraf606d22022-10-26 21:40:12 -07001466{
1467 unsigned long t;
1468
1469 if (parse_strtoul(buf, 0xffff, &t))
1470 return -EINVAL;
1471
1472 dbg_level = t;
1473
1474 return count;
1475}
Googler9398cc32022-12-02 17:21:52 +08001476static DRIVER_ATTR_RW(debug_level);
Googleraf606d22022-10-26 21:40:12 -07001477
1478/* version ------------------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001479static ssize_t version_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001480{
1481 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1482 TPACPI_DESC, TPACPI_VERSION);
1483}
Googler9398cc32022-12-02 17:21:52 +08001484static DRIVER_ATTR_RO(version);
Googleraf606d22022-10-26 21:40:12 -07001485
1486/* --------------------------------------------------------------------- */
1487
1488#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489
1490/* wlsw_emulstate ------------------------------------------------------ */
Googler9398cc32022-12-02 17:21:52 +08001491static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001492{
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1494}
1495
Googler9398cc32022-12-02 17:21:52 +08001496static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1497 size_t count)
Googleraf606d22022-10-26 21:40:12 -07001498{
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1503
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1507 }
1508
1509 return count;
1510}
Googler9398cc32022-12-02 17:21:52 +08001511static DRIVER_ATTR_RW(wlsw_emulstate);
Googleraf606d22022-10-26 21:40:12 -07001512
1513/* bluetooth_emulstate ------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001514static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001515{
1516 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1517}
1518
Googler9398cc32022-12-02 17:21:52 +08001519static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1520 const char *buf, size_t count)
Googleraf606d22022-10-26 21:40:12 -07001521{
1522 unsigned long t;
1523
1524 if (parse_strtoul(buf, 1, &t))
1525 return -EINVAL;
1526
1527 tpacpi_bluetooth_emulstate = !!t;
1528
1529 return count;
1530}
Googler9398cc32022-12-02 17:21:52 +08001531static DRIVER_ATTR_RW(bluetooth_emulstate);
Googleraf606d22022-10-26 21:40:12 -07001532
1533/* wwan_emulstate ------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001534static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001535{
1536 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1537}
1538
Googler9398cc32022-12-02 17:21:52 +08001539static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1540 size_t count)
Googleraf606d22022-10-26 21:40:12 -07001541{
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}
Googler9398cc32022-12-02 17:21:52 +08001551static DRIVER_ATTR_RW(wwan_emulstate);
Googleraf606d22022-10-26 21:40:12 -07001552
1553/* uwb_emulstate ------------------------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08001554static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07001555{
1556 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1557}
1558
Googler9398cc32022-12-02 17:21:52 +08001559static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1560 size_t count)
Googleraf606d22022-10-26 21:40:12 -07001561{
1562 unsigned long t;
1563
1564 if (parse_strtoul(buf, 1, &t))
1565 return -EINVAL;
1566
1567 tpacpi_uwb_emulstate = !!t;
1568
1569 return count;
1570}
Googler9398cc32022-12-02 17:21:52 +08001571static DRIVER_ATTR_RW(uwb_emulstate);
Googleraf606d22022-10-26 21:40:12 -07001572#endif
1573
1574/* --------------------------------------------------------------------- */
1575
1576static struct driver_attribute *tpacpi_driver_attributes[] = {
1577 &driver_attr_debug_level, &driver_attr_version,
1578 &driver_attr_interface_version,
1579};
1580
1581static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1582{
1583 int i, res;
1584
1585 i = 0;
1586 res = 0;
1587 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1588 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1589 i++;
1590 }
1591
1592#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1593 if (!res && dbg_wlswemul)
1594 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1595 if (!res && dbg_bluetoothemul)
1596 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1597 if (!res && dbg_wwanemul)
1598 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1599 if (!res && dbg_uwbemul)
1600 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1601#endif
1602
1603 return res;
1604}
1605
1606static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1607{
1608 int i;
1609
1610 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1611 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1612
1613#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1614 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1615 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1616 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1617 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1618#endif
1619}
1620
1621/*************************************************************************
1622 * Firmware Data
1623 */
1624
1625/*
1626 * Table of recommended minimum BIOS versions
1627 *
1628 * Reasons for listing:
1629 * 1. Stable BIOS, listed because the unknown amount of
1630 * bugs and bad ACPI behaviour on older versions
1631 *
1632 * 2. BIOS or EC fw with known bugs that trigger on Linux
1633 *
1634 * 3. BIOS with known reduced functionality in older versions
1635 *
1636 * We recommend the latest BIOS and EC version.
1637 * We only support the latest BIOS and EC fw version as a rule.
1638 *
1639 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1640 * Information from users in ThinkWiki
1641 *
1642 * WARNING: we use this table also to detect that the machine is
1643 * a ThinkPad in some cases, so don't remove entries lightly.
1644 */
1645
1646#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1647 { .vendor = (__v), \
1648 .bios = TPID(__id1, __id2), \
1649 .ec = TPACPI_MATCH_ANY, \
Googler9398cc32022-12-02 17:21:52 +08001650 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1651 | TPVER(__bv1, __bv2) }
Googleraf606d22022-10-26 21:40:12 -07001652
1653#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1654 __eid, __ev1, __ev2) \
1655 { .vendor = (__v), \
1656 .bios = TPID(__bid1, __bid2), \
1657 .ec = __eid, \
Googler9398cc32022-12-02 17:21:52 +08001658 .quirks = TPVER(__ev1, __ev2) << 16 \
1659 | TPVER(__bv1, __bv2) }
Googleraf606d22022-10-26 21:40:12 -07001660
1661#define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1662 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1663
1664/* Outdated IBM BIOSes often lack the EC id string */
1665#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1666 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1667 __bv1, __bv2, TPID(__id1, __id2), \
1668 __ev1, __ev2), \
1669 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1670 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1671 __ev1, __ev2)
1672
1673/* Outdated IBM BIOSes often lack the EC id string */
1674#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1675 __eid1, __eid2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1677 __bv1, __bv2, TPID(__eid1, __eid2), \
1678 __ev1, __ev2), \
1679 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1680 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1681 __ev1, __ev2)
1682
1683#define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1684 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1685
1686#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2)
1690
1691#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1692 __eid1, __eid2, __ev1, __ev2) \
1693 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1694 __bv1, __bv2, TPID(__eid1, __eid2), \
1695 __ev1, __ev2)
1696
1697static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1698 /* Numeric models ------------------ */
1699 /* FW MODEL BIOS VERS */
1700 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1701 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1702 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1703 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1704 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1705 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1706 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1707 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1708 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1709
1710 /* A-series ------------------------- */
1711 /* FW MODEL BIOS VERS EC VERS */
1712 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1713 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1714 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1715 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1716 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1717 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1718 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1719 TPV_QI0('1', '3', '2', '0'), /* A22m */
1720 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1721 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1722 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1723
1724 /* G-series ------------------------- */
1725 /* FW MODEL BIOS VERS */
1726 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1727 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1728
1729 /* R-series, T-series --------------- */
1730 /* FW MODEL BIOS VERS EC VERS */
1731 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1732 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1733 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1734 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1735 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1736 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1737 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1738 T40/p, T41/p, T42/p (1) */
1739 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1740 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1741 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1742 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1743
1744 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1745 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1746 TPV_QI0('1', '6', '3', '2'), /* T22 */
1747 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1748 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1749 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1750
1751 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1752 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1753 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1754
1755 /* BIOS FW BIOS VERS EC FW EC VERS */
1756 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1757 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1758
1759 /* X-series ------------------------- */
1760 /* FW MODEL BIOS VERS EC VERS */
1761 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1762 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1763 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1764 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1765 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1766 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1767 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1768
1769 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1770 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1771
1772 /* (0) - older versions lack DMI EC fw string and functionality */
1773 /* (1) - older versions known to lack functionality */
1774};
1775
1776#undef TPV_QL1
1777#undef TPV_QL0
1778#undef TPV_QI2
1779#undef TPV_QI1
1780#undef TPV_QI0
1781#undef TPV_Q_X
1782#undef TPV_Q
1783
1784static void __init tpacpi_check_outdated_fw(void)
1785{
1786 unsigned long fwvers;
1787 u16 ec_version, bios_version;
1788
1789 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1790 ARRAY_SIZE(tpacpi_bios_version_qtable));
1791
1792 if (!fwvers)
1793 return;
1794
1795 bios_version = fwvers & 0xffffU;
1796 ec_version = (fwvers >> 16) & 0xffffU;
1797
1798 /* note that unknown versions are set to 0x0000 and we use that */
1799 if ((bios_version > thinkpad_id.bios_release) ||
1800 (ec_version > thinkpad_id.ec_release &&
Googler9398cc32022-12-02 17:21:52 +08001801 ec_version != TPACPI_MATCH_ANY_VERSION)) {
Googleraf606d22022-10-26 21:40:12 -07001802 /*
1803 * The changelogs would let us track down the exact
1804 * reason, but it is just too much of a pain to track
1805 * it. We only list BIOSes that are either really
1806 * broken, or really stable to begin with, so it is
1807 * best if the user upgrades the firmware anyway.
1808 */
1809 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
Googler9398cc32022-12-02 17:21:52 +08001810 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
Googleraf606d22022-10-26 21:40:12 -07001811 }
1812}
1813
1814static bool __init tpacpi_is_fw_known(void)
1815{
1816 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1817 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1818}
1819
1820/****************************************************************************
1821 ****************************************************************************
1822 *
1823 * Subdrivers
1824 *
1825 ****************************************************************************
1826 ****************************************************************************/
1827
1828/*************************************************************************
1829 * thinkpad-acpi metadata subdriver
1830 */
1831
1832static int thinkpad_acpi_driver_read(struct seq_file *m)
1833{
1834 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1835 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1836 return 0;
1837}
1838
1839static struct ibm_struct thinkpad_acpi_driver_data = {
1840 .name = "driver",
1841 .read = thinkpad_acpi_driver_read,
1842};
1843
1844/*************************************************************************
1845 * Hotkey subdriver
1846 */
1847
1848/*
1849 * ThinkPad firmware event model
1850 *
1851 * The ThinkPad firmware has two main event interfaces: normal ACPI
1852 * notifications (which follow the ACPI standard), and a private event
1853 * interface.
1854 *
1855 * The private event interface also issues events for the hotkeys. As
1856 * the driver gained features, the event handling code ended up being
1857 * built around the hotkey subdriver. This will need to be refactored
1858 * to a more formal event API eventually.
1859 *
1860 * Some "hotkeys" are actually supposed to be used as event reports,
1861 * such as "brightness has changed", "volume has changed", depending on
1862 * the ThinkPad model and how the firmware is operating.
1863 *
1864 * Unlike other classes, hotkey-class events have mask/unmask control on
1865 * non-ancient firmware. However, how it behaves changes a lot with the
1866 * firmware model and version.
1867 */
1868
1869enum { /* hot key scan codes (derived from ACPI DSDT) */
1870 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1871 TP_ACPI_HOTKEYSCAN_FNF2,
1872 TP_ACPI_HOTKEYSCAN_FNF3,
1873 TP_ACPI_HOTKEYSCAN_FNF4,
1874 TP_ACPI_HOTKEYSCAN_FNF5,
1875 TP_ACPI_HOTKEYSCAN_FNF6,
1876 TP_ACPI_HOTKEYSCAN_FNF7,
1877 TP_ACPI_HOTKEYSCAN_FNF8,
1878 TP_ACPI_HOTKEYSCAN_FNF9,
1879 TP_ACPI_HOTKEYSCAN_FNF10,
1880 TP_ACPI_HOTKEYSCAN_FNF11,
1881 TP_ACPI_HOTKEYSCAN_FNF12,
1882 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1883 TP_ACPI_HOTKEYSCAN_FNINSERT,
1884 TP_ACPI_HOTKEYSCAN_FNDELETE,
1885 TP_ACPI_HOTKEYSCAN_FNHOME,
1886 TP_ACPI_HOTKEYSCAN_FNEND,
1887 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1888 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1889 TP_ACPI_HOTKEYSCAN_FNSPACE,
1890 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1891 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1892 TP_ACPI_HOTKEYSCAN_MUTE,
1893 TP_ACPI_HOTKEYSCAN_THINKPAD,
1894 TP_ACPI_HOTKEYSCAN_UNK1,
1895 TP_ACPI_HOTKEYSCAN_UNK2,
1896 TP_ACPI_HOTKEYSCAN_UNK3,
1897 TP_ACPI_HOTKEYSCAN_UNK4,
1898 TP_ACPI_HOTKEYSCAN_UNK5,
1899 TP_ACPI_HOTKEYSCAN_UNK6,
1900 TP_ACPI_HOTKEYSCAN_UNK7,
1901 TP_ACPI_HOTKEYSCAN_UNK8,
1902
Googler9398cc32022-12-02 17:21:52 +08001903 /* Adaptive keyboard keycodes */
1904 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1905 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
Googleraf606d22022-10-26 21:40:12 -07001906 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1907 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1908 TP_ACPI_HOTKEYSCAN_CLOUD,
1909 TP_ACPI_HOTKEYSCAN_UNK9,
1910 TP_ACPI_HOTKEYSCAN_VOICE,
1911 TP_ACPI_HOTKEYSCAN_UNK10,
1912 TP_ACPI_HOTKEYSCAN_GESTURES,
1913 TP_ACPI_HOTKEYSCAN_UNK11,
1914 TP_ACPI_HOTKEYSCAN_UNK12,
1915 TP_ACPI_HOTKEYSCAN_UNK13,
1916 TP_ACPI_HOTKEYSCAN_CONFIG,
1917 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1918 TP_ACPI_HOTKEYSCAN_RELOAD,
1919 TP_ACPI_HOTKEYSCAN_BACK,
1920 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1921 TP_ACPI_HOTKEYSCAN_MIC_UP,
1922 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1923 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1924 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1925
Googler9398cc32022-12-02 17:21:52 +08001926 /* Lenovo extended keymap, starting at 0x1300 */
1927 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1928 /* first new observed key (star, favorites) is 0x1311 */
1929 TP_ACPI_HOTKEYSCAN_STAR = 69,
1930 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1931 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1932 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1933 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1934
Googleraf606d22022-10-26 21:40:12 -07001935 /* Hotkey keymap size */
1936 TPACPI_HOTKEY_MAP_LEN
1937};
1938
1939enum { /* Keys/events available through NVRAM polling */
1940 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1941 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1942};
1943
1944enum { /* Positions of some of the keys in hotkey masks */
1945 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1948 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1949 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
Googler9398cc32022-12-02 17:21:52 +08001950 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
Googleraf606d22022-10-26 21:40:12 -07001951 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1952 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1953 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1954 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1955 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1956};
1957
1958enum { /* NVRAM to ACPI HKEY group map */
1959 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1960 TP_ACPI_HKEY_ZOOM_MASK |
1961 TP_ACPI_HKEY_DISPSWTCH_MASK |
1962 TP_ACPI_HKEY_HIBERNATE_MASK,
1963 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1964 TP_ACPI_HKEY_BRGHTDWN_MASK,
1965 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1966 TP_ACPI_HKEY_VOLDWN_MASK |
1967 TP_ACPI_HKEY_MUTE_MASK,
1968};
1969
1970#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971struct tp_nvram_state {
1972 u16 thinkpad_toggle:1;
1973 u16 zoom_toggle:1;
1974 u16 display_toggle:1;
1975 u16 thinklight_toggle:1;
1976 u16 hibernate_toggle:1;
1977 u16 displayexp_toggle:1;
1978 u16 display_state:1;
1979 u16 brightness_toggle:1;
1980 u16 volume_toggle:1;
1981 u16 mute:1;
1982
1983 u8 brightness_level;
1984 u8 volume_level;
1985};
1986
1987/* kthread for the hotkey poller */
1988static struct task_struct *tpacpi_hotkey_task;
1989
1990/*
1991 * Acquire mutex to write poller control variables as an
1992 * atomic block.
1993 *
1994 * Increment hotkey_config_change when changing them if you
1995 * want the kthread to forget old state.
1996 *
1997 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1998 */
1999static struct mutex hotkey_thread_data_mutex;
2000static unsigned int hotkey_config_change;
2001
2002/*
2003 * hotkey poller control variables
2004 *
2005 * Must be atomic or readers will also need to acquire mutex
2006 *
2007 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2008 * should be used only when the changes need to be taken as
2009 * a block, OR when one needs to force the kthread to forget
2010 * old state.
2011 */
2012static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2013static unsigned int hotkey_poll_freq = 10; /* Hz */
2014
2015#define HOTKEY_CONFIG_CRITICAL_START \
2016 do { \
2017 mutex_lock(&hotkey_thread_data_mutex); \
2018 hotkey_config_change++; \
2019 } while (0);
2020#define HOTKEY_CONFIG_CRITICAL_END \
2021 mutex_unlock(&hotkey_thread_data_mutex);
2022
2023#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2024
2025#define hotkey_source_mask 0U
2026#define HOTKEY_CONFIG_CRITICAL_START
2027#define HOTKEY_CONFIG_CRITICAL_END
2028
2029#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2030
2031static struct mutex hotkey_mutex;
2032
2033static enum { /* Reasons for waking up */
2034 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2035 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2036 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2037} hotkey_wakeup_reason;
2038
2039static int hotkey_autosleep_ack;
2040
2041static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2042static u32 hotkey_all_mask; /* all events supported in fw */
2043static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2044static u32 hotkey_reserved_mask; /* events better left disabled */
2045static u32 hotkey_driver_mask; /* events needed by the driver */
2046static u32 hotkey_user_mask; /* events visible to userspace */
2047static u32 hotkey_acpi_mask; /* events enabled in firmware */
2048
2049static u16 *hotkey_keycode_map;
2050
2051static struct attribute_set *hotkey_dev_attributes;
2052
2053static void tpacpi_driver_event(const unsigned int hkey_event);
2054static void hotkey_driver_event(const unsigned int scancode);
2055static void hotkey_poll_setup(const bool may_warn);
2056
2057/* HKEY.MHKG() return bits */
2058#define TP_HOTKEY_TABLET_MASK (1 << 3)
Googler9398cc32022-12-02 17:21:52 +08002059enum {
2060 TP_ACPI_MULTI_MODE_INVALID = 0,
2061 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2062 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2063 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2064 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2065 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2066 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2067 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2068};
2069
2070enum {
2071 /* The following modes are considered tablet mode for the purpose of
2072 * reporting the status to userspace. i.e. in all these modes it makes
2073 * sense to disable the laptop input devices such as touchpad and
2074 * keyboard.
2075 */
2076 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2077 TP_ACPI_MULTI_MODE_STAND |
2078 TP_ACPI_MULTI_MODE_TENT |
2079 TP_ACPI_MULTI_MODE_STAND_TENT,
2080};
Googleraf606d22022-10-26 21:40:12 -07002081
2082static int hotkey_get_wlsw(void)
2083{
2084 int status;
2085
2086 if (!tp_features.hotkey_wlsw)
2087 return -ENODEV;
2088
2089#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2090 if (dbg_wlswemul)
2091 return (tpacpi_wlsw_emulstate) ?
2092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2093#endif
2094
2095 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2096 return -EIO;
2097
2098 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2099}
2100
Googler9398cc32022-12-02 17:21:52 +08002101static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2102{
2103 int type = (s >> 16) & 0xffff;
2104 int value = s & 0xffff;
2105 int mode = TP_ACPI_MULTI_MODE_INVALID;
2106 int valid_modes = 0;
2107
2108 if (has_tablet_mode)
2109 *has_tablet_mode = 0;
2110
2111 switch (type) {
2112 case 1:
2113 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2114 TP_ACPI_MULTI_MODE_TABLET |
2115 TP_ACPI_MULTI_MODE_STAND_TENT;
2116 break;
2117 case 2:
2118 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2119 TP_ACPI_MULTI_MODE_FLAT |
2120 TP_ACPI_MULTI_MODE_TABLET |
2121 TP_ACPI_MULTI_MODE_STAND |
2122 TP_ACPI_MULTI_MODE_TENT;
2123 break;
2124 case 3:
2125 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2126 TP_ACPI_MULTI_MODE_FLAT;
2127 break;
2128 case 4:
2129 case 5:
2130 /* In mode 4, FLAT is not specified as a valid mode. However,
2131 * it can be seen at least on the X1 Yoga 2nd Generation.
2132 */
2133 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2134 TP_ACPI_MULTI_MODE_FLAT |
2135 TP_ACPI_MULTI_MODE_TABLET |
2136 TP_ACPI_MULTI_MODE_STAND |
2137 TP_ACPI_MULTI_MODE_TENT;
2138 break;
2139 default:
2140 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2141 type, value, TPACPI_MAIL);
2142 return 0;
2143 }
2144
2145 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2146 *has_tablet_mode = 1;
2147
2148 switch (value) {
2149 case 1:
2150 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2151 break;
2152 case 2:
2153 mode = TP_ACPI_MULTI_MODE_FLAT;
2154 break;
2155 case 3:
2156 mode = TP_ACPI_MULTI_MODE_TABLET;
2157 break;
2158 case 4:
2159 if (type == 1)
2160 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2161 else
2162 mode = TP_ACPI_MULTI_MODE_STAND;
2163 break;
2164 case 5:
2165 mode = TP_ACPI_MULTI_MODE_TENT;
2166 break;
2167 default:
2168 if (type == 5 && value == 0xffff) {
2169 pr_warn("Multi mode status is undetected, assuming laptop\n");
2170 return 0;
2171 }
2172 }
2173
2174 if (!(mode & valid_modes)) {
2175 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2176 value, type, TPACPI_MAIL);
2177 return 0;
2178 }
2179
2180 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2181}
2182
Googleraf606d22022-10-26 21:40:12 -07002183static int hotkey_get_tablet_mode(int *status)
2184{
2185 int s;
2186
Googler9398cc32022-12-02 17:21:52 +08002187 switch (tp_features.hotkey_tablet) {
2188 case TP_HOTKEY_TABLET_USES_MHKG:
2189 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2190 return -EIO;
Googleraf606d22022-10-26 21:40:12 -07002191
Googler9398cc32022-12-02 17:21:52 +08002192 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2193 break;
2194 case TP_HOTKEY_TABLET_USES_GMMS:
2195 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2196 return -EIO;
2197
2198 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2199 break;
2200 default:
2201 break;
2202 }
2203
Googleraf606d22022-10-26 21:40:12 -07002204 return 0;
2205}
2206
2207/*
2208 * Reads current event mask from firmware, and updates
2209 * hotkey_acpi_mask accordingly. Also resets any bits
2210 * from hotkey_user_mask that are unavailable to be
2211 * delivered (shadow requirement of the userspace ABI).
2212 *
2213 * Call with hotkey_mutex held
2214 */
2215static int hotkey_mask_get(void)
2216{
2217 if (tp_features.hotkey_mask) {
2218 u32 m = 0;
2219
2220 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2221 return -EIO;
2222
2223 hotkey_acpi_mask = m;
2224 } else {
2225 /* no mask support doesn't mean no event support... */
2226 hotkey_acpi_mask = hotkey_all_mask;
2227 }
2228
2229 /* sync userspace-visible mask */
2230 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2231
2232 return 0;
2233}
2234
2235static void hotkey_mask_warn_incomplete_mask(void)
2236{
2237 /* log only what the user can fix... */
2238 const u32 wantedmask = hotkey_driver_mask &
2239 ~(hotkey_acpi_mask | hotkey_source_mask) &
2240 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2241
2242 if (wantedmask)
2243 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2244}
2245
2246/*
2247 * Set the firmware mask when supported
2248 *
2249 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2250 *
2251 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2252 *
2253 * Call with hotkey_mutex held
2254 */
2255static int hotkey_mask_set(u32 mask)
2256{
2257 int i;
2258 int rc = 0;
2259
2260 const u32 fwmask = mask & ~hotkey_source_mask;
2261
2262 if (tp_features.hotkey_mask) {
2263 for (i = 0; i < 32; i++) {
2264 if (!acpi_evalf(hkey_handle,
2265 NULL, "MHKM", "vdd", i + 1,
2266 !!(mask & (1 << i)))) {
2267 rc = -EIO;
2268 break;
2269 }
2270 }
2271 }
2272
2273 /*
2274 * We *must* make an inconditional call to hotkey_mask_get to
2275 * refresh hotkey_acpi_mask and update hotkey_user_mask
2276 *
2277 * Take the opportunity to also log when we cannot _enable_
2278 * a given event.
2279 */
2280 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
Googler9398cc32022-12-02 17:21:52 +08002281 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
Googleraf606d22022-10-26 21:40:12 -07002282 fwmask, hotkey_acpi_mask);
2283 }
2284
2285 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2286 hotkey_mask_warn_incomplete_mask();
2287
2288 return rc;
2289}
2290
2291/*
2292 * Sets hotkey_user_mask and tries to set the firmware mask
2293 *
2294 * Call with hotkey_mutex held
2295 */
2296static int hotkey_user_mask_set(const u32 mask)
2297{
2298 int rc;
2299
2300 /* Give people a chance to notice they are doing something that
2301 * is bound to go boom on their users sooner or later */
2302 if (!tp_warned.hotkey_mask_ff &&
2303 (mask == 0xffff || mask == 0xffffff ||
2304 mask == 0xffffffff)) {
2305 tp_warned.hotkey_mask_ff = 1;
Googler9398cc32022-12-02 17:21:52 +08002306 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2307 mask);
2308 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
Googleraf606d22022-10-26 21:40:12 -07002309 }
2310
2311 /* Try to enable what the user asked for, plus whatever we need.
2312 * this syncs everything but won't enable bits in hotkey_user_mask */
2313 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2314
2315 /* Enable the available bits in hotkey_user_mask */
2316 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2317
2318 return rc;
2319}
2320
2321/*
2322 * Sets the driver hotkey mask.
2323 *
2324 * Can be called even if the hotkey subdriver is inactive
2325 */
2326static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2327{
2328 int rc;
2329
2330 /* Do the right thing if hotkey_init has not been called yet */
2331 if (!tp_features.hotkey) {
2332 hotkey_driver_mask = mask;
2333 return 0;
2334 }
2335
2336 mutex_lock(&hotkey_mutex);
2337
2338 HOTKEY_CONFIG_CRITICAL_START
2339 hotkey_driver_mask = mask;
2340#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2341 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2342#endif
2343 HOTKEY_CONFIG_CRITICAL_END
2344
2345 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2346 ~hotkey_source_mask);
2347 hotkey_poll_setup(true);
2348
2349 mutex_unlock(&hotkey_mutex);
2350
2351 return rc;
2352}
2353
2354static int hotkey_status_get(int *status)
2355{
2356 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2357 return -EIO;
2358
2359 return 0;
2360}
2361
2362static int hotkey_status_set(bool enable)
2363{
2364 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2365 return -EIO;
2366
2367 return 0;
2368}
2369
2370static void tpacpi_input_send_tabletsw(void)
2371{
2372 int state;
2373
2374 if (tp_features.hotkey_tablet &&
2375 !hotkey_get_tablet_mode(&state)) {
2376 mutex_lock(&tpacpi_inputdev_send_mutex);
2377
2378 input_report_switch(tpacpi_inputdev,
2379 SW_TABLET_MODE, !!state);
2380 input_sync(tpacpi_inputdev);
2381
2382 mutex_unlock(&tpacpi_inputdev_send_mutex);
2383 }
2384}
2385
2386/* Do NOT call without validating scancode first */
2387static void tpacpi_input_send_key(const unsigned int scancode)
2388{
2389 const unsigned int keycode = hotkey_keycode_map[scancode];
2390
2391 if (keycode != KEY_RESERVED) {
2392 mutex_lock(&tpacpi_inputdev_send_mutex);
2393
2394 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2395 input_report_key(tpacpi_inputdev, keycode, 1);
2396 input_sync(tpacpi_inputdev);
2397
2398 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2399 input_report_key(tpacpi_inputdev, keycode, 0);
2400 input_sync(tpacpi_inputdev);
2401
2402 mutex_unlock(&tpacpi_inputdev_send_mutex);
2403 }
2404}
2405
2406/* Do NOT call without validating scancode first */
2407static void tpacpi_input_send_key_masked(const unsigned int scancode)
2408{
2409 hotkey_driver_event(scancode);
2410 if (hotkey_user_mask & (1 << scancode))
2411 tpacpi_input_send_key(scancode);
2412}
2413
2414#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2415static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2416
2417/* Do NOT call without validating scancode first */
2418static void tpacpi_hotkey_send_key(unsigned int scancode)
2419{
2420 tpacpi_input_send_key_masked(scancode);
2421}
2422
2423static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2424{
2425 u8 d;
2426
2427 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2428 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2429 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2430 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2431 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2432 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2433 }
Googler9398cc32022-12-02 17:21:52 +08002434 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
Googleraf606d22022-10-26 21:40:12 -07002435 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2436 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2437 }
2438 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2439 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2440 n->displayexp_toggle =
2441 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2442 }
2443 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2444 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2445 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2446 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2447 n->brightness_toggle =
2448 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2449 }
2450 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2451 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2452 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2453 >> TP_NVRAM_POS_LEVEL_VOLUME;
2454 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2455 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2456 }
2457}
2458
2459#define TPACPI_COMPARE_KEY(__scancode, __member) \
2460do { \
2461 if ((event_mask & (1 << __scancode)) && \
2462 oldn->__member != newn->__member) \
2463 tpacpi_hotkey_send_key(__scancode); \
2464} while (0)
2465
2466#define TPACPI_MAY_SEND_KEY(__scancode) \
2467do { \
2468 if (event_mask & (1 << __scancode)) \
2469 tpacpi_hotkey_send_key(__scancode); \
2470} while (0)
2471
2472static void issue_volchange(const unsigned int oldvol,
2473 const unsigned int newvol,
2474 const u32 event_mask)
2475{
2476 unsigned int i = oldvol;
2477
2478 while (i > newvol) {
2479 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2480 i--;
2481 }
2482 while (i < newvol) {
2483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2484 i++;
2485 }
2486}
2487
2488static void issue_brightnesschange(const unsigned int oldbrt,
2489 const unsigned int newbrt,
2490 const u32 event_mask)
2491{
2492 unsigned int i = oldbrt;
2493
2494 while (i > newbrt) {
2495 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2496 i--;
2497 }
2498 while (i < newbrt) {
2499 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2500 i++;
2501 }
2502}
2503
2504static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2505 struct tp_nvram_state *newn,
2506 const u32 event_mask)
2507{
2508
2509 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2510 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2511 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2512 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2513
2514 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2515
2516 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2517
2518 /*
2519 * Handle volume
2520 *
2521 * This code is supposed to duplicate the IBM firmware behaviour:
2522 * - Pressing MUTE issues mute hotkey message, even when already mute
2523 * - Pressing Volume up/down issues volume up/down hotkey messages,
2524 * even when already at maximum or minimum volume
2525 * - The act of unmuting issues volume up/down notification,
2526 * depending which key was used to unmute
2527 *
2528 * We are constrained to what the NVRAM can tell us, which is not much
2529 * and certainly not enough if more than one volume hotkey was pressed
2530 * since the last poll cycle.
2531 *
2532 * Just to make our life interesting, some newer Lenovo ThinkPads have
2533 * bugs in the BIOS and may fail to update volume_toggle properly.
2534 */
2535 if (newn->mute) {
2536 /* muted */
2537 if (!oldn->mute ||
2538 oldn->volume_toggle != newn->volume_toggle ||
2539 oldn->volume_level != newn->volume_level) {
2540 /* recently muted, or repeated mute keypress, or
2541 * multiple presses ending in mute */
2542 issue_volchange(oldn->volume_level, newn->volume_level,
2543 event_mask);
2544 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2545 }
2546 } else {
2547 /* unmute */
2548 if (oldn->mute) {
2549 /* recently unmuted, issue 'unmute' keypress */
2550 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2551 }
2552 if (oldn->volume_level != newn->volume_level) {
2553 issue_volchange(oldn->volume_level, newn->volume_level,
2554 event_mask);
2555 } else if (oldn->volume_toggle != newn->volume_toggle) {
2556 /* repeated vol up/down keypress at end of scale ? */
2557 if (newn->volume_level == 0)
2558 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2559 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2560 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2561 }
2562 }
2563
2564 /* handle brightness */
2565 if (oldn->brightness_level != newn->brightness_level) {
2566 issue_brightnesschange(oldn->brightness_level,
2567 newn->brightness_level, event_mask);
2568 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2569 /* repeated key presses that didn't change state */
2570 if (newn->brightness_level == 0)
2571 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2572 else if (newn->brightness_level >= bright_maxlvl
2573 && !tp_features.bright_unkfw)
2574 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2575 }
2576
2577#undef TPACPI_COMPARE_KEY
2578#undef TPACPI_MAY_SEND_KEY
2579}
2580
2581/*
2582 * Polling driver
2583 *
2584 * We track all events in hotkey_source_mask all the time, since
2585 * most of them are edge-based. We only issue those requested by
2586 * hotkey_user_mask or hotkey_driver_mask, though.
2587 */
2588static int hotkey_kthread(void *data)
2589{
2590 struct tp_nvram_state s[2] = { 0 };
2591 u32 poll_mask, event_mask;
2592 unsigned int si, so;
2593 unsigned long t;
2594 unsigned int change_detector;
2595 unsigned int poll_freq;
2596 bool was_frozen;
2597
2598 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2599 goto exit;
2600
2601 set_freezable();
2602
2603 so = 0;
2604 si = 1;
2605 t = 0;
2606
2607 /* Initial state for compares */
2608 mutex_lock(&hotkey_thread_data_mutex);
2609 change_detector = hotkey_config_change;
2610 poll_mask = hotkey_source_mask;
2611 event_mask = hotkey_source_mask &
2612 (hotkey_driver_mask | hotkey_user_mask);
2613 poll_freq = hotkey_poll_freq;
2614 mutex_unlock(&hotkey_thread_data_mutex);
2615 hotkey_read_nvram(&s[so], poll_mask);
2616
2617 while (!kthread_should_stop()) {
2618 if (t == 0) {
2619 if (likely(poll_freq))
2620 t = 1000/poll_freq;
2621 else
2622 t = 100; /* should never happen... */
2623 }
2624 t = msleep_interruptible(t);
2625 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2626 break;
2627
2628 if (t > 0 && !was_frozen)
2629 continue;
2630
2631 mutex_lock(&hotkey_thread_data_mutex);
2632 if (was_frozen || hotkey_config_change != change_detector) {
2633 /* forget old state on thaw or config change */
2634 si = so;
2635 t = 0;
2636 change_detector = hotkey_config_change;
2637 }
2638 poll_mask = hotkey_source_mask;
2639 event_mask = hotkey_source_mask &
2640 (hotkey_driver_mask | hotkey_user_mask);
2641 poll_freq = hotkey_poll_freq;
2642 mutex_unlock(&hotkey_thread_data_mutex);
2643
2644 if (likely(poll_mask)) {
2645 hotkey_read_nvram(&s[si], poll_mask);
2646 if (likely(si != so)) {
2647 hotkey_compare_and_issue_event(&s[so], &s[si],
2648 event_mask);
2649 }
2650 }
2651
2652 so = si;
2653 si ^= 1;
2654 }
2655
2656exit:
2657 return 0;
2658}
2659
2660/* call with hotkey_mutex held */
2661static void hotkey_poll_stop_sync(void)
2662{
2663 if (tpacpi_hotkey_task) {
2664 kthread_stop(tpacpi_hotkey_task);
2665 tpacpi_hotkey_task = NULL;
2666 }
2667}
2668
2669/* call with hotkey_mutex held */
2670static void hotkey_poll_setup(const bool may_warn)
2671{
2672 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2673 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2674
2675 if (hotkey_poll_freq > 0 &&
2676 (poll_driver_mask ||
2677 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2678 if (!tpacpi_hotkey_task) {
2679 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2680 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2681 if (IS_ERR(tpacpi_hotkey_task)) {
2682 tpacpi_hotkey_task = NULL;
Googler9398cc32022-12-02 17:21:52 +08002683 pr_err("could not create kernel thread for hotkey polling\n");
Googleraf606d22022-10-26 21:40:12 -07002684 }
2685 }
2686 } else {
2687 hotkey_poll_stop_sync();
2688 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2689 hotkey_poll_freq == 0) {
Googler9398cc32022-12-02 17:21:52 +08002690 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
Googleraf606d22022-10-26 21:40:12 -07002691 poll_user_mask, poll_driver_mask);
2692 }
2693 }
2694}
2695
2696static void hotkey_poll_setup_safe(const bool may_warn)
2697{
2698 mutex_lock(&hotkey_mutex);
2699 hotkey_poll_setup(may_warn);
2700 mutex_unlock(&hotkey_mutex);
2701}
2702
2703/* call with hotkey_mutex held */
2704static void hotkey_poll_set_freq(unsigned int freq)
2705{
2706 if (!freq)
2707 hotkey_poll_stop_sync();
2708
2709 hotkey_poll_freq = freq;
2710}
2711
2712#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2713
2714static void hotkey_poll_setup(const bool __unused)
2715{
2716}
2717
2718static void hotkey_poll_setup_safe(const bool __unused)
2719{
2720}
2721
2722#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2723
2724static int hotkey_inputdev_open(struct input_dev *dev)
2725{
2726 switch (tpacpi_lifecycle) {
2727 case TPACPI_LIFE_INIT:
2728 case TPACPI_LIFE_RUNNING:
2729 hotkey_poll_setup_safe(false);
2730 return 0;
2731 case TPACPI_LIFE_EXITING:
2732 return -EBUSY;
2733 }
2734
2735 /* Should only happen if tpacpi_lifecycle is corrupt */
2736 BUG();
2737 return -EBUSY;
2738}
2739
2740static void hotkey_inputdev_close(struct input_dev *dev)
2741{
2742 /* disable hotkey polling when possible */
2743 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2744 !(hotkey_source_mask & hotkey_driver_mask))
2745 hotkey_poll_setup_safe(false);
2746}
2747
2748/* sysfs hotkey enable ------------------------------------------------- */
2749static ssize_t hotkey_enable_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2752{
2753 int res, status;
2754
2755 printk_deprecated_attribute("hotkey_enable",
2756 "Hotkey reporting is always enabled");
2757
2758 res = hotkey_status_get(&status);
2759 if (res)
2760 return res;
2761
2762 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2763}
2764
2765static ssize_t hotkey_enable_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2768{
2769 unsigned long t;
2770
2771 printk_deprecated_attribute("hotkey_enable",
2772 "Hotkeys can be disabled through hotkey_mask");
2773
2774 if (parse_strtoul(buf, 1, &t))
2775 return -EINVAL;
2776
2777 if (t == 0)
2778 return -EPERM;
2779
2780 return count;
2781}
2782
2783static DEVICE_ATTR_RW(hotkey_enable);
2784
2785/* sysfs hotkey mask --------------------------------------------------- */
2786static ssize_t hotkey_mask_show(struct device *dev,
2787 struct device_attribute *attr,
2788 char *buf)
2789{
2790 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2791}
2792
2793static ssize_t hotkey_mask_store(struct device *dev,
2794 struct device_attribute *attr,
2795 const char *buf, size_t count)
2796{
2797 unsigned long t;
2798 int res;
2799
2800 if (parse_strtoul(buf, 0xffffffffUL, &t))
2801 return -EINVAL;
2802
2803 if (mutex_lock_killable(&hotkey_mutex))
2804 return -ERESTARTSYS;
2805
2806 res = hotkey_user_mask_set(t);
2807
2808#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2809 hotkey_poll_setup(true);
2810#endif
2811
2812 mutex_unlock(&hotkey_mutex);
2813
2814 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2815
2816 return (res) ? res : count;
2817}
2818
2819static DEVICE_ATTR_RW(hotkey_mask);
2820
2821/* sysfs hotkey bios_enabled ------------------------------------------- */
2822static ssize_t hotkey_bios_enabled_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2825{
2826 return sprintf(buf, "0\n");
2827}
2828
2829static DEVICE_ATTR_RO(hotkey_bios_enabled);
2830
2831/* sysfs hotkey bios_mask ---------------------------------------------- */
2832static ssize_t hotkey_bios_mask_show(struct device *dev,
2833 struct device_attribute *attr,
2834 char *buf)
2835{
2836 printk_deprecated_attribute("hotkey_bios_mask",
2837 "This attribute is useless.");
2838 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2839}
2840
2841static DEVICE_ATTR_RO(hotkey_bios_mask);
2842
2843/* sysfs hotkey all_mask ----------------------------------------------- */
2844static ssize_t hotkey_all_mask_show(struct device *dev,
2845 struct device_attribute *attr,
2846 char *buf)
2847{
2848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2849 hotkey_all_mask | hotkey_source_mask);
2850}
2851
2852static DEVICE_ATTR_RO(hotkey_all_mask);
2853
2854/* sysfs hotkey all_mask ----------------------------------------------- */
2855static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2856 struct device_attribute *attr,
2857 char *buf)
2858{
2859 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2860 hotkey_adaptive_all_mask | hotkey_source_mask);
2861}
2862
2863static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2864
2865/* sysfs hotkey recommended_mask --------------------------------------- */
2866static ssize_t hotkey_recommended_mask_show(struct device *dev,
2867 struct device_attribute *attr,
2868 char *buf)
2869{
2870 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2871 (hotkey_all_mask | hotkey_source_mask)
2872 & ~hotkey_reserved_mask);
2873}
2874
2875static DEVICE_ATTR_RO(hotkey_recommended_mask);
2876
2877#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2878
2879/* sysfs hotkey hotkey_source_mask ------------------------------------- */
2880static ssize_t hotkey_source_mask_show(struct device *dev,
2881 struct device_attribute *attr,
2882 char *buf)
2883{
2884 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2885}
2886
2887static ssize_t hotkey_source_mask_store(struct device *dev,
2888 struct device_attribute *attr,
2889 const char *buf, size_t count)
2890{
2891 unsigned long t;
2892 u32 r_ev;
2893 int rc;
2894
2895 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2896 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2897 return -EINVAL;
2898
2899 if (mutex_lock_killable(&hotkey_mutex))
2900 return -ERESTARTSYS;
2901
2902 HOTKEY_CONFIG_CRITICAL_START
2903 hotkey_source_mask = t;
2904 HOTKEY_CONFIG_CRITICAL_END
2905
2906 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2907 ~hotkey_source_mask);
2908 hotkey_poll_setup(true);
2909
2910 /* check if events needed by the driver got disabled */
2911 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2912 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2913
2914 mutex_unlock(&hotkey_mutex);
2915
2916 if (rc < 0)
Googler9398cc32022-12-02 17:21:52 +08002917 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
Googleraf606d22022-10-26 21:40:12 -07002918
2919 if (r_ev)
Googler9398cc32022-12-02 17:21:52 +08002920 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
Googleraf606d22022-10-26 21:40:12 -07002921 r_ev);
2922
2923 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2924
2925 return (rc < 0) ? rc : count;
2926}
2927
2928static DEVICE_ATTR_RW(hotkey_source_mask);
2929
2930/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2931static ssize_t hotkey_poll_freq_show(struct device *dev,
2932 struct device_attribute *attr,
2933 char *buf)
2934{
2935 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2936}
2937
2938static ssize_t hotkey_poll_freq_store(struct device *dev,
2939 struct device_attribute *attr,
2940 const char *buf, size_t count)
2941{
2942 unsigned long t;
2943
2944 if (parse_strtoul(buf, 25, &t))
2945 return -EINVAL;
2946
2947 if (mutex_lock_killable(&hotkey_mutex))
2948 return -ERESTARTSYS;
2949
2950 hotkey_poll_set_freq(t);
2951 hotkey_poll_setup(true);
2952
2953 mutex_unlock(&hotkey_mutex);
2954
2955 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2956
2957 return count;
2958}
2959
2960static DEVICE_ATTR_RW(hotkey_poll_freq);
2961
2962#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2963
2964/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2965static ssize_t hotkey_radio_sw_show(struct device *dev,
2966 struct device_attribute *attr,
2967 char *buf)
2968{
2969 int res;
2970 res = hotkey_get_wlsw();
2971 if (res < 0)
2972 return res;
2973
2974 /* Opportunistic update */
2975 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2976
2977 return snprintf(buf, PAGE_SIZE, "%d\n",
2978 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2979}
2980
2981static DEVICE_ATTR_RO(hotkey_radio_sw);
2982
2983static void hotkey_radio_sw_notify_change(void)
2984{
2985 if (tp_features.hotkey_wlsw)
2986 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2987 "hotkey_radio_sw");
2988}
2989
2990/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2991static ssize_t hotkey_tablet_mode_show(struct device *dev,
2992 struct device_attribute *attr,
2993 char *buf)
2994{
2995 int res, s;
2996 res = hotkey_get_tablet_mode(&s);
2997 if (res < 0)
2998 return res;
2999
3000 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3001}
3002
3003static DEVICE_ATTR_RO(hotkey_tablet_mode);
3004
3005static void hotkey_tablet_mode_notify_change(void)
3006{
3007 if (tp_features.hotkey_tablet)
3008 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3009 "hotkey_tablet_mode");
3010}
3011
3012/* sysfs wakeup reason (pollable) -------------------------------------- */
3013static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3014 struct device_attribute *attr,
3015 char *buf)
3016{
3017 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3018}
3019
3020static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3021
3022static void hotkey_wakeup_reason_notify_change(void)
3023{
3024 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3025 "wakeup_reason");
3026}
3027
3028/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3029static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3030 struct device_attribute *attr,
3031 char *buf)
3032{
3033 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3034}
3035
3036static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3037 hotkey_wakeup_hotunplug_complete_show, NULL);
3038
3039static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3040{
3041 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3042 "wakeup_hotunplug_complete");
3043}
3044
3045/* sysfs adaptive kbd mode --------------------------------------------- */
3046
3047static int adaptive_keyboard_get_mode(void);
3048static int adaptive_keyboard_set_mode(int new_mode);
3049
3050enum ADAPTIVE_KEY_MODE {
3051 HOME_MODE,
3052 WEB_BROWSER_MODE,
3053 WEB_CONFERENCE_MODE,
3054 FUNCTION_MODE,
3055 LAYFLAT_MODE
3056};
3057
3058static ssize_t adaptive_kbd_mode_show(struct device *dev,
3059 struct device_attribute *attr,
3060 char *buf)
3061{
3062 int current_mode;
3063
3064 current_mode = adaptive_keyboard_get_mode();
3065 if (current_mode < 0)
3066 return current_mode;
3067
3068 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3069}
3070
3071static ssize_t adaptive_kbd_mode_store(struct device *dev,
3072 struct device_attribute *attr,
3073 const char *buf, size_t count)
3074{
3075 unsigned long t;
3076 int res;
3077
3078 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3079 return -EINVAL;
3080
3081 res = adaptive_keyboard_set_mode(t);
3082 return (res < 0) ? res : count;
3083}
3084
3085static DEVICE_ATTR_RW(adaptive_kbd_mode);
3086
3087static struct attribute *adaptive_kbd_attributes[] = {
3088 &dev_attr_adaptive_kbd_mode.attr,
3089 NULL
3090};
3091
3092static const struct attribute_group adaptive_kbd_attr_group = {
3093 .attrs = adaptive_kbd_attributes,
3094};
3095
3096/* --------------------------------------------------------------------- */
3097
3098static struct attribute *hotkey_attributes[] __initdata = {
3099 &dev_attr_hotkey_enable.attr,
3100 &dev_attr_hotkey_bios_enabled.attr,
3101 &dev_attr_hotkey_bios_mask.attr,
3102 &dev_attr_wakeup_reason.attr,
3103 &dev_attr_wakeup_hotunplug_complete.attr,
3104 &dev_attr_hotkey_mask.attr,
3105 &dev_attr_hotkey_all_mask.attr,
3106 &dev_attr_hotkey_adaptive_all_mask.attr,
3107 &dev_attr_hotkey_recommended_mask.attr,
3108#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3109 &dev_attr_hotkey_source_mask.attr,
3110 &dev_attr_hotkey_poll_freq.attr,
3111#endif
3112};
3113
3114/*
3115 * Sync both the hw and sw blocking state of all switches
3116 */
3117static void tpacpi_send_radiosw_update(void)
3118{
3119 int wlsw;
3120
3121 /*
3122 * We must sync all rfkill controllers *before* issuing any
3123 * rfkill input events, or we will race the rfkill core input
3124 * handler.
3125 *
3126 * tpacpi_inputdev_send_mutex works as a synchronization point
3127 * for the above.
3128 *
3129 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3130 */
3131
3132 wlsw = hotkey_get_wlsw();
3133
3134 /* Sync hw blocking state first if it is hw-blocked */
3135 if (wlsw == TPACPI_RFK_RADIO_OFF)
3136 tpacpi_rfk_update_hwblock_state(true);
3137
Googlerb48fa912023-03-17 12:40:29 +05303138 /* Sync sw blocking state */
3139 tpacpi_rfk_update_swstate_all();
3140
Googleraf606d22022-10-26 21:40:12 -07003141 /* Sync hw blocking state last if it is hw-unblocked */
3142 if (wlsw == TPACPI_RFK_RADIO_ON)
3143 tpacpi_rfk_update_hwblock_state(false);
3144
3145 /* Issue rfkill input event for WLSW switch */
3146 if (!(wlsw < 0)) {
3147 mutex_lock(&tpacpi_inputdev_send_mutex);
3148
3149 input_report_switch(tpacpi_inputdev,
3150 SW_RFKILL_ALL, (wlsw > 0));
3151 input_sync(tpacpi_inputdev);
3152
3153 mutex_unlock(&tpacpi_inputdev_send_mutex);
3154 }
3155
3156 /*
3157 * this can be unconditional, as we will poll state again
3158 * if userspace uses the notify to read data
3159 */
3160 hotkey_radio_sw_notify_change();
3161}
3162
3163static void hotkey_exit(void)
3164{
3165#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3166 mutex_lock(&hotkey_mutex);
3167 hotkey_poll_stop_sync();
3168 mutex_unlock(&hotkey_mutex);
3169#endif
3170
3171 if (hotkey_dev_attributes)
3172 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3173
3174 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3175 "restoring original HKEY status and mask\n");
3176 /* yes, there is a bitwise or below, we want the
3177 * functions to be called even if one of them fail */
3178 if (((tp_features.hotkey_mask &&
3179 hotkey_mask_set(hotkey_orig_mask)) |
3180 hotkey_status_set(false)) != 0)
Googler9398cc32022-12-02 17:21:52 +08003181 pr_err("failed to restore hot key mask to BIOS defaults\n");
Googleraf606d22022-10-26 21:40:12 -07003182}
3183
3184static void __init hotkey_unmap(const unsigned int scancode)
3185{
3186 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3187 clear_bit(hotkey_keycode_map[scancode],
3188 tpacpi_inputdev->keybit);
3189 hotkey_keycode_map[scancode] = KEY_RESERVED;
3190 }
3191}
3192
3193/*
3194 * HKEY quirks:
3195 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3196 */
3197
3198#define TPACPI_HK_Q_INIMASK 0x0001
3199
3200static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3201 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3202 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3203 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3204 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3205 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3206 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3207 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3208 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3209 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3210 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3211 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3212 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3213 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3214 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3215 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3216 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3217 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3218 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3219 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3220};
3221
3222typedef u16 tpacpi_keymap_entry_t;
3223typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3224
Googler9398cc32022-12-02 17:21:52 +08003225static int hotkey_init_tablet_mode(void)
3226{
3227 int in_tablet_mode = 0, res;
3228 char *type = NULL;
3229
3230 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3231 int has_tablet_mode;
3232
3233 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3234 &has_tablet_mode);
3235 /*
3236 * The Yoga 11e series has 2 accelerometers described by a
3237 * BOSC0200 ACPI node. This setup relies on a Windows service
3238 * which calls special ACPI methods on this node to report
3239 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3240 * does not support this, so skip the hotkey on these models.
3241 */
3242 if (has_tablet_mode && !acpi_dev_present("BOSC0200", "1", -1))
3243 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3244 type = "GMMS";
3245 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3246 /* For X41t, X60t, X61t Tablets... */
3247 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3248 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3249 type = "MHKG";
3250 }
3251
3252 if (!tp_features.hotkey_tablet)
3253 return 0;
3254
3255 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3256 type, in_tablet_mode ? "tablet" : "laptop");
3257
3258 res = add_to_attr_set(hotkey_dev_attributes,
3259 &dev_attr_hotkey_tablet_mode.attr);
3260 if (res)
3261 return -1;
3262
3263 return in_tablet_mode;
3264}
3265
Googleraf606d22022-10-26 21:40:12 -07003266static int __init hotkey_init(struct ibm_init_struct *iibm)
3267{
3268 /* Requirements for changing the default keymaps:
3269 *
3270 * 1. Many of the keys are mapped to KEY_RESERVED for very
3271 * good reasons. Do not change them unless you have deep
3272 * knowledge on the IBM and Lenovo ThinkPad firmware for
3273 * the various ThinkPad models. The driver behaves
3274 * differently for KEY_RESERVED: such keys have their
3275 * hot key mask *unset* in mask_recommended, and also
3276 * in the initial hot key mask programmed into the
3277 * firmware at driver load time, which means the firm-
3278 * ware may react very differently if you change them to
3279 * something else;
3280 *
3281 * 2. You must be subscribed to the linux-thinkpad and
3282 * ibm-acpi-devel mailing lists, and you should read the
3283 * list archives since 2007 if you want to change the
3284 * keymaps. This requirement exists so that you will
3285 * know the past history of problems with the thinkpad-
3286 * acpi driver keymaps, and also that you will be
3287 * listening to any bug reports;
3288 *
3289 * 3. Do not send thinkpad-acpi specific patches directly to
3290 * for merging, *ever*. Send them to the linux-acpi
3291 * mailinglist for comments. Merging is to be done only
3292 * through acpi-test and the ACPI maintainer.
3293 *
3294 * If the above is too much to ask, don't change the keymap.
3295 * Ask the thinkpad-acpi maintainer to do it, instead.
3296 */
3297
3298 enum keymap_index {
3299 TPACPI_KEYMAP_IBM_GENERIC = 0,
3300 TPACPI_KEYMAP_LENOVO_GENERIC,
3301 };
3302
3303 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3304 /* Generic keymap for IBM ThinkPads */
3305 [TPACPI_KEYMAP_IBM_GENERIC] = {
3306 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3307 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3308 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3309 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3310
3311 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3312 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3313 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3314 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3315
3316 /* brightness: firmware always reacts to them */
3317 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3318 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3319
3320 /* Thinklight: firmware always react to it */
3321 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3322
3323 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3324 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3325
3326 /* Volume: firmware always react to it and reprograms
3327 * the built-in *extra* mixer. Never map it to control
3328 * another mixer by default. */
3329 KEY_RESERVED, /* 0x14: VOLUME UP */
3330 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3331 KEY_RESERVED, /* 0x16: MUTE */
3332
3333 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3334
3335 /* (assignments unknown, please report if found) */
3336 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3337 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3338
3339 /* No assignments, only used for Adaptive keyboards. */
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
Googler9398cc32022-12-02 17:21:52 +08003345
3346 /* No assignment, used for newer Lenovo models */
3347 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3348 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3349 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3350 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3351 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3352 KEY_UNKNOWN, KEY_UNKNOWN
3353
Googleraf606d22022-10-26 21:40:12 -07003354 },
3355
3356 /* Generic keymap for Lenovo ThinkPads */
3357 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3358 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3359 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3360 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3361 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3362
3363 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3364 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3365 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3366 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3367
3368 /* These should be enabled --only-- when ACPI video
3369 * is disabled (i.e. in "vendor" mode), and are handled
3370 * in a special way by the init code */
3371 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3372 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3373
3374 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3375
3376 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3377 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3378
3379 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3380 * react to it and reprograms the built-in *extra* mixer.
3381 * Never map it to control another mixer by default.
3382 *
3383 * T60?, T61, R60?, R61: firmware and EC tries to send
3384 * these over the regular keyboard, so these are no-ops,
3385 * but there are still weird bugs re. MUTE, so do not
3386 * change unless you get test reports from all Lenovo
3387 * models. May cause the BIOS to interfere with the
3388 * HDA mixer.
3389 */
3390 KEY_RESERVED, /* 0x14: VOLUME UP */
3391 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3392 KEY_RESERVED, /* 0x16: MUTE */
3393
3394 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3395
3396 /* (assignments unknown, please report if found) */
3397 KEY_UNKNOWN, KEY_UNKNOWN,
3398
3399 /*
3400 * The mic mute button only sends 0x1a. It does not
3401 * automatically mute the mic or change the mute light.
3402 */
3403 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3404
3405 /* (assignments unknown, please report if found) */
3406 KEY_UNKNOWN,
3407
3408 /* Extra keys in use since the X240 / T440 / T540 */
3409 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3410
3411 /*
3412 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3413 * The first item in this list is the Mute button which is
3414 * emitted with 0x103 through
3415 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3416 * symbol is held.
3417 * We'll need to offset those by 0x20.
3418 */
3419 KEY_RESERVED, /* Mute held, 0x103 */
3420 KEY_BRIGHTNESS_MIN, /* Backlight off */
3421 KEY_RESERVED, /* Clipping tool */
3422 KEY_RESERVED, /* Cloud */
3423 KEY_RESERVED,
3424 KEY_VOICECOMMAND, /* Voice */
3425 KEY_RESERVED,
3426 KEY_RESERVED, /* Gestures */
3427 KEY_RESERVED,
3428 KEY_RESERVED,
3429 KEY_RESERVED,
3430 KEY_CONFIG, /* Settings */
3431 KEY_RESERVED, /* New tab */
3432 KEY_REFRESH, /* Reload */
3433 KEY_BACK, /* Back */
3434 KEY_RESERVED, /* Microphone down */
3435 KEY_RESERVED, /* Microphone up */
3436 KEY_RESERVED, /* Microphone cancellation */
3437 KEY_RESERVED, /* Camera mode */
3438 KEY_RESERVED, /* Rotate display, 0x116 */
Googler9398cc32022-12-02 17:21:52 +08003439
3440 /*
3441 * These are found in 2017 models (e.g. T470s, X270).
3442 * The lowest known value is 0x311, which according to
3443 * the manual should launch a user defined favorite
3444 * application.
3445 *
3446 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3447 * corresponding to 0x34.
3448 */
3449
3450 /* (assignments unknown, please report if found) */
3451 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3452 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3453 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3455 KEY_UNKNOWN,
3456
3457 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3458 KEY_RESERVED, /* Clipping tool */
3459 KEY_CALC, /* Calculator (above numpad, P52) */
3460 KEY_BLUETOOTH, /* Bluetooth */
3461 KEY_KEYBOARD /* Keyboard, 0x315 */
Googleraf606d22022-10-26 21:40:12 -07003462 },
3463 };
3464
3465 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3466 /* Generic maps (fallback) */
3467 {
3468 .vendor = PCI_VENDOR_ID_IBM,
3469 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3470 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3471 },
3472 {
3473 .vendor = PCI_VENDOR_ID_LENOVO,
3474 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3475 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3476 },
3477 };
3478
3479#define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3480#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3481
3482 int res, i;
3483 int status;
3484 int hkeyv;
3485 bool radiosw_state = false;
3486 bool tabletsw_state = false;
3487
3488 unsigned long quirks;
3489 unsigned long keymap_id;
3490
3491 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3492 "initializing hotkey subdriver\n");
3493
3494 BUG_ON(!tpacpi_inputdev);
3495 BUG_ON(tpacpi_inputdev->open != NULL ||
3496 tpacpi_inputdev->close != NULL);
3497
3498 TPACPI_ACPIHANDLE_INIT(hkey);
3499 mutex_init(&hotkey_mutex);
3500
3501#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3502 mutex_init(&hotkey_thread_data_mutex);
3503#endif
3504
3505 /* hotkey not supported on 570 */
3506 tp_features.hotkey = hkey_handle != NULL;
3507
3508 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3509 "hotkeys are %s\n",
3510 str_supported(tp_features.hotkey));
3511
3512 if (!tp_features.hotkey)
3513 return 1;
3514
3515 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3516 ARRAY_SIZE(tpacpi_hotkey_qtable));
3517
3518 tpacpi_disable_brightness_delay();
3519
3520 /* MUST have enough space for all attributes to be added to
3521 * hotkey_dev_attributes */
3522 hotkey_dev_attributes = create_attr_set(
3523 ARRAY_SIZE(hotkey_attributes) + 2,
3524 NULL);
3525 if (!hotkey_dev_attributes)
3526 return -ENOMEM;
3527 res = add_many_to_attr_set(hotkey_dev_attributes,
3528 hotkey_attributes,
3529 ARRAY_SIZE(hotkey_attributes));
3530 if (res)
3531 goto err_exit;
3532
3533 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3534 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3535 for HKEY interface version 0x100 */
3536 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3537 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3538 "firmware HKEY interface version: 0x%x\n",
3539 hkeyv);
3540
3541 switch (hkeyv >> 8) {
3542 case 1:
3543 /*
3544 * MHKV 0x100 in A31, R40, R40e,
3545 * T4x, X31, and later
3546 */
3547
3548 /* Paranoia check AND init hotkey_all_mask */
3549 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3550 "MHKA", "qd")) {
3551 pr_err("missing MHKA handler, please report this to %s\n",
3552 TPACPI_MAIL);
3553 /* Fallback: pre-init for FN+F3,F4,F12 */
3554 hotkey_all_mask = 0x080cU;
3555 } else {
3556 tp_features.hotkey_mask = 1;
3557 }
3558 break;
3559
3560 case 2:
3561 /*
3562 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3563 */
3564
3565 /* Paranoia check AND init hotkey_all_mask */
3566 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3567 "MHKA", "dd", 1)) {
3568 pr_err("missing MHKA handler, please report this to %s\n",
3569 TPACPI_MAIL);
3570 /* Fallback: pre-init for FN+F3,F4,F12 */
3571 hotkey_all_mask = 0x080cU;
3572 } else {
3573 tp_features.hotkey_mask = 1;
3574 }
3575
3576 /*
3577 * Check if we have an adaptive keyboard, like on the
3578 * Lenovo Carbon X1 2014 (2nd Gen).
3579 */
3580 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3581 "MHKA", "dd", 2)) {
3582 if (hotkey_adaptive_all_mask != 0) {
3583 tp_features.has_adaptive_kbd = true;
3584 res = sysfs_create_group(
3585 &tpacpi_pdev->dev.kobj,
3586 &adaptive_kbd_attr_group);
3587 if (res)
3588 goto err_exit;
3589 }
3590 } else {
3591 tp_features.has_adaptive_kbd = false;
3592 hotkey_adaptive_all_mask = 0x0U;
3593 }
3594 break;
3595
3596 default:
3597 pr_err("unknown version of the HKEY interface: 0x%x\n",
3598 hkeyv);
3599 pr_err("please report this to %s\n", TPACPI_MAIL);
3600 break;
3601 }
3602 }
3603
3604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3605 "hotkey masks are %s\n",
3606 str_supported(tp_features.hotkey_mask));
3607
3608 /* Init hotkey_all_mask if not initialized yet */
3609 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3610 (quirks & TPACPI_HK_Q_INIMASK))
3611 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3612
3613 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3614 if (tp_features.hotkey_mask) {
3615 /* hotkey_source_mask *must* be zero for
3616 * the first hotkey_mask_get to return hotkey_orig_mask */
3617 res = hotkey_mask_get();
3618 if (res)
3619 goto err_exit;
3620
3621 hotkey_orig_mask = hotkey_acpi_mask;
3622 } else {
3623 hotkey_orig_mask = hotkey_all_mask;
3624 hotkey_acpi_mask = hotkey_all_mask;
3625 }
3626
3627#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3628 if (dbg_wlswemul) {
3629 tp_features.hotkey_wlsw = 1;
3630 radiosw_state = !!tpacpi_wlsw_emulstate;
3631 pr_info("radio switch emulation enabled\n");
3632 } else
3633#endif
3634 /* Not all thinkpads have a hardware radio switch */
3635 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3636 tp_features.hotkey_wlsw = 1;
3637 radiosw_state = !!status;
3638 pr_info("radio switch found; radios are %s\n",
3639 enabled(status, 0));
3640 }
3641 if (tp_features.hotkey_wlsw)
3642 res = add_to_attr_set(hotkey_dev_attributes,
3643 &dev_attr_hotkey_radio_sw.attr);
3644
Googler9398cc32022-12-02 17:21:52 +08003645 res = hotkey_init_tablet_mode();
3646 if (res < 0)
3647 goto err_exit;
Googleraf606d22022-10-26 21:40:12 -07003648
Googler9398cc32022-12-02 17:21:52 +08003649 tabletsw_state = res;
3650
3651 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3652 &tpacpi_pdev->dev.kobj);
Googleraf606d22022-10-26 21:40:12 -07003653 if (res)
3654 goto err_exit;
3655
3656 /* Set up key map */
3657 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3658 ARRAY_SIZE(tpacpi_keymap_qtable));
3659 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3660 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3661 "using keymap number %lu\n", keymap_id);
3662
Googler9398cc32022-12-02 17:21:52 +08003663 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3664 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3665 if (!hotkey_keycode_map) {
3666 pr_err("failed to allocate memory for key map\n");
3667 res = -ENOMEM;
3668 goto err_exit;
3669 }
Googleraf606d22022-10-26 21:40:12 -07003670
3671 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3672 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3673 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3674 tpacpi_inputdev->keycode = hotkey_keycode_map;
3675 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3676 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3677 input_set_capability(tpacpi_inputdev, EV_KEY,
3678 hotkey_keycode_map[i]);
3679 } else {
3680 if (i < sizeof(hotkey_reserved_mask)*8)
3681 hotkey_reserved_mask |= 1 << i;
3682 }
3683 }
3684
3685 if (tp_features.hotkey_wlsw) {
3686 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3687 input_report_switch(tpacpi_inputdev,
3688 SW_RFKILL_ALL, radiosw_state);
3689 }
3690 if (tp_features.hotkey_tablet) {
3691 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3692 input_report_switch(tpacpi_inputdev,
3693 SW_TABLET_MODE, tabletsw_state);
3694 }
3695
3696 /* Do not issue duplicate brightness change events to
3697 * userspace. tpacpi_detect_brightness_capabilities() must have
3698 * been called before this point */
3699 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
Googler9398cc32022-12-02 17:21:52 +08003700 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3701 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
Googleraf606d22022-10-26 21:40:12 -07003702
3703 /* Disable brightness up/down on Lenovo thinkpads when
3704 * ACPI is handling them, otherwise it is plain impossible
3705 * for userspace to do something even remotely sane */
3706 hotkey_reserved_mask |=
3707 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3708 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3709 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3710 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3711 }
3712
3713#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3714 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3715 & ~hotkey_all_mask
3716 & ~hotkey_reserved_mask;
3717
3718 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3719 "hotkey source mask 0x%08x, polling freq %u\n",
3720 hotkey_source_mask, hotkey_poll_freq);
3721#endif
3722
3723 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3724 "enabling firmware HKEY event interface...\n");
3725 res = hotkey_status_set(true);
3726 if (res) {
3727 hotkey_exit();
3728 return res;
3729 }
3730 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3731 | hotkey_driver_mask)
3732 & ~hotkey_source_mask);
3733 if (res < 0 && res != -ENXIO) {
3734 hotkey_exit();
3735 return res;
3736 }
3737 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3738 & ~hotkey_reserved_mask;
3739 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3740 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3741 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3742
3743 tpacpi_inputdev->open = &hotkey_inputdev_open;
3744 tpacpi_inputdev->close = &hotkey_inputdev_close;
3745
3746 hotkey_poll_setup_safe(true);
3747
3748 return 0;
3749
3750err_exit:
3751 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3752 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3753 &adaptive_kbd_attr_group);
3754
3755 hotkey_dev_attributes = NULL;
3756
3757 return (res < 0) ? res : 1;
3758}
3759
3760/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3761 * mode, Web conference mode, Function mode and Lay-flat mode.
3762 * We support Home mode and Function mode currently.
3763 *
3764 * Will consider support rest of modes in future.
3765 *
3766 */
3767static const int adaptive_keyboard_modes[] = {
3768 HOME_MODE,
3769/* WEB_BROWSER_MODE = 2,
3770 WEB_CONFERENCE_MODE = 3, */
3771 FUNCTION_MODE
3772};
3773
3774#define DFR_CHANGE_ROW 0x101
3775#define DFR_SHOW_QUICKVIEW_ROW 0x102
3776#define FIRST_ADAPTIVE_KEY 0x103
3777
3778/* press Fn key a while second, it will switch to Function Mode. Then
3779 * release Fn key, previous mode be restored.
3780 */
3781static bool adaptive_keyboard_mode_is_saved;
3782static int adaptive_keyboard_prev_mode;
3783
3784static int adaptive_keyboard_get_mode(void)
3785{
3786 int mode = 0;
3787
3788 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3789 pr_err("Cannot read adaptive keyboard mode\n");
3790 return -EIO;
3791 }
3792
3793 return mode;
3794}
3795
3796static int adaptive_keyboard_set_mode(int new_mode)
3797{
3798 if (new_mode < 0 ||
3799 new_mode > LAYFLAT_MODE)
3800 return -EINVAL;
3801
3802 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3803 pr_err("Cannot set adaptive keyboard mode\n");
3804 return -EIO;
3805 }
3806
3807 return 0;
3808}
3809
3810static int adaptive_keyboard_get_next_mode(int mode)
3811{
3812 size_t i;
3813 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3814
3815 for (i = 0; i <= max_mode; i++) {
3816 if (adaptive_keyboard_modes[i] == mode)
3817 break;
3818 }
3819
3820 if (i >= max_mode)
3821 i = 0;
3822 else
3823 i++;
3824
3825 return adaptive_keyboard_modes[i];
3826}
3827
3828static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3829{
3830 int current_mode = 0;
3831 int new_mode = 0;
3832 int keycode;
3833
3834 switch (scancode) {
3835 case DFR_CHANGE_ROW:
3836 if (adaptive_keyboard_mode_is_saved) {
3837 new_mode = adaptive_keyboard_prev_mode;
3838 adaptive_keyboard_mode_is_saved = false;
3839 } else {
3840 current_mode = adaptive_keyboard_get_mode();
3841 if (current_mode < 0)
3842 return false;
3843 new_mode = adaptive_keyboard_get_next_mode(
3844 current_mode);
3845 }
3846
3847 if (adaptive_keyboard_set_mode(new_mode) < 0)
3848 return false;
3849
3850 return true;
3851
3852 case DFR_SHOW_QUICKVIEW_ROW:
3853 current_mode = adaptive_keyboard_get_mode();
3854 if (current_mode < 0)
3855 return false;
3856
3857 adaptive_keyboard_prev_mode = current_mode;
3858 adaptive_keyboard_mode_is_saved = true;
3859
3860 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3861 return false;
3862 return true;
3863
3864 default:
3865 if (scancode < FIRST_ADAPTIVE_KEY ||
Googler9398cc32022-12-02 17:21:52 +08003866 scancode >= FIRST_ADAPTIVE_KEY +
3867 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3868 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
Googleraf606d22022-10-26 21:40:12 -07003869 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
Googler9398cc32022-12-02 17:21:52 +08003870 scancode);
Googleraf606d22022-10-26 21:40:12 -07003871 return false;
3872 }
Googler9398cc32022-12-02 17:21:52 +08003873 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3874 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
Googleraf606d22022-10-26 21:40:12 -07003875 if (keycode != KEY_RESERVED) {
3876 mutex_lock(&tpacpi_inputdev_send_mutex);
3877
3878 input_report_key(tpacpi_inputdev, keycode, 1);
3879 input_sync(tpacpi_inputdev);
3880
3881 input_report_key(tpacpi_inputdev, keycode, 0);
3882 input_sync(tpacpi_inputdev);
3883
3884 mutex_unlock(&tpacpi_inputdev_send_mutex);
3885 }
3886 return true;
3887 }
3888}
3889
3890static bool hotkey_notify_hotkey(const u32 hkey,
3891 bool *send_acpi_ev,
3892 bool *ignore_acpi_ev)
3893{
3894 /* 0x1000-0x1FFF: key presses */
3895 unsigned int scancode = hkey & 0xfff;
3896 *send_acpi_ev = true;
3897 *ignore_acpi_ev = false;
3898
Googler9398cc32022-12-02 17:21:52 +08003899 /*
3900 * Original events are in the 0x10XX range, the adaptive keyboard
3901 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3902 * models, additional keys are emitted through 0x13XX.
3903 */
3904 switch ((hkey >> 8) & 0xf) {
3905 case 0:
3906 if (scancode > 0 &&
3907 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3908 /* HKEY event 0x1001 is scancode 0x00 */
3909 scancode--;
3910 if (!(hotkey_source_mask & (1 << scancode))) {
3911 tpacpi_input_send_key_masked(scancode);
3912 *send_acpi_ev = false;
3913 } else {
3914 *ignore_acpi_ev = true;
3915 }
3916 return true;
Googleraf606d22022-10-26 21:40:12 -07003917 }
Googler9398cc32022-12-02 17:21:52 +08003918 break;
3919
3920 case 1:
Googleraf606d22022-10-26 21:40:12 -07003921 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
Googler9398cc32022-12-02 17:21:52 +08003922
3923 case 3:
3924 /* Extended keycodes start at 0x300 and our offset into the map
3925 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3926 * will be positive, but might not be in the correct range.
3927 */
3928 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3929 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3930 scancode < TPACPI_HOTKEY_MAP_LEN) {
3931 tpacpi_input_send_key(scancode);
3932 return true;
3933 }
3934 break;
Googleraf606d22022-10-26 21:40:12 -07003935 }
Googler9398cc32022-12-02 17:21:52 +08003936
Googleraf606d22022-10-26 21:40:12 -07003937 return false;
3938}
3939
3940static bool hotkey_notify_wakeup(const u32 hkey,
3941 bool *send_acpi_ev,
3942 bool *ignore_acpi_ev)
3943{
3944 /* 0x2000-0x2FFF: Wakeup reason */
3945 *send_acpi_ev = true;
3946 *ignore_acpi_ev = false;
3947
3948 switch (hkey) {
3949 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3950 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3951 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3952 *ignore_acpi_ev = true;
3953 break;
3954
3955 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3956 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3957 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3958 *ignore_acpi_ev = true;
3959 break;
3960
3961 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3962 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3963 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3964 /* how to auto-heal: */
3965 /* 2313: woke up from S3, go to S4/S5 */
3966 /* 2413: woke up from S4, go to S5 */
3967 break;
3968
3969 default:
3970 return false;
3971 }
3972
3973 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3974 pr_info("woke up due to a hot-unplug request...\n");
3975 hotkey_wakeup_reason_notify_change();
3976 }
3977 return true;
3978}
3979
3980static bool hotkey_notify_dockevent(const u32 hkey,
3981 bool *send_acpi_ev,
3982 bool *ignore_acpi_ev)
3983{
3984 /* 0x4000-0x4FFF: dock-related events */
3985 *send_acpi_ev = true;
3986 *ignore_acpi_ev = false;
3987
3988 switch (hkey) {
3989 case TP_HKEY_EV_UNDOCK_ACK:
3990 /* ACPI undock operation completed after wakeup */
3991 hotkey_autosleep_ack = 1;
3992 pr_info("undocked\n");
3993 hotkey_wakeup_hotunplug_complete_notify_change();
3994 return true;
3995
3996 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3997 pr_info("docked into hotplug port replicator\n");
3998 return true;
3999 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4000 pr_info("undocked from hotplug port replicator\n");
4001 return true;
4002
4003 default:
4004 return false;
4005 }
4006}
4007
4008static bool hotkey_notify_usrevent(const u32 hkey,
4009 bool *send_acpi_ev,
4010 bool *ignore_acpi_ev)
4011{
4012 /* 0x5000-0x5FFF: human interface helpers */
4013 *send_acpi_ev = true;
4014 *ignore_acpi_ev = false;
4015
4016 switch (hkey) {
4017 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4018 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4019 return true;
4020
4021 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4022 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4023 tpacpi_input_send_tabletsw();
4024 hotkey_tablet_mode_notify_change();
4025 *send_acpi_ev = false;
4026 return true;
4027
4028 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4029 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4030 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4031 /* do not propagate these events */
4032 *ignore_acpi_ev = true;
4033 return true;
4034
4035 default:
4036 return false;
4037 }
4038}
4039
4040static void thermal_dump_all_sensors(void);
4041
4042static bool hotkey_notify_6xxx(const u32 hkey,
4043 bool *send_acpi_ev,
4044 bool *ignore_acpi_ev)
4045{
4046 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4047 *send_acpi_ev = true;
4048 *ignore_acpi_ev = false;
4049
4050 switch (hkey) {
4051 case TP_HKEY_EV_THM_TABLE_CHANGED:
Googler9398cc32022-12-02 17:21:52 +08004052 pr_debug("EC reports: Thermal Table has changed\n");
4053 /* recommended action: do nothing, we don't have
4054 * Lenovo ATM information */
4055 return true;
4056 case TP_HKEY_EV_THM_CSM_COMPLETED:
4057 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4058 /* recommended action: do nothing, we don't have
4059 * Lenovo ATM information */
4060 return true;
4061 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4062 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
Googleraf606d22022-10-26 21:40:12 -07004063 /* recommended action: do nothing, we don't have
4064 * Lenovo ATM information */
4065 return true;
4066 case TP_HKEY_EV_ALARM_BAT_HOT:
4067 pr_crit("THERMAL ALARM: battery is too hot!\n");
4068 /* recommended action: warn user through gui */
4069 break;
4070 case TP_HKEY_EV_ALARM_BAT_XHOT:
4071 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4072 /* recommended action: immediate sleep/hibernate */
4073 break;
4074 case TP_HKEY_EV_ALARM_SENSOR_HOT:
Googler9398cc32022-12-02 17:21:52 +08004075 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
Googleraf606d22022-10-26 21:40:12 -07004076 /* recommended action: warn user through gui, that */
4077 /* some internal component is too hot */
4078 break;
4079 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
Googler9398cc32022-12-02 17:21:52 +08004080 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
Googleraf606d22022-10-26 21:40:12 -07004081 /* recommended action: immediate sleep/hibernate */
4082 break;
4083 case TP_HKEY_EV_AC_CHANGED:
4084 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4085 * AC status changed; can be triggered by plugging or
4086 * unplugging AC adapter, docking or undocking. */
4087
4088 /* fallthrough */
4089
4090 case TP_HKEY_EV_KEY_NUMLOCK:
4091 case TP_HKEY_EV_KEY_FN:
Googlerb48fa912023-03-17 12:40:29 +05304092 case TP_HKEY_EV_KEY_FN_ESC:
Googleraf606d22022-10-26 21:40:12 -07004093 /* key press events, we just ignore them as long as the EC
4094 * is still reporting them in the normal keyboard stream */
4095 *send_acpi_ev = false;
4096 *ignore_acpi_ev = true;
4097 return true;
4098
Googler9398cc32022-12-02 17:21:52 +08004099 case TP_HKEY_EV_TABLET_CHANGED:
4100 tpacpi_input_send_tabletsw();
4101 hotkey_tablet_mode_notify_change();
4102 *send_acpi_ev = false;
4103 return true;
4104
4105 case TP_HKEY_EV_PALM_DETECTED:
4106 case TP_HKEY_EV_PALM_UNDETECTED:
4107 /* palm detected hovering the keyboard, forward to user-space
4108 * via netlink for consumption */
4109 return true;
4110
Googleraf606d22022-10-26 21:40:12 -07004111 default:
Googler9398cc32022-12-02 17:21:52 +08004112 /* report simply as unknown, no sensor dump */
4113 return false;
Googleraf606d22022-10-26 21:40:12 -07004114 }
4115
4116 thermal_dump_all_sensors();
Googler9398cc32022-12-02 17:21:52 +08004117 return true;
Googleraf606d22022-10-26 21:40:12 -07004118}
4119
4120static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4121{
4122 u32 hkey;
4123 bool send_acpi_ev;
4124 bool ignore_acpi_ev;
4125 bool known_ev;
4126
4127 if (event != 0x80) {
4128 pr_err("unknown HKEY notification event %d\n", event);
4129 /* forward it to userspace, maybe it knows how to handle it */
4130 acpi_bus_generate_netlink_event(
4131 ibm->acpi->device->pnp.device_class,
4132 dev_name(&ibm->acpi->device->dev),
4133 event, 0);
4134 return;
4135 }
4136
4137 while (1) {
4138 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4139 pr_err("failed to retrieve HKEY event\n");
4140 return;
4141 }
4142
4143 if (hkey == 0) {
4144 /* queue empty */
4145 return;
4146 }
4147
4148 send_acpi_ev = true;
4149 ignore_acpi_ev = false;
4150
4151 switch (hkey >> 12) {
4152 case 1:
4153 /* 0x1000-0x1FFF: key presses */
4154 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4155 &ignore_acpi_ev);
4156 break;
4157 case 2:
4158 /* 0x2000-0x2FFF: Wakeup reason */
4159 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4160 &ignore_acpi_ev);
4161 break;
4162 case 3:
4163 /* 0x3000-0x3FFF: bay-related wakeups */
4164 switch (hkey) {
4165 case TP_HKEY_EV_BAYEJ_ACK:
4166 hotkey_autosleep_ack = 1;
4167 pr_info("bay ejected\n");
4168 hotkey_wakeup_hotunplug_complete_notify_change();
4169 known_ev = true;
4170 break;
4171 case TP_HKEY_EV_OPTDRV_EJ:
4172 /* FIXME: kick libata if SATA link offline */
4173 known_ev = true;
4174 break;
4175 default:
4176 known_ev = false;
4177 }
4178 break;
4179 case 4:
4180 /* 0x4000-0x4FFF: dock-related events */
4181 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4182 &ignore_acpi_ev);
4183 break;
4184 case 5:
4185 /* 0x5000-0x5FFF: human interface helpers */
4186 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4187 &ignore_acpi_ev);
4188 break;
4189 case 6:
4190 /* 0x6000-0x6FFF: thermal alarms/notices and
4191 * keyboard events */
4192 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4193 &ignore_acpi_ev);
4194 break;
4195 case 7:
4196 /* 0x7000-0x7FFF: misc */
4197 if (tp_features.hotkey_wlsw &&
4198 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4199 tpacpi_send_radiosw_update();
4200 send_acpi_ev = 0;
4201 known_ev = true;
4202 break;
4203 }
Googler9398cc32022-12-02 17:21:52 +08004204 /* fallthrough - to default */
Googleraf606d22022-10-26 21:40:12 -07004205 default:
4206 known_ev = false;
4207 }
4208 if (!known_ev) {
4209 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
Googler9398cc32022-12-02 17:21:52 +08004210 pr_notice("please report the conditions when this event happened to %s\n",
4211 TPACPI_MAIL);
Googleraf606d22022-10-26 21:40:12 -07004212 }
4213
4214 /* netlink events */
4215 if (!ignore_acpi_ev && send_acpi_ev) {
4216 acpi_bus_generate_netlink_event(
4217 ibm->acpi->device->pnp.device_class,
4218 dev_name(&ibm->acpi->device->dev),
4219 event, hkey);
4220 }
4221 }
4222}
4223
4224static void hotkey_suspend(void)
4225{
4226 /* Do these on suspend, we get the events on early resume! */
4227 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4228 hotkey_autosleep_ack = 0;
4229
4230 /* save previous mode of adaptive keyboard of X1 Carbon */
4231 if (tp_features.has_adaptive_kbd) {
4232 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4233 "GTRW", "dd", 0)) {
4234 pr_err("Cannot read adaptive keyboard mode.\n");
4235 }
4236 }
4237}
4238
4239static void hotkey_resume(void)
4240{
4241 tpacpi_disable_brightness_delay();
4242
4243 if (hotkey_status_set(true) < 0 ||
4244 hotkey_mask_set(hotkey_acpi_mask) < 0)
Googler9398cc32022-12-02 17:21:52 +08004245 pr_err("error while attempting to reset the event firmware interface\n");
Googleraf606d22022-10-26 21:40:12 -07004246
4247 tpacpi_send_radiosw_update();
Googler9398cc32022-12-02 17:21:52 +08004248 tpacpi_input_send_tabletsw();
Googleraf606d22022-10-26 21:40:12 -07004249 hotkey_tablet_mode_notify_change();
4250 hotkey_wakeup_reason_notify_change();
4251 hotkey_wakeup_hotunplug_complete_notify_change();
4252 hotkey_poll_setup_safe(false);
4253
4254 /* restore previous mode of adapive keyboard of X1 Carbon */
4255 if (tp_features.has_adaptive_kbd) {
4256 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4257 adaptive_keyboard_prev_mode)) {
4258 pr_err("Cannot set adaptive keyboard mode.\n");
4259 }
4260 }
4261}
4262
4263/* procfs -------------------------------------------------------------- */
4264static int hotkey_read(struct seq_file *m)
4265{
4266 int res, status;
4267
4268 if (!tp_features.hotkey) {
4269 seq_printf(m, "status:\t\tnot supported\n");
4270 return 0;
4271 }
4272
4273 if (mutex_lock_killable(&hotkey_mutex))
4274 return -ERESTARTSYS;
4275 res = hotkey_status_get(&status);
4276 if (!res)
4277 res = hotkey_mask_get();
4278 mutex_unlock(&hotkey_mutex);
4279 if (res)
4280 return res;
4281
4282 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4283 if (hotkey_all_mask) {
4284 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4285 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4286 } else {
4287 seq_printf(m, "mask:\t\tnot supported\n");
4288 seq_printf(m, "commands:\tenable, disable, reset\n");
4289 }
4290
4291 return 0;
4292}
4293
4294static void hotkey_enabledisable_warn(bool enable)
4295{
4296 tpacpi_log_usertask("procfs hotkey enable/disable");
4297 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
Googler9398cc32022-12-02 17:21:52 +08004298 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4299 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
Googleraf606d22022-10-26 21:40:12 -07004300}
4301
4302static int hotkey_write(char *buf)
4303{
4304 int res;
4305 u32 mask;
4306 char *cmd;
4307
4308 if (!tp_features.hotkey)
4309 return -ENODEV;
4310
4311 if (mutex_lock_killable(&hotkey_mutex))
4312 return -ERESTARTSYS;
4313
4314 mask = hotkey_user_mask;
4315
4316 res = 0;
4317 while ((cmd = next_cmd(&buf))) {
4318 if (strlencmp(cmd, "enable") == 0) {
4319 hotkey_enabledisable_warn(1);
4320 } else if (strlencmp(cmd, "disable") == 0) {
4321 hotkey_enabledisable_warn(0);
4322 res = -EPERM;
4323 } else if (strlencmp(cmd, "reset") == 0) {
4324 mask = (hotkey_all_mask | hotkey_source_mask)
4325 & ~hotkey_reserved_mask;
4326 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4327 /* mask set */
4328 } else if (sscanf(cmd, "%x", &mask) == 1) {
4329 /* mask set */
4330 } else {
4331 res = -EINVAL;
4332 goto errexit;
4333 }
4334 }
4335
4336 if (!res) {
4337 tpacpi_disclose_usertask("procfs hotkey",
4338 "set mask to 0x%08x\n", mask);
4339 res = hotkey_user_mask_set(mask);
4340 }
4341
4342errexit:
4343 mutex_unlock(&hotkey_mutex);
4344 return res;
4345}
4346
4347static const struct acpi_device_id ibm_htk_device_ids[] = {
4348 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4349 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
Googler9398cc32022-12-02 17:21:52 +08004350 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
Googleraf606d22022-10-26 21:40:12 -07004351 {"", 0},
4352};
4353
4354static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4355 .hid = ibm_htk_device_ids,
4356 .notify = hotkey_notify,
4357 .handle = &hkey_handle,
4358 .type = ACPI_DEVICE_NOTIFY,
4359};
4360
4361static struct ibm_struct hotkey_driver_data = {
4362 .name = "hotkey",
4363 .read = hotkey_read,
4364 .write = hotkey_write,
4365 .exit = hotkey_exit,
4366 .resume = hotkey_resume,
4367 .suspend = hotkey_suspend,
4368 .acpi = &ibm_hotkey_acpidriver,
4369};
4370
4371/*************************************************************************
4372 * Bluetooth subdriver
4373 */
4374
4375enum {
4376 /* ACPI GBDC/SBDC bits */
4377 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4378 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4379 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4380 0 = disable, 1 = enable */
4381};
4382
4383enum {
4384 /* ACPI \BLTH commands */
4385 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4386 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4387 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4388 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4389 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4390};
4391
4392#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4393
4394static int bluetooth_get_status(void)
4395{
4396 int status;
4397
4398#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4399 if (dbg_bluetoothemul)
4400 return (tpacpi_bluetooth_emulstate) ?
4401 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4402#endif
4403
4404 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4405 return -EIO;
4406
4407 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4408 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4409}
4410
4411static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4412{
4413 int status;
4414
4415 vdbg_printk(TPACPI_DBG_RFKILL,
4416 "will attempt to %s bluetooth\n",
4417 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4418
4419#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4420 if (dbg_bluetoothemul) {
4421 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4422 return 0;
4423 }
4424#endif
4425
4426 if (state == TPACPI_RFK_RADIO_ON)
4427 status = TP_ACPI_BLUETOOTH_RADIOSSW
4428 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4429 else
4430 status = 0;
4431
4432 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4433 return -EIO;
4434
4435 return 0;
4436}
4437
4438/* sysfs bluetooth enable ---------------------------------------------- */
4439static ssize_t bluetooth_enable_show(struct device *dev,
4440 struct device_attribute *attr,
4441 char *buf)
4442{
4443 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4444 attr, buf);
4445}
4446
4447static ssize_t bluetooth_enable_store(struct device *dev,
4448 struct device_attribute *attr,
4449 const char *buf, size_t count)
4450{
4451 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4452 attr, buf, count);
4453}
4454
4455static DEVICE_ATTR_RW(bluetooth_enable);
4456
4457/* --------------------------------------------------------------------- */
4458
4459static struct attribute *bluetooth_attributes[] = {
4460 &dev_attr_bluetooth_enable.attr,
4461 NULL
4462};
4463
4464static const struct attribute_group bluetooth_attr_group = {
4465 .attrs = bluetooth_attributes,
4466};
4467
4468static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4469 .get_status = bluetooth_get_status,
4470 .set_status = bluetooth_set_status,
4471};
4472
4473static void bluetooth_shutdown(void)
4474{
4475 /* Order firmware to save current state to NVRAM */
4476 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4477 TP_ACPI_BLTH_SAVE_STATE))
4478 pr_notice("failed to save bluetooth state to NVRAM\n");
4479 else
4480 vdbg_printk(TPACPI_DBG_RFKILL,
4481 "bluetooth state saved to NVRAM\n");
4482}
4483
4484static void bluetooth_exit(void)
4485{
4486 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4487 &bluetooth_attr_group);
4488
4489 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4490
4491 bluetooth_shutdown();
4492}
4493
Googler9398cc32022-12-02 17:21:52 +08004494static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4495 {
4496 .ident = "ThinkPad E485",
4497 .matches = {
4498 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4499 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4500 },
4501 },
4502 {
4503 .ident = "ThinkPad E585",
4504 .matches = {
4505 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4506 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4507 },
4508 },
4509 {
4510 .ident = "ThinkPad A285 - 20MW",
4511 .matches = {
4512 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4513 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4514 },
4515 },
4516 {
4517 .ident = "ThinkPad A285 - 20MX",
4518 .matches = {
4519 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4520 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4521 },
4522 },
4523 {
4524 .ident = "ThinkPad A485 - 20MU",
4525 .matches = {
4526 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4527 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4528 },
4529 },
4530 {
4531 .ident = "ThinkPad A485 - 20MV",
4532 .matches = {
4533 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4534 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4535 },
4536 },
4537 {}
4538};
4539
4540static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4541 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4542 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4543 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4544 {}
4545};
4546
4547
4548static int __init have_bt_fwbug(void)
4549{
4550 /*
4551 * Some AMD based ThinkPads have a firmware bug that calling
4552 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4553 */
4554 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4555 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4556 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4557 return 1;
4558 } else
4559 return 0;
4560}
4561
Googleraf606d22022-10-26 21:40:12 -07004562static int __init bluetooth_init(struct ibm_init_struct *iibm)
4563{
4564 int res;
4565 int status = 0;
4566
4567 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4568 "initializing bluetooth subdriver\n");
4569
4570 TPACPI_ACPIHANDLE_INIT(hkey);
4571
4572 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4573 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Googler9398cc32022-12-02 17:21:52 +08004574 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
Googleraf606d22022-10-26 21:40:12 -07004575 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4576
4577 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4578 "bluetooth is %s, status 0x%02x\n",
4579 str_supported(tp_features.bluetooth),
4580 status);
4581
4582#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4583 if (dbg_bluetoothemul) {
4584 tp_features.bluetooth = 1;
4585 pr_info("bluetooth switch emulation enabled\n");
4586 } else
4587#endif
4588 if (tp_features.bluetooth &&
4589 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4590 /* no bluetooth hardware present in system */
4591 tp_features.bluetooth = 0;
4592 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4593 "bluetooth hardware not installed\n");
4594 }
4595
4596 if (!tp_features.bluetooth)
4597 return 1;
4598
4599 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4600 &bluetooth_tprfk_ops,
4601 RFKILL_TYPE_BLUETOOTH,
4602 TPACPI_RFK_BLUETOOTH_SW_NAME,
4603 true);
4604 if (res)
4605 return res;
4606
4607 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4608 &bluetooth_attr_group);
4609 if (res) {
4610 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4611 return res;
4612 }
4613
4614 return 0;
4615}
4616
4617/* procfs -------------------------------------------------------------- */
4618static int bluetooth_read(struct seq_file *m)
4619{
4620 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4621}
4622
4623static int bluetooth_write(char *buf)
4624{
4625 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4626}
4627
4628static struct ibm_struct bluetooth_driver_data = {
4629 .name = "bluetooth",
4630 .read = bluetooth_read,
4631 .write = bluetooth_write,
4632 .exit = bluetooth_exit,
4633 .shutdown = bluetooth_shutdown,
4634};
4635
4636/*************************************************************************
4637 * Wan subdriver
4638 */
4639
4640enum {
4641 /* ACPI GWAN/SWAN bits */
4642 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4643 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4644 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4645 0 = disable, 1 = enable */
4646};
4647
4648#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4649
4650static int wan_get_status(void)
4651{
4652 int status;
4653
4654#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4655 if (dbg_wwanemul)
4656 return (tpacpi_wwan_emulstate) ?
4657 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4658#endif
4659
4660 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4661 return -EIO;
4662
4663 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4664 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4665}
4666
4667static int wan_set_status(enum tpacpi_rfkill_state state)
4668{
4669 int status;
4670
4671 vdbg_printk(TPACPI_DBG_RFKILL,
4672 "will attempt to %s wwan\n",
4673 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4674
4675#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4676 if (dbg_wwanemul) {
4677 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4678 return 0;
4679 }
4680#endif
4681
4682 if (state == TPACPI_RFK_RADIO_ON)
4683 status = TP_ACPI_WANCARD_RADIOSSW
4684 | TP_ACPI_WANCARD_RESUMECTRL;
4685 else
4686 status = 0;
4687
4688 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4689 return -EIO;
4690
4691 return 0;
4692}
4693
4694/* sysfs wan enable ---------------------------------------------------- */
4695static ssize_t wan_enable_show(struct device *dev,
4696 struct device_attribute *attr,
4697 char *buf)
4698{
4699 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4700 attr, buf);
4701}
4702
4703static ssize_t wan_enable_store(struct device *dev,
4704 struct device_attribute *attr,
4705 const char *buf, size_t count)
4706{
4707 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4708 attr, buf, count);
4709}
4710
4711static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4712 wan_enable_show, wan_enable_store);
4713
4714/* --------------------------------------------------------------------- */
4715
4716static struct attribute *wan_attributes[] = {
4717 &dev_attr_wwan_enable.attr,
4718 NULL
4719};
4720
4721static const struct attribute_group wan_attr_group = {
4722 .attrs = wan_attributes,
4723};
4724
4725static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4726 .get_status = wan_get_status,
4727 .set_status = wan_set_status,
4728};
4729
4730static void wan_shutdown(void)
4731{
4732 /* Order firmware to save current state to NVRAM */
4733 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4734 TP_ACPI_WGSV_SAVE_STATE))
4735 pr_notice("failed to save WWAN state to NVRAM\n");
4736 else
4737 vdbg_printk(TPACPI_DBG_RFKILL,
4738 "WWAN state saved to NVRAM\n");
4739}
4740
4741static void wan_exit(void)
4742{
4743 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4744 &wan_attr_group);
4745
4746 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4747
4748 wan_shutdown();
4749}
4750
4751static int __init wan_init(struct ibm_init_struct *iibm)
4752{
4753 int res;
4754 int status = 0;
4755
4756 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4757 "initializing wan subdriver\n");
4758
4759 TPACPI_ACPIHANDLE_INIT(hkey);
4760
4761 tp_features.wan = hkey_handle &&
4762 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4763
4764 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4765 "wan is %s, status 0x%02x\n",
4766 str_supported(tp_features.wan),
4767 status);
4768
4769#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4770 if (dbg_wwanemul) {
4771 tp_features.wan = 1;
4772 pr_info("wwan switch emulation enabled\n");
4773 } else
4774#endif
4775 if (tp_features.wan &&
4776 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4777 /* no wan hardware present in system */
4778 tp_features.wan = 0;
4779 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4780 "wan hardware not installed\n");
4781 }
4782
4783 if (!tp_features.wan)
4784 return 1;
4785
4786 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4787 &wan_tprfk_ops,
4788 RFKILL_TYPE_WWAN,
4789 TPACPI_RFK_WWAN_SW_NAME,
4790 true);
4791 if (res)
4792 return res;
4793
4794 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4795 &wan_attr_group);
4796
4797 if (res) {
4798 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4799 return res;
4800 }
4801
4802 return 0;
4803}
4804
4805/* procfs -------------------------------------------------------------- */
4806static int wan_read(struct seq_file *m)
4807{
4808 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4809}
4810
4811static int wan_write(char *buf)
4812{
4813 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4814}
4815
4816static struct ibm_struct wan_driver_data = {
4817 .name = "wan",
4818 .read = wan_read,
4819 .write = wan_write,
4820 .exit = wan_exit,
4821 .shutdown = wan_shutdown,
4822};
4823
4824/*************************************************************************
4825 * UWB subdriver
4826 */
4827
4828enum {
4829 /* ACPI GUWB/SUWB bits */
4830 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4831 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4832};
4833
4834#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4835
4836static int uwb_get_status(void)
4837{
4838 int status;
4839
4840#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4841 if (dbg_uwbemul)
4842 return (tpacpi_uwb_emulstate) ?
4843 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4844#endif
4845
4846 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4847 return -EIO;
4848
4849 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4850 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4851}
4852
4853static int uwb_set_status(enum tpacpi_rfkill_state state)
4854{
4855 int status;
4856
4857 vdbg_printk(TPACPI_DBG_RFKILL,
4858 "will attempt to %s UWB\n",
4859 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4860
4861#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4862 if (dbg_uwbemul) {
4863 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4864 return 0;
4865 }
4866#endif
4867
4868 if (state == TPACPI_RFK_RADIO_ON)
4869 status = TP_ACPI_UWB_RADIOSSW;
4870 else
4871 status = 0;
4872
4873 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4874 return -EIO;
4875
4876 return 0;
4877}
4878
4879/* --------------------------------------------------------------------- */
4880
4881static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4882 .get_status = uwb_get_status,
4883 .set_status = uwb_set_status,
4884};
4885
4886static void uwb_exit(void)
4887{
4888 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4889}
4890
4891static int __init uwb_init(struct ibm_init_struct *iibm)
4892{
4893 int res;
4894 int status = 0;
4895
4896 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4897 "initializing uwb subdriver\n");
4898
4899 TPACPI_ACPIHANDLE_INIT(hkey);
4900
4901 tp_features.uwb = hkey_handle &&
4902 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4903
4904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4905 "uwb is %s, status 0x%02x\n",
4906 str_supported(tp_features.uwb),
4907 status);
4908
4909#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4910 if (dbg_uwbemul) {
4911 tp_features.uwb = 1;
4912 pr_info("uwb switch emulation enabled\n");
4913 } else
4914#endif
4915 if (tp_features.uwb &&
4916 !(status & TP_ACPI_UWB_HWPRESENT)) {
4917 /* no uwb hardware present in system */
4918 tp_features.uwb = 0;
4919 dbg_printk(TPACPI_DBG_INIT,
4920 "uwb hardware not installed\n");
4921 }
4922
4923 if (!tp_features.uwb)
4924 return 1;
4925
4926 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4927 &uwb_tprfk_ops,
4928 RFKILL_TYPE_UWB,
4929 TPACPI_RFK_UWB_SW_NAME,
4930 false);
4931 return res;
4932}
4933
4934static struct ibm_struct uwb_driver_data = {
4935 .name = "uwb",
4936 .exit = uwb_exit,
4937 .flags.experimental = 1,
4938};
4939
4940/*************************************************************************
4941 * Video subdriver
4942 */
4943
4944#ifdef CONFIG_THINKPAD_ACPI_VIDEO
4945
4946enum video_access_mode {
4947 TPACPI_VIDEO_NONE = 0,
4948 TPACPI_VIDEO_570, /* 570 */
4949 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4950 TPACPI_VIDEO_NEW, /* all others */
4951};
4952
4953enum { /* video status flags, based on VIDEO_570 */
4954 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4955 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4956 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4957};
4958
4959enum { /* TPACPI_VIDEO_570 constants */
4960 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4961 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4962 * video_status_flags */
4963 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4964 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4965};
4966
4967static enum video_access_mode video_supported;
4968static int video_orig_autosw;
4969
4970static int video_autosw_get(void);
4971static int video_autosw_set(int enable);
4972
4973TPACPI_HANDLE(vid, root,
4974 "\\_SB.PCI.AGP.VGA", /* 570 */
4975 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4976 "\\_SB.PCI0.VID0", /* 770e */
4977 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4978 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4979 "\\_SB.PCI0.AGP.VID", /* all others */
4980 ); /* R30, R31 */
4981
4982TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4983
4984static int __init video_init(struct ibm_init_struct *iibm)
4985{
4986 int ivga;
4987
4988 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4989
4990 TPACPI_ACPIHANDLE_INIT(vid);
4991 if (tpacpi_is_ibm())
4992 TPACPI_ACPIHANDLE_INIT(vid2);
4993
4994 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4995 /* G41, assume IVGA doesn't change */
4996 vid_handle = vid2_handle;
4997
4998 if (!vid_handle)
4999 /* video switching not supported on R30, R31 */
5000 video_supported = TPACPI_VIDEO_NONE;
5001 else if (tpacpi_is_ibm() &&
5002 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
5003 /* 570 */
5004 video_supported = TPACPI_VIDEO_570;
5005 else if (tpacpi_is_ibm() &&
5006 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
5007 /* 600e/x, 770e, 770x */
5008 video_supported = TPACPI_VIDEO_770;
5009 else
5010 /* all others */
5011 video_supported = TPACPI_VIDEO_NEW;
5012
5013 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5014 str_supported(video_supported != TPACPI_VIDEO_NONE),
5015 video_supported);
5016
5017 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
5018}
5019
5020static void video_exit(void)
5021{
5022 dbg_printk(TPACPI_DBG_EXIT,
5023 "restoring original video autoswitch mode\n");
5024 if (video_autosw_set(video_orig_autosw))
Googler9398cc32022-12-02 17:21:52 +08005025 pr_err("error while trying to restore original video autoswitch mode\n");
Googleraf606d22022-10-26 21:40:12 -07005026}
5027
5028static int video_outputsw_get(void)
5029{
5030 int status = 0;
5031 int i;
5032
5033 switch (video_supported) {
5034 case TPACPI_VIDEO_570:
5035 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5036 TP_ACPI_VIDEO_570_PHSCMD))
5037 return -EIO;
5038 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5039 break;
5040 case TPACPI_VIDEO_770:
5041 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5042 return -EIO;
5043 if (i)
5044 status |= TP_ACPI_VIDEO_S_LCD;
5045 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5046 return -EIO;
5047 if (i)
5048 status |= TP_ACPI_VIDEO_S_CRT;
5049 break;
5050 case TPACPI_VIDEO_NEW:
5051 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5052 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5053 return -EIO;
5054 if (i)
5055 status |= TP_ACPI_VIDEO_S_CRT;
5056
5057 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5058 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5059 return -EIO;
5060 if (i)
5061 status |= TP_ACPI_VIDEO_S_LCD;
5062 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5063 return -EIO;
5064 if (i)
5065 status |= TP_ACPI_VIDEO_S_DVI;
5066 break;
5067 default:
5068 return -ENOSYS;
5069 }
5070
5071 return status;
5072}
5073
5074static int video_outputsw_set(int status)
5075{
5076 int autosw;
5077 int res = 0;
5078
5079 switch (video_supported) {
5080 case TPACPI_VIDEO_570:
5081 res = acpi_evalf(NULL, NULL,
5082 "\\_SB.PHS2", "vdd",
5083 TP_ACPI_VIDEO_570_PHS2CMD,
5084 status | TP_ACPI_VIDEO_570_PHS2SET);
5085 break;
5086 case TPACPI_VIDEO_770:
5087 autosw = video_autosw_get();
5088 if (autosw < 0)
5089 return autosw;
5090
5091 res = video_autosw_set(1);
5092 if (res)
5093 return res;
5094 res = acpi_evalf(vid_handle, NULL,
5095 "ASWT", "vdd", status * 0x100, 0);
5096 if (!autosw && video_autosw_set(autosw)) {
5097 pr_err("video auto-switch left enabled due to error\n");
5098 return -EIO;
5099 }
5100 break;
5101 case TPACPI_VIDEO_NEW:
5102 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5103 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5104 break;
5105 default:
5106 return -ENOSYS;
5107 }
5108
5109 return (res) ? 0 : -EIO;
5110}
5111
5112static int video_autosw_get(void)
5113{
5114 int autosw = 0;
5115
5116 switch (video_supported) {
5117 case TPACPI_VIDEO_570:
5118 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5119 return -EIO;
5120 break;
5121 case TPACPI_VIDEO_770:
5122 case TPACPI_VIDEO_NEW:
5123 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5124 return -EIO;
5125 break;
5126 default:
5127 return -ENOSYS;
5128 }
5129
5130 return autosw & 1;
5131}
5132
5133static int video_autosw_set(int enable)
5134{
5135 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5136 return -EIO;
5137 return 0;
5138}
5139
5140static int video_outputsw_cycle(void)
5141{
5142 int autosw = video_autosw_get();
5143 int res;
5144
5145 if (autosw < 0)
5146 return autosw;
5147
5148 switch (video_supported) {
5149 case TPACPI_VIDEO_570:
5150 res = video_autosw_set(1);
5151 if (res)
5152 return res;
5153 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5154 break;
5155 case TPACPI_VIDEO_770:
5156 case TPACPI_VIDEO_NEW:
5157 res = video_autosw_set(1);
5158 if (res)
5159 return res;
5160 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5161 break;
5162 default:
5163 return -ENOSYS;
5164 }
5165 if (!autosw && video_autosw_set(autosw)) {
5166 pr_err("video auto-switch left enabled due to error\n");
5167 return -EIO;
5168 }
5169
5170 return (res) ? 0 : -EIO;
5171}
5172
5173static int video_expand_toggle(void)
5174{
5175 switch (video_supported) {
5176 case TPACPI_VIDEO_570:
5177 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5178 0 : -EIO;
5179 case TPACPI_VIDEO_770:
5180 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5181 0 : -EIO;
5182 case TPACPI_VIDEO_NEW:
5183 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5184 0 : -EIO;
5185 default:
5186 return -ENOSYS;
5187 }
5188 /* not reached */
5189}
5190
5191static int video_read(struct seq_file *m)
5192{
5193 int status, autosw;
5194
5195 if (video_supported == TPACPI_VIDEO_NONE) {
5196 seq_printf(m, "status:\t\tnot supported\n");
5197 return 0;
5198 }
5199
5200 /* Even reads can crash X.org, so... */
5201 if (!capable(CAP_SYS_ADMIN))
5202 return -EPERM;
5203
5204 status = video_outputsw_get();
5205 if (status < 0)
5206 return status;
5207
5208 autosw = video_autosw_get();
5209 if (autosw < 0)
5210 return autosw;
5211
5212 seq_printf(m, "status:\t\tsupported\n");
5213 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5214 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5215 if (video_supported == TPACPI_VIDEO_NEW)
5216 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5217 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5218 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5219 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5220 if (video_supported == TPACPI_VIDEO_NEW)
5221 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5222 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5223 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5224
5225 return 0;
5226}
5227
5228static int video_write(char *buf)
5229{
5230 char *cmd;
5231 int enable, disable, status;
5232 int res;
5233
5234 if (video_supported == TPACPI_VIDEO_NONE)
5235 return -ENODEV;
5236
5237 /* Even reads can crash X.org, let alone writes... */
5238 if (!capable(CAP_SYS_ADMIN))
5239 return -EPERM;
5240
5241 enable = 0;
5242 disable = 0;
5243
5244 while ((cmd = next_cmd(&buf))) {
5245 if (strlencmp(cmd, "lcd_enable") == 0) {
5246 enable |= TP_ACPI_VIDEO_S_LCD;
5247 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5248 disable |= TP_ACPI_VIDEO_S_LCD;
5249 } else if (strlencmp(cmd, "crt_enable") == 0) {
5250 enable |= TP_ACPI_VIDEO_S_CRT;
5251 } else if (strlencmp(cmd, "crt_disable") == 0) {
5252 disable |= TP_ACPI_VIDEO_S_CRT;
5253 } else if (video_supported == TPACPI_VIDEO_NEW &&
5254 strlencmp(cmd, "dvi_enable") == 0) {
5255 enable |= TP_ACPI_VIDEO_S_DVI;
5256 } else if (video_supported == TPACPI_VIDEO_NEW &&
5257 strlencmp(cmd, "dvi_disable") == 0) {
5258 disable |= TP_ACPI_VIDEO_S_DVI;
5259 } else if (strlencmp(cmd, "auto_enable") == 0) {
5260 res = video_autosw_set(1);
5261 if (res)
5262 return res;
5263 } else if (strlencmp(cmd, "auto_disable") == 0) {
5264 res = video_autosw_set(0);
5265 if (res)
5266 return res;
5267 } else if (strlencmp(cmd, "video_switch") == 0) {
5268 res = video_outputsw_cycle();
5269 if (res)
5270 return res;
5271 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5272 res = video_expand_toggle();
5273 if (res)
5274 return res;
5275 } else
5276 return -EINVAL;
5277 }
5278
5279 if (enable || disable) {
5280 status = video_outputsw_get();
5281 if (status < 0)
5282 return status;
5283 res = video_outputsw_set((status & ~disable) | enable);
5284 if (res)
5285 return res;
5286 }
5287
5288 return 0;
5289}
5290
5291static struct ibm_struct video_driver_data = {
5292 .name = "video",
5293 .read = video_read,
5294 .write = video_write,
5295 .exit = video_exit,
5296};
5297
5298#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5299
5300/*************************************************************************
5301 * Keyboard backlight subdriver
5302 */
5303
Googler9398cc32022-12-02 17:21:52 +08005304static enum led_brightness kbdlight_brightness;
5305static DEFINE_MUTEX(kbdlight_mutex);
5306
Googleraf606d22022-10-26 21:40:12 -07005307static int kbdlight_set_level(int level)
5308{
Googler9398cc32022-12-02 17:21:52 +08005309 int ret = 0;
5310
Googleraf606d22022-10-26 21:40:12 -07005311 if (!hkey_handle)
5312 return -ENXIO;
5313
Googler9398cc32022-12-02 17:21:52 +08005314 mutex_lock(&kbdlight_mutex);
5315
Googleraf606d22022-10-26 21:40:12 -07005316 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
Googler9398cc32022-12-02 17:21:52 +08005317 ret = -EIO;
5318 else
5319 kbdlight_brightness = level;
Googleraf606d22022-10-26 21:40:12 -07005320
Googler9398cc32022-12-02 17:21:52 +08005321 mutex_unlock(&kbdlight_mutex);
5322
5323 return ret;
Googleraf606d22022-10-26 21:40:12 -07005324}
5325
5326static int kbdlight_get_level(void)
5327{
5328 int status = 0;
5329
5330 if (!hkey_handle)
5331 return -ENXIO;
5332
5333 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5334 return -EIO;
5335
5336 if (status < 0)
5337 return status;
5338
5339 return status & 0x3;
5340}
5341
5342static bool kbdlight_is_supported(void)
5343{
5344 int status = 0;
5345
5346 if (!hkey_handle)
5347 return false;
5348
5349 if (!acpi_has_method(hkey_handle, "MLCG")) {
5350 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5351 return false;
5352 }
5353
5354 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5355 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5356 return false;
5357 }
5358
5359 if (status < 0) {
5360 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5361 return false;
5362 }
5363
5364 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5365 /*
5366 * Guessed test for keyboard backlight:
5367 *
5368 * Machines with backlight keyboard return:
5369 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5370 * b110100010010000000XX - ThinkPad x230
5371 * b010100000010000000XX - ThinkPad x240
5372 * b010100000010000000XX - ThinkPad W541
5373 * (XX is current backlight level)
5374 *
5375 * Machines without backlight keyboard return:
5376 * b10100001000000000000 - ThinkPad x230
5377 * b10110001000000000000 - ThinkPad E430
5378 * b00000000000000000000 - ThinkPad E450
5379 *
5380 * Candidate BITs for detection test (XOR):
5381 * b01000000001000000000
5382 * ^
5383 */
5384 return status & BIT(9);
5385}
5386
Googler9398cc32022-12-02 17:21:52 +08005387static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
Googleraf606d22022-10-26 21:40:12 -07005388 enum led_brightness brightness)
5389{
Googler9398cc32022-12-02 17:21:52 +08005390 return kbdlight_set_level(brightness);
Googleraf606d22022-10-26 21:40:12 -07005391}
5392
5393static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5394{
5395 int level;
5396
5397 level = kbdlight_get_level();
5398 if (level < 0)
5399 return 0;
5400
5401 return level;
5402}
5403
5404static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5405 .led_classdev = {
5406 .name = "tpacpi::kbd_backlight",
5407 .max_brightness = 2,
Googler9398cc32022-12-02 17:21:52 +08005408 .flags = LED_BRIGHT_HW_CHANGED,
5409 .brightness_set_blocking = &kbdlight_sysfs_set,
Googleraf606d22022-10-26 21:40:12 -07005410 .brightness_get = &kbdlight_sysfs_get,
5411 }
5412};
5413
5414static int __init kbdlight_init(struct ibm_init_struct *iibm)
5415{
5416 int rc;
5417
5418 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5419
5420 TPACPI_ACPIHANDLE_INIT(hkey);
5421
5422 if (!kbdlight_is_supported()) {
5423 tp_features.kbdlight = 0;
5424 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5425 return 1;
5426 }
5427
Googler9398cc32022-12-02 17:21:52 +08005428 kbdlight_brightness = kbdlight_sysfs_get(NULL);
Googleraf606d22022-10-26 21:40:12 -07005429 tp_features.kbdlight = 1;
5430
5431 rc = led_classdev_register(&tpacpi_pdev->dev,
5432 &tpacpi_led_kbdlight.led_classdev);
5433 if (rc < 0) {
5434 tp_features.kbdlight = 0;
5435 return rc;
5436 }
5437
Googler9398cc32022-12-02 17:21:52 +08005438 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5439 TP_ACPI_HKEY_KBD_LIGHT_MASK);
Googleraf606d22022-10-26 21:40:12 -07005440 return 0;
5441}
5442
5443static void kbdlight_exit(void)
5444{
5445 if (tp_features.kbdlight)
5446 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5447}
5448
5449static int kbdlight_set_level_and_update(int level)
5450{
5451 int ret;
5452 struct led_classdev *led_cdev;
5453
5454 ret = kbdlight_set_level(level);
5455 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5456
5457 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5458 led_cdev->brightness = level;
5459
5460 return ret;
5461}
5462
5463static int kbdlight_read(struct seq_file *m)
5464{
5465 int level;
5466
5467 if (!tp_features.kbdlight) {
5468 seq_printf(m, "status:\t\tnot supported\n");
5469 } else {
5470 level = kbdlight_get_level();
5471 if (level < 0)
5472 seq_printf(m, "status:\t\terror %d\n", level);
5473 else
5474 seq_printf(m, "status:\t\t%d\n", level);
5475 seq_printf(m, "commands:\t0, 1, 2\n");
5476 }
5477
5478 return 0;
5479}
5480
5481static int kbdlight_write(char *buf)
5482{
5483 char *cmd;
5484 int level = -1;
5485
5486 if (!tp_features.kbdlight)
5487 return -ENODEV;
5488
5489 while ((cmd = next_cmd(&buf))) {
5490 if (strlencmp(cmd, "0") == 0)
5491 level = 0;
5492 else if (strlencmp(cmd, "1") == 0)
5493 level = 1;
5494 else if (strlencmp(cmd, "2") == 0)
5495 level = 2;
5496 else
5497 return -EINVAL;
5498 }
5499
5500 if (level == -1)
5501 return -EINVAL;
5502
5503 return kbdlight_set_level_and_update(level);
5504}
5505
5506static void kbdlight_suspend(void)
5507{
5508 struct led_classdev *led_cdev;
5509
5510 if (!tp_features.kbdlight)
5511 return;
5512
5513 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5514 led_update_brightness(led_cdev);
5515 led_classdev_suspend(led_cdev);
5516}
5517
5518static void kbdlight_resume(void)
5519{
5520 if (!tp_features.kbdlight)
5521 return;
5522
5523 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5524}
5525
5526static struct ibm_struct kbdlight_driver_data = {
5527 .name = "kbdlight",
5528 .read = kbdlight_read,
5529 .write = kbdlight_write,
5530 .suspend = kbdlight_suspend,
5531 .resume = kbdlight_resume,
5532 .exit = kbdlight_exit,
5533};
5534
5535/*************************************************************************
5536 * Light (thinklight) subdriver
5537 */
5538
5539TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5540TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5541
5542static int light_get_status(void)
5543{
5544 int status = 0;
5545
5546 if (tp_features.light_status) {
5547 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5548 return -EIO;
5549 return (!!status);
5550 }
5551
5552 return -ENXIO;
5553}
5554
5555static int light_set_status(int status)
5556{
5557 int rc;
5558
5559 if (tp_features.light) {
5560 if (cmos_handle) {
5561 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5562 (status) ?
5563 TP_CMOS_THINKLIGHT_ON :
5564 TP_CMOS_THINKLIGHT_OFF);
5565 } else {
5566 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5567 (status) ? 1 : 0);
5568 }
5569 return (rc) ? 0 : -EIO;
5570 }
5571
5572 return -ENXIO;
5573}
5574
Googler9398cc32022-12-02 17:21:52 +08005575static int light_sysfs_set(struct led_classdev *led_cdev,
Googleraf606d22022-10-26 21:40:12 -07005576 enum led_brightness brightness)
5577{
Googler9398cc32022-12-02 17:21:52 +08005578 return light_set_status((brightness != LED_OFF) ?
5579 TPACPI_LED_ON : TPACPI_LED_OFF);
Googleraf606d22022-10-26 21:40:12 -07005580}
5581
5582static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5583{
5584 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5585}
5586
5587static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5588 .led_classdev = {
5589 .name = "tpacpi::thinklight",
Googler9398cc32022-12-02 17:21:52 +08005590 .brightness_set_blocking = &light_sysfs_set,
Googleraf606d22022-10-26 21:40:12 -07005591 .brightness_get = &light_sysfs_get,
5592 }
5593};
5594
5595static int __init light_init(struct ibm_init_struct *iibm)
5596{
5597 int rc;
5598
5599 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5600
5601 if (tpacpi_is_ibm()) {
5602 TPACPI_ACPIHANDLE_INIT(ledb);
5603 TPACPI_ACPIHANDLE_INIT(lght);
5604 }
5605 TPACPI_ACPIHANDLE_INIT(cmos);
5606
5607 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5608 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5609
5610 if (tp_features.light)
5611 /* light status not supported on
5612 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5613 tp_features.light_status =
5614 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5615
5616 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5617 str_supported(tp_features.light),
5618 str_supported(tp_features.light_status));
5619
5620 if (!tp_features.light)
5621 return 1;
5622
5623 rc = led_classdev_register(&tpacpi_pdev->dev,
5624 &tpacpi_led_thinklight.led_classdev);
5625
5626 if (rc < 0) {
5627 tp_features.light = 0;
5628 tp_features.light_status = 0;
5629 } else {
5630 rc = 0;
5631 }
5632
5633 return rc;
5634}
5635
5636static void light_exit(void)
5637{
5638 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5639}
5640
5641static int light_read(struct seq_file *m)
5642{
5643 int status;
5644
5645 if (!tp_features.light) {
5646 seq_printf(m, "status:\t\tnot supported\n");
5647 } else if (!tp_features.light_status) {
5648 seq_printf(m, "status:\t\tunknown\n");
5649 seq_printf(m, "commands:\ton, off\n");
5650 } else {
5651 status = light_get_status();
5652 if (status < 0)
5653 return status;
5654 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5655 seq_printf(m, "commands:\ton, off\n");
5656 }
5657
5658 return 0;
5659}
5660
5661static int light_write(char *buf)
5662{
5663 char *cmd;
5664 int newstatus = 0;
5665
5666 if (!tp_features.light)
5667 return -ENODEV;
5668
5669 while ((cmd = next_cmd(&buf))) {
5670 if (strlencmp(cmd, "on") == 0) {
5671 newstatus = 1;
5672 } else if (strlencmp(cmd, "off") == 0) {
5673 newstatus = 0;
5674 } else
5675 return -EINVAL;
5676 }
5677
5678 return light_set_status(newstatus);
5679}
5680
5681static struct ibm_struct light_driver_data = {
5682 .name = "light",
5683 .read = light_read,
5684 .write = light_write,
5685 .exit = light_exit,
5686};
5687
5688/*************************************************************************
5689 * CMOS subdriver
5690 */
5691
5692/* sysfs cmos_command -------------------------------------------------- */
5693static ssize_t cmos_command_store(struct device *dev,
5694 struct device_attribute *attr,
5695 const char *buf, size_t count)
5696{
5697 unsigned long cmos_cmd;
5698 int res;
5699
5700 if (parse_strtoul(buf, 21, &cmos_cmd))
5701 return -EINVAL;
5702
5703 res = issue_thinkpad_cmos_command(cmos_cmd);
5704 return (res) ? res : count;
5705}
5706
5707static DEVICE_ATTR_WO(cmos_command);
5708
5709/* --------------------------------------------------------------------- */
5710
5711static int __init cmos_init(struct ibm_init_struct *iibm)
5712{
5713 int res;
5714
5715 vdbg_printk(TPACPI_DBG_INIT,
5716 "initializing cmos commands subdriver\n");
5717
5718 TPACPI_ACPIHANDLE_INIT(cmos);
5719
5720 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5721 str_supported(cmos_handle != NULL));
5722
5723 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5724 if (res)
5725 return res;
5726
5727 return (cmos_handle) ? 0 : 1;
5728}
5729
5730static void cmos_exit(void)
5731{
5732 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5733}
5734
5735static int cmos_read(struct seq_file *m)
5736{
5737 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5738 R30, R31, T20-22, X20-21 */
5739 if (!cmos_handle)
5740 seq_printf(m, "status:\t\tnot supported\n");
5741 else {
5742 seq_printf(m, "status:\t\tsupported\n");
5743 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5744 }
5745
5746 return 0;
5747}
5748
5749static int cmos_write(char *buf)
5750{
5751 char *cmd;
5752 int cmos_cmd, res;
5753
5754 while ((cmd = next_cmd(&buf))) {
5755 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5756 cmos_cmd >= 0 && cmos_cmd <= 21) {
5757 /* cmos_cmd set */
5758 } else
5759 return -EINVAL;
5760
5761 res = issue_thinkpad_cmos_command(cmos_cmd);
5762 if (res)
5763 return res;
5764 }
5765
5766 return 0;
5767}
5768
5769static struct ibm_struct cmos_driver_data = {
5770 .name = "cmos",
5771 .read = cmos_read,
5772 .write = cmos_write,
5773 .exit = cmos_exit,
5774};
5775
5776/*************************************************************************
5777 * LED subdriver
5778 */
5779
5780enum led_access_mode {
5781 TPACPI_LED_NONE = 0,
5782 TPACPI_LED_570, /* 570 */
5783 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5784 TPACPI_LED_NEW, /* all others */
5785};
5786
5787enum { /* For TPACPI_LED_OLD */
5788 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5789 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5790 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5791};
5792
5793static enum led_access_mode led_supported;
5794
5795static acpi_handle led_handle;
5796
5797#define TPACPI_LED_NUMLEDS 16
5798static struct tpacpi_led_classdev *tpacpi_leds;
5799static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5800static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5801 /* there's a limit of 19 chars + NULL before 2.6.26 */
5802 "tpacpi::power",
5803 "tpacpi:orange:batt",
5804 "tpacpi:green:batt",
5805 "tpacpi::dock_active",
5806 "tpacpi::bay_active",
5807 "tpacpi::dock_batt",
5808 "tpacpi::unknown_led",
5809 "tpacpi::standby",
5810 "tpacpi::dock_status1",
5811 "tpacpi::dock_status2",
5812 "tpacpi::unknown_led2",
5813 "tpacpi::unknown_led3",
5814 "tpacpi::thinkvantage",
5815};
5816#define TPACPI_SAFE_LEDS 0x1081U
5817
5818static inline bool tpacpi_is_led_restricted(const unsigned int led)
5819{
5820#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5821 return false;
5822#else
5823 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5824#endif
5825}
5826
5827static int led_get_status(const unsigned int led)
5828{
5829 int status;
5830 enum led_status_t led_s;
5831
5832 switch (led_supported) {
5833 case TPACPI_LED_570:
5834 if (!acpi_evalf(ec_handle,
5835 &status, "GLED", "dd", 1 << led))
5836 return -EIO;
5837 led_s = (status == 0) ?
5838 TPACPI_LED_OFF :
5839 ((status == 1) ?
5840 TPACPI_LED_ON :
5841 TPACPI_LED_BLINK);
5842 tpacpi_led_state_cache[led] = led_s;
5843 return led_s;
5844 default:
5845 return -ENXIO;
5846 }
5847
5848 /* not reached */
5849}
5850
5851static int led_set_status(const unsigned int led,
5852 const enum led_status_t ledstatus)
5853{
5854 /* off, on, blink. Index is led_status_t */
5855 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5856 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5857
5858 int rc = 0;
5859
5860 switch (led_supported) {
5861 case TPACPI_LED_570:
5862 /* 570 */
5863 if (unlikely(led > 7))
5864 return -EINVAL;
5865 if (unlikely(tpacpi_is_led_restricted(led)))
5866 return -EPERM;
5867 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5868 (1 << led), led_sled_arg1[ledstatus]))
Googler9398cc32022-12-02 17:21:52 +08005869 return -EIO;
Googleraf606d22022-10-26 21:40:12 -07005870 break;
5871 case TPACPI_LED_OLD:
5872 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5873 if (unlikely(led > 7))
5874 return -EINVAL;
5875 if (unlikely(tpacpi_is_led_restricted(led)))
5876 return -EPERM;
5877 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5878 if (rc >= 0)
5879 rc = ec_write(TPACPI_LED_EC_HLBL,
5880 (ledstatus == TPACPI_LED_BLINK) << led);
5881 if (rc >= 0)
5882 rc = ec_write(TPACPI_LED_EC_HLCL,
5883 (ledstatus != TPACPI_LED_OFF) << led);
5884 break;
5885 case TPACPI_LED_NEW:
5886 /* all others */
5887 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5888 return -EINVAL;
5889 if (unlikely(tpacpi_is_led_restricted(led)))
5890 return -EPERM;
5891 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5892 led, led_led_arg1[ledstatus]))
Googler9398cc32022-12-02 17:21:52 +08005893 return -EIO;
Googleraf606d22022-10-26 21:40:12 -07005894 break;
5895 default:
Googler9398cc32022-12-02 17:21:52 +08005896 return -ENXIO;
Googleraf606d22022-10-26 21:40:12 -07005897 }
5898
5899 if (!rc)
5900 tpacpi_led_state_cache[led] = ledstatus;
5901
5902 return rc;
5903}
5904
Googler9398cc32022-12-02 17:21:52 +08005905static int led_sysfs_set(struct led_classdev *led_cdev,
Googleraf606d22022-10-26 21:40:12 -07005906 enum led_brightness brightness)
5907{
5908 struct tpacpi_led_classdev *data = container_of(led_cdev,
5909 struct tpacpi_led_classdev, led_classdev);
Googler9398cc32022-12-02 17:21:52 +08005910 enum led_status_t new_state;
Googleraf606d22022-10-26 21:40:12 -07005911
5912 if (brightness == LED_OFF)
Googler9398cc32022-12-02 17:21:52 +08005913 new_state = TPACPI_LED_OFF;
Googleraf606d22022-10-26 21:40:12 -07005914 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
Googler9398cc32022-12-02 17:21:52 +08005915 new_state = TPACPI_LED_ON;
Googleraf606d22022-10-26 21:40:12 -07005916 else
Googler9398cc32022-12-02 17:21:52 +08005917 new_state = TPACPI_LED_BLINK;
Googleraf606d22022-10-26 21:40:12 -07005918
Googler9398cc32022-12-02 17:21:52 +08005919 return led_set_status(data->led, new_state);
Googleraf606d22022-10-26 21:40:12 -07005920}
5921
5922static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5923 unsigned long *delay_on, unsigned long *delay_off)
5924{
5925 struct tpacpi_led_classdev *data = container_of(led_cdev,
5926 struct tpacpi_led_classdev, led_classdev);
5927
5928 /* Can we choose the flash rate? */
5929 if (*delay_on == 0 && *delay_off == 0) {
5930 /* yes. set them to the hardware blink rate (1 Hz) */
5931 *delay_on = 500; /* ms */
5932 *delay_off = 500; /* ms */
5933 } else if ((*delay_on != 500) || (*delay_off != 500))
5934 return -EINVAL;
5935
Googler9398cc32022-12-02 17:21:52 +08005936 return led_set_status(data->led, TPACPI_LED_BLINK);
Googleraf606d22022-10-26 21:40:12 -07005937}
5938
5939static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5940{
5941 int rc;
5942
5943 struct tpacpi_led_classdev *data = container_of(led_cdev,
5944 struct tpacpi_led_classdev, led_classdev);
5945
5946 rc = led_get_status(data->led);
5947
5948 if (rc == TPACPI_LED_OFF || rc < 0)
5949 rc = LED_OFF; /* no error handling in led class :( */
5950 else
5951 rc = LED_FULL;
5952
5953 return rc;
5954}
5955
5956static void led_exit(void)
5957{
5958 unsigned int i;
5959
5960 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5961 if (tpacpi_leds[i].led_classdev.name)
5962 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5963 }
5964
5965 kfree(tpacpi_leds);
5966}
5967
5968static int __init tpacpi_init_led(unsigned int led)
5969{
5970 int rc;
5971
5972 tpacpi_leds[led].led = led;
5973
5974 /* LEDs with no name don't get registered */
5975 if (!tpacpi_led_names[led])
5976 return 0;
5977
Googler9398cc32022-12-02 17:21:52 +08005978 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
Googleraf606d22022-10-26 21:40:12 -07005979 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5980 if (led_supported == TPACPI_LED_570)
5981 tpacpi_leds[led].led_classdev.brightness_get =
5982 &led_sysfs_get;
5983
5984 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5985
5986 rc = led_classdev_register(&tpacpi_pdev->dev,
5987 &tpacpi_leds[led].led_classdev);
5988 if (rc < 0)
5989 tpacpi_leds[led].led_classdev.name = NULL;
5990
5991 return rc;
5992}
5993
5994static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5995 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5996 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5997 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5998
5999 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
6000 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
6001 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
6002 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
6003 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
6004 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
6005 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
6006 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
6007
6008 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
6009 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
6010 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
6011 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
6012 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
6013
6014 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6015 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6016 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6017 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6018
6019 /* (1) - may have excess leds enabled on MSB */
6020
6021 /* Defaults (order matters, keep last, don't reorder!) */
6022 { /* Lenovo */
6023 .vendor = PCI_VENDOR_ID_LENOVO,
6024 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6025 .quirks = 0x1fffU,
6026 },
6027 { /* IBM ThinkPads with no EC version string */
6028 .vendor = PCI_VENDOR_ID_IBM,
6029 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6030 .quirks = 0x00ffU,
6031 },
6032 { /* IBM ThinkPads with EC version string */
6033 .vendor = PCI_VENDOR_ID_IBM,
6034 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6035 .quirks = 0x00bfU,
6036 },
6037};
6038
6039static enum led_access_mode __init led_init_detect_mode(void)
6040{
6041 acpi_status status;
6042
6043 if (tpacpi_is_ibm()) {
6044 /* 570 */
6045 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6046 if (ACPI_SUCCESS(status))
6047 return TPACPI_LED_570;
6048
6049 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6050 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6051 if (ACPI_SUCCESS(status))
6052 return TPACPI_LED_OLD;
6053 }
6054
6055 /* most others */
6056 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6057 if (ACPI_SUCCESS(status))
6058 return TPACPI_LED_NEW;
6059
6060 /* R30, R31, and unknown firmwares */
6061 led_handle = NULL;
6062 return TPACPI_LED_NONE;
6063}
6064
6065static int __init led_init(struct ibm_init_struct *iibm)
6066{
6067 unsigned int i;
6068 int rc;
6069 unsigned long useful_leds;
6070
6071 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6072
6073 led_supported = led_init_detect_mode();
6074
6075 if (led_supported != TPACPI_LED_NONE) {
6076 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6077 ARRAY_SIZE(led_useful_qtable));
6078
6079 if (!useful_leds) {
6080 led_handle = NULL;
6081 led_supported = TPACPI_LED_NONE;
6082 }
6083 }
6084
6085 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6086 str_supported(led_supported), led_supported);
6087
6088 if (led_supported == TPACPI_LED_NONE)
6089 return 1;
6090
Googler9398cc32022-12-02 17:21:52 +08006091 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
Googleraf606d22022-10-26 21:40:12 -07006092 GFP_KERNEL);
6093 if (!tpacpi_leds) {
6094 pr_err("Out of memory for LED data\n");
6095 return -ENOMEM;
6096 }
6097
6098 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6099 tpacpi_leds[i].led = -1;
6100
6101 if (!tpacpi_is_led_restricted(i) &&
6102 test_bit(i, &useful_leds)) {
6103 rc = tpacpi_init_led(i);
6104 if (rc < 0) {
6105 led_exit();
6106 return rc;
6107 }
6108 }
6109 }
6110
6111#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
Googler9398cc32022-12-02 17:21:52 +08006112 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
Googleraf606d22022-10-26 21:40:12 -07006113#endif
6114 return 0;
6115}
6116
6117#define str_led_status(s) \
6118 ((s) == TPACPI_LED_OFF ? "off" : \
6119 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6120
6121static int led_read(struct seq_file *m)
6122{
6123 if (!led_supported) {
6124 seq_printf(m, "status:\t\tnot supported\n");
6125 return 0;
6126 }
6127 seq_printf(m, "status:\t\tsupported\n");
6128
6129 if (led_supported == TPACPI_LED_570) {
6130 /* 570 */
6131 int i, status;
6132 for (i = 0; i < 8; i++) {
6133 status = led_get_status(i);
6134 if (status < 0)
6135 return -EIO;
6136 seq_printf(m, "%d:\t\t%s\n",
6137 i, str_led_status(status));
6138 }
6139 }
6140
Googler9398cc32022-12-02 17:21:52 +08006141 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
Googleraf606d22022-10-26 21:40:12 -07006142
6143 return 0;
6144}
6145
6146static int led_write(char *buf)
6147{
6148 char *cmd;
6149 int led, rc;
6150 enum led_status_t s;
6151
6152 if (!led_supported)
6153 return -ENODEV;
6154
6155 while ((cmd = next_cmd(&buf))) {
6156 if (sscanf(cmd, "%d", &led) != 1)
6157 return -EINVAL;
6158
6159 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6160 tpacpi_leds[led].led < 0)
6161 return -ENODEV;
6162
6163 if (strstr(cmd, "off")) {
6164 s = TPACPI_LED_OFF;
6165 } else if (strstr(cmd, "on")) {
6166 s = TPACPI_LED_ON;
6167 } else if (strstr(cmd, "blink")) {
6168 s = TPACPI_LED_BLINK;
6169 } else {
6170 return -EINVAL;
6171 }
6172
6173 rc = led_set_status(led, s);
6174 if (rc < 0)
6175 return rc;
6176 }
6177
6178 return 0;
6179}
6180
6181static struct ibm_struct led_driver_data = {
6182 .name = "led",
6183 .read = led_read,
6184 .write = led_write,
6185 .exit = led_exit,
6186};
6187
6188/*************************************************************************
6189 * Beep subdriver
6190 */
6191
6192TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6193
6194#define TPACPI_BEEP_Q1 0x0001
6195
6196static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6197 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6198 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6199};
6200
6201static int __init beep_init(struct ibm_init_struct *iibm)
6202{
6203 unsigned long quirks;
6204
6205 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6206
6207 TPACPI_ACPIHANDLE_INIT(beep);
6208
6209 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6210 str_supported(beep_handle != NULL));
6211
6212 quirks = tpacpi_check_quirks(beep_quirk_table,
6213 ARRAY_SIZE(beep_quirk_table));
6214
6215 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6216
6217 return (beep_handle) ? 0 : 1;
6218}
6219
6220static int beep_read(struct seq_file *m)
6221{
6222 if (!beep_handle)
6223 seq_printf(m, "status:\t\tnot supported\n");
6224 else {
6225 seq_printf(m, "status:\t\tsupported\n");
6226 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6227 }
6228
6229 return 0;
6230}
6231
6232static int beep_write(char *buf)
6233{
6234 char *cmd;
6235 int beep_cmd;
6236
6237 if (!beep_handle)
6238 return -ENODEV;
6239
6240 while ((cmd = next_cmd(&buf))) {
6241 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6242 beep_cmd >= 0 && beep_cmd <= 17) {
6243 /* beep_cmd set */
6244 } else
6245 return -EINVAL;
6246 if (tp_features.beep_needs_two_args) {
6247 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6248 beep_cmd, 0))
6249 return -EIO;
6250 } else {
6251 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6252 beep_cmd))
6253 return -EIO;
6254 }
6255 }
6256
6257 return 0;
6258}
6259
6260static struct ibm_struct beep_driver_data = {
6261 .name = "beep",
6262 .read = beep_read,
6263 .write = beep_write,
6264};
6265
6266/*************************************************************************
6267 * Thermal subdriver
6268 */
6269
6270enum thermal_access_mode {
6271 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6272 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6273 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6274 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6275 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6276};
6277
6278enum { /* TPACPI_THERMAL_TPEC_* */
6279 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6280 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6281 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6282
6283 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6284};
6285
6286
6287#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6288struct ibm_thermal_sensors_struct {
6289 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6290};
6291
6292static enum thermal_access_mode thermal_read_mode;
6293
6294/* idx is zero-based */
6295static int thermal_get_sensor(int idx, s32 *value)
6296{
6297 int t;
6298 s8 tmp;
6299 char tmpi[5];
6300
6301 t = TP_EC_THERMAL_TMP0;
6302
6303 switch (thermal_read_mode) {
6304#if TPACPI_MAX_THERMAL_SENSORS >= 16
6305 case TPACPI_THERMAL_TPEC_16:
6306 if (idx >= 8 && idx <= 15) {
6307 t = TP_EC_THERMAL_TMP8;
6308 idx -= 8;
6309 }
Googler012a81c2022-09-15 14:55:24 +08006310#endif
Googler9398cc32022-12-02 17:21:52 +08006311 /* fallthrough */
Googleraf606d22022-10-26 21:40:12 -07006312 case TPACPI_THERMAL_TPEC_8:
6313 if (idx <= 7) {
6314 if (!acpi_ec_read(t + idx, &tmp))
6315 return -EIO;
6316 *value = tmp * 1000;
6317 return 0;
6318 }
6319 break;
6320
6321 case TPACPI_THERMAL_ACPI_UPDT:
6322 if (idx <= 7) {
6323 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6324 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6325 return -EIO;
6326 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6327 return -EIO;
6328 *value = (t - 2732) * 100;
6329 return 0;
6330 }
6331 break;
6332
6333 case TPACPI_THERMAL_ACPI_TMP07:
6334 if (idx <= 7) {
6335 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6336 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6337 return -EIO;
6338 if (t > 127 || t < -127)
6339 t = TP_EC_THERMAL_TMP_NA;
6340 *value = t * 1000;
6341 return 0;
6342 }
6343 break;
6344
6345 case TPACPI_THERMAL_NONE:
6346 default:
6347 return -ENOSYS;
6348 }
6349
6350 return -EINVAL;
6351}
6352
6353static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6354{
6355 int res, i;
6356 int n;
6357
6358 n = 8;
6359 i = 0;
6360
6361 if (!s)
6362 return -EINVAL;
6363
6364 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6365 n = 16;
6366
6367 for (i = 0 ; i < n; i++) {
6368 res = thermal_get_sensor(i, &s->temp[i]);
6369 if (res)
6370 return res;
6371 }
6372
6373 return n;
6374}
6375
6376static void thermal_dump_all_sensors(void)
6377{
6378 int n, i;
6379 struct ibm_thermal_sensors_struct t;
6380
6381 n = thermal_get_sensors(&t);
6382 if (n <= 0)
6383 return;
6384
6385 pr_notice("temperatures (Celsius):");
6386
6387 for (i = 0; i < n; i++) {
6388 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6389 pr_cont(" %d", (int)(t.temp[i] / 1000));
6390 else
6391 pr_cont(" N/A");
6392 }
6393
6394 pr_cont("\n");
6395}
6396
6397/* sysfs temp##_input -------------------------------------------------- */
6398
6399static ssize_t thermal_temp_input_show(struct device *dev,
6400 struct device_attribute *attr,
6401 char *buf)
6402{
6403 struct sensor_device_attribute *sensor_attr =
6404 to_sensor_dev_attr(attr);
6405 int idx = sensor_attr->index;
6406 s32 value;
6407 int res;
6408
6409 res = thermal_get_sensor(idx, &value);
6410 if (res)
6411 return res;
6412 if (value == TPACPI_THERMAL_SENSOR_NA)
6413 return -ENXIO;
6414
6415 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6416}
6417
6418#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6419 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6420 thermal_temp_input_show, NULL, _idxB)
6421
6422static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6423 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6424 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6425 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6426 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6427 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6428 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6429 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6430 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6431 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6432 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6433 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6434 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6435 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6436 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6437 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6438 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6439};
6440
6441#define THERMAL_ATTRS(X) \
6442 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6443
6444static struct attribute *thermal_temp_input_attr[] = {
6445 THERMAL_ATTRS(8),
6446 THERMAL_ATTRS(9),
6447 THERMAL_ATTRS(10),
6448 THERMAL_ATTRS(11),
6449 THERMAL_ATTRS(12),
6450 THERMAL_ATTRS(13),
6451 THERMAL_ATTRS(14),
6452 THERMAL_ATTRS(15),
6453 THERMAL_ATTRS(0),
6454 THERMAL_ATTRS(1),
6455 THERMAL_ATTRS(2),
6456 THERMAL_ATTRS(3),
6457 THERMAL_ATTRS(4),
6458 THERMAL_ATTRS(5),
6459 THERMAL_ATTRS(6),
6460 THERMAL_ATTRS(7),
6461 NULL
6462};
6463
6464static const struct attribute_group thermal_temp_input16_group = {
6465 .attrs = thermal_temp_input_attr
6466};
6467
6468static const struct attribute_group thermal_temp_input8_group = {
6469 .attrs = &thermal_temp_input_attr[8]
6470};
6471
6472#undef THERMAL_SENSOR_ATTR_TEMP
6473#undef THERMAL_ATTRS
6474
6475/* --------------------------------------------------------------------- */
6476
6477static int __init thermal_init(struct ibm_init_struct *iibm)
6478{
Googlerb48fa912023-03-17 12:40:29 +05306479 u8 t, ta1, ta2;
Googleraf606d22022-10-26 21:40:12 -07006480 int i;
6481 int acpi_tmp7;
6482 int res;
6483
6484 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6485
6486 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6487
6488 if (thinkpad_id.ec_model) {
6489 /*
6490 * Direct EC access mode: sensors at registers
6491 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6492 * non-implemented, thermal sensors return 0x80 when
6493 * not available
6494 */
6495
6496 ta1 = ta2 = 0;
6497 for (i = 0; i < 8; i++) {
6498 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6499 ta1 |= t;
6500 } else {
6501 ta1 = 0;
6502 break;
6503 }
Googlerb48fa912023-03-17 12:40:29 +05306504 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6505 ta2 |= t;
6506 } else {
6507 ta1 = 0;
6508 break;
Googleraf606d22022-10-26 21:40:12 -07006509 }
6510 }
6511 if (ta1 == 0) {
6512 /* This is sheer paranoia, but we handle it anyway */
6513 if (acpi_tmp7) {
Googler9398cc32022-12-02 17:21:52 +08006514 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
Googleraf606d22022-10-26 21:40:12 -07006515 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6516 } else {
Googler9398cc32022-12-02 17:21:52 +08006517 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
Googleraf606d22022-10-26 21:40:12 -07006518 thermal_read_mode = TPACPI_THERMAL_NONE;
6519 }
6520 } else {
Googlerb48fa912023-03-17 12:40:29 +05306521 thermal_read_mode =
6522 (ta2 != 0) ?
6523 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Googleraf606d22022-10-26 21:40:12 -07006524 }
6525 } else if (acpi_tmp7) {
6526 if (tpacpi_is_ibm() &&
6527 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6528 /* 600e/x, 770e, 770x */
6529 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6530 } else {
6531 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6532 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6533 }
6534 } else {
6535 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6536 thermal_read_mode = TPACPI_THERMAL_NONE;
6537 }
6538
6539 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6540 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6541 thermal_read_mode);
6542
6543 switch (thermal_read_mode) {
6544 case TPACPI_THERMAL_TPEC_16:
Googler9398cc32022-12-02 17:21:52 +08006545 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07006546 &thermal_temp_input16_group);
6547 if (res)
6548 return res;
6549 break;
6550 case TPACPI_THERMAL_TPEC_8:
6551 case TPACPI_THERMAL_ACPI_TMP07:
6552 case TPACPI_THERMAL_ACPI_UPDT:
Googler9398cc32022-12-02 17:21:52 +08006553 res = sysfs_create_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07006554 &thermal_temp_input8_group);
6555 if (res)
6556 return res;
6557 break;
6558 case TPACPI_THERMAL_NONE:
6559 default:
6560 return 1;
6561 }
6562
6563 return 0;
6564}
6565
6566static void thermal_exit(void)
6567{
6568 switch (thermal_read_mode) {
6569 case TPACPI_THERMAL_TPEC_16:
Googler9398cc32022-12-02 17:21:52 +08006570 sysfs_remove_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07006571 &thermal_temp_input16_group);
6572 break;
6573 case TPACPI_THERMAL_TPEC_8:
6574 case TPACPI_THERMAL_ACPI_TMP07:
6575 case TPACPI_THERMAL_ACPI_UPDT:
Googler9398cc32022-12-02 17:21:52 +08006576 sysfs_remove_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07006577 &thermal_temp_input8_group);
6578 break;
6579 case TPACPI_THERMAL_NONE:
6580 default:
6581 break;
6582 }
6583}
6584
6585static int thermal_read(struct seq_file *m)
6586{
6587 int n, i;
6588 struct ibm_thermal_sensors_struct t;
6589
6590 n = thermal_get_sensors(&t);
6591 if (unlikely(n < 0))
6592 return n;
6593
6594 seq_printf(m, "temperatures:\t");
6595
6596 if (n > 0) {
6597 for (i = 0; i < (n - 1); i++)
6598 seq_printf(m, "%d ", t.temp[i] / 1000);
6599 seq_printf(m, "%d\n", t.temp[i] / 1000);
6600 } else
6601 seq_printf(m, "not supported\n");
6602
6603 return 0;
6604}
6605
6606static struct ibm_struct thermal_driver_data = {
6607 .name = "thermal",
6608 .read = thermal_read,
6609 .exit = thermal_exit,
6610};
6611
6612/*************************************************************************
6613 * Backlight/brightness subdriver
6614 */
6615
6616#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6617
6618/*
6619 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6620 * CMOS NVRAM byte 0x5E, bits 0-3.
6621 *
6622 * EC HBRV (0x31) has the following layout
6623 * Bit 7: unknown function
6624 * Bit 6: unknown function
6625 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6626 * Bit 4: must be set to zero to avoid problems
6627 * Bit 3-0: backlight brightness level
6628 *
6629 * brightness_get_raw returns status data in the HBRV layout
6630 *
6631 * WARNING: The X61 has been verified to use HBRV for something else, so
6632 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6633 * testing on the very early *60 Lenovo models...
6634 */
6635
6636enum {
6637 TP_EC_BACKLIGHT = 0x31,
6638
6639 /* TP_EC_BACKLIGHT bitmasks */
6640 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6641 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6642 TP_EC_BACKLIGHT_MAPSW = 0x20,
6643};
6644
6645enum tpacpi_brightness_access_mode {
6646 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6647 TPACPI_BRGHT_MODE_EC, /* EC control */
6648 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6649 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6650 TPACPI_BRGHT_MODE_MAX
6651};
6652
6653static struct backlight_device *ibm_backlight_device;
6654
6655static enum tpacpi_brightness_access_mode brightness_mode =
6656 TPACPI_BRGHT_MODE_MAX;
6657
6658static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6659
6660static struct mutex brightness_mutex;
6661
6662/* NVRAM brightness access,
6663 * call with brightness_mutex held! */
6664static unsigned int tpacpi_brightness_nvram_get(void)
6665{
6666 u8 lnvram;
6667
6668 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6669 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6670 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6671 lnvram &= bright_maxlvl;
6672
6673 return lnvram;
6674}
6675
6676static void tpacpi_brightness_checkpoint_nvram(void)
6677{
6678 u8 lec = 0;
6679 u8 b_nvram;
6680
6681 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6682 return;
6683
6684 vdbg_printk(TPACPI_DBG_BRGHT,
6685 "trying to checkpoint backlight level to NVRAM...\n");
6686
6687 if (mutex_lock_killable(&brightness_mutex) < 0)
6688 return;
6689
6690 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6691 goto unlock;
6692 lec &= TP_EC_BACKLIGHT_LVLMSK;
6693 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6694
6695 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6696 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6697 /* NVRAM needs update */
6698 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6699 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6700 b_nvram |= lec;
6701 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6702 dbg_printk(TPACPI_DBG_BRGHT,
6703 "updated NVRAM backlight level to %u (0x%02x)\n",
6704 (unsigned int) lec, (unsigned int) b_nvram);
6705 } else
6706 vdbg_printk(TPACPI_DBG_BRGHT,
6707 "NVRAM backlight level already is %u (0x%02x)\n",
6708 (unsigned int) lec, (unsigned int) b_nvram);
6709
6710unlock:
6711 mutex_unlock(&brightness_mutex);
6712}
6713
6714
6715/* call with brightness_mutex held! */
6716static int tpacpi_brightness_get_raw(int *status)
6717{
6718 u8 lec = 0;
6719
6720 switch (brightness_mode) {
6721 case TPACPI_BRGHT_MODE_UCMS_STEP:
6722 *status = tpacpi_brightness_nvram_get();
6723 return 0;
6724 case TPACPI_BRGHT_MODE_EC:
6725 case TPACPI_BRGHT_MODE_ECNVRAM:
6726 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6727 return -EIO;
6728 *status = lec;
6729 return 0;
6730 default:
6731 return -ENXIO;
6732 }
6733}
6734
6735/* call with brightness_mutex held! */
6736/* do NOT call with illegal backlight level value */
6737static int tpacpi_brightness_set_ec(unsigned int value)
6738{
6739 u8 lec = 0;
6740
6741 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6742 return -EIO;
6743
6744 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6745 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6746 (value & TP_EC_BACKLIGHT_LVLMSK))))
6747 return -EIO;
6748
6749 return 0;
6750}
6751
6752/* call with brightness_mutex held! */
6753static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6754{
6755 int cmos_cmd, inc;
6756 unsigned int current_value, i;
6757
6758 current_value = tpacpi_brightness_nvram_get();
6759
6760 if (value == current_value)
6761 return 0;
6762
6763 cmos_cmd = (value > current_value) ?
6764 TP_CMOS_BRIGHTNESS_UP :
6765 TP_CMOS_BRIGHTNESS_DOWN;
6766 inc = (value > current_value) ? 1 : -1;
6767
6768 for (i = current_value; i != value; i += inc)
6769 if (issue_thinkpad_cmos_command(cmos_cmd))
6770 return -EIO;
6771
6772 return 0;
6773}
6774
6775/* May return EINTR which can always be mapped to ERESTARTSYS */
6776static int brightness_set(unsigned int value)
6777{
6778 int res;
6779
6780 if (value > bright_maxlvl)
6781 return -EINVAL;
6782
6783 vdbg_printk(TPACPI_DBG_BRGHT,
6784 "set backlight level to %d\n", value);
6785
6786 res = mutex_lock_killable(&brightness_mutex);
6787 if (res < 0)
6788 return res;
6789
6790 switch (brightness_mode) {
6791 case TPACPI_BRGHT_MODE_EC:
6792 case TPACPI_BRGHT_MODE_ECNVRAM:
6793 res = tpacpi_brightness_set_ec(value);
6794 break;
6795 case TPACPI_BRGHT_MODE_UCMS_STEP:
6796 res = tpacpi_brightness_set_ucmsstep(value);
6797 break;
6798 default:
6799 res = -ENXIO;
6800 }
6801
6802 mutex_unlock(&brightness_mutex);
6803 return res;
6804}
6805
6806/* sysfs backlight class ----------------------------------------------- */
6807
6808static int brightness_update_status(struct backlight_device *bd)
6809{
6810 unsigned int level =
6811 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6812 bd->props.power == FB_BLANK_UNBLANK) ?
6813 bd->props.brightness : 0;
6814
6815 dbg_printk(TPACPI_DBG_BRGHT,
6816 "backlight: attempt to set level to %d\n",
6817 level);
6818
6819 /* it is the backlight class's job (caller) to handle
6820 * EINTR and other errors properly */
6821 return brightness_set(level);
6822}
6823
6824static int brightness_get(struct backlight_device *bd)
6825{
6826 int status, res;
6827
6828 res = mutex_lock_killable(&brightness_mutex);
6829 if (res < 0)
6830 return 0;
6831
6832 res = tpacpi_brightness_get_raw(&status);
6833
6834 mutex_unlock(&brightness_mutex);
6835
6836 if (res < 0)
6837 return 0;
6838
6839 return status & TP_EC_BACKLIGHT_LVLMSK;
6840}
6841
6842static void tpacpi_brightness_notify_change(void)
6843{
6844 backlight_force_update(ibm_backlight_device,
6845 BACKLIGHT_UPDATE_HOTKEY);
6846}
6847
6848static const struct backlight_ops ibm_backlight_data = {
6849 .get_brightness = brightness_get,
6850 .update_status = brightness_update_status,
6851};
6852
6853/* --------------------------------------------------------------------- */
6854
6855/*
6856 * Call _BCL method of video device. On some ThinkPads this will
6857 * switch the firmware to the ACPI brightness control mode.
6858 */
6859
6860static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6861{
6862 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6863 union acpi_object *obj;
6864 struct acpi_device *device, *child;
6865 int rc;
6866
6867 if (acpi_bus_get_device(handle, &device))
6868 return 0;
6869
6870 rc = 0;
6871 list_for_each_entry(child, &device->children, node) {
6872 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6873 NULL, &buffer);
6874 if (ACPI_FAILURE(status)) {
6875 buffer.length = ACPI_ALLOCATE_BUFFER;
6876 continue;
6877 }
6878
6879 obj = (union acpi_object *)buffer.pointer;
6880 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6881 pr_err("Unknown _BCL data, please report this to %s\n",
6882 TPACPI_MAIL);
6883 rc = 0;
6884 } else {
6885 rc = obj->package.count;
6886 }
6887 break;
6888 }
6889
6890 kfree(buffer.pointer);
6891 return rc;
6892}
6893
6894
6895/*
6896 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6897 */
6898static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6899{
6900 acpi_handle video_device;
6901 int bcl_levels = 0;
6902
6903 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6904 if (video_device)
6905 bcl_levels = tpacpi_query_bcl_levels(video_device);
6906
6907 tp_features.bright_acpimode = (bcl_levels > 0);
6908
6909 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6910}
6911
6912/*
6913 * These are only useful for models that have only one possibility
6914 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6915 * these quirks.
6916 */
6917#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6918#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6919#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6920
6921static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6922 /* Models with ATI GPUs known to require ECNVRAM mode */
6923 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6924
6925 /* Models with ATI GPUs that can use ECNVRAM */
6926 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6927 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6928 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6929 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6930
6931 /* Models with Intel Extreme Graphics 2 */
6932 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6933 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6934 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6935
6936 /* Models with Intel GMA900 */
6937 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6938 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6939 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6940};
6941
6942/*
6943 * Returns < 0 for error, otherwise sets tp_features.bright_*
6944 * and bright_maxlvl.
6945 */
6946static void __init tpacpi_detect_brightness_capabilities(void)
6947{
6948 unsigned int b;
6949
6950 vdbg_printk(TPACPI_DBG_INIT,
6951 "detecting firmware brightness interface capabilities\n");
6952
6953 /* we could run a quirks check here (same table used by
6954 * brightness_init) if needed */
6955
6956 /*
6957 * We always attempt to detect acpi support, so as to switch
6958 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6959 * going to publish a backlight interface
6960 */
6961 b = tpacpi_check_std_acpi_brightness_support();
6962 switch (b) {
6963 case 16:
6964 bright_maxlvl = 15;
6965 break;
6966 case 8:
6967 case 0:
6968 bright_maxlvl = 7;
6969 break;
6970 default:
6971 tp_features.bright_unkfw = 1;
6972 bright_maxlvl = b - 1;
6973 }
6974 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6975}
6976
6977static int __init brightness_init(struct ibm_init_struct *iibm)
6978{
6979 struct backlight_properties props;
6980 int b;
6981 unsigned long quirks;
6982
6983 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6984
6985 mutex_init(&brightness_mutex);
6986
6987 quirks = tpacpi_check_quirks(brightness_quirk_table,
6988 ARRAY_SIZE(brightness_quirk_table));
6989
6990 /* tpacpi_detect_brightness_capabilities() must have run already */
6991
6992 /* if it is unknown, we don't handle it: it wouldn't be safe */
6993 if (tp_features.bright_unkfw)
6994 return 1;
6995
6996 if (!brightness_enable) {
6997 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Googler9398cc32022-12-02 17:21:52 +08006998 "brightness support disabled by module parameter\n");
Googleraf606d22022-10-26 21:40:12 -07006999 return 1;
7000 }
7001
7002 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
7003 if (brightness_enable > 1) {
Googler9398cc32022-12-02 17:21:52 +08007004 pr_info("Standard ACPI backlight interface available, not loading native one\n");
Googleraf606d22022-10-26 21:40:12 -07007005 return 1;
7006 } else if (brightness_enable == 1) {
Googler9398cc32022-12-02 17:21:52 +08007007 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
Googleraf606d22022-10-26 21:40:12 -07007008 return 1;
7009 }
7010 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
Googler9398cc32022-12-02 17:21:52 +08007011 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
Googleraf606d22022-10-26 21:40:12 -07007012 }
7013
7014 /*
7015 * Check for module parameter bogosity, note that we
7016 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7017 * able to detect "unspecified"
7018 */
7019 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7020 return -EINVAL;
7021
7022 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7023 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7024 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7025 if (quirks & TPACPI_BRGHT_Q_EC)
7026 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7027 else
7028 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7029
7030 dbg_printk(TPACPI_DBG_BRGHT,
7031 "driver auto-selected brightness_mode=%d\n",
7032 brightness_mode);
7033 }
7034
7035 /* Safety */
7036 if (!tpacpi_is_ibm() &&
7037 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7038 brightness_mode == TPACPI_BRGHT_MODE_EC))
7039 return -EINVAL;
7040
7041 if (tpacpi_brightness_get_raw(&b) < 0)
7042 return 1;
7043
7044 memset(&props, 0, sizeof(struct backlight_properties));
7045 props.type = BACKLIGHT_PLATFORM;
7046 props.max_brightness = bright_maxlvl;
7047 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7048 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7049 NULL, NULL,
7050 &ibm_backlight_data,
7051 &props);
7052 if (IS_ERR(ibm_backlight_device)) {
7053 int rc = PTR_ERR(ibm_backlight_device);
7054 ibm_backlight_device = NULL;
7055 pr_err("Could not register backlight device\n");
7056 return rc;
7057 }
7058 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7059 "brightness is supported\n");
7060
7061 if (quirks & TPACPI_BRGHT_Q_ASK) {
Googler9398cc32022-12-02 17:21:52 +08007062 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7063 brightness_mode);
7064 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7065 TPACPI_MAIL);
Googleraf606d22022-10-26 21:40:12 -07007066 }
7067
7068 /* Added by mistake in early 2007. Probably useless, but it could
7069 * be working around some unknown firmware problem where the value
7070 * read at startup doesn't match the real hardware state... so leave
7071 * it in place just in case */
7072 backlight_update_status(ibm_backlight_device);
7073
7074 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
Googler9398cc32022-12-02 17:21:52 +08007075 "brightness: registering brightness hotkeys as change notification\n");
Googleraf606d22022-10-26 21:40:12 -07007076 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7077 | TP_ACPI_HKEY_BRGHTUP_MASK
7078 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7079 return 0;
7080}
7081
7082static void brightness_suspend(void)
7083{
7084 tpacpi_brightness_checkpoint_nvram();
7085}
7086
7087static void brightness_shutdown(void)
7088{
7089 tpacpi_brightness_checkpoint_nvram();
7090}
7091
7092static void brightness_exit(void)
7093{
7094 if (ibm_backlight_device) {
7095 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7096 "calling backlight_device_unregister()\n");
7097 backlight_device_unregister(ibm_backlight_device);
7098 }
7099
7100 tpacpi_brightness_checkpoint_nvram();
7101}
7102
7103static int brightness_read(struct seq_file *m)
7104{
7105 int level;
7106
7107 level = brightness_get(NULL);
7108 if (level < 0) {
7109 seq_printf(m, "level:\t\tunreadable\n");
7110 } else {
7111 seq_printf(m, "level:\t\t%d\n", level);
7112 seq_printf(m, "commands:\tup, down\n");
7113 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7114 bright_maxlvl);
7115 }
7116
7117 return 0;
7118}
7119
7120static int brightness_write(char *buf)
7121{
7122 int level;
7123 int rc;
7124 char *cmd;
7125
7126 level = brightness_get(NULL);
7127 if (level < 0)
7128 return level;
7129
7130 while ((cmd = next_cmd(&buf))) {
7131 if (strlencmp(cmd, "up") == 0) {
7132 if (level < bright_maxlvl)
7133 level++;
7134 } else if (strlencmp(cmd, "down") == 0) {
7135 if (level > 0)
7136 level--;
7137 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7138 level >= 0 && level <= bright_maxlvl) {
7139 /* new level set */
7140 } else
7141 return -EINVAL;
7142 }
7143
7144 tpacpi_disclose_usertask("procfs brightness",
7145 "set level to %d\n", level);
7146
7147 /*
7148 * Now we know what the final level should be, so we try to set it.
7149 * Doing it this way makes the syscall restartable in case of EINTR
7150 */
7151 rc = brightness_set(level);
7152 if (!rc && ibm_backlight_device)
7153 backlight_force_update(ibm_backlight_device,
7154 BACKLIGHT_UPDATE_SYSFS);
7155 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7156}
7157
7158static struct ibm_struct brightness_driver_data = {
7159 .name = "brightness",
7160 .read = brightness_read,
7161 .write = brightness_write,
7162 .exit = brightness_exit,
7163 .suspend = brightness_suspend,
7164 .shutdown = brightness_shutdown,
7165};
7166
7167/*************************************************************************
7168 * Volume subdriver
7169 */
7170
7171/*
7172 * IBM ThinkPads have a simple volume controller with MUTE gating.
7173 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7174 *
7175 * Since the *61 series (and probably also the later *60 series), Lenovo
7176 * ThinkPads only implement the MUTE gate.
7177 *
7178 * EC register 0x30
7179 * Bit 6: MUTE (1 mutes sound)
7180 * Bit 3-0: Volume
7181 * Other bits should be zero as far as we know.
7182 *
7183 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7184 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7185 * such as bit 7 which is used to detect repeated presses of MUTE,
7186 * and we leave them unchanged.
7187 *
7188 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7189 * in response to user input. Unfortunately, this rarely works well.
7190 * The laptop changes the state of its internal MUTE gate and, on some
7191 * models, sends KEY_MUTE, causing any user code that responds to the
7192 * mute button to get confused. The hardware MUTE gate is also
7193 * unnecessary, since user code can handle the mute button without
7194 * kernel or EC help.
7195 *
7196 * To avoid confusing userspace, we simply disable all EC-based mute
7197 * and volume controls when possible.
7198 */
7199
7200#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7201
7202#define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7203#define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7204#define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7205
7206#if SNDRV_CARDS <= 32
7207#define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7208#else
7209#define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7210#endif
7211static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7212static char *alsa_id = "ThinkPadEC";
7213static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7214
7215struct tpacpi_alsa_data {
7216 struct snd_card *card;
7217 struct snd_ctl_elem_id *ctl_mute_id;
7218 struct snd_ctl_elem_id *ctl_vol_id;
7219};
7220
7221static struct snd_card *alsa_card;
7222
7223enum {
7224 TP_EC_AUDIO = 0x30,
7225
7226 /* TP_EC_AUDIO bits */
7227 TP_EC_AUDIO_MUTESW = 6,
7228
7229 /* TP_EC_AUDIO bitmasks */
7230 TP_EC_AUDIO_LVL_MSK = 0x0F,
7231 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7232
7233 /* Maximum volume */
7234 TP_EC_VOLUME_MAX = 14,
7235};
7236
7237enum tpacpi_volume_access_mode {
7238 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7239 TPACPI_VOL_MODE_EC, /* Pure EC control */
7240 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7241 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7242 TPACPI_VOL_MODE_MAX
7243};
7244
7245enum tpacpi_volume_capabilities {
7246 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7247 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7248 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7249 TPACPI_VOL_CAP_MAX
7250};
7251
7252enum tpacpi_mute_btn_mode {
7253 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7254 /* We don't know what mode 1 is. */
7255 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7256 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7257};
7258
7259static enum tpacpi_volume_access_mode volume_mode =
7260 TPACPI_VOL_MODE_MAX;
7261
7262static enum tpacpi_volume_capabilities volume_capabilities;
7263static bool volume_control_allowed;
7264static bool software_mute_requested = true;
7265static bool software_mute_active;
7266static int software_mute_orig_mode;
7267
7268/*
7269 * Used to syncronize writers to TP_EC_AUDIO and
7270 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7271 */
7272static struct mutex volume_mutex;
7273
7274static void tpacpi_volume_checkpoint_nvram(void)
7275{
7276 u8 lec = 0;
7277 u8 b_nvram;
7278 u8 ec_mask;
7279
7280 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7281 return;
7282 if (!volume_control_allowed)
7283 return;
7284 if (software_mute_active)
7285 return;
7286
7287 vdbg_printk(TPACPI_DBG_MIXER,
7288 "trying to checkpoint mixer state to NVRAM...\n");
7289
7290 if (tp_features.mixer_no_level_control)
7291 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7292 else
7293 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7294
7295 if (mutex_lock_killable(&volume_mutex) < 0)
7296 return;
7297
7298 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7299 goto unlock;
7300 lec &= ec_mask;
7301 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7302
7303 if (lec != (b_nvram & ec_mask)) {
7304 /* NVRAM needs update */
7305 b_nvram &= ~ec_mask;
7306 b_nvram |= lec;
7307 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7308 dbg_printk(TPACPI_DBG_MIXER,
7309 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7310 (unsigned int) lec, (unsigned int) b_nvram);
7311 } else {
7312 vdbg_printk(TPACPI_DBG_MIXER,
7313 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7314 (unsigned int) lec, (unsigned int) b_nvram);
7315 }
7316
7317unlock:
7318 mutex_unlock(&volume_mutex);
7319}
7320
7321static int volume_get_status_ec(u8 *status)
7322{
7323 u8 s;
7324
7325 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7326 return -EIO;
7327
7328 *status = s;
7329
7330 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7331
7332 return 0;
7333}
7334
7335static int volume_get_status(u8 *status)
7336{
7337 return volume_get_status_ec(status);
7338}
7339
7340static int volume_set_status_ec(const u8 status)
7341{
7342 if (!acpi_ec_write(TP_EC_AUDIO, status))
7343 return -EIO;
7344
7345 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7346
7347 /*
7348 * On X200s, and possibly on others, it can take a while for
7349 * reads to become correct.
7350 */
7351 msleep(1);
7352
7353 return 0;
7354}
7355
7356static int volume_set_status(const u8 status)
7357{
7358 return volume_set_status_ec(status);
7359}
7360
7361/* returns < 0 on error, 0 on no change, 1 on change */
7362static int __volume_set_mute_ec(const bool mute)
7363{
7364 int rc;
7365 u8 s, n;
7366
7367 if (mutex_lock_killable(&volume_mutex) < 0)
7368 return -EINTR;
7369
7370 rc = volume_get_status_ec(&s);
7371 if (rc)
7372 goto unlock;
7373
7374 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7375 s & ~TP_EC_AUDIO_MUTESW_MSK;
7376
7377 if (n != s) {
7378 rc = volume_set_status_ec(n);
7379 if (!rc)
7380 rc = 1;
7381 }
7382
7383unlock:
7384 mutex_unlock(&volume_mutex);
7385 return rc;
7386}
7387
7388static int volume_alsa_set_mute(const bool mute)
7389{
7390 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7391 (mute) ? "" : "un");
7392 return __volume_set_mute_ec(mute);
7393}
7394
7395static int volume_set_mute(const bool mute)
7396{
7397 int rc;
7398
7399 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7400 (mute) ? "" : "un");
7401
7402 rc = __volume_set_mute_ec(mute);
7403 return (rc < 0) ? rc : 0;
7404}
7405
7406/* returns < 0 on error, 0 on no change, 1 on change */
7407static int __volume_set_volume_ec(const u8 vol)
7408{
7409 int rc;
7410 u8 s, n;
7411
7412 if (vol > TP_EC_VOLUME_MAX)
7413 return -EINVAL;
7414
7415 if (mutex_lock_killable(&volume_mutex) < 0)
7416 return -EINTR;
7417
7418 rc = volume_get_status_ec(&s);
7419 if (rc)
7420 goto unlock;
7421
7422 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7423
7424 if (n != s) {
7425 rc = volume_set_status_ec(n);
7426 if (!rc)
7427 rc = 1;
7428 }
7429
7430unlock:
7431 mutex_unlock(&volume_mutex);
7432 return rc;
7433}
7434
7435static int volume_set_software_mute(bool startup)
7436{
7437 int result;
7438
7439 if (!tpacpi_is_lenovo())
7440 return -ENODEV;
7441
7442 if (startup) {
7443 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7444 "HAUM", "qd"))
7445 return -EIO;
7446
7447 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7448 "Initial HAUM setting was %d\n",
7449 software_mute_orig_mode);
7450 }
7451
7452 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7453 (int)TP_EC_MUTE_BTN_NONE))
7454 return -EIO;
7455
7456 if (result != TP_EC_MUTE_BTN_NONE)
7457 pr_warn("Unexpected SAUM result %d\n",
7458 result);
7459
7460 /*
7461 * In software mute mode, the standard codec controls take
7462 * precendence, so we unmute the ThinkPad HW switch at
7463 * startup. Just on case there are SAUM-capable ThinkPads
7464 * with level controls, set max HW volume as well.
7465 */
7466 if (tp_features.mixer_no_level_control)
7467 result = volume_set_mute(false);
7468 else
7469 result = volume_set_status(TP_EC_VOLUME_MAX);
7470
7471 if (result != 0)
7472 pr_warn("Failed to unmute the HW mute switch\n");
7473
7474 return 0;
7475}
7476
7477static void volume_exit_software_mute(void)
7478{
7479 int r;
7480
7481 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7482 || r != software_mute_orig_mode)
7483 pr_warn("Failed to restore mute mode\n");
7484}
7485
7486static int volume_alsa_set_volume(const u8 vol)
7487{
7488 dbg_printk(TPACPI_DBG_MIXER,
7489 "ALSA: trying to set volume level to %hu\n", vol);
7490 return __volume_set_volume_ec(vol);
7491}
7492
7493static void volume_alsa_notify_change(void)
7494{
7495 struct tpacpi_alsa_data *d;
7496
7497 if (alsa_card && alsa_card->private_data) {
7498 d = alsa_card->private_data;
7499 if (d->ctl_mute_id)
7500 snd_ctl_notify(alsa_card,
7501 SNDRV_CTL_EVENT_MASK_VALUE,
7502 d->ctl_mute_id);
7503 if (d->ctl_vol_id)
7504 snd_ctl_notify(alsa_card,
7505 SNDRV_CTL_EVENT_MASK_VALUE,
7506 d->ctl_vol_id);
7507 }
7508}
7509
7510static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7511 struct snd_ctl_elem_info *uinfo)
7512{
7513 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7514 uinfo->count = 1;
7515 uinfo->value.integer.min = 0;
7516 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7517 return 0;
7518}
7519
7520static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7521 struct snd_ctl_elem_value *ucontrol)
7522{
7523 u8 s;
7524 int rc;
7525
7526 rc = volume_get_status(&s);
7527 if (rc < 0)
7528 return rc;
7529
7530 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7531 return 0;
7532}
7533
7534static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7535 struct snd_ctl_elem_value *ucontrol)
7536{
7537 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7538 ucontrol->value.integer.value[0]);
7539 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7540}
7541
7542#define volume_alsa_mute_info snd_ctl_boolean_mono_info
7543
7544static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7545 struct snd_ctl_elem_value *ucontrol)
7546{
7547 u8 s;
7548 int rc;
7549
7550 rc = volume_get_status(&s);
7551 if (rc < 0)
7552 return rc;
7553
7554 ucontrol->value.integer.value[0] =
7555 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7556 return 0;
7557}
7558
7559static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7560 struct snd_ctl_elem_value *ucontrol)
7561{
7562 tpacpi_disclose_usertask("ALSA", "%smute\n",
7563 ucontrol->value.integer.value[0] ?
7564 "un" : "");
7565 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7566}
7567
7568static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7570 .name = "Console Playback Volume",
7571 .index = 0,
7572 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7573 .info = volume_alsa_vol_info,
7574 .get = volume_alsa_vol_get,
7575};
7576
7577static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7579 .name = "Console Playback Switch",
7580 .index = 0,
7581 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7582 .info = volume_alsa_mute_info,
7583 .get = volume_alsa_mute_get,
7584};
7585
7586static void volume_suspend(void)
7587{
7588 tpacpi_volume_checkpoint_nvram();
7589}
7590
7591static void volume_resume(void)
7592{
7593 if (software_mute_active) {
7594 if (volume_set_software_mute(false) < 0)
7595 pr_warn("Failed to restore software mute\n");
7596 } else {
7597 volume_alsa_notify_change();
7598 }
7599}
7600
7601static void volume_shutdown(void)
7602{
7603 tpacpi_volume_checkpoint_nvram();
7604}
7605
7606static void volume_exit(void)
7607{
7608 if (alsa_card) {
7609 snd_card_free(alsa_card);
7610 alsa_card = NULL;
7611 }
7612
7613 tpacpi_volume_checkpoint_nvram();
7614
7615 if (software_mute_active)
7616 volume_exit_software_mute();
7617}
7618
7619static int __init volume_create_alsa_mixer(void)
7620{
7621 struct snd_card *card;
7622 struct tpacpi_alsa_data *data;
7623 struct snd_kcontrol *ctl_vol;
7624 struct snd_kcontrol *ctl_mute;
7625 int rc;
7626
7627 rc = snd_card_new(&tpacpi_pdev->dev,
7628 alsa_index, alsa_id, THIS_MODULE,
7629 sizeof(struct tpacpi_alsa_data), &card);
7630 if (rc < 0 || !card) {
7631 pr_err("Failed to create ALSA card structures: %d\n", rc);
7632 return 1;
7633 }
7634
7635 BUG_ON(!card->private_data);
7636 data = card->private_data;
7637 data->card = card;
7638
7639 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7640 sizeof(card->driver));
7641 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7642 sizeof(card->shortname));
7643 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7644 (thinkpad_id.ec_version_str) ?
7645 thinkpad_id.ec_version_str : "(unknown)");
7646 snprintf(card->longname, sizeof(card->longname),
7647 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7648 (thinkpad_id.ec_version_str) ?
7649 thinkpad_id.ec_version_str : "unknown");
7650
7651 if (volume_control_allowed) {
7652 volume_alsa_control_vol.put = volume_alsa_vol_put;
7653 volume_alsa_control_vol.access =
7654 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7655
7656 volume_alsa_control_mute.put = volume_alsa_mute_put;
7657 volume_alsa_control_mute.access =
7658 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7659 }
7660
7661 if (!tp_features.mixer_no_level_control) {
7662 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7663 rc = snd_ctl_add(card, ctl_vol);
7664 if (rc < 0) {
7665 pr_err("Failed to create ALSA volume control: %d\n",
7666 rc);
7667 goto err_exit;
7668 }
7669 data->ctl_vol_id = &ctl_vol->id;
7670 }
7671
7672 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7673 rc = snd_ctl_add(card, ctl_mute);
7674 if (rc < 0) {
7675 pr_err("Failed to create ALSA mute control: %d\n", rc);
7676 goto err_exit;
7677 }
7678 data->ctl_mute_id = &ctl_mute->id;
7679
7680 rc = snd_card_register(card);
7681 if (rc < 0) {
7682 pr_err("Failed to register ALSA card: %d\n", rc);
7683 goto err_exit;
7684 }
7685
7686 alsa_card = card;
7687 return 0;
7688
7689err_exit:
7690 snd_card_free(card);
7691 return 1;
7692}
7693
7694#define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7695#define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7696
7697static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7698 /* Whitelist volume level on all IBM by default */
7699 { .vendor = PCI_VENDOR_ID_IBM,
7700 .bios = TPACPI_MATCH_ANY,
7701 .ec = TPACPI_MATCH_ANY,
7702 .quirks = TPACPI_VOL_Q_LEVEL },
7703
7704 /* Lenovo models with volume control (needs confirmation) */
7705 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7706 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7707 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7708 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7709 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7710 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7711 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7712
7713 /* Whitelist mute-only on all Lenovo by default */
7714 { .vendor = PCI_VENDOR_ID_LENOVO,
7715 .bios = TPACPI_MATCH_ANY,
7716 .ec = TPACPI_MATCH_ANY,
7717 .quirks = TPACPI_VOL_Q_MUTEONLY }
7718};
7719
7720static int __init volume_init(struct ibm_init_struct *iibm)
7721{
7722 unsigned long quirks;
7723 int rc;
7724
7725 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7726
7727 mutex_init(&volume_mutex);
7728
7729 /*
7730 * Check for module parameter bogosity, note that we
7731 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7732 * able to detect "unspecified"
7733 */
7734 if (volume_mode > TPACPI_VOL_MODE_MAX)
7735 return -EINVAL;
7736
7737 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
Googler9398cc32022-12-02 17:21:52 +08007738 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7739 TPACPI_MAIL);
Googleraf606d22022-10-26 21:40:12 -07007740 return 1;
7741 }
7742
7743 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7744 return -EINVAL;
7745
7746 /*
7747 * The ALSA mixer is our primary interface.
7748 * When disabled, don't install the subdriver at all
7749 */
7750 if (!alsa_enable) {
7751 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
Googler9398cc32022-12-02 17:21:52 +08007752 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
Googleraf606d22022-10-26 21:40:12 -07007753 return 1;
7754 }
7755
7756 quirks = tpacpi_check_quirks(volume_quirk_table,
7757 ARRAY_SIZE(volume_quirk_table));
7758
7759 switch (volume_capabilities) {
7760 case TPACPI_VOL_CAP_AUTO:
7761 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7762 tp_features.mixer_no_level_control = 1;
7763 else if (quirks & TPACPI_VOL_Q_LEVEL)
7764 tp_features.mixer_no_level_control = 0;
7765 else
7766 return 1; /* no mixer */
7767 break;
7768 case TPACPI_VOL_CAP_VOLMUTE:
7769 tp_features.mixer_no_level_control = 0;
7770 break;
7771 case TPACPI_VOL_CAP_MUTEONLY:
7772 tp_features.mixer_no_level_control = 1;
7773 break;
7774 default:
7775 return 1;
7776 }
7777
7778 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7779 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7780 "using user-supplied volume_capabilities=%d\n",
7781 volume_capabilities);
7782
7783 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7784 volume_mode == TPACPI_VOL_MODE_MAX) {
7785 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7786
7787 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7788 "driver auto-selected volume_mode=%d\n",
7789 volume_mode);
7790 } else {
7791 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7792 "using user-supplied volume_mode=%d\n",
7793 volume_mode);
7794 }
7795
7796 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7797 "mute is supported, volume control is %s\n",
7798 str_supported(!tp_features.mixer_no_level_control));
7799
7800 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7801 software_mute_active = true;
7802 } else {
7803 rc = volume_create_alsa_mixer();
7804 if (rc) {
7805 pr_err("Could not create the ALSA mixer interface\n");
7806 return rc;
7807 }
7808
7809 pr_info("Console audio control enabled, mode: %s\n",
7810 (volume_control_allowed) ?
7811 "override (read/write)" :
7812 "monitor (read only)");
7813 }
7814
7815 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7816 "registering volume hotkeys as change notification\n");
7817 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7818 | TP_ACPI_HKEY_VOLUP_MASK
7819 | TP_ACPI_HKEY_VOLDWN_MASK
7820 | TP_ACPI_HKEY_MUTE_MASK);
7821
7822 return 0;
7823}
7824
7825static int volume_read(struct seq_file *m)
7826{
7827 u8 status;
7828
7829 if (volume_get_status(&status) < 0) {
7830 seq_printf(m, "level:\t\tunreadable\n");
7831 } else {
7832 if (tp_features.mixer_no_level_control)
7833 seq_printf(m, "level:\t\tunsupported\n");
7834 else
7835 seq_printf(m, "level:\t\t%d\n",
7836 status & TP_EC_AUDIO_LVL_MSK);
7837
7838 seq_printf(m, "mute:\t\t%s\n",
7839 onoff(status, TP_EC_AUDIO_MUTESW));
7840
7841 if (volume_control_allowed) {
7842 seq_printf(m, "commands:\tunmute, mute\n");
7843 if (!tp_features.mixer_no_level_control) {
Googler9398cc32022-12-02 17:21:52 +08007844 seq_printf(m, "commands:\tup, down\n");
7845 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7846 TP_EC_VOLUME_MAX);
Googleraf606d22022-10-26 21:40:12 -07007847 }
7848 }
7849 }
7850
7851 return 0;
7852}
7853
7854static int volume_write(char *buf)
7855{
7856 u8 s;
7857 u8 new_level, new_mute;
7858 int l;
7859 char *cmd;
7860 int rc;
7861
7862 /*
7863 * We do allow volume control at driver startup, so that the
7864 * user can set initial state through the volume=... parameter hack.
7865 */
7866 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7867 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7868 tp_warned.volume_ctrl_forbidden = 1;
Googler9398cc32022-12-02 17:21:52 +08007869 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7870 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
Googleraf606d22022-10-26 21:40:12 -07007871 }
7872 return -EPERM;
7873 }
7874
7875 rc = volume_get_status(&s);
7876 if (rc < 0)
7877 return rc;
7878
7879 new_level = s & TP_EC_AUDIO_LVL_MSK;
7880 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7881
7882 while ((cmd = next_cmd(&buf))) {
7883 if (!tp_features.mixer_no_level_control) {
7884 if (strlencmp(cmd, "up") == 0) {
7885 if (new_mute)
7886 new_mute = 0;
7887 else if (new_level < TP_EC_VOLUME_MAX)
7888 new_level++;
7889 continue;
7890 } else if (strlencmp(cmd, "down") == 0) {
7891 if (new_mute)
7892 new_mute = 0;
7893 else if (new_level > 0)
7894 new_level--;
7895 continue;
7896 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7897 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7898 new_level = l;
7899 continue;
7900 }
7901 }
7902 if (strlencmp(cmd, "mute") == 0)
7903 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7904 else if (strlencmp(cmd, "unmute") == 0)
7905 new_mute = 0;
7906 else
7907 return -EINVAL;
7908 }
7909
7910 if (tp_features.mixer_no_level_control) {
7911 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7912 new_mute ? "" : "un");
7913 rc = volume_set_mute(!!new_mute);
7914 } else {
7915 tpacpi_disclose_usertask("procfs volume",
7916 "%smute and set level to %d\n",
7917 new_mute ? "" : "un", new_level);
7918 rc = volume_set_status(new_mute | new_level);
7919 }
7920 volume_alsa_notify_change();
7921
7922 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7923}
7924
7925static struct ibm_struct volume_driver_data = {
7926 .name = "volume",
7927 .read = volume_read,
7928 .write = volume_write,
7929 .exit = volume_exit,
7930 .suspend = volume_suspend,
7931 .resume = volume_resume,
7932 .shutdown = volume_shutdown,
7933};
7934
7935#else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7936
7937#define alsa_card NULL
7938
Googler9398cc32022-12-02 17:21:52 +08007939static inline void volume_alsa_notify_change(void)
Googleraf606d22022-10-26 21:40:12 -07007940{
7941}
7942
7943static int __init volume_init(struct ibm_init_struct *iibm)
7944{
7945 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7946
7947 return 1;
7948}
7949
7950static struct ibm_struct volume_driver_data = {
7951 .name = "volume",
7952};
7953
7954#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7955
7956/*************************************************************************
7957 * Fan subdriver
7958 */
7959
7960/*
7961 * FAN ACCESS MODES
7962 *
7963 * TPACPI_FAN_RD_ACPI_GFAN:
7964 * ACPI GFAN method: returns fan level
7965 *
7966 * see TPACPI_FAN_WR_ACPI_SFAN
7967 * EC 0x2f (HFSP) not available if GFAN exists
7968 *
7969 * TPACPI_FAN_WR_ACPI_SFAN:
7970 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7971 *
7972 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7973 * it for writing.
7974 *
7975 * TPACPI_FAN_WR_TPEC:
7976 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7977 * Supported on almost all ThinkPads
7978 *
7979 * Fan speed changes of any sort (including those caused by the
7980 * disengaged mode) are usually done slowly by the firmware as the
7981 * maximum amount of fan duty cycle change per second seems to be
7982 * limited.
7983 *
7984 * Reading is not available if GFAN exists.
7985 * Writing is not available if SFAN exists.
7986 *
7987 * Bits
7988 * 7 automatic mode engaged;
7989 * (default operation mode of the ThinkPad)
7990 * fan level is ignored in this mode.
7991 * 6 full speed mode (takes precedence over bit 7);
7992 * not available on all thinkpads. May disable
7993 * the tachometer while the fan controller ramps up
7994 * the speed (which can take up to a few *minutes*).
7995 * Speeds up fan to 100% duty-cycle, which is far above
7996 * the standard RPM levels. It is not impossible that
7997 * it could cause hardware damage.
7998 * 5-3 unused in some models. Extra bits for fan level
7999 * in others, but still useless as all values above
8000 * 7 map to the same speed as level 7 in these models.
8001 * 2-0 fan level (0..7 usually)
8002 * 0x00 = stop
8003 * 0x07 = max (set when temperatures critical)
8004 * Some ThinkPads may have other levels, see
8005 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
8006 *
8007 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
8008 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
8009 * does so, its initial value is meaningless (0x07).
8010 *
8011 * For firmware bugs, refer to:
8012 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8013 *
8014 * ----
8015 *
8016 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8017 * Main fan tachometer reading (in RPM)
8018 *
8019 * This register is present on all ThinkPads with a new-style EC, and
8020 * it is known not to be present on the A21m/e, and T22, as there is
8021 * something else in offset 0x84 according to the ACPI DSDT. Other
8022 * ThinkPads from this same time period (and earlier) probably lack the
8023 * tachometer as well.
8024 *
8025 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8026 * was never fixed by IBM to report the EC firmware version string
8027 * probably support the tachometer (like the early X models), so
8028 * detecting it is quite hard. We need more data to know for sure.
8029 *
8030 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8031 * might result.
8032 *
8033 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8034 * mode.
8035 *
8036 * For firmware bugs, refer to:
8037 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8038 *
8039 * ----
8040 *
8041 * ThinkPad EC register 0x31 bit 0 (only on select models)
8042 *
8043 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8044 * show the speed of the main fan. When bit 0 of EC register 0x31
8045 * is one, the tachometer registers show the speed of the auxiliary
8046 * fan.
8047 *
8048 * Fan control seems to affect both fans, regardless of the state
8049 * of this bit.
8050 *
8051 * So far, only the firmware for the X60/X61 non-tablet versions
8052 * seem to support this (firmware TP-7M).
8053 *
8054 * TPACPI_FAN_WR_ACPI_FANS:
8055 * ThinkPad X31, X40, X41. Not available in the X60.
8056 *
8057 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8058 * high speed. ACPI DSDT seems to map these three speeds to levels
8059 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8060 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8061 *
8062 * The speeds are stored on handles
8063 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8064 *
8065 * There are three default speed sets, accessible as handles:
8066 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8067 *
8068 * ACPI DSDT switches which set is in use depending on various
8069 * factors.
8070 *
8071 * TPACPI_FAN_WR_TPEC is also available and should be used to
8072 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8073 * but the ACPI tables just mention level 7.
8074 */
8075
8076enum { /* Fan control constants */
8077 fan_status_offset = 0x2f, /* EC register 0x2f */
8078 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8079 * 0x84 must be read before 0x85 */
8080 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8081 bit 0 selects which fan is active */
8082
8083 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8084 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8085
8086 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8087};
8088
8089enum fan_status_access_mode {
8090 TPACPI_FAN_NONE = 0, /* No fan status or control */
8091 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8092 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8093};
8094
8095enum fan_control_access_mode {
8096 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8097 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8098 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8099 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8100};
8101
8102enum fan_control_commands {
8103 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8104 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8105 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8106 * and also watchdog cmd */
8107};
8108
8109static bool fan_control_allowed;
8110
8111static enum fan_status_access_mode fan_status_access_mode;
8112static enum fan_control_access_mode fan_control_access_mode;
8113static enum fan_control_commands fan_control_commands;
8114
8115static u8 fan_control_initial_status;
8116static u8 fan_control_desired_level;
8117static u8 fan_control_resume_level;
8118static int fan_watchdog_maxinterval;
8119
8120static struct mutex fan_mutex;
8121
8122static void fan_watchdog_fire(struct work_struct *ignored);
8123static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8124
8125TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8126TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8127 "\\FSPD", /* 600e/x, 770e, 770x */
8128 ); /* all others */
8129TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8130 "JFNS", /* 770x-JL */
8131 ); /* all others */
8132
8133/*
8134 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8135 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8136 * be in auto mode (0x80).
8137 *
8138 * This is corrected by any write to HFSP either by the driver, or
8139 * by the firmware.
8140 *
8141 * We assume 0x07 really means auto mode while this quirk is active,
8142 * as this is far more likely than the ThinkPad being in level 7,
8143 * which is only used by the firmware during thermal emergencies.
8144 *
8145 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8146 * TP-70 (T43, R52), which are known to be buggy.
8147 */
8148
8149static void fan_quirk1_setup(void)
8150{
8151 if (fan_control_initial_status == 0x07) {
Googler9398cc32022-12-02 17:21:52 +08008152 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
Googleraf606d22022-10-26 21:40:12 -07008153 tp_features.fan_ctrl_status_undef = 1;
8154 }
8155}
8156
8157static void fan_quirk1_handle(u8 *fan_status)
8158{
8159 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8160 if (*fan_status != fan_control_initial_status) {
8161 /* something changed the HFSP regisnter since
8162 * driver init time, so it is not undefined
8163 * anymore */
8164 tp_features.fan_ctrl_status_undef = 0;
8165 } else {
8166 /* Return most likely status. In fact, it
8167 * might be the only possible status */
8168 *fan_status = TP_EC_FAN_AUTO;
8169 }
8170 }
8171}
8172
8173/* Select main fan on X60/X61, NOOP on others */
8174static bool fan_select_fan1(void)
8175{
8176 if (tp_features.second_fan) {
8177 u8 val;
8178
8179 if (ec_read(fan_select_offset, &val) < 0)
8180 return false;
8181 val &= 0xFEU;
8182 if (ec_write(fan_select_offset, val) < 0)
8183 return false;
8184 }
8185 return true;
8186}
8187
8188/* Select secondary fan on X60/X61 */
8189static bool fan_select_fan2(void)
8190{
8191 u8 val;
8192
8193 if (!tp_features.second_fan)
8194 return false;
8195
8196 if (ec_read(fan_select_offset, &val) < 0)
8197 return false;
8198 val |= 0x01U;
8199 if (ec_write(fan_select_offset, val) < 0)
8200 return false;
8201
8202 return true;
8203}
8204
8205/*
8206 * Call with fan_mutex held
8207 */
8208static void fan_update_desired_level(u8 status)
8209{
8210 if ((status &
8211 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8212 if (status > 7)
8213 fan_control_desired_level = 7;
8214 else
8215 fan_control_desired_level = status;
8216 }
8217}
8218
8219static int fan_get_status(u8 *status)
8220{
8221 u8 s;
8222
8223 /* TODO:
8224 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8225
8226 switch (fan_status_access_mode) {
8227 case TPACPI_FAN_RD_ACPI_GFAN: {
8228 /* 570, 600e/x, 770e, 770x */
8229 int res;
8230
8231 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8232 return -EIO;
8233
8234 if (likely(status))
8235 *status = res & 0x07;
8236
8237 break;
8238 }
8239 case TPACPI_FAN_RD_TPEC:
8240 /* all except 570, 600e/x, 770e, 770x */
8241 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8242 return -EIO;
8243
8244 if (likely(status)) {
8245 *status = s;
8246 fan_quirk1_handle(status);
8247 }
8248
8249 break;
8250
8251 default:
8252 return -ENXIO;
8253 }
8254
8255 return 0;
8256}
8257
8258static int fan_get_status_safe(u8 *status)
8259{
8260 int rc;
8261 u8 s;
8262
8263 if (mutex_lock_killable(&fan_mutex))
8264 return -ERESTARTSYS;
8265 rc = fan_get_status(&s);
8266 if (!rc)
8267 fan_update_desired_level(s);
8268 mutex_unlock(&fan_mutex);
8269
8270 if (rc)
8271 return rc;
8272 if (status)
8273 *status = s;
8274
8275 return 0;
8276}
8277
8278static int fan_get_speed(unsigned int *speed)
8279{
8280 u8 hi, lo;
8281
8282 switch (fan_status_access_mode) {
8283 case TPACPI_FAN_RD_TPEC:
8284 /* all except 570, 600e/x, 770e, 770x */
8285 if (unlikely(!fan_select_fan1()))
8286 return -EIO;
8287 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8288 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8289 return -EIO;
8290
8291 if (likely(speed))
8292 *speed = (hi << 8) | lo;
8293
8294 break;
8295
8296 default:
8297 return -ENXIO;
8298 }
8299
8300 return 0;
8301}
8302
8303static int fan2_get_speed(unsigned int *speed)
8304{
8305 u8 hi, lo;
8306 bool rc;
8307
8308 switch (fan_status_access_mode) {
8309 case TPACPI_FAN_RD_TPEC:
8310 /* all except 570, 600e/x, 770e, 770x */
8311 if (unlikely(!fan_select_fan2()))
8312 return -EIO;
8313 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8314 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8315 fan_select_fan1(); /* play it safe */
8316 if (rc)
8317 return -EIO;
8318
8319 if (likely(speed))
8320 *speed = (hi << 8) | lo;
8321
8322 break;
8323
8324 default:
8325 return -ENXIO;
8326 }
8327
8328 return 0;
8329}
8330
8331static int fan_set_level(int level)
8332{
8333 if (!fan_control_allowed)
8334 return -EPERM;
8335
8336 switch (fan_control_access_mode) {
8337 case TPACPI_FAN_WR_ACPI_SFAN:
8338 if (level >= 0 && level <= 7) {
8339 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8340 return -EIO;
8341 } else
8342 return -EINVAL;
8343 break;
8344
8345 case TPACPI_FAN_WR_ACPI_FANS:
8346 case TPACPI_FAN_WR_TPEC:
8347 if (!(level & TP_EC_FAN_AUTO) &&
8348 !(level & TP_EC_FAN_FULLSPEED) &&
8349 ((level < 0) || (level > 7)))
8350 return -EINVAL;
8351
8352 /* safety net should the EC not support AUTO
8353 * or FULLSPEED mode bits and just ignore them */
8354 if (level & TP_EC_FAN_FULLSPEED)
8355 level |= 7; /* safety min speed 7 */
8356 else if (level & TP_EC_FAN_AUTO)
8357 level |= 4; /* safety min speed 4 */
8358
8359 if (!acpi_ec_write(fan_status_offset, level))
8360 return -EIO;
8361 else
8362 tp_features.fan_ctrl_status_undef = 0;
8363 break;
8364
8365 default:
8366 return -ENXIO;
8367 }
8368
8369 vdbg_printk(TPACPI_DBG_FAN,
8370 "fan control: set fan control register to 0x%02x\n", level);
8371 return 0;
8372}
8373
8374static int fan_set_level_safe(int level)
8375{
8376 int rc;
8377
8378 if (!fan_control_allowed)
8379 return -EPERM;
8380
8381 if (mutex_lock_killable(&fan_mutex))
8382 return -ERESTARTSYS;
8383
8384 if (level == TPACPI_FAN_LAST_LEVEL)
8385 level = fan_control_desired_level;
8386
8387 rc = fan_set_level(level);
8388 if (!rc)
8389 fan_update_desired_level(level);
8390
8391 mutex_unlock(&fan_mutex);
8392 return rc;
8393}
8394
8395static int fan_set_enable(void)
8396{
8397 u8 s;
8398 int rc;
8399
8400 if (!fan_control_allowed)
8401 return -EPERM;
8402
8403 if (mutex_lock_killable(&fan_mutex))
8404 return -ERESTARTSYS;
8405
8406 switch (fan_control_access_mode) {
8407 case TPACPI_FAN_WR_ACPI_FANS:
8408 case TPACPI_FAN_WR_TPEC:
8409 rc = fan_get_status(&s);
8410 if (rc < 0)
8411 break;
8412
8413 /* Don't go out of emergency fan mode */
8414 if (s != 7) {
8415 s &= 0x07;
8416 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8417 }
8418
8419 if (!acpi_ec_write(fan_status_offset, s))
8420 rc = -EIO;
8421 else {
8422 tp_features.fan_ctrl_status_undef = 0;
8423 rc = 0;
8424 }
8425 break;
8426
8427 case TPACPI_FAN_WR_ACPI_SFAN:
8428 rc = fan_get_status(&s);
8429 if (rc < 0)
8430 break;
8431
8432 s &= 0x07;
8433
8434 /* Set fan to at least level 4 */
8435 s |= 4;
8436
8437 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8438 rc = -EIO;
8439 else
8440 rc = 0;
8441 break;
8442
8443 default:
8444 rc = -ENXIO;
8445 }
8446
8447 mutex_unlock(&fan_mutex);
8448
8449 if (!rc)
8450 vdbg_printk(TPACPI_DBG_FAN,
8451 "fan control: set fan control register to 0x%02x\n",
8452 s);
8453 return rc;
8454}
8455
8456static int fan_set_disable(void)
8457{
8458 int rc;
8459
8460 if (!fan_control_allowed)
8461 return -EPERM;
8462
8463 if (mutex_lock_killable(&fan_mutex))
8464 return -ERESTARTSYS;
8465
8466 rc = 0;
8467 switch (fan_control_access_mode) {
8468 case TPACPI_FAN_WR_ACPI_FANS:
8469 case TPACPI_FAN_WR_TPEC:
8470 if (!acpi_ec_write(fan_status_offset, 0x00))
8471 rc = -EIO;
8472 else {
8473 fan_control_desired_level = 0;
8474 tp_features.fan_ctrl_status_undef = 0;
8475 }
8476 break;
8477
8478 case TPACPI_FAN_WR_ACPI_SFAN:
8479 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8480 rc = -EIO;
8481 else
8482 fan_control_desired_level = 0;
8483 break;
8484
8485 default:
8486 rc = -ENXIO;
8487 }
8488
8489 if (!rc)
8490 vdbg_printk(TPACPI_DBG_FAN,
8491 "fan control: set fan control register to 0\n");
8492
8493 mutex_unlock(&fan_mutex);
8494 return rc;
8495}
8496
8497static int fan_set_speed(int speed)
8498{
8499 int rc;
8500
8501 if (!fan_control_allowed)
8502 return -EPERM;
8503
8504 if (mutex_lock_killable(&fan_mutex))
8505 return -ERESTARTSYS;
8506
8507 rc = 0;
8508 switch (fan_control_access_mode) {
8509 case TPACPI_FAN_WR_ACPI_FANS:
8510 if (speed >= 0 && speed <= 65535) {
8511 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8512 speed, speed, speed))
8513 rc = -EIO;
8514 } else
8515 rc = -EINVAL;
8516 break;
8517
8518 default:
8519 rc = -ENXIO;
8520 }
8521
8522 mutex_unlock(&fan_mutex);
8523 return rc;
8524}
8525
8526static void fan_watchdog_reset(void)
8527{
8528 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8529 return;
8530
8531 if (fan_watchdog_maxinterval > 0 &&
8532 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8533 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8534 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8535 else
8536 cancel_delayed_work(&fan_watchdog_task);
8537}
8538
8539static void fan_watchdog_fire(struct work_struct *ignored)
8540{
8541 int rc;
8542
8543 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8544 return;
8545
8546 pr_notice("fan watchdog: enabling fan\n");
8547 rc = fan_set_enable();
8548 if (rc < 0) {
Googler9398cc32022-12-02 17:21:52 +08008549 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8550 rc);
Googleraf606d22022-10-26 21:40:12 -07008551 /* reschedule for later */
8552 fan_watchdog_reset();
8553 }
8554}
8555
8556/*
8557 * SYSFS fan layout: hwmon compatible (device)
8558 *
8559 * pwm*_enable:
8560 * 0: "disengaged" mode
8561 * 1: manual mode
8562 * 2: native EC "auto" mode (recommended, hardware default)
8563 *
8564 * pwm*: set speed in manual mode, ignored otherwise.
8565 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8566 * interpolation.
8567 *
8568 * fan*_input: tachometer reading, RPM
8569 *
8570 *
8571 * SYSFS fan layout: extensions
8572 *
8573 * fan_watchdog (driver):
8574 * fan watchdog interval in seconds, 0 disables (default), max 120
8575 */
8576
8577/* sysfs fan pwm1_enable ----------------------------------------------- */
8578static ssize_t fan_pwm1_enable_show(struct device *dev,
8579 struct device_attribute *attr,
8580 char *buf)
8581{
8582 int res, mode;
8583 u8 status;
8584
8585 res = fan_get_status_safe(&status);
8586 if (res)
8587 return res;
8588
8589 if (status & TP_EC_FAN_FULLSPEED) {
8590 mode = 0;
8591 } else if (status & TP_EC_FAN_AUTO) {
8592 mode = 2;
8593 } else
8594 mode = 1;
8595
8596 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8597}
8598
8599static ssize_t fan_pwm1_enable_store(struct device *dev,
8600 struct device_attribute *attr,
8601 const char *buf, size_t count)
8602{
8603 unsigned long t;
8604 int res, level;
8605
8606 if (parse_strtoul(buf, 2, &t))
8607 return -EINVAL;
8608
8609 tpacpi_disclose_usertask("hwmon pwm1_enable",
8610 "set fan mode to %lu\n", t);
8611
8612 switch (t) {
8613 case 0:
8614 level = TP_EC_FAN_FULLSPEED;
8615 break;
8616 case 1:
8617 level = TPACPI_FAN_LAST_LEVEL;
8618 break;
8619 case 2:
8620 level = TP_EC_FAN_AUTO;
8621 break;
8622 case 3:
8623 /* reserved for software-controlled auto mode */
8624 return -ENOSYS;
8625 default:
8626 return -EINVAL;
8627 }
8628
8629 res = fan_set_level_safe(level);
8630 if (res == -ENXIO)
8631 return -EINVAL;
8632 else if (res < 0)
8633 return res;
8634
8635 fan_watchdog_reset();
8636
8637 return count;
8638}
8639
8640static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8641 fan_pwm1_enable_show, fan_pwm1_enable_store);
8642
8643/* sysfs fan pwm1 ------------------------------------------------------ */
8644static ssize_t fan_pwm1_show(struct device *dev,
8645 struct device_attribute *attr,
8646 char *buf)
8647{
8648 int res;
8649 u8 status;
8650
8651 res = fan_get_status_safe(&status);
8652 if (res)
8653 return res;
8654
8655 if ((status &
8656 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8657 status = fan_control_desired_level;
8658
8659 if (status > 7)
8660 status = 7;
8661
8662 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8663}
8664
8665static ssize_t fan_pwm1_store(struct device *dev,
8666 struct device_attribute *attr,
8667 const char *buf, size_t count)
8668{
8669 unsigned long s;
8670 int rc;
8671 u8 status, newlevel;
8672
8673 if (parse_strtoul(buf, 255, &s))
8674 return -EINVAL;
8675
8676 tpacpi_disclose_usertask("hwmon pwm1",
8677 "set fan speed to %lu\n", s);
8678
8679 /* scale down from 0-255 to 0-7 */
8680 newlevel = (s >> 5) & 0x07;
8681
8682 if (mutex_lock_killable(&fan_mutex))
8683 return -ERESTARTSYS;
8684
8685 rc = fan_get_status(&status);
8686 if (!rc && (status &
8687 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8688 rc = fan_set_level(newlevel);
8689 if (rc == -ENXIO)
8690 rc = -EINVAL;
8691 else if (!rc) {
8692 fan_update_desired_level(newlevel);
8693 fan_watchdog_reset();
8694 }
8695 }
8696
8697 mutex_unlock(&fan_mutex);
8698 return (rc) ? rc : count;
8699}
8700
8701static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8702
8703/* sysfs fan fan1_input ------------------------------------------------ */
8704static ssize_t fan_fan1_input_show(struct device *dev,
8705 struct device_attribute *attr,
8706 char *buf)
8707{
8708 int res;
8709 unsigned int speed;
8710
8711 res = fan_get_speed(&speed);
8712 if (res < 0)
8713 return res;
8714
8715 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8716}
8717
8718static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8719
8720/* sysfs fan fan2_input ------------------------------------------------ */
8721static ssize_t fan_fan2_input_show(struct device *dev,
8722 struct device_attribute *attr,
8723 char *buf)
8724{
8725 int res;
8726 unsigned int speed;
8727
8728 res = fan2_get_speed(&speed);
8729 if (res < 0)
8730 return res;
8731
8732 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8733}
8734
8735static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8736
8737/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Googler9398cc32022-12-02 17:21:52 +08008738static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
Googleraf606d22022-10-26 21:40:12 -07008739{
8740 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8741}
8742
Googler9398cc32022-12-02 17:21:52 +08008743static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8744 size_t count)
Googleraf606d22022-10-26 21:40:12 -07008745{
8746 unsigned long t;
8747
8748 if (parse_strtoul(buf, 120, &t))
8749 return -EINVAL;
8750
8751 if (!fan_control_allowed)
8752 return -EPERM;
8753
8754 fan_watchdog_maxinterval = t;
8755 fan_watchdog_reset();
8756
8757 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8758
8759 return count;
8760}
Googler9398cc32022-12-02 17:21:52 +08008761static DRIVER_ATTR_RW(fan_watchdog);
Googleraf606d22022-10-26 21:40:12 -07008762
8763/* --------------------------------------------------------------------- */
8764static struct attribute *fan_attributes[] = {
8765 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8766 &dev_attr_fan1_input.attr,
8767 NULL, /* for fan2_input */
8768 NULL
8769};
8770
8771static const struct attribute_group fan_attr_group = {
8772 .attrs = fan_attributes,
8773};
8774
Googler9398cc32022-12-02 17:21:52 +08008775#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
Googleraf606d22022-10-26 21:40:12 -07008776#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8777
8778static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
Googler9398cc32022-12-02 17:21:52 +08008779 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8780 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8781 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8782 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8783 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8784 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
Googleraf606d22022-10-26 21:40:12 -07008785};
8786
8787static int __init fan_init(struct ibm_init_struct *iibm)
8788{
8789 int rc;
8790 unsigned long quirks;
8791
8792 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8793 "initializing fan subdriver\n");
8794
8795 mutex_init(&fan_mutex);
8796 fan_status_access_mode = TPACPI_FAN_NONE;
8797 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8798 fan_control_commands = 0;
8799 fan_watchdog_maxinterval = 0;
8800 tp_features.fan_ctrl_status_undef = 0;
8801 tp_features.second_fan = 0;
8802 fan_control_desired_level = 7;
8803
8804 if (tpacpi_is_ibm()) {
8805 TPACPI_ACPIHANDLE_INIT(fans);
8806 TPACPI_ACPIHANDLE_INIT(gfan);
8807 TPACPI_ACPIHANDLE_INIT(sfan);
8808 }
8809
8810 quirks = tpacpi_check_quirks(fan_quirk_table,
8811 ARRAY_SIZE(fan_quirk_table));
8812
8813 if (gfan_handle) {
8814 /* 570, 600e/x, 770e, 770x */
8815 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8816 } else {
8817 /* all other ThinkPads: note that even old-style
8818 * ThinkPad ECs supports the fan control register */
8819 if (likely(acpi_ec_read(fan_status_offset,
8820 &fan_control_initial_status))) {
8821 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8822 if (quirks & TPACPI_FAN_Q1)
8823 fan_quirk1_setup();
8824 if (quirks & TPACPI_FAN_2FAN) {
8825 tp_features.second_fan = 1;
8826 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8827 "secondary fan support enabled\n");
8828 }
8829 } else {
Googler9398cc32022-12-02 17:21:52 +08008830 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
Googleraf606d22022-10-26 21:40:12 -07008831 return 1;
8832 }
8833 }
8834
8835 if (sfan_handle) {
8836 /* 570, 770x-JL */
8837 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8838 fan_control_commands |=
8839 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8840 } else {
8841 if (!gfan_handle) {
8842 /* gfan without sfan means no fan control */
8843 /* all other models implement TP EC 0x2f control */
8844
8845 if (fans_handle) {
8846 /* X31, X40, X41 */
8847 fan_control_access_mode =
8848 TPACPI_FAN_WR_ACPI_FANS;
8849 fan_control_commands |=
8850 TPACPI_FAN_CMD_SPEED |
8851 TPACPI_FAN_CMD_LEVEL |
8852 TPACPI_FAN_CMD_ENABLE;
8853 } else {
8854 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8855 fan_control_commands |=
8856 TPACPI_FAN_CMD_LEVEL |
8857 TPACPI_FAN_CMD_ENABLE;
8858 }
8859 }
8860 }
8861
8862 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8863 "fan is %s, modes %d, %d\n",
8864 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8865 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8866 fan_status_access_mode, fan_control_access_mode);
8867
8868 /* fan control master switch */
8869 if (!fan_control_allowed) {
8870 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8871 fan_control_commands = 0;
8872 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8873 "fan control features disabled by parameter\n");
8874 }
8875
8876 /* update fan_control_desired_level */
8877 if (fan_status_access_mode != TPACPI_FAN_NONE)
8878 fan_get_status_safe(NULL);
8879
8880 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8881 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8882 if (tp_features.second_fan) {
8883 /* attach second fan tachometer */
8884 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8885 &dev_attr_fan2_input.attr;
8886 }
Googler9398cc32022-12-02 17:21:52 +08008887 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07008888 &fan_attr_group);
8889 if (rc < 0)
8890 return rc;
8891
8892 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8893 &driver_attr_fan_watchdog);
8894 if (rc < 0) {
Googler9398cc32022-12-02 17:21:52 +08008895 sysfs_remove_group(&tpacpi_hwmon->kobj,
Googleraf606d22022-10-26 21:40:12 -07008896 &fan_attr_group);
8897 return rc;
8898 }
8899 return 0;
8900 } else
8901 return 1;
8902}
8903
8904static void fan_exit(void)
8905{
8906 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8907 "cancelling any pending fan watchdog tasks\n");
8908
8909 /* FIXME: can we really do this unconditionally? */
Googler9398cc32022-12-02 17:21:52 +08008910 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
Googleraf606d22022-10-26 21:40:12 -07008911 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8912 &driver_attr_fan_watchdog);
8913
8914 cancel_delayed_work(&fan_watchdog_task);
8915 flush_workqueue(tpacpi_wq);
8916}
8917
8918static void fan_suspend(void)
8919{
8920 int rc;
8921
8922 if (!fan_control_allowed)
8923 return;
8924
8925 /* Store fan status in cache */
8926 fan_control_resume_level = 0;
8927 rc = fan_get_status_safe(&fan_control_resume_level);
8928 if (rc < 0)
Googler9398cc32022-12-02 17:21:52 +08008929 pr_notice("failed to read fan level for later restore during resume: %d\n",
8930 rc);
Googleraf606d22022-10-26 21:40:12 -07008931
8932 /* if it is undefined, don't attempt to restore it.
8933 * KEEP THIS LAST */
8934 if (tp_features.fan_ctrl_status_undef)
8935 fan_control_resume_level = 0;
8936}
8937
8938static void fan_resume(void)
8939{
8940 u8 current_level = 7;
8941 bool do_set = false;
8942 int rc;
8943
8944 /* DSDT *always* updates status on resume */
8945 tp_features.fan_ctrl_status_undef = 0;
8946
8947 if (!fan_control_allowed ||
8948 !fan_control_resume_level ||
8949 (fan_get_status_safe(&current_level) < 0))
8950 return;
8951
8952 switch (fan_control_access_mode) {
8953 case TPACPI_FAN_WR_ACPI_SFAN:
8954 /* never decrease fan level */
8955 do_set = (fan_control_resume_level > current_level);
8956 break;
8957 case TPACPI_FAN_WR_ACPI_FANS:
8958 case TPACPI_FAN_WR_TPEC:
8959 /* never decrease fan level, scale is:
8960 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8961 *
8962 * We expect the firmware to set either 7 or AUTO, but we
8963 * handle FULLSPEED out of paranoia.
8964 *
8965 * So, we can safely only restore FULLSPEED or 7, anything
8966 * else could slow the fan. Restoring AUTO is useless, at
8967 * best that's exactly what the DSDT already set (it is the
8968 * slower it uses).
8969 *
8970 * Always keep in mind that the DSDT *will* have set the
8971 * fans to what the vendor supposes is the best level. We
8972 * muck with it only to speed the fan up.
8973 */
8974 if (fan_control_resume_level != 7 &&
8975 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8976 return;
8977 else
8978 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8979 (current_level != fan_control_resume_level);
8980 break;
8981 default:
8982 return;
8983 }
8984 if (do_set) {
8985 pr_notice("restoring fan level to 0x%02x\n",
8986 fan_control_resume_level);
8987 rc = fan_set_level_safe(fan_control_resume_level);
8988 if (rc < 0)
8989 pr_notice("failed to restore fan level: %d\n", rc);
8990 }
8991}
8992
8993static int fan_read(struct seq_file *m)
8994{
8995 int rc;
8996 u8 status;
8997 unsigned int speed = 0;
8998
8999 switch (fan_status_access_mode) {
9000 case TPACPI_FAN_RD_ACPI_GFAN:
9001 /* 570, 600e/x, 770e, 770x */
9002 rc = fan_get_status_safe(&status);
9003 if (rc < 0)
9004 return rc;
9005
9006 seq_printf(m, "status:\t\t%s\n"
9007 "level:\t\t%d\n",
9008 (status != 0) ? "enabled" : "disabled", status);
9009 break;
9010
9011 case TPACPI_FAN_RD_TPEC:
9012 /* all except 570, 600e/x, 770e, 770x */
9013 rc = fan_get_status_safe(&status);
9014 if (rc < 0)
9015 return rc;
9016
9017 seq_printf(m, "status:\t\t%s\n",
9018 (status != 0) ? "enabled" : "disabled");
9019
9020 rc = fan_get_speed(&speed);
9021 if (rc < 0)
9022 return rc;
9023
9024 seq_printf(m, "speed:\t\t%d\n", speed);
9025
9026 if (status & TP_EC_FAN_FULLSPEED)
9027 /* Disengaged mode takes precedence */
9028 seq_printf(m, "level:\t\tdisengaged\n");
9029 else if (status & TP_EC_FAN_AUTO)
9030 seq_printf(m, "level:\t\tauto\n");
9031 else
9032 seq_printf(m, "level:\t\t%d\n", status);
9033 break;
9034
9035 case TPACPI_FAN_NONE:
9036 default:
9037 seq_printf(m, "status:\t\tnot supported\n");
9038 }
9039
9040 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9041 seq_printf(m, "commands:\tlevel <level>");
9042
9043 switch (fan_control_access_mode) {
9044 case TPACPI_FAN_WR_ACPI_SFAN:
9045 seq_printf(m, " (<level> is 0-7)\n");
9046 break;
9047
9048 default:
Googler9398cc32022-12-02 17:21:52 +08009049 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
Googleraf606d22022-10-26 21:40:12 -07009050 break;
9051 }
9052 }
9053
9054 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9055 seq_printf(m, "commands:\tenable, disable\n"
Googler9398cc32022-12-02 17:21:52 +08009056 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
Googleraf606d22022-10-26 21:40:12 -07009057
9058 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Googler9398cc32022-12-02 17:21:52 +08009059 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
Googleraf606d22022-10-26 21:40:12 -07009060
9061 return 0;
9062}
9063
9064static int fan_write_cmd_level(const char *cmd, int *rc)
9065{
9066 int level;
9067
9068 if (strlencmp(cmd, "level auto") == 0)
9069 level = TP_EC_FAN_AUTO;
Googlerb48fa912023-03-17 12:40:29 +05309070 else if ((strlencmp(cmd, "level disengaged") == 0) |
Googleraf606d22022-10-26 21:40:12 -07009071 (strlencmp(cmd, "level full-speed") == 0))
9072 level = TP_EC_FAN_FULLSPEED;
9073 else if (sscanf(cmd, "level %d", &level) != 1)
9074 return 0;
9075
9076 *rc = fan_set_level_safe(level);
9077 if (*rc == -ENXIO)
9078 pr_err("level command accepted for unsupported access mode %d\n",
9079 fan_control_access_mode);
9080 else if (!*rc)
9081 tpacpi_disclose_usertask("procfs fan",
9082 "set level to %d\n", level);
9083
9084 return 1;
9085}
9086
9087static int fan_write_cmd_enable(const char *cmd, int *rc)
9088{
9089 if (strlencmp(cmd, "enable") != 0)
9090 return 0;
9091
9092 *rc = fan_set_enable();
9093 if (*rc == -ENXIO)
9094 pr_err("enable command accepted for unsupported access mode %d\n",
9095 fan_control_access_mode);
9096 else if (!*rc)
9097 tpacpi_disclose_usertask("procfs fan", "enable\n");
9098
9099 return 1;
9100}
9101
9102static int fan_write_cmd_disable(const char *cmd, int *rc)
9103{
9104 if (strlencmp(cmd, "disable") != 0)
9105 return 0;
9106
9107 *rc = fan_set_disable();
9108 if (*rc == -ENXIO)
9109 pr_err("disable command accepted for unsupported access mode %d\n",
9110 fan_control_access_mode);
9111 else if (!*rc)
9112 tpacpi_disclose_usertask("procfs fan", "disable\n");
9113
9114 return 1;
9115}
9116
9117static int fan_write_cmd_speed(const char *cmd, int *rc)
9118{
9119 int speed;
9120
9121 /* TODO:
9122 * Support speed <low> <medium> <high> ? */
9123
9124 if (sscanf(cmd, "speed %d", &speed) != 1)
9125 return 0;
9126
9127 *rc = fan_set_speed(speed);
9128 if (*rc == -ENXIO)
9129 pr_err("speed command accepted for unsupported access mode %d\n",
9130 fan_control_access_mode);
9131 else if (!*rc)
9132 tpacpi_disclose_usertask("procfs fan",
9133 "set speed to %d\n", speed);
9134
9135 return 1;
9136}
9137
9138static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9139{
9140 int interval;
9141
9142 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9143 return 0;
9144
9145 if (interval < 0 || interval > 120)
9146 *rc = -EINVAL;
9147 else {
9148 fan_watchdog_maxinterval = interval;
9149 tpacpi_disclose_usertask("procfs fan",
9150 "set watchdog timer to %d\n",
9151 interval);
9152 }
9153
9154 return 1;
9155}
9156
9157static int fan_write(char *buf)
9158{
9159 char *cmd;
9160 int rc = 0;
9161
9162 while (!rc && (cmd = next_cmd(&buf))) {
9163 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9164 fan_write_cmd_level(cmd, &rc)) &&
9165 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9166 (fan_write_cmd_enable(cmd, &rc) ||
9167 fan_write_cmd_disable(cmd, &rc) ||
9168 fan_write_cmd_watchdog(cmd, &rc))) &&
9169 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9170 fan_write_cmd_speed(cmd, &rc))
9171 )
9172 rc = -EINVAL;
9173 else if (!rc)
9174 fan_watchdog_reset();
9175 }
9176
9177 return rc;
9178}
9179
9180static struct ibm_struct fan_driver_data = {
9181 .name = "fan",
9182 .read = fan_read,
9183 .write = fan_write,
9184 .exit = fan_exit,
9185 .suspend = fan_suspend,
9186 .resume = fan_resume,
9187};
9188
9189/*************************************************************************
9190 * Mute LED subdriver
9191 */
9192
Googler9398cc32022-12-02 17:21:52 +08009193#define TPACPI_LED_MAX 2
Googleraf606d22022-10-26 21:40:12 -07009194
9195struct tp_led_table {
9196 acpi_string name;
9197 int on_value;
9198 int off_value;
9199 int state;
9200};
9201
Googler9398cc32022-12-02 17:21:52 +08009202static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9203 [LED_AUDIO_MUTE] = {
Googleraf606d22022-10-26 21:40:12 -07009204 .name = "SSMS",
9205 .on_value = 1,
9206 .off_value = 0,
9207 },
Googler9398cc32022-12-02 17:21:52 +08009208 [LED_AUDIO_MICMUTE] = {
Googleraf606d22022-10-26 21:40:12 -07009209 .name = "MMTS",
9210 .on_value = 2,
9211 .off_value = 0,
9212 },
9213};
9214
9215static int mute_led_on_off(struct tp_led_table *t, bool state)
9216{
9217 acpi_handle temp;
9218 int output;
9219
Googler9398cc32022-12-02 17:21:52 +08009220 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
Googleraf606d22022-10-26 21:40:12 -07009221 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9222 return -EIO;
9223 }
9224
9225 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9226 state ? t->on_value : t->off_value))
9227 return -EIO;
9228
9229 t->state = state;
9230 return state;
9231}
9232
Googler9398cc32022-12-02 17:21:52 +08009233static int tpacpi_led_set(int whichled, bool on)
Googleraf606d22022-10-26 21:40:12 -07009234{
9235 struct tp_led_table *t;
9236
9237 t = &led_tables[whichled];
9238 if (t->state < 0 || t->state == on)
9239 return t->state;
9240 return mute_led_on_off(t, on);
9241}
Googler9398cc32022-12-02 17:21:52 +08009242
9243static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9244 enum led_brightness brightness)
9245{
9246 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9247}
9248
9249static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9250 enum led_brightness brightness)
9251{
9252 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9253}
9254
9255static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9256 [LED_AUDIO_MUTE] = {
9257 .name = "platform::mute",
9258 .max_brightness = 1,
9259 .brightness_set_blocking = tpacpi_led_mute_set,
9260 .default_trigger = "audio-mute",
9261 },
9262 [LED_AUDIO_MICMUTE] = {
9263 .name = "platform::micmute",
9264 .max_brightness = 1,
9265 .brightness_set_blocking = tpacpi_led_micmute_set,
9266 .default_trigger = "audio-micmute",
9267 },
9268};
Googleraf606d22022-10-26 21:40:12 -07009269
9270static int mute_led_init(struct ibm_init_struct *iibm)
9271{
9272 acpi_handle temp;
Googler9398cc32022-12-02 17:21:52 +08009273 int i, err;
Googleraf606d22022-10-26 21:40:12 -07009274
9275 for (i = 0; i < TPACPI_LED_MAX; i++) {
9276 struct tp_led_table *t = &led_tables[i];
Googler9398cc32022-12-02 17:21:52 +08009277 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
Googleraf606d22022-10-26 21:40:12 -07009278 t->state = -ENODEV;
Googler9398cc32022-12-02 17:21:52 +08009279 continue;
9280 }
9281
9282 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9283 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9284 if (err < 0) {
9285 while (i--) {
9286 if (led_tables[i].state >= 0)
9287 led_classdev_unregister(&mute_led_cdev[i]);
9288 }
9289 return err;
9290 }
Googleraf606d22022-10-26 21:40:12 -07009291 }
9292 return 0;
9293}
9294
9295static void mute_led_exit(void)
9296{
9297 int i;
9298
Googler9398cc32022-12-02 17:21:52 +08009299 for (i = 0; i < TPACPI_LED_MAX; i++) {
9300 if (led_tables[i].state >= 0) {
9301 led_classdev_unregister(&mute_led_cdev[i]);
9302 tpacpi_led_set(i, false);
9303 }
9304 }
Googleraf606d22022-10-26 21:40:12 -07009305}
9306
9307static void mute_led_resume(void)
9308{
9309 int i;
9310
9311 for (i = 0; i < TPACPI_LED_MAX; i++) {
9312 struct tp_led_table *t = &led_tables[i];
9313 if (t->state >= 0)
9314 mute_led_on_off(t, t->state);
9315 }
9316}
9317
9318static struct ibm_struct mute_led_driver_data = {
9319 .name = "mute_led",
9320 .exit = mute_led_exit,
9321 .resume = mute_led_resume,
9322};
9323
Googler9398cc32022-12-02 17:21:52 +08009324/*
9325 * Battery Wear Control Driver
9326 * Contact: Ognjen Galic <smclt30p@gmail.com>
9327 */
9328
9329/* Metadata */
9330
9331#define GET_START "BCTG"
9332#define SET_START "BCCS"
9333#define GET_STOP "BCSG"
9334#define SET_STOP "BCSS"
9335
9336#define START_ATTR "charge_start_threshold"
9337#define STOP_ATTR "charge_stop_threshold"
9338
9339enum {
9340 BAT_ANY = 0,
9341 BAT_PRIMARY = 1,
9342 BAT_SECONDARY = 2
9343};
9344
9345enum {
9346 /* Error condition bit */
9347 METHOD_ERR = BIT(31),
9348};
9349
9350enum {
9351 /* This is used in the get/set helpers */
9352 THRESHOLD_START,
9353 THRESHOLD_STOP,
9354};
9355
9356struct tpacpi_battery_data {
9357 int charge_start;
9358 int start_support;
9359 int charge_stop;
9360 int stop_support;
9361};
9362
9363struct tpacpi_battery_driver_data {
9364 struct tpacpi_battery_data batteries[3];
9365 int individual_addressing;
9366};
9367
9368static struct tpacpi_battery_driver_data battery_info;
9369
9370/* ACPI helpers/functions/probes */
9371
9372/**
9373 * This evaluates a ACPI method call specific to the battery
9374 * ACPI extension. The specifics are that an error is marked
9375 * in the 32rd bit of the response, so we just check that here.
9376 */
9377static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9378{
9379 int response;
9380
9381 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9382 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9383 return AE_ERROR;
9384 }
9385 if (response & METHOD_ERR) {
9386 acpi_handle_err(hkey_handle,
9387 "%s evaluated but flagged as error", method);
9388 return AE_ERROR;
9389 }
9390 *ret = response;
9391 return AE_OK;
9392}
9393
9394static int tpacpi_battery_get(int what, int battery, int *ret)
9395{
9396 switch (what) {
9397 case THRESHOLD_START:
9398 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9399 return -ENODEV;
9400
9401 /* The value is in the low 8 bits of the response */
9402 *ret = *ret & 0xFF;
9403 return 0;
9404 case THRESHOLD_STOP:
9405 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9406 return -ENODEV;
9407 /* Value is in lower 8 bits */
9408 *ret = *ret & 0xFF;
9409 /*
9410 * On the stop value, if we return 0 that
9411 * does not make any sense. 0 means Default, which
9412 * means that charging stops at 100%, so we return
9413 * that.
9414 */
9415 if (*ret == 0)
9416 *ret = 100;
9417 return 0;
9418 default:
9419 pr_crit("wrong parameter: %d", what);
9420 return -EINVAL;
9421 }
9422}
9423
9424static int tpacpi_battery_set(int what, int battery, int value)
9425{
9426 int param, ret;
9427 /* The first 8 bits are the value of the threshold */
9428 param = value;
9429 /* The battery ID is in bits 8-9, 2 bits */
9430 param |= battery << 8;
9431
9432 switch (what) {
9433 case THRESHOLD_START:
9434 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9435 pr_err("failed to set charge threshold on battery %d",
9436 battery);
9437 return -ENODEV;
9438 }
9439 return 0;
9440 case THRESHOLD_STOP:
9441 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9442 pr_err("failed to set stop threshold: %d", battery);
9443 return -ENODEV;
9444 }
9445 return 0;
9446 default:
9447 pr_crit("wrong parameter: %d", what);
9448 return -EINVAL;
9449 }
9450}
9451
9452static int tpacpi_battery_probe(int battery)
9453{
9454 int ret = 0;
9455
9456 memset(&battery_info.batteries[battery], 0,
9457 sizeof(battery_info.batteries[battery]));
9458
9459 /*
9460 * 1) Get the current start threshold
9461 * 2) Check for support
9462 * 3) Get the current stop threshold
9463 * 4) Check for support
9464 */
9465 if (acpi_has_method(hkey_handle, GET_START)) {
9466 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9467 pr_err("Error probing battery %d\n", battery);
9468 return -ENODEV;
9469 }
9470 /* Individual addressing is in bit 9 */
9471 if (ret & BIT(9))
9472 battery_info.individual_addressing = true;
9473 /* Support is marked in bit 8 */
9474 if (ret & BIT(8))
9475 battery_info.batteries[battery].start_support = 1;
9476 else
9477 return -ENODEV;
9478 if (tpacpi_battery_get(THRESHOLD_START, battery,
9479 &battery_info.batteries[battery].charge_start)) {
9480 pr_err("Error probing battery %d\n", battery);
9481 return -ENODEV;
9482 }
9483 }
9484 if (acpi_has_method(hkey_handle, GET_STOP)) {
9485 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9486 pr_err("Error probing battery stop; %d\n", battery);
9487 return -ENODEV;
9488 }
9489 /* Support is marked in bit 8 */
9490 if (ret & BIT(8))
9491 battery_info.batteries[battery].stop_support = 1;
9492 else
9493 return -ENODEV;
9494 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9495 &battery_info.batteries[battery].charge_stop)) {
9496 pr_err("Error probing battery stop: %d\n", battery);
9497 return -ENODEV;
9498 }
9499 }
9500 pr_info("battery %d registered (start %d, stop %d)",
9501 battery,
9502 battery_info.batteries[battery].charge_start,
9503 battery_info.batteries[battery].charge_stop);
9504
9505 return 0;
9506}
9507
9508/* General helper functions */
9509
9510static int tpacpi_battery_get_id(const char *battery_name)
9511{
9512
9513 if (strcmp(battery_name, "BAT0") == 0 ||
9514 tp_features.battery_force_primary)
9515 return BAT_PRIMARY;
9516 if (strcmp(battery_name, "BAT1") == 0)
9517 return BAT_SECONDARY;
9518 /*
9519 * If for some reason the battery is not BAT0 nor is it
9520 * BAT1, we will assume it's the default, first battery,
9521 * AKA primary.
9522 */
9523 pr_warn("unknown battery %s, assuming primary", battery_name);
9524 return BAT_PRIMARY;
9525}
9526
9527/* sysfs interface */
9528
9529static ssize_t tpacpi_battery_store(int what,
9530 struct device *dev,
9531 const char *buf, size_t count)
9532{
9533 struct power_supply *supply = to_power_supply(dev);
9534 unsigned long value;
9535 int battery, rval;
9536 /*
9537 * Some systems have support for more than
9538 * one battery. If that is the case,
9539 * tpacpi_battery_probe marked that addressing
9540 * them individually is supported, so we do that
9541 * based on the device struct.
9542 *
9543 * On systems that are not supported, we assume
9544 * the primary as most of the ACPI calls fail
9545 * with "Any Battery" as the parameter.
9546 */
9547 if (battery_info.individual_addressing)
9548 /* BAT_PRIMARY or BAT_SECONDARY */
9549 battery = tpacpi_battery_get_id(supply->desc->name);
9550 else
9551 battery = BAT_PRIMARY;
9552
9553 rval = kstrtoul(buf, 10, &value);
9554 if (rval)
9555 return rval;
9556
9557 switch (what) {
9558 case THRESHOLD_START:
9559 if (!battery_info.batteries[battery].start_support)
9560 return -ENODEV;
9561 /* valid values are [0, 99] */
9562 if (value < 0 || value > 99)
9563 return -EINVAL;
9564 if (value > battery_info.batteries[battery].charge_stop)
9565 return -EINVAL;
9566 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9567 return -ENODEV;
9568 battery_info.batteries[battery].charge_start = value;
9569 return count;
9570
9571 case THRESHOLD_STOP:
9572 if (!battery_info.batteries[battery].stop_support)
9573 return -ENODEV;
9574 /* valid values are [1, 100] */
9575 if (value < 1 || value > 100)
9576 return -EINVAL;
9577 if (value < battery_info.batteries[battery].charge_start)
9578 return -EINVAL;
9579 battery_info.batteries[battery].charge_stop = value;
9580 /*
9581 * When 100 is passed to stop, we need to flip
9582 * it to 0 as that the EC understands that as
9583 * "Default", which will charge to 100%
9584 */
9585 if (value == 100)
9586 value = 0;
9587 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9588 return -EINVAL;
9589 return count;
9590 default:
9591 pr_crit("Wrong parameter: %d", what);
9592 return -EINVAL;
9593 }
9594 return count;
9595}
9596
9597static ssize_t tpacpi_battery_show(int what,
9598 struct device *dev,
9599 char *buf)
9600{
9601 struct power_supply *supply = to_power_supply(dev);
9602 int ret, battery;
9603 /*
9604 * Some systems have support for more than
9605 * one battery. If that is the case,
9606 * tpacpi_battery_probe marked that addressing
9607 * them individually is supported, so we;
9608 * based on the device struct.
9609 *
9610 * On systems that are not supported, we assume
9611 * the primary as most of the ACPI calls fail
9612 * with "Any Battery" as the parameter.
9613 */
9614 if (battery_info.individual_addressing)
9615 /* BAT_PRIMARY or BAT_SECONDARY */
9616 battery = tpacpi_battery_get_id(supply->desc->name);
9617 else
9618 battery = BAT_PRIMARY;
9619 if (tpacpi_battery_get(what, battery, &ret))
9620 return -ENODEV;
9621 return sprintf(buf, "%d\n", ret);
9622}
9623
9624static ssize_t charge_start_threshold_show(struct device *device,
9625 struct device_attribute *attr,
9626 char *buf)
9627{
9628 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9629}
9630
9631static ssize_t charge_stop_threshold_show(struct device *device,
9632 struct device_attribute *attr,
9633 char *buf)
9634{
9635 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9636}
9637
9638static ssize_t charge_start_threshold_store(struct device *dev,
9639 struct device_attribute *attr,
9640 const char *buf, size_t count)
9641{
9642 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9643}
9644
9645static ssize_t charge_stop_threshold_store(struct device *dev,
9646 struct device_attribute *attr,
9647 const char *buf, size_t count)
9648{
9649 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9650}
9651
9652static DEVICE_ATTR_RW(charge_start_threshold);
9653static DEVICE_ATTR_RW(charge_stop_threshold);
9654
9655static struct attribute *tpacpi_battery_attrs[] = {
9656 &dev_attr_charge_start_threshold.attr,
9657 &dev_attr_charge_stop_threshold.attr,
9658 NULL,
9659};
9660
9661ATTRIBUTE_GROUPS(tpacpi_battery);
9662
9663/* ACPI battery hooking */
9664
9665static int tpacpi_battery_add(struct power_supply *battery)
9666{
9667 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9668
9669 if (tpacpi_battery_probe(batteryid))
9670 return -ENODEV;
9671 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9672 return -ENODEV;
9673 return 0;
9674}
9675
9676static int tpacpi_battery_remove(struct power_supply *battery)
9677{
9678 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9679 return 0;
9680}
9681
9682static struct acpi_battery_hook battery_hook = {
9683 .add_battery = tpacpi_battery_add,
9684 .remove_battery = tpacpi_battery_remove,
9685 .name = "ThinkPad Battery Extension",
9686};
9687
9688/* Subdriver init/exit */
9689
9690static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9691 /*
9692 * Individual addressing is broken on models that expose the
9693 * primary battery as BAT1.
9694 */
9695 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9696 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9697 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9698 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9699 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9700 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
9701};
9702
9703static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9704{
9705 memset(&battery_info, 0, sizeof(battery_info));
9706
9707 tp_features.battery_force_primary = tpacpi_check_quirks(
9708 battery_quirk_table,
9709 ARRAY_SIZE(battery_quirk_table));
9710
9711 battery_hook_register(&battery_hook);
9712 return 0;
9713}
9714
9715static void tpacpi_battery_exit(void)
9716{
9717 battery_hook_unregister(&battery_hook);
9718}
9719
9720static struct ibm_struct battery_driver_data = {
9721 .name = "battery",
9722 .exit = tpacpi_battery_exit,
9723};
9724
9725/*************************************************************************
9726 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9727 */
9728
9729static int lcdshadow_state;
9730
9731static int lcdshadow_on_off(bool state)
9732{
9733 acpi_handle set_shadow_handle;
9734 int output;
9735
9736 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSSS", &set_shadow_handle))) {
9737 pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9738 return -EIO;
9739 }
9740
9741 if (!acpi_evalf(set_shadow_handle, &output, NULL, "dd", (int)state))
9742 return -EIO;
9743
9744 lcdshadow_state = state;
9745 return 0;
9746}
9747
9748static int lcdshadow_set(bool on)
9749{
9750 if (lcdshadow_state < 0)
9751 return lcdshadow_state;
9752 if (lcdshadow_state == on)
9753 return 0;
9754 return lcdshadow_on_off(on);
9755}
9756
9757static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9758{
9759 acpi_handle get_shadow_handle;
9760 int output;
9761
9762 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSSS", &get_shadow_handle))) {
9763 lcdshadow_state = -ENODEV;
9764 return 0;
9765 }
9766
9767 if (!acpi_evalf(get_shadow_handle, &output, NULL, "dd", 0)) {
9768 lcdshadow_state = -EIO;
9769 return -EIO;
9770 }
9771 if (!(output & 0x10000)) {
9772 lcdshadow_state = -ENODEV;
9773 return 0;
9774 }
9775 lcdshadow_state = output & 0x1;
9776
9777 return 0;
9778}
9779
9780static void lcdshadow_resume(void)
9781{
9782 if (lcdshadow_state >= 0)
9783 lcdshadow_on_off(lcdshadow_state);
9784}
9785
9786static int lcdshadow_read(struct seq_file *m)
9787{
9788 if (lcdshadow_state < 0) {
9789 seq_puts(m, "status:\t\tnot supported\n");
9790 } else {
9791 seq_printf(m, "status:\t\t%d\n", lcdshadow_state);
9792 seq_puts(m, "commands:\t0, 1\n");
9793 }
9794
9795 return 0;
9796}
9797
9798static int lcdshadow_write(char *buf)
9799{
9800 char *cmd;
9801 int state = -1;
9802
9803 if (lcdshadow_state < 0)
9804 return -ENODEV;
9805
9806 while ((cmd = next_cmd(&buf))) {
9807 if (strlencmp(cmd, "0") == 0)
9808 state = 0;
9809 else if (strlencmp(cmd, "1") == 0)
9810 state = 1;
9811 }
9812
9813 if (state == -1)
9814 return -EINVAL;
9815
9816 return lcdshadow_set(state);
9817}
9818
9819static struct ibm_struct lcdshadow_driver_data = {
9820 .name = "lcdshadow",
9821 .resume = lcdshadow_resume,
9822 .read = lcdshadow_read,
9823 .write = lcdshadow_write,
9824};
9825
Googleraf606d22022-10-26 21:40:12 -07009826/****************************************************************************
9827 ****************************************************************************
9828 *
9829 * Infrastructure
9830 *
9831 ****************************************************************************
9832 ****************************************************************************/
9833
9834/*
9835 * HKEY event callout for other subdrivers go here
9836 * (yes, it is ugly, but it is quick, safe, and gets the job done
9837 */
9838static void tpacpi_driver_event(const unsigned int hkey_event)
9839{
9840 if (ibm_backlight_device) {
9841 switch (hkey_event) {
9842 case TP_HKEY_EV_BRGHT_UP:
9843 case TP_HKEY_EV_BRGHT_DOWN:
9844 tpacpi_brightness_notify_change();
9845 }
9846 }
9847 if (alsa_card) {
9848 switch (hkey_event) {
9849 case TP_HKEY_EV_VOL_UP:
9850 case TP_HKEY_EV_VOL_DOWN:
9851 case TP_HKEY_EV_VOL_MUTE:
9852 volume_alsa_notify_change();
9853 }
9854 }
Googler9398cc32022-12-02 17:21:52 +08009855 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9856 enum led_brightness brightness;
9857
9858 mutex_lock(&kbdlight_mutex);
9859
9860 /*
9861 * Check the brightness actually changed, setting the brightness
9862 * through kbdlight_set_level() also triggers this event.
9863 */
9864 brightness = kbdlight_sysfs_get(NULL);
9865 if (kbdlight_brightness != brightness) {
9866 kbdlight_brightness = brightness;
9867 led_classdev_notify_brightness_hw_changed(
9868 &tpacpi_led_kbdlight.led_classdev, brightness);
9869 }
9870
9871 mutex_unlock(&kbdlight_mutex);
9872 }
Googleraf606d22022-10-26 21:40:12 -07009873}
9874
9875static void hotkey_driver_event(const unsigned int scancode)
9876{
9877 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9878}
9879
9880/* --------------------------------------------------------------------- */
9881
9882/* /proc support */
9883static struct proc_dir_entry *proc_dir;
9884
9885/*
9886 * Module and infrastructure proble, init and exit handling
9887 */
9888
9889static bool force_load;
9890
9891#ifdef CONFIG_THINKPAD_ACPI_DEBUG
9892static const char * __init str_supported(int is_supported)
9893{
9894 static char text_unsupported[] __initdata = "not supported";
9895
9896 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9897}
9898#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9899
9900static void ibm_exit(struct ibm_struct *ibm)
9901{
9902 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9903
9904 list_del_init(&ibm->all_drivers);
9905
9906 if (ibm->flags.acpi_notify_installed) {
9907 dbg_printk(TPACPI_DBG_EXIT,
9908 "%s: acpi_remove_notify_handler\n", ibm->name);
9909 BUG_ON(!ibm->acpi);
9910 acpi_remove_notify_handler(*ibm->acpi->handle,
9911 ibm->acpi->type,
9912 dispatch_acpi_notify);
9913 ibm->flags.acpi_notify_installed = 0;
9914 }
9915
9916 if (ibm->flags.proc_created) {
9917 dbg_printk(TPACPI_DBG_EXIT,
9918 "%s: remove_proc_entry\n", ibm->name);
9919 remove_proc_entry(ibm->name, proc_dir);
9920 ibm->flags.proc_created = 0;
9921 }
9922
9923 if (ibm->flags.acpi_driver_registered) {
9924 dbg_printk(TPACPI_DBG_EXIT,
9925 "%s: acpi_bus_unregister_driver\n", ibm->name);
9926 BUG_ON(!ibm->acpi);
9927 acpi_bus_unregister_driver(ibm->acpi->driver);
9928 kfree(ibm->acpi->driver);
9929 ibm->acpi->driver = NULL;
9930 ibm->flags.acpi_driver_registered = 0;
9931 }
9932
9933 if (ibm->flags.init_called && ibm->exit) {
9934 ibm->exit();
9935 ibm->flags.init_called = 0;
9936 }
9937
9938 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9939}
9940
9941static int __init ibm_init(struct ibm_init_struct *iibm)
9942{
9943 int ret;
9944 struct ibm_struct *ibm = iibm->data;
9945 struct proc_dir_entry *entry;
9946
9947 BUG_ON(ibm == NULL);
9948
9949 INIT_LIST_HEAD(&ibm->all_drivers);
9950
9951 if (ibm->flags.experimental && !experimental)
9952 return 0;
9953
9954 dbg_printk(TPACPI_DBG_INIT,
9955 "probing for %s\n", ibm->name);
9956
9957 if (iibm->init) {
9958 ret = iibm->init(iibm);
9959 if (ret > 0)
9960 return 0; /* probe failed */
9961 if (ret)
9962 return ret;
9963
9964 ibm->flags.init_called = 1;
9965 }
9966
9967 if (ibm->acpi) {
9968 if (ibm->acpi->hid) {
9969 ret = register_tpacpi_subdriver(ibm);
9970 if (ret)
9971 goto err_out;
9972 }
9973
9974 if (ibm->acpi->notify) {
9975 ret = setup_acpi_notify(ibm);
9976 if (ret == -ENODEV) {
9977 pr_notice("disabling subdriver %s\n",
9978 ibm->name);
9979 ret = 0;
9980 goto err_out;
9981 }
9982 if (ret < 0)
9983 goto err_out;
9984 }
9985 }
9986
9987 dbg_printk(TPACPI_DBG_INIT,
9988 "%s installed\n", ibm->name);
9989
9990 if (ibm->read) {
9991 umode_t mode = iibm->base_procfs_mode;
9992
9993 if (!mode)
9994 mode = S_IRUGO;
9995 if (ibm->write)
9996 mode |= S_IWUSR;
9997 entry = proc_create_data(ibm->name, mode, proc_dir,
9998 &dispatch_proc_fops, ibm);
9999 if (!entry) {
10000 pr_err("unable to create proc entry %s\n", ibm->name);
10001 ret = -ENODEV;
10002 goto err_out;
10003 }
10004 ibm->flags.proc_created = 1;
10005 }
10006
10007 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
10008
10009 return 0;
10010
10011err_out:
10012 dbg_printk(TPACPI_DBG_INIT,
10013 "%s: at error exit path with result %d\n",
10014 ibm->name, ret);
10015
10016 ibm_exit(ibm);
10017 return (ret < 0) ? ret : 0;
10018}
10019
10020/* Probing */
10021
Googler9398cc32022-12-02 17:21:52 +080010022static char __init tpacpi_parse_fw_id(const char * const s,
10023 u32 *model, u16 *release)
Googleraf606d22022-10-26 21:40:12 -070010024{
Googler9398cc32022-12-02 17:21:52 +080010025 int i;
Googleraf606d22022-10-26 21:40:12 -070010026
Googler9398cc32022-12-02 17:21:52 +080010027 if (!s || strlen(s) < 8)
10028 goto invalid;
10029
10030 for (i = 0; i < 8; i++)
10031 if (!((s[i] >= '0' && s[i] <= '9') ||
10032 (s[i] >= 'A' && s[i] <= 'Z')))
10033 goto invalid;
10034
Googleraf606d22022-10-26 21:40:12 -070010035 /*
10036 * Most models: xxyTkkWW (#.##c)
10037 * Ancient 570/600 and -SL lacks (#.##c)
10038 */
Googler9398cc32022-12-02 17:21:52 +080010039 if (s[3] == 'T' || s[3] == 'N') {
10040 *model = TPID(s[0], s[1]);
10041 *release = TPVER(s[4], s[5]);
10042 return s[2];
Googleraf606d22022-10-26 21:40:12 -070010043
10044 /* New models: xxxyTkkW (#.##c); T550 and some others */
Googler9398cc32022-12-02 17:21:52 +080010045 } else if (s[4] == 'T' || s[4] == 'N') {
10046 *model = TPID3(s[0], s[1], s[2]);
10047 *release = TPVER(s[5], s[6]);
10048 return s[3];
10049 }
10050
10051invalid:
10052 return '\0';
10053}
10054
10055static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10056{
10057 char *ec_fw_string = (char *) private;
10058 const char *dmi_data = (const char *)dm;
10059 /*
10060 * ThinkPad Embedded Controller Program Table on newer models
10061 *
10062 * Offset | Name | Width | Description
10063 * ----------------------------------------------------
10064 * 0x00 | Type | BYTE | 0x8C
10065 * 0x01 | Length | BYTE |
10066 * 0x02 | Handle | WORD | Varies
10067 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10068 * 0x0A | OEM struct offset | BYTE | 0x0B
10069 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10070 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10071 * 0x0D | ECP version ID | STR ID |
10072 * 0x0E | ECP release date | STR ID |
10073 */
10074
10075 /* Return if data structure not match */
10076 if (dm->type != 140 || dm->length < 0x0F ||
10077 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10078 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10079 dmi_data[0x0C] != 0x01)
10080 return;
10081
10082 /* fwstr is the first 8byte string */
10083 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
Googleraf606d22022-10-26 21:40:12 -070010084}
10085
10086/* returns 0 - probe ok, or < 0 - probe error.
10087 * Probe ok doesn't mean thinkpad found.
10088 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
10089static int __must_check __init get_thinkpad_model_data(
10090 struct thinkpad_id_data *tp)
10091{
10092 const struct dmi_device *dev = NULL;
Googler9398cc32022-12-02 17:21:52 +080010093 char ec_fw_string[18] = {0};
Googleraf606d22022-10-26 21:40:12 -070010094 char const *s;
Googler9398cc32022-12-02 17:21:52 +080010095 char t;
Googleraf606d22022-10-26 21:40:12 -070010096
10097 if (!tp)
10098 return -EINVAL;
10099
10100 memset(tp, 0, sizeof(*tp));
10101
10102 if (dmi_name_in_vendors("IBM"))
10103 tp->vendor = PCI_VENDOR_ID_IBM;
10104 else if (dmi_name_in_vendors("LENOVO"))
10105 tp->vendor = PCI_VENDOR_ID_LENOVO;
10106 else
10107 return 0;
10108
10109 s = dmi_get_system_info(DMI_BIOS_VERSION);
10110 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10111 if (s && !tp->bios_version_str)
10112 return -ENOMEM;
10113
10114 /* Really ancient ThinkPad 240X will fail this, which is fine */
Googler9398cc32022-12-02 17:21:52 +080010115 t = tpacpi_parse_fw_id(tp->bios_version_str,
10116 &tp->bios_model, &tp->bios_release);
10117 if (t != 'E' && t != 'C')
Googleraf606d22022-10-26 21:40:12 -070010118 return 0;
10119
10120 /*
10121 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10122 * X32 or newer, all Z series; Some models must have an
10123 * up-to-date BIOS or they will not be detected.
10124 *
10125 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10126 */
10127 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
10128 if (sscanf(dev->name,
10129 "IBM ThinkPad Embedded Controller -[%17c",
10130 ec_fw_string) == 1) {
10131 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10132 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
10133 break;
10134 }
10135 }
10136
Googler9398cc32022-12-02 17:21:52 +080010137 /* Newer ThinkPads have different EC program info table */
10138 if (!ec_fw_string[0])
10139 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10140
10141 if (ec_fw_string[0]) {
10142 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10143 if (!tp->ec_version_str)
10144 return -ENOMEM;
10145
10146 t = tpacpi_parse_fw_id(ec_fw_string,
10147 &tp->ec_model, &tp->ec_release);
10148 if (t != 'H') {
10149 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10150 ec_fw_string);
10151 pr_notice("please report this to %s\n", TPACPI_MAIL);
10152 }
10153 }
10154
Googleraf606d22022-10-26 21:40:12 -070010155 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
10156 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
10157 tp->model_str = kstrdup(s, GFP_KERNEL);
10158 if (!tp->model_str)
10159 return -ENOMEM;
10160 } else {
10161 s = dmi_get_system_info(DMI_BIOS_VENDOR);
10162 if (s && !(strncasecmp(s, "Lenovo", 6))) {
10163 tp->model_str = kstrdup(s, GFP_KERNEL);
10164 if (!tp->model_str)
10165 return -ENOMEM;
10166 }
10167 }
10168
10169 s = dmi_get_system_info(DMI_PRODUCT_NAME);
10170 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10171 if (s && !tp->nummodel_str)
10172 return -ENOMEM;
10173
10174 return 0;
10175}
10176
10177static int __init probe_for_thinkpad(void)
10178{
10179 int is_thinkpad;
10180
10181 if (acpi_disabled)
10182 return -ENODEV;
10183
10184 /* It would be dangerous to run the driver in this case */
10185 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10186 return -ENODEV;
10187
10188 /*
10189 * Non-ancient models have better DMI tagging, but very old models
10190 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
10191 */
10192 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10193 (thinkpad_id.ec_model != 0) ||
10194 tpacpi_is_fw_known();
10195
10196 /* The EC handler is required */
10197 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
10198 if (!ec_handle) {
10199 if (is_thinkpad)
10200 pr_err("Not yet supported ThinkPad detected!\n");
10201 return -ENODEV;
10202 }
10203
10204 if (!is_thinkpad && !force_load)
10205 return -ENODEV;
10206
10207 return 0;
10208}
10209
10210static void __init thinkpad_acpi_init_banner(void)
10211{
10212 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10213 pr_info("%s\n", TPACPI_URL);
10214
10215 pr_info("ThinkPad BIOS %s, EC %s\n",
10216 (thinkpad_id.bios_version_str) ?
10217 thinkpad_id.bios_version_str : "unknown",
10218 (thinkpad_id.ec_version_str) ?
10219 thinkpad_id.ec_version_str : "unknown");
10220
10221 BUG_ON(!thinkpad_id.vendor);
10222
10223 if (thinkpad_id.model_str)
10224 pr_info("%s %s, model %s\n",
10225 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10226 "IBM" : ((thinkpad_id.vendor ==
10227 PCI_VENDOR_ID_LENOVO) ?
10228 "Lenovo" : "Unknown vendor"),
10229 thinkpad_id.model_str,
10230 (thinkpad_id.nummodel_str) ?
10231 thinkpad_id.nummodel_str : "unknown");
10232}
10233
10234/* Module init, exit, parameters */
10235
10236static struct ibm_init_struct ibms_init[] __initdata = {
10237 {
10238 .data = &thinkpad_acpi_driver_data,
10239 },
10240 {
10241 .init = hotkey_init,
10242 .data = &hotkey_driver_data,
10243 },
10244 {
10245 .init = bluetooth_init,
10246 .data = &bluetooth_driver_data,
10247 },
10248 {
10249 .init = wan_init,
10250 .data = &wan_driver_data,
10251 },
10252 {
10253 .init = uwb_init,
10254 .data = &uwb_driver_data,
10255 },
10256#ifdef CONFIG_THINKPAD_ACPI_VIDEO
10257 {
10258 .init = video_init,
10259 .base_procfs_mode = S_IRUSR,
10260 .data = &video_driver_data,
10261 },
10262#endif
10263 {
10264 .init = kbdlight_init,
10265 .data = &kbdlight_driver_data,
10266 },
10267 {
10268 .init = light_init,
10269 .data = &light_driver_data,
10270 },
10271 {
10272 .init = cmos_init,
10273 .data = &cmos_driver_data,
10274 },
10275 {
10276 .init = led_init,
10277 .data = &led_driver_data,
10278 },
10279 {
10280 .init = beep_init,
10281 .data = &beep_driver_data,
10282 },
10283 {
10284 .init = thermal_init,
10285 .data = &thermal_driver_data,
10286 },
10287 {
10288 .init = brightness_init,
10289 .data = &brightness_driver_data,
10290 },
10291 {
10292 .init = volume_init,
10293 .data = &volume_driver_data,
10294 },
10295 {
10296 .init = fan_init,
10297 .data = &fan_driver_data,
10298 },
10299 {
10300 .init = mute_led_init,
10301 .data = &mute_led_driver_data,
10302 },
Googler9398cc32022-12-02 17:21:52 +080010303 {
10304 .init = tpacpi_battery_init,
10305 .data = &battery_driver_data,
10306 },
10307 {
10308 .init = tpacpi_lcdshadow_init,
10309 .data = &lcdshadow_driver_data,
10310 },
Googleraf606d22022-10-26 21:40:12 -070010311};
10312
10313static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
10314{
10315 unsigned int i;
10316 struct ibm_struct *ibm;
10317
10318 if (!kp || !kp->name || !val)
10319 return -EINVAL;
10320
10321 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10322 ibm = ibms_init[i].data;
10323 WARN_ON(ibm == NULL);
10324
10325 if (!ibm || !ibm->name)
10326 continue;
10327
10328 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10329 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
10330 return -ENOSPC;
10331 strcpy(ibms_init[i].param, val);
10332 strcat(ibms_init[i].param, ",");
10333 return 0;
10334 }
10335 }
10336
10337 return -EINVAL;
10338}
10339
10340module_param(experimental, int, 0444);
10341MODULE_PARM_DESC(experimental,
10342 "Enables experimental features when non-zero");
10343
10344module_param_named(debug, dbg_level, uint, 0);
10345MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
10346
10347module_param(force_load, bool, 0444);
10348MODULE_PARM_DESC(force_load,
Googler9398cc32022-12-02 17:21:52 +080010349 "Attempts to load the driver even on a mis-identified ThinkPad when true");
Googleraf606d22022-10-26 21:40:12 -070010350
10351module_param_named(fan_control, fan_control_allowed, bool, 0444);
10352MODULE_PARM_DESC(fan_control,
10353 "Enables setting fan parameters features when true");
10354
10355module_param_named(brightness_mode, brightness_mode, uint, 0444);
10356MODULE_PARM_DESC(brightness_mode,
Googler9398cc32022-12-02 17:21:52 +080010357 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
Googleraf606d22022-10-26 21:40:12 -070010358
10359module_param(brightness_enable, uint, 0444);
10360MODULE_PARM_DESC(brightness_enable,
10361 "Enables backlight control when 1, disables when 0");
10362
10363#ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
10364module_param_named(volume_mode, volume_mode, uint, 0444);
10365MODULE_PARM_DESC(volume_mode,
Googler9398cc32022-12-02 17:21:52 +080010366 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
Googleraf606d22022-10-26 21:40:12 -070010367
10368module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10369MODULE_PARM_DESC(volume_capabilities,
Googler9398cc32022-12-02 17:21:52 +080010370 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
Googleraf606d22022-10-26 21:40:12 -070010371
10372module_param_named(volume_control, volume_control_allowed, bool, 0444);
10373MODULE_PARM_DESC(volume_control,
Googler9398cc32022-12-02 17:21:52 +080010374 "Enables software override for the console audio control when true");
Googleraf606d22022-10-26 21:40:12 -070010375
10376module_param_named(software_mute, software_mute_requested, bool, 0444);
10377MODULE_PARM_DESC(software_mute,
10378 "Request full software mute control");
10379
10380/* ALSA module API parameters */
10381module_param_named(index, alsa_index, int, 0444);
10382MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10383module_param_named(id, alsa_id, charp, 0444);
10384MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10385module_param_named(enable, alsa_enable, bool, 0444);
10386MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
10387#endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
10388
Googler9398cc32022-12-02 17:21:52 +080010389/* The module parameter can't be read back, that's why 0 is used here */
Googleraf606d22022-10-26 21:40:12 -070010390#define TPACPI_PARAM(feature) \
10391 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Googler9398cc32022-12-02 17:21:52 +080010392 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
Googleraf606d22022-10-26 21:40:12 -070010393
10394TPACPI_PARAM(hotkey);
10395TPACPI_PARAM(bluetooth);
10396TPACPI_PARAM(video);
10397TPACPI_PARAM(light);
10398TPACPI_PARAM(cmos);
10399TPACPI_PARAM(led);
10400TPACPI_PARAM(beep);
10401TPACPI_PARAM(brightness);
10402TPACPI_PARAM(volume);
10403TPACPI_PARAM(fan);
10404
10405#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10406module_param(dbg_wlswemul, uint, 0444);
10407MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10408module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10409MODULE_PARM_DESC(wlsw_state,
10410 "Initial state of the emulated WLSW switch");
10411
10412module_param(dbg_bluetoothemul, uint, 0444);
10413MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10414module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10415MODULE_PARM_DESC(bluetooth_state,
10416 "Initial state of the emulated bluetooth switch");
10417
10418module_param(dbg_wwanemul, uint, 0444);
10419MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10420module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10421MODULE_PARM_DESC(wwan_state,
10422 "Initial state of the emulated WWAN switch");
10423
10424module_param(dbg_uwbemul, uint, 0444);
10425MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10426module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10427MODULE_PARM_DESC(uwb_state,
10428 "Initial state of the emulated UWB switch");
10429#endif
10430
10431static void thinkpad_acpi_module_exit(void)
10432{
10433 struct ibm_struct *ibm, *itmp;
10434
10435 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10436
10437 list_for_each_entry_safe_reverse(ibm, itmp,
10438 &tpacpi_all_drivers,
10439 all_drivers) {
10440 ibm_exit(ibm);
10441 }
10442
10443 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10444
10445 if (tpacpi_inputdev) {
10446 if (tp_features.input_device_registered)
10447 input_unregister_device(tpacpi_inputdev);
10448 else
10449 input_free_device(tpacpi_inputdev);
10450 kfree(hotkey_keycode_map);
10451 }
10452
10453 if (tpacpi_hwmon)
10454 hwmon_device_unregister(tpacpi_hwmon);
10455
10456 if (tpacpi_sensors_pdev)
10457 platform_device_unregister(tpacpi_sensors_pdev);
10458 if (tpacpi_pdev)
10459 platform_device_unregister(tpacpi_pdev);
10460
10461 if (tp_features.sensors_pdrv_attrs_registered)
10462 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10463 if (tp_features.platform_drv_attrs_registered)
10464 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10465
10466 if (tp_features.sensors_pdrv_registered)
10467 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10468
10469 if (tp_features.platform_drv_registered)
10470 platform_driver_unregister(&tpacpi_pdriver);
10471
10472 if (proc_dir)
10473 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
10474
10475 if (tpacpi_wq)
10476 destroy_workqueue(tpacpi_wq);
10477
10478 kfree(thinkpad_id.bios_version_str);
10479 kfree(thinkpad_id.ec_version_str);
10480 kfree(thinkpad_id.model_str);
10481 kfree(thinkpad_id.nummodel_str);
10482}
10483
10484
10485static int __init thinkpad_acpi_module_init(void)
10486{
10487 int ret, i;
10488
10489 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10490
10491 /* Driver-level probe */
10492
10493 ret = get_thinkpad_model_data(&thinkpad_id);
10494 if (ret) {
10495 pr_err("unable to get DMI data: %d\n", ret);
10496 thinkpad_acpi_module_exit();
10497 return ret;
10498 }
10499 ret = probe_for_thinkpad();
10500 if (ret) {
10501 thinkpad_acpi_module_exit();
10502 return ret;
10503 }
10504
10505 /* Driver initialization */
10506
10507 thinkpad_acpi_init_banner();
10508 tpacpi_check_outdated_fw();
10509
10510 TPACPI_ACPIHANDLE_INIT(ecrd);
10511 TPACPI_ACPIHANDLE_INIT(ecwr);
10512
10513 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10514 if (!tpacpi_wq) {
10515 thinkpad_acpi_module_exit();
10516 return -ENOMEM;
10517 }
10518
10519 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
10520 if (!proc_dir) {
10521 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
10522 thinkpad_acpi_module_exit();
10523 return -ENODEV;
10524 }
10525
10526 ret = platform_driver_register(&tpacpi_pdriver);
10527 if (ret) {
10528 pr_err("unable to register main platform driver\n");
10529 thinkpad_acpi_module_exit();
10530 return ret;
10531 }
10532 tp_features.platform_drv_registered = 1;
10533
10534 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10535 if (ret) {
10536 pr_err("unable to register hwmon platform driver\n");
10537 thinkpad_acpi_module_exit();
10538 return ret;
10539 }
10540 tp_features.sensors_pdrv_registered = 1;
10541
10542 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
10543 if (!ret) {
10544 tp_features.platform_drv_attrs_registered = 1;
10545 ret = tpacpi_create_driver_attributes(
10546 &tpacpi_hwmon_pdriver.driver);
10547 }
10548 if (ret) {
10549 pr_err("unable to create sysfs driver attributes\n");
10550 thinkpad_acpi_module_exit();
10551 return ret;
10552 }
10553 tp_features.sensors_pdrv_attrs_registered = 1;
10554
10555
10556 /* Device initialization */
10557 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
10558 NULL, 0);
10559 if (IS_ERR(tpacpi_pdev)) {
10560 ret = PTR_ERR(tpacpi_pdev);
10561 tpacpi_pdev = NULL;
10562 pr_err("unable to register platform device\n");
10563 thinkpad_acpi_module_exit();
10564 return ret;
10565 }
10566 tpacpi_sensors_pdev = platform_device_register_simple(
10567 TPACPI_HWMON_DRVR_NAME,
10568 -1, NULL, 0);
10569 if (IS_ERR(tpacpi_sensors_pdev)) {
10570 ret = PTR_ERR(tpacpi_sensors_pdev);
10571 tpacpi_sensors_pdev = NULL;
10572 pr_err("unable to register hwmon platform device\n");
10573 thinkpad_acpi_module_exit();
10574 return ret;
10575 }
10576 tp_features.sensors_pdev_attrs_registered = 1;
Googler9398cc32022-12-02 17:21:52 +080010577 tpacpi_hwmon = hwmon_device_register_with_groups(
10578 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10579
Googleraf606d22022-10-26 21:40:12 -070010580 if (IS_ERR(tpacpi_hwmon)) {
10581 ret = PTR_ERR(tpacpi_hwmon);
10582 tpacpi_hwmon = NULL;
10583 pr_err("unable to register hwmon device\n");
10584 thinkpad_acpi_module_exit();
10585 return ret;
10586 }
10587 mutex_init(&tpacpi_inputdev_send_mutex);
10588 tpacpi_inputdev = input_allocate_device();
10589 if (!tpacpi_inputdev) {
10590 thinkpad_acpi_module_exit();
10591 return -ENOMEM;
10592 } else {
10593 /* Prepare input device, but don't register */
10594 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
10595 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
10596 tpacpi_inputdev->id.bustype = BUS_HOST;
10597 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
10598 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10599 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
10600 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
10601 }
10602
10603 /* Init subdriver dependencies */
10604 tpacpi_detect_brightness_capabilities();
10605
10606 /* Init subdrivers */
10607 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10608 ret = ibm_init(&ibms_init[i]);
10609 if (ret >= 0 && *ibms_init[i].param)
10610 ret = ibms_init[i].data->write(ibms_init[i].param);
10611 if (ret < 0) {
10612 thinkpad_acpi_module_exit();
10613 return ret;
10614 }
10615 }
10616
10617 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10618
10619 ret = input_register_device(tpacpi_inputdev);
10620 if (ret < 0) {
10621 pr_err("unable to register input device\n");
10622 thinkpad_acpi_module_exit();
10623 return ret;
10624 } else {
10625 tp_features.input_device_registered = 1;
10626 }
10627
10628 return 0;
10629}
10630
10631MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10632
10633/*
10634 * This will autoload the driver in almost every ThinkPad
10635 * in widespread use.
10636 *
10637 * Only _VERY_ old models, like the 240, 240x and 570 lack
10638 * the HKEY event interface.
10639 */
10640MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10641
10642/*
10643 * DMI matching for module autoloading
10644 *
10645 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10646 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10647 *
10648 * Only models listed in thinkwiki will be supported, so add yours
10649 * if it is not there yet.
10650 */
10651#define IBM_BIOS_MODULE_ALIAS(__type) \
10652 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10653
10654/* Ancient thinkpad BIOSes have to be identified by
10655 * BIOS type or model number, and there are far less
10656 * BIOS types than model numbers... */
10657IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10658
10659MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10660MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10661MODULE_DESCRIPTION(TPACPI_DESC);
10662MODULE_VERSION(TPACPI_VERSION);
10663MODULE_LICENSE("GPL");
10664
10665module_init(thinkpad_acpi_module_init);
10666module_exit(thinkpad_acpi_module_exit);