1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
11 #define TPACPI_VERSION "0.26"
12 #define TPACPI_SYSFS_VERSION 0x030000
13
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/acpi.h>
38 #include <linux/backlight.h>
39 #include <linux/bitfield.h>
40 #include <linux/bitops.h>
41 #include <linux/cleanup.h>
42 #include <linux/debugfs.h>
43 #include <linux/delay.h>
44 #include <linux/dmi.h>
45 #include <linux/freezer.h>
46 #include <linux/hwmon.h>
47 #include <linux/hwmon-sysfs.h>
48 #include <linux/init.h>
49 #include <linux/input.h>
50 #include <linux/input/sparse-keymap.h>
51 #include <linux/jiffies.h>
52 #include <linux/kernel.h>
53 #include <linux/kthread.h>
54 #include <linux/leds.h>
55 #include <linux/list.h>
56 #include <linux/lockdep.h>
57 #include <linux/module.h>
58 #include <linux/mutex.h>
59 #include <linux/nvram.h>
60 #include <linux/pci.h>
61 #include <linux/platform_device.h>
62 #include <linux/platform_profile.h>
63 #include <linux/power_supply.h>
64 #include <linux/proc_fs.h>
65 #include <linux/rfkill.h>
66 #include <linux/sched.h>
67 #include <linux/sched/signal.h>
68 #include <linux/seq_file.h>
69 #include <linux/slab.h>
70 #include <linux/string.h>
71 #include <linux/string_choices.h>
72 #include <linux/string_helpers.h>
73 #include <linux/sysfs.h>
74 #include <linux/types.h>
75 #include <linux/uaccess.h>
76 #include <linux/units.h>
77 #include <linux/workqueue.h>
78
79 #include <acpi/battery.h>
80 #include <acpi/video.h>
81
82 #include <drm/drm_privacy_screen_driver.h>
83
84 #include <sound/control.h>
85 #include <sound/core.h>
86 #include <sound/initval.h>
87
88 #include "../dual_accel_detect.h"
89
90 /* ThinkPad CMOS commands */
91 #define TP_CMOS_VOLUME_DOWN 0
92 #define TP_CMOS_VOLUME_UP 1
93 #define TP_CMOS_VOLUME_MUTE 2
94 #define TP_CMOS_BRIGHTNESS_UP 4
95 #define TP_CMOS_BRIGHTNESS_DOWN 5
96 #define TP_CMOS_THINKLIGHT_ON 12
97 #define TP_CMOS_THINKLIGHT_OFF 13
98
99 /* NVRAM Addresses */
100 enum tp_nvram_addr {
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
106 };
107
108 /* NVRAM bit masks */
109 enum {
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123 };
124
125 /* Misc NVRAM-related */
126 enum {
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
128 };
129
130 /* ACPI HIDs */
131 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
132 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
133 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
134 #define TPACPI_ACPI_EC_HID "PNP0C09"
135
136 /* Input IDs */
137 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
138 #define TPACPI_HKEY_INPUT_VERSION 0x4101
139
140 /* ACPI \WGSV commands */
141 enum {
142 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
143 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
144 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
145 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 };
147
148 /* TP_ACPI_WGSV_GET_STATE bits */
149 enum {
150 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
151 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
152 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
153 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
154 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
155 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
156 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
157 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
158 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
159 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 };
161
162 /* HKEY events */
163 enum tpacpi_hkey_event_t {
164 /* Original hotkeys */
165 TP_HKEY_EV_ORIG_KEY_START = 0x1001, /* First hotkey (FN+F1) */
166 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
167 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
168 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
172 TP_HKEY_EV_ORIG_KEY_END = 0x1020, /* Last original hotkey code */
173
174 /* Adaptive keyboard (2014 X1 Carbon) */
175 TP_HKEY_EV_DFR_CHANGE_ROW = 0x1101, /* Change adaptive kbd Fn row mode */
176 TP_HKEY_EV_DFR_S_QUICKVIEW_ROW = 0x1102, /* Set adap. kbd Fn row to function mode */
177 TP_HKEY_EV_ADAPTIVE_KEY_START = 0x1103, /* First hotkey code on adaptive kbd */
178 TP_HKEY_EV_ADAPTIVE_KEY_END = 0x1116, /* Last hotkey code on adaptive kbd */
179
180 /* Extended hotkey events in 2017+ models */
181 TP_HKEY_EV_EXTENDED_KEY_START = 0x1300, /* First extended hotkey code */
182 TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
183 TP_HKEY_EV_EXTENDED_KEY_END = 0x1319, /* Last extended hotkey code using
184 * hkey -> scancode translation for
185 * compat. Later codes are entered
186 * directly in the sparse-keymap.
187 */
188 TP_HKEY_EV_AMT_TOGGLE = 0x131a, /* Toggle AMT on/off */
189 TP_HKEY_EV_CAMERASHUTTER_TOGGLE = 0x131b, /* Toggle Camera Shutter */
190 TP_HKEY_EV_DOUBLETAP_TOGGLE = 0x131c, /* Toggle trackpoint doubletap on/off */
191 TP_HKEY_EV_USB_C_SECURITY = 0x131e, /* USB C Security (Fn+U, Fn+S) */
192 TP_HKEY_EV_PROFILE_TOGGLE = 0x131f, /* Toggle platform profile in 2024 systems */
193 TP_HKEY_EV_PROFILE_TOGGLE2 = 0x1401, /* Toggle platform profile in 2025 + systems */
194
195 /* Reasons for waking up from S3/S4 */
196 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
197 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
198 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
199 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
200 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
201 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
202
203 /* Auto-sleep after eject request */
204 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
205 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
206
207 /* Misc bay events */
208 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
209 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
210 or port replicator */
211 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
212 dock or port replicator */
213 /*
214 * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
215 * when keyboard cover is attached, detached or folded onto the back
216 */
217 TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
218 TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
219
220 /* User-interface events */
221 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
222 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
223 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
224 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
225 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
226 * enter/leave tablet mode
227 */
228 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
229 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
230 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
231
232 /* Key-related user-interface events */
233 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
234 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
235 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
236
237 /* Thermal events */
238 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
239 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
240 TP_HKEY_EV_ALARM_BAT_LIM_CHANGE = 0x6013, /* battery charge limit changed*/
241 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
242 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
243 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
244 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
245 * command completed. Related to
246 * AML DYTC */
247 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
248 * changed. Related to AML GMTS */
249
250 /* AC-related events */
251 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
252
253 /* Further user-interface events */
254 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
255 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
256
257 /* Misc */
258 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
259
260 /* Misc2 */
261 TP_HKEY_EV_TRACK_DOUBLETAP = 0x8036, /* trackpoint doubletap */
262 };
263
264 /****************************************************************************
265 * Main driver
266 */
267
268 #define TPACPI_NAME "thinkpad"
269 #define TPACPI_DESC "ThinkPad ACPI Extras"
270 #define TPACPI_FILE TPACPI_NAME "_acpi"
271 #define TPACPI_URL "http://ibm-acpi.sf.net/"
272 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
273
274 #define TPACPI_PROC_DIR "ibm"
275 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
276 #define TPACPI_DRVR_NAME TPACPI_FILE
277 #define TPACPI_DRVR_SHORTNAME "tpacpi"
278 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
279
280 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
281 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
282
283 #define TPACPI_MAX_ACPI_ARGS 3
284
285 /* Debugging printk groups */
286 #define TPACPI_DBG_ALL 0xffff
287 #define TPACPI_DBG_DISCLOSETASK 0x8000
288 #define TPACPI_DBG_INIT 0x0001
289 #define TPACPI_DBG_EXIT 0x0002
290 #define TPACPI_DBG_RFKILL 0x0004
291 #define TPACPI_DBG_HKEY 0x0008
292 #define TPACPI_DBG_FAN 0x0010
293 #define TPACPI_DBG_BRGHT 0x0020
294 #define TPACPI_DBG_MIXER 0x0040
295
296 #define FAN_NOT_PRESENT 65535
297
298 /****************************************************************************
299 * Driver-wide structs and misc. variables
300 */
301
302 struct ibm_struct;
303
304 struct tp_acpi_drv_struct {
305 const struct acpi_device_id *hid;
306
307 void (*notify) (struct ibm_struct *, u32);
308 acpi_handle *handle;
309 u32 type;
310 struct acpi_device *device;
311 };
312
313 struct ibm_struct {
314 char *name;
315 acpi_device_class device_class;
316
317 int (*read) (struct seq_file *);
318 int (*write) (char *);
319 void (*exit) (void);
320 void (*resume) (void);
321 void (*suspend) (void);
322 void (*shutdown) (void);
323
324 struct list_head all_drivers;
325
326 struct tp_acpi_drv_struct *acpi;
327
328 struct {
329 u8 acpi_notify_installed:1;
330 u8 proc_created:1;
331 u8 init_called:1;
332 u8 experimental:1;
333 } flags;
334 };
335
336 struct ibm_init_struct {
337 char param[32];
338
339 int (*init) (struct ibm_init_struct *);
340 umode_t base_procfs_mode;
341 struct ibm_struct *data;
342 };
343
344 /* DMI Quirks */
345 struct quirk_entry {
346 bool btusb_bug;
347 };
348
349 static struct quirk_entry quirk_btusb_bug = {
350 .btusb_bug = true,
351 };
352
353 static struct {
354 u32 bluetooth:1;
355 u32 hotkey:1;
356 u32 hotkey_mask:1;
357 u32 hotkey_wlsw:1;
358 enum {
359 TP_HOTKEY_TABLET_NONE = 0,
360 TP_HOTKEY_TABLET_USES_MHKG,
361 TP_HOTKEY_TABLET_USES_GMMS,
362 } hotkey_tablet;
363 u32 kbdlight:1;
364 u32 light:1;
365 u32 light_status:1;
366 u32 bright_acpimode:1;
367 u32 bright_unkfw:1;
368 u32 wan:1;
369 u32 uwb:1;
370 u32 fan_ctrl_status_undef:1;
371 u32 second_fan:1;
372 u32 second_fan_ctl:1;
373 u32 beep_needs_two_args:1;
374 u32 mixer_no_level_control:1;
375 u32 battery_force_primary:1;
376 u32 platform_drv_registered:1;
377 u32 hotkey_poll_active:1;
378 u32 has_adaptive_kbd:1;
379 u32 kbd_lang:1;
380 u32 trackpoint_doubletap_enable:1;
381 u32 usbc_security_supported:1;
382 bool usbc_security_enabled;
383 struct quirk_entry *quirks;
384 } tp_features;
385
386 static struct {
387 u16 hotkey_mask_ff:1;
388 u16 volume_ctrl_forbidden:1;
389 } tp_warned;
390
391 struct thinkpad_id_data {
392 unsigned int vendor; /* ThinkPad vendor:
393 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
394
395 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
396 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
397
398 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
399 u32 ec_model;
400 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
401 u16 ec_release;
402
403 char *model_str; /* ThinkPad T43 */
404 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
405 };
406 static struct thinkpad_id_data thinkpad_id;
407
408 static enum {
409 TPACPI_LIFE_INIT = 0,
410 TPACPI_LIFE_RUNNING,
411 TPACPI_LIFE_EXITING,
412 } tpacpi_lifecycle;
413
414 static int experimental;
415 static u32 dbg_level;
416
417 static struct workqueue_struct *tpacpi_wq;
418
419 enum led_status_t {
420 TPACPI_LED_OFF = 0,
421 TPACPI_LED_ON,
422 TPACPI_LED_BLINK,
423 };
424
425 /* tpacpi LED class */
426 struct tpacpi_led_classdev {
427 struct led_classdev led_classdev;
428 int led;
429 };
430
431 /* brightness level capabilities */
432 static unsigned int bright_maxlvl; /* 0 = unknown */
433
434 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
435 static int dbg_wlswemul;
436 static bool tpacpi_wlsw_emulstate;
437 static int dbg_bluetoothemul;
438 static bool tpacpi_bluetooth_emulstate;
439 static int dbg_wwanemul;
440 static bool tpacpi_wwan_emulstate;
441 static int dbg_uwbemul;
442 static bool tpacpi_uwb_emulstate;
443 #endif
444
445
446 /*************************************************************************
447 * Debugging helpers
448 */
449
450 #define dbg_printk(a_dbg_level, format, arg...) \
451 do { \
452 if (dbg_level & (a_dbg_level)) \
453 printk(KERN_DEBUG pr_fmt("%s: " format), \
454 __func__, ##arg); \
455 } while (0)
456
457 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
458 #define vdbg_printk dbg_printk
459 static const char *str_supported(int is_supported);
460 #else
str_supported(int is_supported)461 static inline const char *str_supported(int is_supported) { return ""; }
462 #define vdbg_printk(a_dbg_level, format, arg...) \
463 do { if (0) no_printk(format, ##arg); } while (0)
464 #endif
465
tpacpi_log_usertask(const char * const what)466 static void tpacpi_log_usertask(const char * const what)
467 {
468 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
469 what, task_tgid_vnr(current));
470 }
471
472 #define tpacpi_disclose_usertask(what, format, arg...) \
473 do { \
474 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
475 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
476 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
477 what, task_tgid_vnr(current), ## arg); \
478 } \
479 } while (0)
480
481 /*
482 * Quirk handling helpers
483 *
484 * ThinkPad IDs and versions seen in the field so far are
485 * two or three characters from the set [0-9A-Z], i.e. base 36.
486 *
487 * We use values well outside that range as specials.
488 */
489
490 #define TPACPI_MATCH_ANY 0xffffffffU
491 #define TPACPI_MATCH_ANY_VERSION 0xffffU
492 #define TPACPI_MATCH_UNKNOWN 0U
493
494 /* TPID('1', 'Y') == 0x3159 */
495 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
496 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
497 #define TPVER TPID
498
499 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
500 { .vendor = PCI_VENDOR_ID_IBM, \
501 .bios = TPID(__id1, __id2), \
502 .ec = TPACPI_MATCH_ANY, \
503 .quirks = (__quirk) }
504
505 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
506 { .vendor = PCI_VENDOR_ID_LENOVO, \
507 .bios = TPID(__id1, __id2), \
508 .ec = TPACPI_MATCH_ANY, \
509 .quirks = (__quirk) }
510
511 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
512 { .vendor = PCI_VENDOR_ID_LENOVO, \
513 .bios = TPID3(__id1, __id2, __id3), \
514 .ec = TPACPI_MATCH_ANY, \
515 .quirks = (__quirk) }
516
517 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
518 { .vendor = PCI_VENDOR_ID_IBM, \
519 .bios = TPACPI_MATCH_ANY, \
520 .ec = TPID(__id1, __id2), \
521 .quirks = (__quirk) }
522
523 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
524 { .vendor = PCI_VENDOR_ID_LENOVO, \
525 .bios = TPACPI_MATCH_ANY, \
526 .ec = TPID(__id1, __id2), \
527 .quirks = (__quirk) }
528
529 struct tpacpi_quirk {
530 unsigned int vendor;
531 u32 bios;
532 u32 ec;
533 unsigned long quirks;
534 };
535
536 /**
537 * tpacpi_check_quirks() - search BIOS/EC version on a list
538 * @qlist: array of &struct tpacpi_quirk
539 * @qlist_size: number of elements in @qlist
540 *
541 * Iterates over a quirks list until one is found that matches the
542 * ThinkPad's vendor, BIOS and EC model.
543 *
544 * Returns: %0 if nothing matches, otherwise returns the quirks field of
545 * the matching &struct tpacpi_quirk entry.
546 *
547 * The match criteria is: vendor, ec and bios must match.
548 */
tpacpi_check_quirks(const struct tpacpi_quirk * qlist,unsigned int qlist_size)549 static unsigned long __init tpacpi_check_quirks(
550 const struct tpacpi_quirk *qlist,
551 unsigned int qlist_size)
552 {
553 while (qlist_size) {
554 if ((qlist->vendor == thinkpad_id.vendor ||
555 qlist->vendor == TPACPI_MATCH_ANY) &&
556 (qlist->bios == thinkpad_id.bios_model ||
557 qlist->bios == TPACPI_MATCH_ANY) &&
558 (qlist->ec == thinkpad_id.ec_model ||
559 qlist->ec == TPACPI_MATCH_ANY))
560 return qlist->quirks;
561
562 qlist_size--;
563 qlist++;
564 }
565 return 0;
566 }
567
tpacpi_is_lenovo(void)568 static __always_inline bool __pure __init tpacpi_is_lenovo(void)
569 {
570 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
571 }
572
tpacpi_is_ibm(void)573 static __always_inline bool __pure __init tpacpi_is_ibm(void)
574 {
575 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
576 }
577
578 /****************************************************************************
579 ****************************************************************************
580 *
581 * ACPI Helpers and device model
582 *
583 ****************************************************************************
584 ****************************************************************************/
585
586 /*************************************************************************
587 * ACPI basic handles
588 */
589
590 static acpi_handle root_handle;
591 static acpi_handle ec_handle;
592
593 #define TPACPI_HANDLE(object, parent, paths...) \
594 static acpi_handle object##_handle; \
595 static const acpi_handle * const object##_parent __initconst = \
596 &parent##_handle; \
597 static char *object##_paths[] __initdata = { paths }
598
599 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
600 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
601
602 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
603 /* T4x, X31, X40 */
604 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
605 "\\CMS", /* R40, R40e */
606 ); /* all others */
607
608 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
609 "^HKEY", /* R30, R31 */
610 "HKEY", /* all others */
611 ); /* 570 */
612
613 /*************************************************************************
614 * ACPI helpers
615 */
616
acpi_evalf(acpi_handle handle,int * res,char * method,char * fmt,...)617 static int acpi_evalf(acpi_handle handle,
618 int *res, char *method, char *fmt, ...)
619 {
620 char *fmt0 = fmt;
621 struct acpi_object_list params;
622 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
623 struct acpi_buffer result, *resultp;
624 union acpi_object out_obj;
625 acpi_status status;
626 va_list ap;
627 char res_type;
628 int success;
629 int quiet;
630
631 if (!*fmt) {
632 pr_err("acpi_evalf() called with empty format\n");
633 return 0;
634 }
635
636 if (*fmt == 'q') {
637 quiet = 1;
638 fmt++;
639 } else
640 quiet = 0;
641
642 res_type = *(fmt++);
643
644 params.count = 0;
645 params.pointer = &in_objs[0];
646
647 va_start(ap, fmt);
648 while (*fmt) {
649 char c = *(fmt++);
650 switch (c) {
651 case 'd': /* int */
652 in_objs[params.count].integer.value = va_arg(ap, int);
653 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
654 break;
655 /* add more types as needed */
656 default:
657 pr_err("acpi_evalf() called with invalid format character '%c'\n",
658 c);
659 va_end(ap);
660 return 0;
661 }
662 }
663 va_end(ap);
664
665 if (res_type != 'v') {
666 result.length = sizeof(out_obj);
667 result.pointer = &out_obj;
668 resultp = &result;
669 } else
670 resultp = NULL;
671
672 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
673
674 switch (res_type) {
675 case 'd': /* int */
676 success = (status == AE_OK &&
677 out_obj.type == ACPI_TYPE_INTEGER);
678 if (success && res)
679 *res = out_obj.integer.value;
680 break;
681 case 'v': /* void */
682 success = status == AE_OK;
683 break;
684 /* add more types as needed */
685 default:
686 pr_err("acpi_evalf() called with invalid format character '%c'\n",
687 res_type);
688 return 0;
689 }
690
691 if (!success && !quiet)
692 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
693 method, fmt0, acpi_format_exception(status));
694
695 return success;
696 }
697
acpi_ec_read(int i,u8 * p)698 static int acpi_ec_read(int i, u8 *p)
699 {
700 int v;
701
702 if (ecrd_handle) {
703 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
704 return 0;
705 *p = v;
706 } else {
707 if (ec_read(i, p) < 0)
708 return 0;
709 }
710
711 return 1;
712 }
713
acpi_ec_write(int i,u8 v)714 static int acpi_ec_write(int i, u8 v)
715 {
716 if (ecwr_handle) {
717 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
718 return 0;
719 } else {
720 if (ec_write(i, v) < 0)
721 return 0;
722 }
723
724 return 1;
725 }
726
issue_thinkpad_cmos_command(int cmos_cmd)727 static int issue_thinkpad_cmos_command(int cmos_cmd)
728 {
729 if (!cmos_handle)
730 return -ENXIO;
731
732 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
733 return -EIO;
734
735 return 0;
736 }
737
738 /*************************************************************************
739 * ACPI device model
740 */
741
742 #define TPACPI_ACPIHANDLE_INIT(object) \
743 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
744 object##_paths, ARRAY_SIZE(object##_paths))
745
drv_acpi_handle_init(const char * name,acpi_handle * handle,const acpi_handle parent,char ** paths,const int num_paths)746 static void __init drv_acpi_handle_init(const char *name,
747 acpi_handle *handle, const acpi_handle parent,
748 char **paths, const int num_paths)
749 {
750 int i;
751 acpi_status status;
752
753 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
754 name);
755
756 for (i = 0; i < num_paths; i++) {
757 status = acpi_get_handle(parent, paths[i], handle);
758 if (ACPI_SUCCESS(status)) {
759 dbg_printk(TPACPI_DBG_INIT,
760 "Found ACPI handle %s for %s\n",
761 paths[i], name);
762 return;
763 }
764 }
765
766 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
767 name);
768 *handle = NULL;
769 }
770
tpacpi_acpi_handle_locate_callback(acpi_handle handle,u32 level,void * context,void ** return_value)771 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
772 u32 level, void *context, void **return_value)
773 {
774 if (!strcmp(context, "video")) {
775 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
776
777 if (!acpi_dev_is_video_device(dev))
778 return AE_OK;
779 }
780
781 *(acpi_handle *)return_value = handle;
782
783 return AE_CTRL_TERMINATE;
784 }
785
tpacpi_acpi_handle_locate(const char * name,const char * hid,acpi_handle * handle)786 static void __init tpacpi_acpi_handle_locate(const char *name,
787 const char *hid,
788 acpi_handle *handle)
789 {
790 acpi_status status;
791 acpi_handle device_found;
792
793 BUG_ON(!name || !handle);
794 vdbg_printk(TPACPI_DBG_INIT,
795 "trying to locate ACPI handle for %s, using HID %s\n",
796 name, hid ? hid : "NULL");
797
798 memset(&device_found, 0, sizeof(device_found));
799 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
800 (void *)name, &device_found);
801
802 *handle = NULL;
803
804 if (ACPI_SUCCESS(status)) {
805 *handle = device_found;
806 dbg_printk(TPACPI_DBG_INIT,
807 "Found ACPI handle for %s\n", name);
808 } else {
809 vdbg_printk(TPACPI_DBG_INIT,
810 "Could not locate an ACPI handle for %s: %s\n",
811 name, acpi_format_exception(status));
812 }
813 }
814
dispatch_acpi_notify(acpi_handle handle,u32 event,void * data)815 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
816 {
817 struct ibm_struct *ibm = data;
818
819 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
820 return;
821
822 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
823 return;
824
825 ibm->acpi->notify(ibm, event);
826 }
827
setup_acpi_notify(struct ibm_struct * ibm)828 static int __init setup_acpi_notify(struct ibm_struct *ibm)
829 {
830 acpi_status status;
831
832 BUG_ON(!ibm->acpi);
833
834 if (!*ibm->acpi->handle)
835 return 0;
836
837 vdbg_printk(TPACPI_DBG_INIT,
838 "setting up ACPI notify for %s\n", ibm->name);
839
840 ibm->acpi->device = acpi_get_acpi_dev(*ibm->acpi->handle);
841 if (!ibm->acpi->device) {
842 pr_err("acpi_get_acpi_dev(%s) failed\n", ibm->name);
843 return -ENODEV;
844 }
845
846 ibm->acpi->device->driver_data = ibm;
847 scnprintf(ibm->device_class, sizeof(ibm->device_class), "%s/%s",
848 TPACPI_ACPI_EVENT_PREFIX, ibm->name);
849
850 status = acpi_install_notify_handler(*ibm->acpi->handle,
851 ibm->acpi->type, dispatch_acpi_notify, ibm);
852 if (ACPI_FAILURE(status)) {
853 if (status == AE_ALREADY_EXISTS) {
854 pr_notice("another device driver is already handling %s events\n",
855 ibm->name);
856 } else {
857 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
858 ibm->name, acpi_format_exception(status));
859 }
860 return -ENODEV;
861 }
862 ibm->flags.acpi_notify_installed = 1;
863 return 0;
864 }
865
866 /****************************************************************************
867 ****************************************************************************
868 *
869 * Procfs Helpers
870 *
871 ****************************************************************************
872 ****************************************************************************/
873
dispatch_proc_show(struct seq_file * m,void * v)874 static int dispatch_proc_show(struct seq_file *m, void *v)
875 {
876 struct ibm_struct *ibm = m->private;
877
878 if (!ibm || !ibm->read)
879 return -EINVAL;
880 return ibm->read(m);
881 }
882
dispatch_proc_open(struct inode * inode,struct file * file)883 static int dispatch_proc_open(struct inode *inode, struct file *file)
884 {
885 return single_open(file, dispatch_proc_show, pde_data(inode));
886 }
887
dispatch_proc_write(struct file * file,const char __user * userbuf,size_t count,loff_t * pos)888 static ssize_t dispatch_proc_write(struct file *file,
889 const char __user *userbuf,
890 size_t count, loff_t *pos)
891 {
892 struct ibm_struct *ibm = pde_data(file_inode(file));
893 char *kernbuf;
894 int ret;
895
896 if (!ibm || !ibm->write)
897 return -EINVAL;
898 if (count > PAGE_SIZE - 1)
899 return -EINVAL;
900
901 kernbuf = memdup_user_nul(userbuf, count);
902 if (IS_ERR(kernbuf))
903 return PTR_ERR(kernbuf);
904 ret = ibm->write(kernbuf);
905 if (ret == 0)
906 ret = count;
907
908 kfree(kernbuf);
909
910 return ret;
911 }
912
913 static const struct proc_ops dispatch_proc_ops = {
914 .proc_open = dispatch_proc_open,
915 .proc_read = seq_read,
916 .proc_lseek = seq_lseek,
917 .proc_release = single_release,
918 .proc_write = dispatch_proc_write,
919 };
920
921 /****************************************************************************
922 ****************************************************************************
923 *
924 * Device model: input, hwmon and platform
925 *
926 ****************************************************************************
927 ****************************************************************************/
928
929 static struct platform_device *tpacpi_pdev;
930 static struct platform_device *tpacpi_sensors_pdev;
931 static struct device *tpacpi_hwmon;
932 static struct device *tpacpi_pprof;
933 static struct input_dev *tpacpi_inputdev;
934 static struct mutex tpacpi_inputdev_send_mutex;
935 static LIST_HEAD(tpacpi_all_drivers);
936
937 #ifdef CONFIG_PM_SLEEP
tpacpi_suspend_handler(struct device * dev)938 static int tpacpi_suspend_handler(struct device *dev)
939 {
940 struct ibm_struct *ibm, *itmp;
941
942 list_for_each_entry_safe(ibm, itmp,
943 &tpacpi_all_drivers,
944 all_drivers) {
945 if (ibm->suspend)
946 (ibm->suspend)();
947 }
948
949 return 0;
950 }
951
tpacpi_resume_handler(struct device * dev)952 static int tpacpi_resume_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->resume)
960 (ibm->resume)();
961 }
962
963 return 0;
964 }
965 #endif
966
967 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
968 tpacpi_suspend_handler, tpacpi_resume_handler);
969
tpacpi_shutdown_handler(struct platform_device * pdev)970 static void tpacpi_shutdown_handler(struct platform_device *pdev)
971 {
972 struct ibm_struct *ibm, *itmp;
973
974 list_for_each_entry_safe(ibm, itmp,
975 &tpacpi_all_drivers,
976 all_drivers) {
977 if (ibm->shutdown)
978 (ibm->shutdown)();
979 }
980 }
981
982 /*************************************************************************
983 * sysfs support helpers
984 */
985
parse_strtoul(const char * buf,unsigned long max,unsigned long * value)986 static int parse_strtoul(const char *buf,
987 unsigned long max, unsigned long *value)
988 {
989 char *endp;
990
991 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
992 endp = skip_spaces(endp);
993 if (*endp || *value > max)
994 return -EINVAL;
995
996 return 0;
997 }
998
tpacpi_disable_brightness_delay(void)999 static void tpacpi_disable_brightness_delay(void)
1000 {
1001 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1002 pr_notice("ACPI backlight control delay disabled\n");
1003 }
1004
printk_deprecated_attribute(const char * const what,const char * const details)1005 static void printk_deprecated_attribute(const char * const what,
1006 const char * const details)
1007 {
1008 tpacpi_log_usertask("deprecated sysfs attribute");
1009 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1010 what, details);
1011 }
1012
1013 /*************************************************************************
1014 * rfkill and radio control support helpers
1015 */
1016
1017 /*
1018 * ThinkPad-ACPI firmware handling model:
1019 *
1020 * WLSW (master wireless switch) is event-driven, and is common to all
1021 * firmware-controlled radios. It cannot be controlled, just monitored,
1022 * as expected. It overrides all radio state in firmware
1023 *
1024 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1025 * (TODO: verify how WLSW interacts with the returned radio state).
1026 *
1027 * The only time there are shadow radio state changes, is when
1028 * masked-off hotkeys are used.
1029 */
1030
1031 /*
1032 * Internal driver API for radio state:
1033 *
1034 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1035 * bool: true means radio blocked (off)
1036 */
1037 enum tpacpi_rfkill_state {
1038 TPACPI_RFK_RADIO_OFF = 0,
1039 TPACPI_RFK_RADIO_ON
1040 };
1041
1042 /* rfkill switches */
1043 enum tpacpi_rfk_id {
1044 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1045 TPACPI_RFK_WWAN_SW_ID,
1046 TPACPI_RFK_UWB_SW_ID,
1047 TPACPI_RFK_SW_MAX
1048 };
1049
1050 static const char *tpacpi_rfkill_names[] = {
1051 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1052 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1053 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1054 [TPACPI_RFK_SW_MAX] = NULL
1055 };
1056
1057 /* ThinkPad-ACPI rfkill subdriver */
1058 struct tpacpi_rfk {
1059 struct rfkill *rfkill;
1060 enum tpacpi_rfk_id id;
1061 const struct tpacpi_rfk_ops *ops;
1062 };
1063
1064 struct tpacpi_rfk_ops {
1065 /* firmware interface */
1066 int (*get_status)(void);
1067 int (*set_status)(const enum tpacpi_rfkill_state);
1068 };
1069
1070 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1071
1072 /* Query FW and update rfkill sw state for a given rfkill switch */
tpacpi_rfk_update_swstate(const struct tpacpi_rfk * tp_rfk)1073 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1074 {
1075 int status;
1076
1077 if (!tp_rfk)
1078 return -ENODEV;
1079
1080 status = (tp_rfk->ops->get_status)();
1081 if (status < 0)
1082 return status;
1083
1084 rfkill_set_sw_state(tp_rfk->rfkill,
1085 (status == TPACPI_RFK_RADIO_OFF));
1086
1087 return status;
1088 }
1089
1090 /*
1091 * Sync the HW-blocking state of all rfkill switches,
1092 * do notice it causes the rfkill core to schedule uevents
1093 */
tpacpi_rfk_update_hwblock_state(bool blocked)1094 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1095 {
1096 unsigned int i;
1097 struct tpacpi_rfk *tp_rfk;
1098
1099 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1100 tp_rfk = tpacpi_rfkill_switches[i];
1101 if (tp_rfk) {
1102 if (rfkill_set_hw_state(tp_rfk->rfkill,
1103 blocked)) {
1104 /* ignore -- we track sw block */
1105 }
1106 }
1107 }
1108 }
1109
1110 /* Call to get the WLSW state from the firmware */
1111 static int hotkey_get_wlsw(void);
1112
1113 /* Call to query WLSW state and update all rfkill switches */
tpacpi_rfk_check_hwblock_state(void)1114 static bool tpacpi_rfk_check_hwblock_state(void)
1115 {
1116 int res = hotkey_get_wlsw();
1117 int hw_blocked;
1118
1119 /* When unknown or unsupported, we have to assume it is unblocked */
1120 if (res < 0)
1121 return false;
1122
1123 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1124 tpacpi_rfk_update_hwblock_state(hw_blocked);
1125
1126 return hw_blocked;
1127 }
1128
tpacpi_rfk_hook_set_block(void * data,bool blocked)1129 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1130 {
1131 struct tpacpi_rfk *tp_rfk = data;
1132 int res;
1133
1134 dbg_printk(TPACPI_DBG_RFKILL,
1135 "request to change radio state to %s\n",
1136 blocked ? "blocked" : "unblocked");
1137
1138 /* try to set radio state */
1139 res = (tp_rfk->ops->set_status)(blocked ?
1140 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1141
1142 /* and update the rfkill core with whatever the FW really did */
1143 tpacpi_rfk_update_swstate(tp_rfk);
1144
1145 return (res < 0) ? res : 0;
1146 }
1147
1148 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1149 .set_block = tpacpi_rfk_hook_set_block,
1150 };
1151
tpacpi_new_rfkill(const enum tpacpi_rfk_id id,const struct tpacpi_rfk_ops * tp_rfkops,const enum rfkill_type rfktype,const char * name,const bool set_default)1152 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1153 const struct tpacpi_rfk_ops *tp_rfkops,
1154 const enum rfkill_type rfktype,
1155 const char *name,
1156 const bool set_default)
1157 {
1158 struct tpacpi_rfk *atp_rfk;
1159 int res;
1160 bool sw_state = false;
1161 bool hw_state;
1162 int sw_status;
1163
1164 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1165
1166 atp_rfk = kzalloc_obj(struct tpacpi_rfk);
1167 if (atp_rfk)
1168 atp_rfk->rfkill = rfkill_alloc(name,
1169 &tpacpi_pdev->dev,
1170 rfktype,
1171 &tpacpi_rfk_rfkill_ops,
1172 atp_rfk);
1173 if (!atp_rfk || !atp_rfk->rfkill) {
1174 pr_err("failed to allocate memory for rfkill class\n");
1175 kfree(atp_rfk);
1176 return -ENOMEM;
1177 }
1178
1179 atp_rfk->id = id;
1180 atp_rfk->ops = tp_rfkops;
1181
1182 sw_status = (tp_rfkops->get_status)();
1183 if (sw_status < 0) {
1184 pr_err("failed to read initial state for %s, error %d\n",
1185 name, sw_status);
1186 } else {
1187 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1188 if (set_default) {
1189 /* try to keep the initial state, since we ask the
1190 * firmware to preserve it across S5 in NVRAM */
1191 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1192 }
1193 }
1194 hw_state = tpacpi_rfk_check_hwblock_state();
1195 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1196
1197 res = rfkill_register(atp_rfk->rfkill);
1198 if (res < 0) {
1199 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1200 rfkill_destroy(atp_rfk->rfkill);
1201 kfree(atp_rfk);
1202 return res;
1203 }
1204
1205 tpacpi_rfkill_switches[id] = atp_rfk;
1206
1207 pr_info("rfkill switch %s: radio is %sblocked\n",
1208 name, (sw_state || hw_state) ? "" : "un");
1209 return 0;
1210 }
1211
tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)1212 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1213 {
1214 struct tpacpi_rfk *tp_rfk;
1215
1216 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1217
1218 tp_rfk = tpacpi_rfkill_switches[id];
1219 if (tp_rfk) {
1220 rfkill_unregister(tp_rfk->rfkill);
1221 rfkill_destroy(tp_rfk->rfkill);
1222 tpacpi_rfkill_switches[id] = NULL;
1223 kfree(tp_rfk);
1224 }
1225 }
1226
printk_deprecated_rfkill_attribute(const char * const what)1227 static void printk_deprecated_rfkill_attribute(const char * const what)
1228 {
1229 printk_deprecated_attribute(what,
1230 "Please switch to generic rfkill before year 2010");
1231 }
1232
1233 /* sysfs <radio> enable ------------------------------------------------ */
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,struct device_attribute * attr,char * buf)1234 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1235 struct device_attribute *attr,
1236 char *buf)
1237 {
1238 int status;
1239
1240 printk_deprecated_rfkill_attribute(attr->attr.name);
1241
1242 /* This is in the ABI... */
1243 if (tpacpi_rfk_check_hwblock_state()) {
1244 status = TPACPI_RFK_RADIO_OFF;
1245 } else {
1246 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1247 if (status < 0)
1248 return status;
1249 }
1250
1251 return sysfs_emit(buf, "%d\n",
1252 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1253 }
1254
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,struct device_attribute * attr,const char * buf,size_t count)1255 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1256 struct device_attribute *attr,
1257 const char *buf, size_t count)
1258 {
1259 unsigned long t;
1260 int res;
1261
1262 printk_deprecated_rfkill_attribute(attr->attr.name);
1263
1264 if (parse_strtoul(buf, 1, &t))
1265 return -EINVAL;
1266
1267 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1268
1269 /* This is in the ABI... */
1270 if (tpacpi_rfk_check_hwblock_state() && !!t)
1271 return -EPERM;
1272
1273 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1275 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1276
1277 return (res < 0) ? res : count;
1278 }
1279
1280 /* procfs -------------------------------------------------------------- */
tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,struct seq_file * m)1281 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1282 {
1283 if (id >= TPACPI_RFK_SW_MAX)
1284 seq_puts(m, "status:\t\tnot supported\n");
1285 else {
1286 int status;
1287
1288 /* This is in the ABI... */
1289 if (tpacpi_rfk_check_hwblock_state()) {
1290 status = TPACPI_RFK_RADIO_OFF;
1291 } else {
1292 status = tpacpi_rfk_update_swstate(
1293 tpacpi_rfkill_switches[id]);
1294 if (status < 0)
1295 return status;
1296 }
1297
1298 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
1299 seq_puts(m, "commands:\tenable, disable\n");
1300 }
1301
1302 return 0;
1303 }
1304
tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id,char * buf)1305 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1306 {
1307 char *cmd;
1308 int status = -1;
1309 int res = 0;
1310
1311 if (id >= TPACPI_RFK_SW_MAX)
1312 return -ENODEV;
1313
1314 while ((cmd = strsep(&buf, ","))) {
1315 if (strstarts(cmd, "enable"))
1316 status = TPACPI_RFK_RADIO_ON;
1317 else if (strstarts(cmd, "disable"))
1318 status = TPACPI_RFK_RADIO_OFF;
1319 else
1320 return -EINVAL;
1321 }
1322
1323 if (status != -1) {
1324 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1325 str_enable_disable(status == TPACPI_RFK_RADIO_ON),
1326 tpacpi_rfkill_names[id]);
1327 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1328 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1329 }
1330
1331 return res;
1332 }
1333
1334 /*************************************************************************
1335 * thinkpad-acpi driver attributes
1336 */
1337
1338 /* interface_version --------------------------------------------------- */
interface_version_show(struct device_driver * drv,char * buf)1339 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1340 {
1341 return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
1342 }
1343 static DRIVER_ATTR_RO(interface_version);
1344
1345 /* debug_level --------------------------------------------------------- */
debug_level_show(struct device_driver * drv,char * buf)1346 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1347 {
1348 return sysfs_emit(buf, "0x%04x\n", dbg_level);
1349 }
1350
debug_level_store(struct device_driver * drv,const char * buf,size_t count)1351 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1352 size_t count)
1353 {
1354 unsigned long t;
1355
1356 if (parse_strtoul(buf, 0xffff, &t))
1357 return -EINVAL;
1358
1359 dbg_level = t;
1360
1361 return count;
1362 }
1363 static DRIVER_ATTR_RW(debug_level);
1364
1365 /* version ------------------------------------------------------------- */
version_show(struct device_driver * drv,char * buf)1366 static ssize_t version_show(struct device_driver *drv, char *buf)
1367 {
1368 return sysfs_emit(buf, "%s v%s\n",
1369 TPACPI_DESC, TPACPI_VERSION);
1370 }
1371 static DRIVER_ATTR_RO(version);
1372
1373 /* --------------------------------------------------------------------- */
1374
1375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1376
1377 /* wlsw_emulstate ------------------------------------------------------ */
wlsw_emulstate_show(struct device_driver * drv,char * buf)1378 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1379 {
1380 return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
1381 }
1382
wlsw_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1383 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1384 size_t count)
1385 {
1386 unsigned long t;
1387
1388 if (parse_strtoul(buf, 1, &t))
1389 return -EINVAL;
1390
1391 if (tpacpi_wlsw_emulstate != !!t) {
1392 tpacpi_wlsw_emulstate = !!t;
1393 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1394 }
1395
1396 return count;
1397 }
1398 static DRIVER_ATTR_RW(wlsw_emulstate);
1399
1400 /* bluetooth_emulstate ------------------------------------------------- */
bluetooth_emulstate_show(struct device_driver * drv,char * buf)1401 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1402 {
1403 return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
1404 }
1405
bluetooth_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1406 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1407 const char *buf, size_t count)
1408 {
1409 unsigned long t;
1410
1411 if (parse_strtoul(buf, 1, &t))
1412 return -EINVAL;
1413
1414 tpacpi_bluetooth_emulstate = !!t;
1415
1416 return count;
1417 }
1418 static DRIVER_ATTR_RW(bluetooth_emulstate);
1419
1420 /* wwan_emulstate ------------------------------------------------- */
wwan_emulstate_show(struct device_driver * drv,char * buf)1421 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1422 {
1423 return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
1424 }
1425
wwan_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1426 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1427 size_t count)
1428 {
1429 unsigned long t;
1430
1431 if (parse_strtoul(buf, 1, &t))
1432 return -EINVAL;
1433
1434 tpacpi_wwan_emulstate = !!t;
1435
1436 return count;
1437 }
1438 static DRIVER_ATTR_RW(wwan_emulstate);
1439
1440 /* uwb_emulstate ------------------------------------------------- */
uwb_emulstate_show(struct device_driver * drv,char * buf)1441 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1442 {
1443 return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
1444 }
1445
uwb_emulstate_store(struct device_driver * drv,const char * buf,size_t count)1446 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1447 size_t count)
1448 {
1449 unsigned long t;
1450
1451 if (parse_strtoul(buf, 1, &t))
1452 return -EINVAL;
1453
1454 tpacpi_uwb_emulstate = !!t;
1455
1456 return count;
1457 }
1458 static DRIVER_ATTR_RW(uwb_emulstate);
1459 #endif
1460
1461 /*************************************************************************
1462 * Firmware Data
1463 */
1464
1465 /*
1466 * Table of recommended minimum BIOS versions
1467 *
1468 * Reasons for listing:
1469 * 1. Stable BIOS, listed because the unknown amount of
1470 * bugs and bad ACPI behaviour on older versions
1471 *
1472 * 2. BIOS or EC fw with known bugs that trigger on Linux
1473 *
1474 * 3. BIOS with known reduced functionality in older versions
1475 *
1476 * We recommend the latest BIOS and EC version.
1477 * We only support the latest BIOS and EC fw version as a rule.
1478 *
1479 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1480 * Information from users in ThinkWiki
1481 *
1482 * WARNING: we use this table also to detect that the machine is
1483 * a ThinkPad in some cases, so don't remove entries lightly.
1484 */
1485
1486 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1487 { .vendor = (__v), \
1488 .bios = TPID(__id1, __id2), \
1489 .ec = TPACPI_MATCH_ANY, \
1490 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1491 | TPVER(__bv1, __bv2) }
1492
1493 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1494 __eid, __ev1, __ev2) \
1495 { .vendor = (__v), \
1496 .bios = TPID(__bid1, __bid2), \
1497 .ec = __eid, \
1498 .quirks = TPVER(__ev1, __ev2) << 16 \
1499 | TPVER(__bv1, __bv2) }
1500
1501 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1502 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1503
1504 /* Outdated IBM BIOSes often lack the EC id string */
1505 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1506 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1507 __bv1, __bv2, TPID(__id1, __id2), \
1508 __ev1, __ev2), \
1509 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1510 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1511 __ev1, __ev2)
1512
1513 /* Outdated IBM BIOSes often lack the EC id string */
1514 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1515 __eid1, __eid2, __ev1, __ev2) \
1516 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1517 __bv1, __bv2, TPID(__eid1, __eid2), \
1518 __ev1, __ev2), \
1519 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1520 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1521 __ev1, __ev2)
1522
1523 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1524 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1525
1526 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1527 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1528 __bv1, __bv2, TPID(__id1, __id2), \
1529 __ev1, __ev2)
1530
1531 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1532 __eid1, __eid2, __ev1, __ev2) \
1533 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1534 __bv1, __bv2, TPID(__eid1, __eid2), \
1535 __ev1, __ev2)
1536
1537 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1538 /* Numeric models ------------------ */
1539 /* FW MODEL BIOS VERS */
1540 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1541 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1542 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1543 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1544 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1545 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1546 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1547 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1548 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1549
1550 /* A-series ------------------------- */
1551 /* FW MODEL BIOS VERS EC VERS */
1552 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1553 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1554 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1555 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1556 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1557 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1558 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1559 TPV_QI0('1', '3', '2', '0'), /* A22m */
1560 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1561 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1562 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1563
1564 /* G-series ------------------------- */
1565 /* FW MODEL BIOS VERS */
1566 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1567 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1568
1569 /* R-series, T-series --------------- */
1570 /* FW MODEL BIOS VERS EC VERS */
1571 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1572 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1573 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1574 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1575 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1576 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1577 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1578 T40/p, T41/p, T42/p (1) */
1579 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1580 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1581 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1582 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1583
1584 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1585 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1586 TPV_QI0('1', '6', '3', '2'), /* T22 */
1587 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1588 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1589 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1590
1591 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1592 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1593 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1594
1595 /* BIOS FW BIOS VERS EC FW EC VERS */
1596 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1597 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1598
1599 /* X-series ------------------------- */
1600 /* FW MODEL BIOS VERS EC VERS */
1601 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1602 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1603 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1604 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1605 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1606 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1607 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1608
1609 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1610 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1611
1612 /* (0) - older versions lack DMI EC fw string and functionality */
1613 /* (1) - older versions known to lack functionality */
1614 };
1615
1616 #undef TPV_QL1
1617 #undef TPV_QL0
1618 #undef TPV_QI2
1619 #undef TPV_QI1
1620 #undef TPV_QI0
1621 #undef TPV_Q_X
1622 #undef TPV_Q
1623
tpacpi_check_outdated_fw(void)1624 static void __init tpacpi_check_outdated_fw(void)
1625 {
1626 unsigned long fwvers;
1627 u16 ec_version, bios_version;
1628
1629 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1630 ARRAY_SIZE(tpacpi_bios_version_qtable));
1631
1632 if (!fwvers)
1633 return;
1634
1635 bios_version = fwvers & 0xffffU;
1636 ec_version = (fwvers >> 16) & 0xffffU;
1637
1638 /* note that unknown versions are set to 0x0000 and we use that */
1639 if ((bios_version > thinkpad_id.bios_release) ||
1640 (ec_version > thinkpad_id.ec_release &&
1641 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1642 /*
1643 * The changelogs would let us track down the exact
1644 * reason, but it is just too much of a pain to track
1645 * it. We only list BIOSes that are either really
1646 * broken, or really stable to begin with, so it is
1647 * best if the user upgrades the firmware anyway.
1648 */
1649 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1650 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1651 }
1652 }
1653
tpacpi_is_fw_known(void)1654 static bool __init tpacpi_is_fw_known(void)
1655 {
1656 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1657 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1658 }
1659
1660 /****************************************************************************
1661 ****************************************************************************
1662 *
1663 * Subdrivers
1664 *
1665 ****************************************************************************
1666 ****************************************************************************/
1667
1668 /*************************************************************************
1669 * thinkpad-acpi metadata subdriver
1670 */
1671
thinkpad_acpi_driver_read(struct seq_file * m)1672 static int thinkpad_acpi_driver_read(struct seq_file *m)
1673 {
1674 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1675 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1676 return 0;
1677 }
1678
1679 static struct ibm_struct thinkpad_acpi_driver_data = {
1680 .name = "driver",
1681 .read = thinkpad_acpi_driver_read,
1682 };
1683
1684 /*************************************************************************
1685 * Hotkey subdriver
1686 */
1687
1688 /*
1689 * ThinkPad firmware event model
1690 *
1691 * The ThinkPad firmware has two main event interfaces: normal ACPI
1692 * notifications (which follow the ACPI standard), and a private event
1693 * interface.
1694 *
1695 * The private event interface also issues events for the hotkeys. As
1696 * the driver gained features, the event handling code ended up being
1697 * built around the hotkey subdriver. This will need to be refactored
1698 * to a more formal event API eventually.
1699 *
1700 * Some "hotkeys" are actually supposed to be used as event reports,
1701 * such as "brightness has changed", "volume has changed", depending on
1702 * the ThinkPad model and how the firmware is operating.
1703 *
1704 * Unlike other classes, hotkey-class events have mask/unmask control on
1705 * non-ancient firmware. However, how it behaves changes a lot with the
1706 * firmware model and version.
1707 */
1708
1709 enum { /* hot key scan codes (derived from ACPI DSDT) */
1710 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1711 TP_ACPI_HOTKEYSCAN_FNF2,
1712 TP_ACPI_HOTKEYSCAN_FNF3,
1713 TP_ACPI_HOTKEYSCAN_FNF4,
1714 TP_ACPI_HOTKEYSCAN_FNF5,
1715 TP_ACPI_HOTKEYSCAN_FNF6,
1716 TP_ACPI_HOTKEYSCAN_FNF7,
1717 TP_ACPI_HOTKEYSCAN_FNF8,
1718 TP_ACPI_HOTKEYSCAN_FNF9,
1719 TP_ACPI_HOTKEYSCAN_FNF10,
1720 TP_ACPI_HOTKEYSCAN_FNF11,
1721 TP_ACPI_HOTKEYSCAN_FNF12,
1722 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1723 TP_ACPI_HOTKEYSCAN_FNINSERT,
1724 TP_ACPI_HOTKEYSCAN_FNDELETE,
1725 TP_ACPI_HOTKEYSCAN_FNHOME,
1726 TP_ACPI_HOTKEYSCAN_FNEND,
1727 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1728 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1729 TP_ACPI_HOTKEYSCAN_FNSPACE,
1730 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1731 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1732 TP_ACPI_HOTKEYSCAN_MUTE,
1733 TP_ACPI_HOTKEYSCAN_THINKPAD,
1734 TP_ACPI_HOTKEYSCAN_UNK1,
1735 TP_ACPI_HOTKEYSCAN_UNK2,
1736 TP_ACPI_HOTKEYSCAN_MICMUTE,
1737 TP_ACPI_HOTKEYSCAN_UNK4,
1738 TP_ACPI_HOTKEYSCAN_CONFIG,
1739 TP_ACPI_HOTKEYSCAN_SEARCH,
1740 TP_ACPI_HOTKEYSCAN_SCALE,
1741 TP_ACPI_HOTKEYSCAN_FILE,
1742
1743 /* Adaptive keyboard keycodes */
1744 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START, /* 32 / 0x20 */
1745 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1746 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1747 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1748 TP_ACPI_HOTKEYSCAN_CLOUD,
1749 TP_ACPI_HOTKEYSCAN_UNK9,
1750 TP_ACPI_HOTKEYSCAN_VOICE,
1751 TP_ACPI_HOTKEYSCAN_UNK10,
1752 TP_ACPI_HOTKEYSCAN_GESTURES,
1753 TP_ACPI_HOTKEYSCAN_UNK11,
1754 TP_ACPI_HOTKEYSCAN_UNK12,
1755 TP_ACPI_HOTKEYSCAN_UNK13,
1756 TP_ACPI_HOTKEYSCAN_CONFIG2,
1757 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1758 TP_ACPI_HOTKEYSCAN_RELOAD,
1759 TP_ACPI_HOTKEYSCAN_BACK,
1760 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1761 TP_ACPI_HOTKEYSCAN_MIC_UP,
1762 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1763 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1764 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1765
1766 /* Lenovo extended keymap, starting at 0x1300 */
1767 TP_ACPI_HOTKEYSCAN_EXTENDED_START, /* 52 / 0x34 */
1768 /* first new observed key (star, favorites) is 0x1311 */
1769 TP_ACPI_HOTKEYSCAN_STAR = 69,
1770 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1771 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1772 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1773 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1774 TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
1775 TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
1776 TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
1777 TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
1778 };
1779
1780 enum { /* Keys/events available through NVRAM polling */
1781 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1782 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1783 };
1784
1785 enum { /* Positions of some of the keys in hotkey masks */
1786 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1787 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1788 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1789 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1790 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1791 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1792 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1793 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1794 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1795 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1796 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1797 };
1798
1799 enum { /* NVRAM to ACPI HKEY group map */
1800 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1801 TP_ACPI_HKEY_ZOOM_MASK |
1802 TP_ACPI_HKEY_DISPSWTCH_MASK |
1803 TP_ACPI_HKEY_HIBERNATE_MASK,
1804 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1805 TP_ACPI_HKEY_BRGHTDWN_MASK,
1806 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1807 TP_ACPI_HKEY_VOLDWN_MASK |
1808 TP_ACPI_HKEY_MUTE_MASK,
1809 };
1810
1811 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1812 struct tp_nvram_state {
1813 u16 thinkpad_toggle:1;
1814 u16 zoom_toggle:1;
1815 u16 display_toggle:1;
1816 u16 thinklight_toggle:1;
1817 u16 hibernate_toggle:1;
1818 u16 displayexp_toggle:1;
1819 u16 display_state:1;
1820 u16 brightness_toggle:1;
1821 u16 volume_toggle:1;
1822 u16 mute:1;
1823
1824 u8 brightness_level;
1825 u8 volume_level;
1826 };
1827
1828 /* kthread for the hotkey poller */
1829 static struct task_struct *tpacpi_hotkey_task;
1830
1831 /*
1832 * Acquire mutex to write poller control variables as an
1833 * atomic block.
1834 *
1835 * Increment hotkey_config_change when changing them if you
1836 * want the kthread to forget old state.
1837 *
1838 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1839 */
1840 static struct mutex hotkey_thread_data_mutex;
1841 static unsigned int hotkey_config_change;
1842
1843 /*
1844 * hotkey poller control variables
1845 *
1846 * Must be atomic or readers will also need to acquire mutex
1847 *
1848 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1849 * should be used only when the changes need to be taken as
1850 * a block, OR when one needs to force the kthread to forget
1851 * old state.
1852 */
1853 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1854 static unsigned int hotkey_poll_freq = 10; /* Hz */
1855
1856 #define HOTKEY_CONFIG_CRITICAL_START \
1857 do { \
1858 mutex_lock(&hotkey_thread_data_mutex); \
1859 hotkey_config_change++; \
1860 } while (0);
1861 #define HOTKEY_CONFIG_CRITICAL_END \
1862 mutex_unlock(&hotkey_thread_data_mutex);
1863
1864 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1865
1866 #define hotkey_source_mask 0U
1867 #define HOTKEY_CONFIG_CRITICAL_START
1868 #define HOTKEY_CONFIG_CRITICAL_END
1869
1870 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1871
1872 static struct mutex hotkey_mutex;
1873
1874 static enum { /* Reasons for waking up */
1875 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1876 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1877 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1878 } hotkey_wakeup_reason;
1879
1880 static int hotkey_autosleep_ack;
1881
1882 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1883 static u32 hotkey_all_mask; /* all events supported in fw */
1884 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
1885 static u32 hotkey_reserved_mask; /* events better left disabled */
1886 static u32 hotkey_driver_mask; /* events needed by the driver */
1887 static u32 hotkey_user_mask; /* events visible to userspace */
1888 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1889
1890 static bool tpacpi_driver_event(const unsigned int hkey_event);
1891 static void hotkey_poll_setup(const bool may_warn);
1892
1893 /* HKEY.MHKG() return bits */
1894 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1895 enum {
1896 TP_ACPI_MULTI_MODE_INVALID = 0,
1897 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
1898 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
1899 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
1900 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
1901 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
1902 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
1903 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
1904 };
1905
1906 enum {
1907 /* The following modes are considered tablet mode for the purpose of
1908 * reporting the status to userspace. i.e. in all these modes it makes
1909 * sense to disable the laptop input devices such as touchpad and
1910 * keyboard.
1911 */
1912 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
1913 TP_ACPI_MULTI_MODE_STAND |
1914 TP_ACPI_MULTI_MODE_TENT |
1915 TP_ACPI_MULTI_MODE_STAND_TENT,
1916 };
1917
hotkey_get_wlsw(void)1918 static int hotkey_get_wlsw(void)
1919 {
1920 int status;
1921
1922 if (!tp_features.hotkey_wlsw)
1923 return -ENODEV;
1924
1925 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1926 if (dbg_wlswemul)
1927 return (tpacpi_wlsw_emulstate) ?
1928 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1929 #endif
1930
1931 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1932 return -EIO;
1933
1934 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1935 }
1936
hotkey_gmms_get_tablet_mode(int s,int * has_tablet_mode)1937 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
1938 {
1939 int type = (s >> 16) & 0xffff;
1940 int value = s & 0xffff;
1941 int mode = TP_ACPI_MULTI_MODE_INVALID;
1942 int valid_modes = 0;
1943
1944 if (has_tablet_mode)
1945 *has_tablet_mode = 0;
1946
1947 switch (type) {
1948 case 1:
1949 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1950 TP_ACPI_MULTI_MODE_TABLET |
1951 TP_ACPI_MULTI_MODE_STAND_TENT;
1952 break;
1953 case 2:
1954 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1955 TP_ACPI_MULTI_MODE_FLAT |
1956 TP_ACPI_MULTI_MODE_TABLET |
1957 TP_ACPI_MULTI_MODE_STAND |
1958 TP_ACPI_MULTI_MODE_TENT;
1959 break;
1960 case 3:
1961 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1962 TP_ACPI_MULTI_MODE_FLAT;
1963 break;
1964 case 4:
1965 case 5:
1966 /* In mode 4, FLAT is not specified as a valid mode. However,
1967 * it can be seen at least on the X1 Yoga 2nd Generation.
1968 */
1969 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
1970 TP_ACPI_MULTI_MODE_FLAT |
1971 TP_ACPI_MULTI_MODE_TABLET |
1972 TP_ACPI_MULTI_MODE_STAND |
1973 TP_ACPI_MULTI_MODE_TENT;
1974 break;
1975 default:
1976 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
1977 type, value, TPACPI_MAIL);
1978 return 0;
1979 }
1980
1981 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
1982 *has_tablet_mode = 1;
1983
1984 switch (value) {
1985 case 1:
1986 mode = TP_ACPI_MULTI_MODE_LAPTOP;
1987 break;
1988 case 2:
1989 mode = TP_ACPI_MULTI_MODE_FLAT;
1990 break;
1991 case 3:
1992 mode = TP_ACPI_MULTI_MODE_TABLET;
1993 break;
1994 case 4:
1995 if (type == 1)
1996 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
1997 else
1998 mode = TP_ACPI_MULTI_MODE_STAND;
1999 break;
2000 case 5:
2001 mode = TP_ACPI_MULTI_MODE_TENT;
2002 break;
2003 default:
2004 if (type == 5 && value == 0xffff) {
2005 pr_warn("Multi mode status is undetected, assuming laptop\n");
2006 return 0;
2007 }
2008 }
2009
2010 if (!(mode & valid_modes)) {
2011 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2012 value, type, TPACPI_MAIL);
2013 return 0;
2014 }
2015
2016 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2017 }
2018
hotkey_get_tablet_mode(int * status)2019 static int hotkey_get_tablet_mode(int *status)
2020 {
2021 int s;
2022
2023 switch (tp_features.hotkey_tablet) {
2024 case TP_HOTKEY_TABLET_USES_MHKG:
2025 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2026 return -EIO;
2027
2028 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2029 break;
2030 case TP_HOTKEY_TABLET_USES_GMMS:
2031 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2032 return -EIO;
2033
2034 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2035 break;
2036 default:
2037 break;
2038 }
2039
2040 return 0;
2041 }
2042
2043 /*
2044 * Reads current event mask from firmware, and updates
2045 * hotkey_acpi_mask accordingly. Also resets any bits
2046 * from hotkey_user_mask that are unavailable to be
2047 * delivered (shadow requirement of the userspace ABI).
2048 */
hotkey_mask_get(void)2049 static int hotkey_mask_get(void)
2050 {
2051 lockdep_assert_held(&hotkey_mutex);
2052
2053 if (tp_features.hotkey_mask) {
2054 u32 m = 0;
2055
2056 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2057 return -EIO;
2058
2059 hotkey_acpi_mask = m;
2060 } else {
2061 /* no mask support doesn't mean no event support... */
2062 hotkey_acpi_mask = hotkey_all_mask;
2063 }
2064
2065 /* sync userspace-visible mask */
2066 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2067
2068 return 0;
2069 }
2070
hotkey_mask_warn_incomplete_mask(void)2071 static void hotkey_mask_warn_incomplete_mask(void)
2072 {
2073 /* log only what the user can fix... */
2074 const u32 wantedmask = hotkey_driver_mask &
2075 ~(hotkey_acpi_mask | hotkey_source_mask) &
2076 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2077
2078 if (wantedmask)
2079 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2080 }
2081
2082 /*
2083 * Set the firmware mask when supported
2084 *
2085 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2086 *
2087 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2088 */
hotkey_mask_set(u32 mask)2089 static int hotkey_mask_set(u32 mask)
2090 {
2091 int i;
2092 int rc = 0;
2093
2094 const u32 fwmask = mask & ~hotkey_source_mask;
2095
2096 lockdep_assert_held(&hotkey_mutex);
2097
2098 if (tp_features.hotkey_mask) {
2099 for (i = 0; i < 32; i++) {
2100 if (!acpi_evalf(hkey_handle,
2101 NULL, "MHKM", "vdd", i + 1,
2102 !!(mask & (1 << i)))) {
2103 rc = -EIO;
2104 break;
2105 }
2106 }
2107 }
2108
2109 /*
2110 * We *must* make an inconditional call to hotkey_mask_get to
2111 * refresh hotkey_acpi_mask and update hotkey_user_mask
2112 *
2113 * Take the opportunity to also log when we cannot _enable_
2114 * a given event.
2115 */
2116 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2117 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2118 fwmask, hotkey_acpi_mask);
2119 }
2120
2121 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2122 hotkey_mask_warn_incomplete_mask();
2123
2124 return rc;
2125 }
2126
2127 /*
2128 * Sets hotkey_user_mask and tries to set the firmware mask
2129 */
hotkey_user_mask_set(const u32 mask)2130 static int hotkey_user_mask_set(const u32 mask)
2131 {
2132 int rc;
2133
2134 lockdep_assert_held(&hotkey_mutex);
2135
2136 /* Give people a chance to notice they are doing something that
2137 * is bound to go boom on their users sooner or later */
2138 if (!tp_warned.hotkey_mask_ff &&
2139 (mask == 0xffff || mask == 0xffffff ||
2140 mask == 0xffffffff)) {
2141 tp_warned.hotkey_mask_ff = 1;
2142 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2143 mask);
2144 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2145 }
2146
2147 /* Try to enable what the user asked for, plus whatever we need.
2148 * this syncs everything but won't enable bits in hotkey_user_mask */
2149 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2150
2151 /* Enable the available bits in hotkey_user_mask */
2152 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2153
2154 return rc;
2155 }
2156
2157 /*
2158 * Sets the driver hotkey mask.
2159 *
2160 * Can be called even if the hotkey subdriver is inactive
2161 */
tpacpi_hotkey_driver_mask_set(const u32 mask)2162 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2163 {
2164 int rc;
2165
2166 /* Do the right thing if hotkey_init has not been called yet */
2167 if (!tp_features.hotkey) {
2168 hotkey_driver_mask = mask;
2169 return 0;
2170 }
2171
2172 mutex_lock(&hotkey_mutex);
2173
2174 HOTKEY_CONFIG_CRITICAL_START
2175 hotkey_driver_mask = mask;
2176 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2177 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2178 #endif
2179 HOTKEY_CONFIG_CRITICAL_END
2180
2181 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2182 ~hotkey_source_mask);
2183 hotkey_poll_setup(true);
2184
2185 mutex_unlock(&hotkey_mutex);
2186
2187 return rc;
2188 }
2189
hotkey_status_get(int * status)2190 static int hotkey_status_get(int *status)
2191 {
2192 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2193 return -EIO;
2194
2195 return 0;
2196 }
2197
hotkey_status_set(bool enable)2198 static int hotkey_status_set(bool enable)
2199 {
2200 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2201 return -EIO;
2202
2203 return 0;
2204 }
2205
tpacpi_input_send_tabletsw(void)2206 static void tpacpi_input_send_tabletsw(void)
2207 {
2208 int state;
2209
2210 if (tp_features.hotkey_tablet &&
2211 !hotkey_get_tablet_mode(&state)) {
2212 mutex_lock(&tpacpi_inputdev_send_mutex);
2213
2214 input_report_switch(tpacpi_inputdev,
2215 SW_TABLET_MODE, !!state);
2216 input_sync(tpacpi_inputdev);
2217
2218 mutex_unlock(&tpacpi_inputdev_send_mutex);
2219 }
2220 }
2221
2222 #define GCES_NO_SHUTTER_DEVICE BIT(31)
2223
get_camera_shutter(void)2224 static int get_camera_shutter(void)
2225 {
2226 acpi_handle gces_handle;
2227 int output;
2228
2229 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GCES", &gces_handle)))
2230 return -ENODEV;
2231
2232 if (!acpi_evalf(gces_handle, &output, NULL, "dd", 0))
2233 return -EIO;
2234
2235 if (output & GCES_NO_SHUTTER_DEVICE)
2236 return -ENODEV;
2237
2238 return output;
2239 }
2240
tpacpi_input_send_key(const u32 hkey,bool * send_acpi_ev)2241 static bool tpacpi_input_send_key(const u32 hkey, bool *send_acpi_ev)
2242 {
2243 bool known_ev;
2244 u32 scancode;
2245
2246 if (tpacpi_driver_event(hkey))
2247 return true;
2248
2249 /*
2250 * Before the conversion to using the sparse-keymap helpers the driver used to
2251 * map the hkey event codes to 0x00 - 0x4d scancodes so that a straight scancode
2252 * indexed array could be used to map scancodes to keycodes:
2253 *
2254 * 0x1001 - 0x1020 -> 0x00 - 0x1f (Original ThinkPad events)
2255 * 0x1103 - 0x1116 -> 0x20 - 0x33 (Adaptive keyboard, 2014 X1 Carbon)
2256 * 0x1300 - 0x1319 -> 0x34 - 0x4d (Additional keys send in 2017+ models)
2257 *
2258 * The sparse-keymap tables still use these scancodes for these ranges to
2259 * preserve userspace API compatibility (e.g. hwdb keymappings).
2260 */
2261 if (hkey >= TP_HKEY_EV_ORIG_KEY_START &&
2262 hkey <= TP_HKEY_EV_ORIG_KEY_END) {
2263 scancode = hkey - TP_HKEY_EV_ORIG_KEY_START;
2264 if (!(hotkey_user_mask & (1 << scancode)))
2265 return true; /* Not reported but still a known code */
2266 } else if (hkey >= TP_HKEY_EV_ADAPTIVE_KEY_START &&
2267 hkey <= TP_HKEY_EV_ADAPTIVE_KEY_END) {
2268 scancode = hkey - TP_HKEY_EV_ADAPTIVE_KEY_START +
2269 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START;
2270 } else if (hkey >= TP_HKEY_EV_EXTENDED_KEY_START &&
2271 hkey <= TP_HKEY_EV_EXTENDED_KEY_END) {
2272 scancode = hkey - TP_HKEY_EV_EXTENDED_KEY_START +
2273 TP_ACPI_HOTKEYSCAN_EXTENDED_START;
2274 } else {
2275 /*
2276 * Do not send ACPI netlink events for unknown hotkeys, to
2277 * avoid userspace starting to rely on them. Instead these
2278 * should be added to the keymap to send evdev events.
2279 */
2280 if (send_acpi_ev)
2281 *send_acpi_ev = false;
2282
2283 scancode = hkey;
2284 }
2285
2286 mutex_lock(&tpacpi_inputdev_send_mutex);
2287 known_ev = sparse_keymap_report_event(tpacpi_inputdev, scancode, 1, true);
2288 mutex_unlock(&tpacpi_inputdev_send_mutex);
2289
2290 return known_ev;
2291 }
2292
2293 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2294 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2295
2296 /* Do NOT call without validating scancode first */
tpacpi_hotkey_send_key(unsigned int scancode)2297 static void tpacpi_hotkey_send_key(unsigned int scancode)
2298 {
2299 tpacpi_input_send_key(TP_HKEY_EV_ORIG_KEY_START + scancode, NULL);
2300 }
2301
hotkey_read_nvram(struct tp_nvram_state * n,const u32 m)2302 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2303 {
2304 u8 d;
2305
2306 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2308 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2309 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2310 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2311 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2312 }
2313 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2315 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2316 }
2317 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2318 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2319 n->displayexp_toggle =
2320 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2321 }
2322 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2324 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2325 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2326 n->brightness_toggle =
2327 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2328 }
2329 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2331 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2332 >> TP_NVRAM_POS_LEVEL_VOLUME;
2333 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2334 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2335 }
2336 }
2337
2338 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2339 do { \
2340 if ((event_mask & (1 << __scancode)) && \
2341 oldn->__member != newn->__member) \
2342 tpacpi_hotkey_send_key(__scancode); \
2343 } while (0)
2344
2345 #define TPACPI_MAY_SEND_KEY(__scancode) \
2346 do { \
2347 if (event_mask & (1 << __scancode)) \
2348 tpacpi_hotkey_send_key(__scancode); \
2349 } while (0)
2350
issue_volchange(const unsigned int oldvol,const unsigned int newvol,const u32 event_mask)2351 static void issue_volchange(const unsigned int oldvol,
2352 const unsigned int newvol,
2353 const u32 event_mask)
2354 {
2355 unsigned int i = oldvol;
2356
2357 while (i > newvol) {
2358 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2359 i--;
2360 }
2361 while (i < newvol) {
2362 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2363 i++;
2364 }
2365 }
2366
issue_brightnesschange(const unsigned int oldbrt,const unsigned int newbrt,const u32 event_mask)2367 static void issue_brightnesschange(const unsigned int oldbrt,
2368 const unsigned int newbrt,
2369 const u32 event_mask)
2370 {
2371 unsigned int i = oldbrt;
2372
2373 while (i > newbrt) {
2374 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2375 i--;
2376 }
2377 while (i < newbrt) {
2378 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2379 i++;
2380 }
2381 }
2382
hotkey_compare_and_issue_event(struct tp_nvram_state * oldn,struct tp_nvram_state * newn,const u32 event_mask)2383 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2384 struct tp_nvram_state *newn,
2385 const u32 event_mask)
2386 {
2387
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2392
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2394
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2396
2397 /*
2398 * Handle volume
2399 *
2400 * This code is supposed to duplicate the IBM firmware behaviour:
2401 * - Pressing MUTE issues mute hotkey message, even when already mute
2402 * - Pressing Volume up/down issues volume up/down hotkey messages,
2403 * even when already at maximum or minimum volume
2404 * - The act of unmuting issues volume up/down notification,
2405 * depending which key was used to unmute
2406 *
2407 * We are constrained to what the NVRAM can tell us, which is not much
2408 * and certainly not enough if more than one volume hotkey was pressed
2409 * since the last poll cycle.
2410 *
2411 * Just to make our life interesting, some newer Lenovo ThinkPads have
2412 * bugs in the BIOS and may fail to update volume_toggle properly.
2413 */
2414 if (newn->mute) {
2415 /* muted */
2416 if (!oldn->mute ||
2417 oldn->volume_toggle != newn->volume_toggle ||
2418 oldn->volume_level != newn->volume_level) {
2419 /* recently muted, or repeated mute keypress, or
2420 * multiple presses ending in mute */
2421 issue_volchange(oldn->volume_level, newn->volume_level,
2422 event_mask);
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2424 }
2425 } else {
2426 /* unmute */
2427 if (oldn->mute) {
2428 /* recently unmuted, issue 'unmute' keypress */
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 }
2431 if (oldn->volume_level != newn->volume_level) {
2432 issue_volchange(oldn->volume_level, newn->volume_level,
2433 event_mask);
2434 } else if (oldn->volume_toggle != newn->volume_toggle) {
2435 /* repeated vol up/down keypress at end of scale ? */
2436 if (newn->volume_level == 0)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2438 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2440 }
2441 }
2442
2443 /* handle brightness */
2444 if (oldn->brightness_level != newn->brightness_level) {
2445 issue_brightnesschange(oldn->brightness_level,
2446 newn->brightness_level, event_mask);
2447 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2448 /* repeated key presses that didn't change state */
2449 if (newn->brightness_level == 0)
2450 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2451 else if (newn->brightness_level >= bright_maxlvl
2452 && !tp_features.bright_unkfw)
2453 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2454 }
2455
2456 #undef TPACPI_COMPARE_KEY
2457 #undef TPACPI_MAY_SEND_KEY
2458 }
2459
2460 /*
2461 * Polling driver
2462 *
2463 * We track all events in hotkey_source_mask all the time, since
2464 * most of them are edge-based. We only issue those requested by
2465 * hotkey_user_mask or hotkey_driver_mask, though.
2466 */
hotkey_kthread(void * data)2467 static int hotkey_kthread(void *data)
2468 {
2469 struct tp_nvram_state s[2] = { 0 };
2470 u32 poll_mask, event_mask;
2471 unsigned int si, so;
2472 unsigned long t;
2473 unsigned int change_detector;
2474 unsigned int poll_freq;
2475 bool was_frozen;
2476
2477 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2478 return 0;
2479
2480 set_freezable();
2481
2482 so = 0;
2483 si = 1;
2484 t = 0;
2485
2486 /* Initial state for compares */
2487 mutex_lock(&hotkey_thread_data_mutex);
2488 change_detector = hotkey_config_change;
2489 poll_mask = hotkey_source_mask;
2490 event_mask = hotkey_source_mask &
2491 (hotkey_driver_mask | hotkey_user_mask);
2492 poll_freq = hotkey_poll_freq;
2493 mutex_unlock(&hotkey_thread_data_mutex);
2494 hotkey_read_nvram(&s[so], poll_mask);
2495
2496 while (!kthread_should_stop()) {
2497 if (t == 0) {
2498 if (likely(poll_freq))
2499 t = 1000/poll_freq;
2500 else
2501 t = 100; /* should never happen... */
2502 }
2503 t = msleep_interruptible(t);
2504 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2505 break;
2506
2507 if (t > 0 && !was_frozen)
2508 continue;
2509
2510 mutex_lock(&hotkey_thread_data_mutex);
2511 if (was_frozen || hotkey_config_change != change_detector) {
2512 /* forget old state on thaw or config change */
2513 si = so;
2514 t = 0;
2515 change_detector = hotkey_config_change;
2516 }
2517 poll_mask = hotkey_source_mask;
2518 event_mask = hotkey_source_mask &
2519 (hotkey_driver_mask | hotkey_user_mask);
2520 poll_freq = hotkey_poll_freq;
2521 mutex_unlock(&hotkey_thread_data_mutex);
2522
2523 if (likely(poll_mask)) {
2524 hotkey_read_nvram(&s[si], poll_mask);
2525 if (likely(si != so)) {
2526 hotkey_compare_and_issue_event(&s[so], &s[si],
2527 event_mask);
2528 }
2529 }
2530
2531 so = si;
2532 si ^= 1;
2533 }
2534
2535 return 0;
2536 }
2537
hotkey_poll_stop_sync(void)2538 static void hotkey_poll_stop_sync(void)
2539 {
2540 lockdep_assert_held(&hotkey_mutex);
2541
2542 if (tpacpi_hotkey_task) {
2543 kthread_stop(tpacpi_hotkey_task);
2544 tpacpi_hotkey_task = NULL;
2545 }
2546 }
2547
hotkey_poll_setup(const bool may_warn)2548 static void hotkey_poll_setup(const bool may_warn)
2549 {
2550 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2551 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2552
2553 lockdep_assert_held(&hotkey_mutex);
2554
2555 if (hotkey_poll_freq > 0 &&
2556 (poll_driver_mask ||
2557 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2558 if (!tpacpi_hotkey_task) {
2559 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2560 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2561 if (IS_ERR(tpacpi_hotkey_task)) {
2562 tpacpi_hotkey_task = NULL;
2563 pr_err("could not create kernel thread for hotkey polling\n");
2564 }
2565 }
2566 } else {
2567 hotkey_poll_stop_sync();
2568 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2569 hotkey_poll_freq == 0) {
2570 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2571 poll_user_mask, poll_driver_mask);
2572 }
2573 }
2574 }
2575
hotkey_poll_setup_safe(const bool may_warn)2576 static void hotkey_poll_setup_safe(const bool may_warn)
2577 {
2578 mutex_lock(&hotkey_mutex);
2579 hotkey_poll_setup(may_warn);
2580 mutex_unlock(&hotkey_mutex);
2581 }
2582
hotkey_poll_set_freq(unsigned int freq)2583 static void hotkey_poll_set_freq(unsigned int freq)
2584 {
2585 lockdep_assert_held(&hotkey_mutex);
2586
2587 if (!freq)
2588 hotkey_poll_stop_sync();
2589
2590 hotkey_poll_freq = freq;
2591 }
2592
2593 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2594
hotkey_poll_setup(const bool __unused)2595 static void hotkey_poll_setup(const bool __unused)
2596 {
2597 }
2598
hotkey_poll_setup_safe(const bool __unused)2599 static void hotkey_poll_setup_safe(const bool __unused)
2600 {
2601 }
2602
hotkey_poll_stop_sync(void)2603 static void hotkey_poll_stop_sync(void)
2604 {
2605 }
2606 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2607
hotkey_inputdev_open(struct input_dev * dev)2608 static int hotkey_inputdev_open(struct input_dev *dev)
2609 {
2610 switch (tpacpi_lifecycle) {
2611 case TPACPI_LIFE_INIT:
2612 case TPACPI_LIFE_RUNNING:
2613 hotkey_poll_setup_safe(false);
2614 return 0;
2615 case TPACPI_LIFE_EXITING:
2616 return -EBUSY;
2617 }
2618
2619 /* Should only happen if tpacpi_lifecycle is corrupt */
2620 BUG();
2621 return -EBUSY;
2622 }
2623
hotkey_inputdev_close(struct input_dev * dev)2624 static void hotkey_inputdev_close(struct input_dev *dev)
2625 {
2626 /* disable hotkey polling when possible */
2627 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2628 !(hotkey_source_mask & hotkey_driver_mask))
2629 hotkey_poll_setup_safe(false);
2630 }
2631
2632 /* sysfs hotkey enable ------------------------------------------------- */
hotkey_enable_show(struct device * dev,struct device_attribute * attr,char * buf)2633 static ssize_t hotkey_enable_show(struct device *dev,
2634 struct device_attribute *attr,
2635 char *buf)
2636 {
2637 int res, status;
2638
2639 printk_deprecated_attribute("hotkey_enable",
2640 "Hotkey reporting is always enabled");
2641
2642 res = hotkey_status_get(&status);
2643 if (res)
2644 return res;
2645
2646 return sysfs_emit(buf, "%d\n", status);
2647 }
2648
hotkey_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2649 static ssize_t hotkey_enable_store(struct device *dev,
2650 struct device_attribute *attr,
2651 const char *buf, size_t count)
2652 {
2653 unsigned long t;
2654
2655 printk_deprecated_attribute("hotkey_enable",
2656 "Hotkeys can be disabled through hotkey_mask");
2657
2658 if (parse_strtoul(buf, 1, &t))
2659 return -EINVAL;
2660
2661 if (t == 0)
2662 return -EPERM;
2663
2664 return count;
2665 }
2666
2667 static DEVICE_ATTR_RW(hotkey_enable);
2668
2669 /* sysfs hotkey mask --------------------------------------------------- */
hotkey_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2670 static ssize_t hotkey_mask_show(struct device *dev,
2671 struct device_attribute *attr,
2672 char *buf)
2673 {
2674 return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
2675 }
2676
hotkey_mask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2677 static ssize_t hotkey_mask_store(struct device *dev,
2678 struct device_attribute *attr,
2679 const char *buf, size_t count)
2680 {
2681 unsigned long t;
2682 int res;
2683
2684 if (parse_strtoul(buf, 0xffffffffUL, &t))
2685 return -EINVAL;
2686
2687 if (mutex_lock_killable(&hotkey_mutex))
2688 return -ERESTARTSYS;
2689
2690 res = hotkey_user_mask_set(t);
2691
2692 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2693 hotkey_poll_setup(true);
2694 #endif
2695
2696 mutex_unlock(&hotkey_mutex);
2697
2698 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2699
2700 return (res) ? res : count;
2701 }
2702
2703 static DEVICE_ATTR_RW(hotkey_mask);
2704
2705 /* sysfs hotkey bios_enabled ------------------------------------------- */
hotkey_bios_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)2706 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2707 struct device_attribute *attr,
2708 char *buf)
2709 {
2710 return sysfs_emit(buf, "0\n");
2711 }
2712
2713 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2714
2715 /* sysfs hotkey bios_mask ---------------------------------------------- */
hotkey_bios_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2716 static ssize_t hotkey_bios_mask_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2719 {
2720 printk_deprecated_attribute("hotkey_bios_mask",
2721 "This attribute is useless.");
2722 return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
2723 }
2724
2725 static DEVICE_ATTR_RO(hotkey_bios_mask);
2726
2727 /* sysfs hotkey all_mask ----------------------------------------------- */
hotkey_all_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2728 static ssize_t hotkey_all_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2731 {
2732 return sysfs_emit(buf, "0x%08x\n",
2733 hotkey_all_mask | hotkey_source_mask);
2734 }
2735
2736 static DEVICE_ATTR_RO(hotkey_all_mask);
2737
2738 /* sysfs hotkey all_mask ----------------------------------------------- */
hotkey_adaptive_all_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2739 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2742 {
2743 return sysfs_emit(buf, "0x%08x\n",
2744 hotkey_adaptive_all_mask | hotkey_source_mask);
2745 }
2746
2747 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2748
2749 /* sysfs hotkey recommended_mask --------------------------------------- */
hotkey_recommended_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2750 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2751 struct device_attribute *attr,
2752 char *buf)
2753 {
2754 return sysfs_emit(buf, "0x%08x\n",
2755 (hotkey_all_mask | hotkey_source_mask)
2756 & ~hotkey_reserved_mask);
2757 }
2758
2759 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2760
2761 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2762
2763 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
hotkey_source_mask_show(struct device * dev,struct device_attribute * attr,char * buf)2764 static ssize_t hotkey_source_mask_show(struct device *dev,
2765 struct device_attribute *attr,
2766 char *buf)
2767 {
2768 return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
2769 }
2770
hotkey_source_mask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2771 static ssize_t hotkey_source_mask_store(struct device *dev,
2772 struct device_attribute *attr,
2773 const char *buf, size_t count)
2774 {
2775 unsigned long t;
2776 u32 r_ev;
2777 int rc;
2778
2779 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2780 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2781 return -EINVAL;
2782
2783 if (mutex_lock_killable(&hotkey_mutex))
2784 return -ERESTARTSYS;
2785
2786 HOTKEY_CONFIG_CRITICAL_START
2787 hotkey_source_mask = t;
2788 HOTKEY_CONFIG_CRITICAL_END
2789
2790 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2791 ~hotkey_source_mask);
2792 hotkey_poll_setup(true);
2793
2794 /* check if events needed by the driver got disabled */
2795 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2796 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2797
2798 mutex_unlock(&hotkey_mutex);
2799
2800 if (rc < 0)
2801 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2802
2803 if (r_ev)
2804 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2805 r_ev);
2806
2807 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2808
2809 return (rc < 0) ? rc : count;
2810 }
2811
2812 static DEVICE_ATTR_RW(hotkey_source_mask);
2813
2814 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
hotkey_poll_freq_show(struct device * dev,struct device_attribute * attr,char * buf)2815 static ssize_t hotkey_poll_freq_show(struct device *dev,
2816 struct device_attribute *attr,
2817 char *buf)
2818 {
2819 return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
2820 }
2821
hotkey_poll_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2822 static ssize_t hotkey_poll_freq_store(struct device *dev,
2823 struct device_attribute *attr,
2824 const char *buf, size_t count)
2825 {
2826 unsigned long t;
2827
2828 if (parse_strtoul(buf, 25, &t))
2829 return -EINVAL;
2830
2831 if (mutex_lock_killable(&hotkey_mutex))
2832 return -ERESTARTSYS;
2833
2834 hotkey_poll_set_freq(t);
2835 hotkey_poll_setup(true);
2836
2837 mutex_unlock(&hotkey_mutex);
2838
2839 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2840
2841 return count;
2842 }
2843
2844 static DEVICE_ATTR_RW(hotkey_poll_freq);
2845
2846 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2847
2848 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
hotkey_radio_sw_show(struct device * dev,struct device_attribute * attr,char * buf)2849 static ssize_t hotkey_radio_sw_show(struct device *dev,
2850 struct device_attribute *attr,
2851 char *buf)
2852 {
2853 int res;
2854 res = hotkey_get_wlsw();
2855 if (res < 0)
2856 return res;
2857
2858 /* Opportunistic update */
2859 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2860
2861 return sysfs_emit(buf, "%d\n",
2862 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2863 }
2864
2865 static DEVICE_ATTR_RO(hotkey_radio_sw);
2866
hotkey_radio_sw_notify_change(void)2867 static void hotkey_radio_sw_notify_change(void)
2868 {
2869 if (tp_features.hotkey_wlsw)
2870 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2871 "hotkey_radio_sw");
2872 }
2873
2874 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
hotkey_tablet_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2875 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2876 struct device_attribute *attr,
2877 char *buf)
2878 {
2879 int res, s;
2880 res = hotkey_get_tablet_mode(&s);
2881 if (res < 0)
2882 return res;
2883
2884 return sysfs_emit(buf, "%d\n", !!s);
2885 }
2886
2887 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2888
hotkey_tablet_mode_notify_change(void)2889 static void hotkey_tablet_mode_notify_change(void)
2890 {
2891 if (tp_features.hotkey_tablet)
2892 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2893 "hotkey_tablet_mode");
2894 }
2895
2896 /* sysfs wakeup reason (pollable) -------------------------------------- */
hotkey_wakeup_reason_show(struct device * dev,struct device_attribute * attr,char * buf)2897 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2898 struct device_attribute *attr,
2899 char *buf)
2900 {
2901 return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
2902 }
2903
2904 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2905
hotkey_wakeup_reason_notify_change(void)2906 static void hotkey_wakeup_reason_notify_change(void)
2907 {
2908 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2909 "wakeup_reason");
2910 }
2911
2912 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
hotkey_wakeup_hotunplug_complete_show(struct device * dev,struct device_attribute * attr,char * buf)2913 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2914 struct device_attribute *attr,
2915 char *buf)
2916 {
2917 return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
2918 }
2919
2920 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2921 hotkey_wakeup_hotunplug_complete_show, NULL);
2922
hotkey_wakeup_hotunplug_complete_notify_change(void)2923 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2924 {
2925 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2926 "wakeup_hotunplug_complete");
2927 }
2928
2929 /* sysfs adaptive kbd mode --------------------------------------------- */
2930
2931 static int adaptive_keyboard_get_mode(void);
2932 static int adaptive_keyboard_set_mode(int new_mode);
2933
2934 enum ADAPTIVE_KEY_MODE {
2935 HOME_MODE,
2936 WEB_BROWSER_MODE,
2937 WEB_CONFERENCE_MODE,
2938 FUNCTION_MODE,
2939 LAYFLAT_MODE
2940 };
2941
adaptive_kbd_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2942 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2943 struct device_attribute *attr,
2944 char *buf)
2945 {
2946 int current_mode;
2947
2948 current_mode = adaptive_keyboard_get_mode();
2949 if (current_mode < 0)
2950 return current_mode;
2951
2952 return sysfs_emit(buf, "%d\n", current_mode);
2953 }
2954
adaptive_kbd_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2955 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2956 struct device_attribute *attr,
2957 const char *buf, size_t count)
2958 {
2959 unsigned long t;
2960 int res;
2961
2962 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2963 return -EINVAL;
2964
2965 res = adaptive_keyboard_set_mode(t);
2966 return (res < 0) ? res : count;
2967 }
2968
2969 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2970
2971 static struct attribute *adaptive_kbd_attributes[] = {
2972 &dev_attr_adaptive_kbd_mode.attr,
2973 NULL
2974 };
2975
hadaptive_kbd_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)2976 static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
2977 struct attribute *attr, int n)
2978 {
2979 return tp_features.has_adaptive_kbd ? attr->mode : 0;
2980 }
2981
2982 static const struct attribute_group adaptive_kbd_attr_group = {
2983 .is_visible = hadaptive_kbd_attr_is_visible,
2984 .attrs = adaptive_kbd_attributes,
2985 };
2986
2987 /* sysfs doubletap enable --------------------------------------------- */
doubletap_enable_show(struct device * dev,struct device_attribute * attr,char * buf)2988 static ssize_t doubletap_enable_show(struct device *dev,
2989 struct device_attribute *attr,
2990 char *buf)
2991 {
2992 return sysfs_emit(buf, "%d\n", tp_features.trackpoint_doubletap_enable);
2993 }
2994
doubletap_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2995 static ssize_t doubletap_enable_store(struct device *dev,
2996 struct device_attribute *attr,
2997 const char *buf, size_t count)
2998 {
2999 bool enable;
3000 int err;
3001
3002 err = kstrtobool(buf, &enable);
3003 if (err)
3004 return err;
3005
3006 tp_features.trackpoint_doubletap_enable = enable;
3007 return count;
3008 }
3009
3010 static DEVICE_ATTR_RW(doubletap_enable);
3011
3012 /* --------------------------------------------------------------------- */
3013
3014 static struct attribute *hotkey_attributes[] = {
3015 &dev_attr_hotkey_enable.attr,
3016 &dev_attr_hotkey_bios_enabled.attr,
3017 &dev_attr_hotkey_bios_mask.attr,
3018 &dev_attr_wakeup_reason.attr,
3019 &dev_attr_wakeup_hotunplug_complete.attr,
3020 &dev_attr_hotkey_mask.attr,
3021 &dev_attr_hotkey_all_mask.attr,
3022 &dev_attr_hotkey_adaptive_all_mask.attr,
3023 &dev_attr_hotkey_recommended_mask.attr,
3024 &dev_attr_hotkey_tablet_mode.attr,
3025 &dev_attr_hotkey_radio_sw.attr,
3026 &dev_attr_doubletap_enable.attr,
3027 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3028 &dev_attr_hotkey_source_mask.attr,
3029 &dev_attr_hotkey_poll_freq.attr,
3030 #endif
3031 NULL
3032 };
3033
hotkey_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)3034 static umode_t hotkey_attr_is_visible(struct kobject *kobj,
3035 struct attribute *attr, int n)
3036 {
3037 if (attr == &dev_attr_hotkey_tablet_mode.attr) {
3038 if (!tp_features.hotkey_tablet)
3039 return 0;
3040 } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
3041 if (!tp_features.hotkey_wlsw)
3042 return 0;
3043 }
3044
3045 return attr->mode;
3046 }
3047
3048 static const struct attribute_group hotkey_attr_group = {
3049 .is_visible = hotkey_attr_is_visible,
3050 .attrs = hotkey_attributes,
3051 };
3052
3053 /*
3054 * Sync both the hw and sw blocking state of all switches
3055 */
tpacpi_send_radiosw_update(void)3056 static void tpacpi_send_radiosw_update(void)
3057 {
3058 int wlsw;
3059
3060 /*
3061 * We must sync all rfkill controllers *before* issuing any
3062 * rfkill input events, or we will race the rfkill core input
3063 * handler.
3064 *
3065 * tpacpi_inputdev_send_mutex works as a synchronization point
3066 * for the above.
3067 *
3068 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3069 */
3070
3071 wlsw = hotkey_get_wlsw();
3072
3073 /* Sync hw blocking state first if it is hw-blocked */
3074 if (wlsw == TPACPI_RFK_RADIO_OFF)
3075 tpacpi_rfk_update_hwblock_state(true);
3076
3077 /* Sync hw blocking state last if it is hw-unblocked */
3078 if (wlsw == TPACPI_RFK_RADIO_ON)
3079 tpacpi_rfk_update_hwblock_state(false);
3080
3081 /* Issue rfkill input event for WLSW switch */
3082 if (!(wlsw < 0)) {
3083 mutex_lock(&tpacpi_inputdev_send_mutex);
3084
3085 input_report_switch(tpacpi_inputdev,
3086 SW_RFKILL_ALL, (wlsw > 0));
3087 input_sync(tpacpi_inputdev);
3088
3089 mutex_unlock(&tpacpi_inputdev_send_mutex);
3090 }
3091
3092 /*
3093 * this can be unconditional, as we will poll state again
3094 * if userspace uses the notify to read data
3095 */
3096 hotkey_radio_sw_notify_change();
3097 }
3098
hotkey_exit(void)3099 static void hotkey_exit(void)
3100 {
3101 mutex_lock(&hotkey_mutex);
3102 hotkey_poll_stop_sync();
3103 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3104 "restoring original HKEY status and mask\n");
3105 /* yes, there is a bitwise or below, we want the
3106 * functions to be called even if one of them fail */
3107 if (((tp_features.hotkey_mask &&
3108 hotkey_mask_set(hotkey_orig_mask)) |
3109 hotkey_status_set(false)) != 0)
3110 pr_err("failed to restore hot key mask to BIOS defaults\n");
3111
3112 mutex_unlock(&hotkey_mutex);
3113 }
3114
3115 /*
3116 * HKEY quirks:
3117 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3118 */
3119
3120 #define TPACPI_HK_Q_INIMASK 0x0001
3121
3122 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3123 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3124 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3125 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3126 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3127 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3128 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3129 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3130 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3131 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3132 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3133 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3134 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3135 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3136 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3137 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3138 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3139 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3140 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3141 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3142 };
3143
hotkey_init_tablet_mode(void)3144 static int hotkey_init_tablet_mode(void)
3145 {
3146 int in_tablet_mode = 0, res;
3147 char *type = NULL;
3148
3149 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3150 int has_tablet_mode;
3151
3152 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3153 &has_tablet_mode);
3154 /*
3155 * The Yoga 11e series has 2 accelerometers described by a
3156 * BOSC0200 ACPI node. This setup relies on a Windows service
3157 * which calls special ACPI methods on this node to report
3158 * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
3159 * does not support this, so skip the hotkey on these models.
3160 */
3161 if (has_tablet_mode && !dual_accel_detect())
3162 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3163 type = "GMMS";
3164 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3165 /* For X41t, X60t, X61t Tablets... */
3166 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3167 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3168 type = "MHKG";
3169 }
3170
3171 if (!tp_features.hotkey_tablet)
3172 return 0;
3173
3174 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3175 type, in_tablet_mode ? "tablet" : "laptop");
3176
3177 return in_tablet_mode;
3178 }
3179
3180 static const struct key_entry keymap_ibm[] __initconst = {
3181 /* Original hotkey mappings translated scancodes 0x00 - 0x1f */
3182 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF1, { KEY_FN_F1 } },
3183 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF2, { KEY_BATTERY } },
3184 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF3, { KEY_COFFEE } },
3185 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF4, { KEY_SLEEP } },
3186 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF5, { KEY_WLAN } },
3187 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF6, { KEY_FN_F6 } },
3188 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF7, { KEY_SWITCHVIDEOMODE } },
3189 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF8, { KEY_FN_F8 } },
3190 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF9, { KEY_FN_F9 } },
3191 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF10, { KEY_FN_F10 } },
3192 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF11, { KEY_FN_F11 } },
3193 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF12, { KEY_SUSPEND } },
3194 /* Brightness: firmware always reacts, suppressed through hotkey_reserved_mask. */
3195 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNHOME, { KEY_BRIGHTNESSUP } },
3196 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNEND, { KEY_BRIGHTNESSDOWN } },
3197 /* Thinklight: firmware always reacts, suppressed through hotkey_reserved_mask. */
3198 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNPAGEUP, { KEY_KBDILLUMTOGGLE } },
3199 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNSPACE, { KEY_ZOOM } },
3200 /*
3201 * Volume: firmware always reacts and reprograms the built-in *extra* mixer.
3202 * Suppressed by default through hotkey_reserved_mask.
3203 */
3204 { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEUP, { KEY_VOLUMEUP } },
3205 { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, { KEY_VOLUMEDOWN } },
3206 { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE, { KEY_MUTE } },
3207 { KE_KEY, TP_ACPI_HOTKEYSCAN_THINKPAD, { KEY_VENDOR } },
3208 { KE_END }
3209 };
3210
3211 static const struct key_entry keymap_lenovo[] __initconst = {
3212 /* Original hotkey mappings translated scancodes 0x00 - 0x1f */
3213 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF1, { KEY_FN_F1 } },
3214 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF2, { KEY_COFFEE } },
3215 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF3, { KEY_BATTERY } },
3216 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF4, { KEY_SLEEP } },
3217 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF5, { KEY_WLAN } },
3218 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF6, { KEY_CAMERA, } },
3219 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF7, { KEY_SWITCHVIDEOMODE } },
3220 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF8, { KEY_FN_F8 } },
3221 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF9, { KEY_FN_F9 } },
3222 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF10, { KEY_FN_F10 } },
3223 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF11, { KEY_FN_F11 } },
3224 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF12, { KEY_SUSPEND } },
3225 /*
3226 * These should be enabled --only-- when ACPI video is disabled and
3227 * are handled in a special way by the init code.
3228 */
3229 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNHOME, { KEY_BRIGHTNESSUP } },
3230 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNEND, { KEY_BRIGHTNESSDOWN } },
3231 /* Suppressed by default through hotkey_reserved_mask. */
3232 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNPAGEUP, { KEY_KBDILLUMTOGGLE } },
3233 { KE_KEY, TP_ACPI_HOTKEYSCAN_FNSPACE, { KEY_ZOOM } },
3234 /*
3235 * Volume: z60/z61, T60 (BIOS version?): firmware always reacts and
3236 * reprograms the built-in *extra* mixer.
3237 * T60?, T61, R60?, R61: firmware and EC tries to send these over
3238 * the regular keyboard (not through tpacpi). There are still weird bugs
3239 * re. MUTE. May cause the BIOS to interfere with the HDA mixer.
3240 * Suppressed by default through hotkey_reserved_mask.
3241 */
3242 { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEUP, { KEY_VOLUMEUP } },
3243 { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, { KEY_VOLUMEDOWN } },
3244 { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE, { KEY_MUTE } },
3245 { KE_KEY, TP_ACPI_HOTKEYSCAN_THINKPAD, { KEY_VENDOR } },
3246 { KE_KEY, TP_ACPI_HOTKEYSCAN_MICMUTE, { KEY_MICMUTE } },
3247 { KE_KEY, TP_ACPI_HOTKEYSCAN_CONFIG, { KEY_CONFIG } },
3248 { KE_KEY, TP_ACPI_HOTKEYSCAN_SEARCH, { KEY_SEARCH } },
3249 { KE_KEY, TP_ACPI_HOTKEYSCAN_SCALE, { KEY_SCALE } },
3250 { KE_KEY, TP_ACPI_HOTKEYSCAN_FILE, { KEY_FILE } },
3251 /* Adaptive keyboard mappings for Carbon X1 2014 translated scancodes 0x20 - 0x33 */
3252 { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE2, { KEY_RESERVED } },
3253 { KE_KEY, TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO, { KEY_BRIGHTNESS_MIN } },
3254 { KE_KEY, TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL, { KEY_SELECTIVE_SCREENSHOT } },
3255 { KE_KEY, TP_ACPI_HOTKEYSCAN_CLOUD, { KEY_XFER } },
3256 { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK9, { KEY_RESERVED } },
3257 { KE_KEY, TP_ACPI_HOTKEYSCAN_VOICE, { KEY_VOICECOMMAND } },
3258 { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK10, { KEY_RESERVED } },
3259 { KE_KEY, TP_ACPI_HOTKEYSCAN_GESTURES, { KEY_RESERVED } },
3260 { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK11, { KEY_RESERVED } },
3261 { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK12, { KEY_RESERVED } },
3262 { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK13, { KEY_RESERVED } },
3263 { KE_KEY, TP_ACPI_HOTKEYSCAN_CONFIG2, { KEY_CONFIG } },
3264 { KE_KEY, TP_ACPI_HOTKEYSCAN_NEW_TAB, { KEY_RESERVED } },
3265 { KE_KEY, TP_ACPI_HOTKEYSCAN_RELOAD, { KEY_REFRESH } },
3266 { KE_KEY, TP_ACPI_HOTKEYSCAN_BACK, { KEY_BACK } },
3267 { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_DOWN, { KEY_RESERVED } },
3268 { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_UP, { KEY_RESERVED } },
3269 { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION, { KEY_RESERVED } },
3270 { KE_KEY, TP_ACPI_HOTKEYSCAN_CAMERA_MODE, { KEY_RESERVED } },
3271 { KE_KEY, TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY, { KEY_RESERVED } },
3272 /* Extended hotkeys mappings translated scancodes 0x34 - 0x4d */
3273 { KE_KEY, TP_ACPI_HOTKEYSCAN_STAR, { KEY_BOOKMARKS } },
3274 { KE_KEY, TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2, { KEY_SELECTIVE_SCREENSHOT } },
3275 { KE_KEY, TP_ACPI_HOTKEYSCAN_CALCULATOR, { KEY_CALC } },
3276 { KE_KEY, TP_ACPI_HOTKEYSCAN_BLUETOOTH, { KEY_BLUETOOTH } },
3277 { KE_KEY, TP_ACPI_HOTKEYSCAN_KEYBOARD, { KEY_KEYBOARD } },
3278 /* Used by "Lenovo Quick Clean" */
3279 { KE_KEY, TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, { KEY_FN_RIGHT_SHIFT } },
3280 { KE_KEY, TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER, { KEY_NOTIFICATION_CENTER } },
3281 { KE_KEY, TP_ACPI_HOTKEYSCAN_PICKUP_PHONE, { KEY_PICKUP_PHONE } },
3282 { KE_KEY, TP_ACPI_HOTKEYSCAN_HANGUP_PHONE, { KEY_HANGUP_PHONE } },
3283 /*
3284 * All mapping below are for raw untranslated hkey event codes mapped directly
3285 * after switching to sparse keymap support. The mappings above use translated
3286 * scancodes to preserve uAPI compatibility, see tpacpi_input_send_key().
3287 */
3288 { KE_KEY, 0x131d, { KEY_VENDOR } }, /* System debug info, similar to old ThinkPad key */
3289 { KE_KEY, 0x1320, { KEY_LINK_PHONE } },
3290 { KE_KEY, 0x1402, { KEY_LINK_PHONE } },
3291 { KE_KEY, TP_HKEY_EV_TRACK_DOUBLETAP /* 0x8036 */, { KEY_PROG4 } },
3292 { KE_END }
3293 };
3294
hotkey_init(struct ibm_init_struct * iibm)3295 static int __init hotkey_init(struct ibm_init_struct *iibm)
3296 {
3297 enum keymap_index {
3298 TPACPI_KEYMAP_IBM_GENERIC = 0,
3299 TPACPI_KEYMAP_LENOVO_GENERIC,
3300 };
3301
3302 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3303 /* Generic maps (fallback) */
3304 {
3305 .vendor = PCI_VENDOR_ID_IBM,
3306 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3307 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3308 },
3309 {
3310 .vendor = PCI_VENDOR_ID_LENOVO,
3311 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3312 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3313 },
3314 };
3315
3316 unsigned long keymap_id, quirks;
3317 const struct key_entry *keymap;
3318 bool radiosw_state = false;
3319 bool tabletsw_state = false;
3320 int hkeyv, res, status, camera_shutter_state;
3321
3322 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3323 "initializing hotkey subdriver\n");
3324
3325 BUG_ON(!tpacpi_inputdev);
3326 BUG_ON(tpacpi_inputdev->open != NULL ||
3327 tpacpi_inputdev->close != NULL);
3328
3329 TPACPI_ACPIHANDLE_INIT(hkey);
3330 mutex_init(&hotkey_mutex);
3331
3332 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3333 mutex_init(&hotkey_thread_data_mutex);
3334 #endif
3335
3336 /* hotkey not supported on 570 */
3337 tp_features.hotkey = hkey_handle != NULL;
3338
3339 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3340 "hotkeys are %s\n",
3341 str_supported(tp_features.hotkey));
3342
3343 if (!tp_features.hotkey)
3344 return -ENODEV;
3345
3346 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3347 ARRAY_SIZE(tpacpi_hotkey_qtable));
3348
3349 tpacpi_disable_brightness_delay();
3350
3351 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3352 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3353 for HKEY interface version 0x100 */
3354 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3355 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3356 "firmware HKEY interface version: 0x%x\n",
3357 hkeyv);
3358
3359 switch (hkeyv >> 8) {
3360 case 1:
3361 /*
3362 * MHKV 0x100 in A31, R40, R40e,
3363 * T4x, X31, and later
3364 */
3365
3366 /* Paranoia check AND init hotkey_all_mask */
3367 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3368 "MHKA", "qd")) {
3369 pr_err("missing MHKA handler, please report this to %s\n",
3370 TPACPI_MAIL);
3371 /* Fallback: pre-init for FN+F3,F4,F12 */
3372 hotkey_all_mask = 0x080cU;
3373 } else {
3374 tp_features.hotkey_mask = 1;
3375 }
3376 break;
3377
3378 case 2:
3379 /*
3380 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3381 */
3382
3383 /* Paranoia check AND init hotkey_all_mask */
3384 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3385 "MHKA", "dd", 1)) {
3386 pr_err("missing MHKA handler, please report this to %s\n",
3387 TPACPI_MAIL);
3388 /* Fallback: pre-init for FN+F3,F4,F12 */
3389 hotkey_all_mask = 0x080cU;
3390 } else {
3391 tp_features.hotkey_mask = 1;
3392 }
3393
3394 /*
3395 * Check if we have an adaptive keyboard, like on the
3396 * Lenovo Carbon X1 2014 (2nd Gen).
3397 */
3398 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3399 "MHKA", "dd", 2)) {
3400 if (hotkey_adaptive_all_mask != 0)
3401 tp_features.has_adaptive_kbd = true;
3402 } else {
3403 tp_features.has_adaptive_kbd = false;
3404 hotkey_adaptive_all_mask = 0x0U;
3405 }
3406 break;
3407
3408 default:
3409 pr_err("unknown version of the HKEY interface: 0x%x\n",
3410 hkeyv);
3411 pr_err("please report this to %s\n", TPACPI_MAIL);
3412 break;
3413 }
3414 }
3415
3416 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3417 "hotkey masks are %s\n",
3418 str_supported(tp_features.hotkey_mask));
3419
3420 /* Init hotkey_all_mask if not initialized yet */
3421 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3422 (quirks & TPACPI_HK_Q_INIMASK))
3423 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3424
3425 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3426 if (tp_features.hotkey_mask) {
3427 /* hotkey_source_mask *must* be zero for
3428 * the first hotkey_mask_get to return hotkey_orig_mask */
3429 mutex_lock(&hotkey_mutex);
3430 res = hotkey_mask_get();
3431 mutex_unlock(&hotkey_mutex);
3432 if (res)
3433 return res;
3434
3435 hotkey_orig_mask = hotkey_acpi_mask;
3436 } else {
3437 hotkey_orig_mask = hotkey_all_mask;
3438 hotkey_acpi_mask = hotkey_all_mask;
3439 }
3440
3441 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3442 if (dbg_wlswemul) {
3443 tp_features.hotkey_wlsw = 1;
3444 radiosw_state = !!tpacpi_wlsw_emulstate;
3445 pr_info("radio switch emulation enabled\n");
3446 } else
3447 #endif
3448 /* Not all thinkpads have a hardware radio switch */
3449 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3450 tp_features.hotkey_wlsw = 1;
3451 radiosw_state = !!status;
3452 pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
3453 }
3454
3455 tabletsw_state = hotkey_init_tablet_mode();
3456
3457 /* Set up key map */
3458 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3459 ARRAY_SIZE(tpacpi_keymap_qtable));
3460 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3461 "using keymap number %lu\n", keymap_id);
3462
3463 /* Keys which should be reserved on both IBM and Lenovo models */
3464 hotkey_reserved_mask = TP_ACPI_HKEY_KBD_LIGHT_MASK |
3465 TP_ACPI_HKEY_VOLUP_MASK |
3466 TP_ACPI_HKEY_VOLDWN_MASK |
3467 TP_ACPI_HKEY_MUTE_MASK;
3468 /*
3469 * Reserve brightness up/down unconditionally on IBM models, on Lenovo
3470 * models these are disabled based on acpi_video_get_backlight_type().
3471 */
3472 if (keymap_id == TPACPI_KEYMAP_IBM_GENERIC) {
3473 hotkey_reserved_mask |= TP_ACPI_HKEY_BRGHTUP_MASK |
3474 TP_ACPI_HKEY_BRGHTDWN_MASK;
3475 keymap = keymap_ibm;
3476 } else {
3477 keymap = keymap_lenovo;
3478 }
3479
3480 res = sparse_keymap_setup(tpacpi_inputdev, keymap, NULL);
3481 if (res)
3482 return res;
3483
3484 camera_shutter_state = get_camera_shutter();
3485 if (camera_shutter_state >= 0) {
3486 input_set_capability(tpacpi_inputdev, EV_SW, SW_CAMERA_LENS_COVER);
3487 input_report_switch(tpacpi_inputdev, SW_CAMERA_LENS_COVER, camera_shutter_state);
3488 }
3489
3490 if (tp_features.hotkey_wlsw) {
3491 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3492 input_report_switch(tpacpi_inputdev,
3493 SW_RFKILL_ALL, radiosw_state);
3494 }
3495 if (tp_features.hotkey_tablet) {
3496 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3497 input_report_switch(tpacpi_inputdev,
3498 SW_TABLET_MODE, tabletsw_state);
3499 }
3500
3501 /* Do not issue duplicate brightness change events to
3502 * userspace. tpacpi_detect_brightness_capabilities() must have
3503 * been called before this point */
3504 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3505 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3506 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3507
3508 /* Disable brightness up/down on Lenovo thinkpads when
3509 * ACPI is handling them, otherwise it is plain impossible
3510 * for userspace to do something even remotely sane */
3511 hotkey_reserved_mask |= TP_ACPI_HKEY_BRGHTUP_MASK |
3512 TP_ACPI_HKEY_BRGHTDWN_MASK;
3513 }
3514
3515 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3516 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3517 & ~hotkey_all_mask
3518 & ~hotkey_reserved_mask;
3519
3520 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3521 "hotkey source mask 0x%08x, polling freq %u\n",
3522 hotkey_source_mask, hotkey_poll_freq);
3523 #endif
3524
3525 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3526 "enabling firmware HKEY event interface...\n");
3527 res = hotkey_status_set(true);
3528 if (res) {
3529 hotkey_exit();
3530 return res;
3531 }
3532 mutex_lock(&hotkey_mutex);
3533 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3534 | hotkey_driver_mask)
3535 & ~hotkey_source_mask);
3536 mutex_unlock(&hotkey_mutex);
3537 if (res < 0 && res != -ENXIO) {
3538 hotkey_exit();
3539 return res;
3540 }
3541 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3542 & ~hotkey_reserved_mask;
3543 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3544 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3545 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3546
3547 tpacpi_inputdev->open = &hotkey_inputdev_open;
3548 tpacpi_inputdev->close = &hotkey_inputdev_close;
3549
3550 hotkey_poll_setup_safe(true);
3551
3552 /* Enable TrackPoint doubletap event reporting by default. */
3553 tp_features.trackpoint_doubletap_enable = 1;
3554
3555 return 0;
3556 }
3557
3558 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3559 * mode, Web conference mode, Function mode and Lay-flat mode.
3560 * We support Home mode and Function mode currently.
3561 *
3562 * Will consider support rest of modes in future.
3563 *
3564 */
3565 static const int adaptive_keyboard_modes[] = {
3566 HOME_MODE,
3567 /* WEB_BROWSER_MODE = 2,
3568 WEB_CONFERENCE_MODE = 3, */
3569 FUNCTION_MODE
3570 };
3571
3572 /* press Fn key a while second, it will switch to Function Mode. Then
3573 * release Fn key, previous mode be restored.
3574 */
3575 static bool adaptive_keyboard_mode_is_saved;
3576 static int adaptive_keyboard_prev_mode;
3577
adaptive_keyboard_get_mode(void)3578 static int adaptive_keyboard_get_mode(void)
3579 {
3580 int mode = 0;
3581
3582 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3583 pr_err("Cannot read adaptive keyboard mode\n");
3584 return -EIO;
3585 }
3586
3587 return mode;
3588 }
3589
adaptive_keyboard_set_mode(int new_mode)3590 static int adaptive_keyboard_set_mode(int new_mode)
3591 {
3592 if (new_mode < 0 ||
3593 new_mode > LAYFLAT_MODE)
3594 return -EINVAL;
3595
3596 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3597 pr_err("Cannot set adaptive keyboard mode\n");
3598 return -EIO;
3599 }
3600
3601 return 0;
3602 }
3603
adaptive_keyboard_get_next_mode(int mode)3604 static int adaptive_keyboard_get_next_mode(int mode)
3605 {
3606 size_t i;
3607 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3608
3609 for (i = 0; i <= max_mode; i++) {
3610 if (adaptive_keyboard_modes[i] == mode)
3611 break;
3612 }
3613
3614 if (i >= max_mode)
3615 i = 0;
3616 else
3617 i++;
3618
3619 return adaptive_keyboard_modes[i];
3620 }
3621
adaptive_keyboard_change_row(void)3622 static void adaptive_keyboard_change_row(void)
3623 {
3624 int mode;
3625
3626 if (adaptive_keyboard_mode_is_saved) {
3627 mode = adaptive_keyboard_prev_mode;
3628 adaptive_keyboard_mode_is_saved = false;
3629 } else {
3630 mode = adaptive_keyboard_get_mode();
3631 if (mode < 0)
3632 return;
3633 mode = adaptive_keyboard_get_next_mode(mode);
3634 }
3635
3636 adaptive_keyboard_set_mode(mode);
3637 }
3638
adaptive_keyboard_s_quickview_row(void)3639 static void adaptive_keyboard_s_quickview_row(void)
3640 {
3641 int mode;
3642
3643 mode = adaptive_keyboard_get_mode();
3644 if (mode < 0)
3645 return;
3646
3647 adaptive_keyboard_prev_mode = mode;
3648 adaptive_keyboard_mode_is_saved = true;
3649
3650 adaptive_keyboard_set_mode(FUNCTION_MODE);
3651 }
3652
3653 /* 0x1000-0x1FFF: key presses */
hotkey_notify_hotkey(const u32 hkey,bool * send_acpi_ev)3654 static bool hotkey_notify_hotkey(const u32 hkey, bool *send_acpi_ev)
3655 {
3656 /* Never send ACPI netlink events for original hotkeys (hkey: 0x1001 - 0x1020) */
3657 if (hkey >= TP_HKEY_EV_ORIG_KEY_START && hkey <= TP_HKEY_EV_ORIG_KEY_END) {
3658 *send_acpi_ev = false;
3659
3660 /* Original hotkeys may be polled from NVRAM instead */
3661 unsigned int scancode = hkey - TP_HKEY_EV_ORIG_KEY_START;
3662 if (hotkey_source_mask & (1 << scancode))
3663 return true;
3664 }
3665
3666 return tpacpi_input_send_key(hkey, send_acpi_ev);
3667 }
3668
3669 /* 0x2000-0x2FFF: Wakeup reason */
hotkey_notify_wakeup(const u32 hkey,bool * send_acpi_ev)3670 static bool hotkey_notify_wakeup(const u32 hkey, bool *send_acpi_ev)
3671 {
3672 switch (hkey) {
3673 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3674 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3675 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3676 *send_acpi_ev = false;
3677 break;
3678
3679 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3680 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3681 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3682 *send_acpi_ev = false;
3683 break;
3684
3685 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3686 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3687 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3688 /* how to auto-heal: */
3689 /* 2313: woke up from S3, go to S4/S5 */
3690 /* 2413: woke up from S4, go to S5 */
3691 break;
3692
3693 default:
3694 return false;
3695 }
3696
3697 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3698 pr_info("woke up due to a hot-unplug request...\n");
3699 hotkey_wakeup_reason_notify_change();
3700 }
3701 return true;
3702 }
3703
3704 /* 0x4000-0x4FFF: dock-related events */
hotkey_notify_dockevent(const u32 hkey,bool * send_acpi_ev)3705 static bool hotkey_notify_dockevent(const u32 hkey, bool *send_acpi_ev)
3706 {
3707 switch (hkey) {
3708 case TP_HKEY_EV_UNDOCK_ACK:
3709 /* ACPI undock operation completed after wakeup */
3710 hotkey_autosleep_ack = 1;
3711 pr_info("undocked\n");
3712 hotkey_wakeup_hotunplug_complete_notify_change();
3713 return true;
3714
3715 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3716 pr_info("docked into hotplug port replicator\n");
3717 return true;
3718 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3719 pr_info("undocked from hotplug port replicator\n");
3720 return true;
3721
3722 /*
3723 * Deliberately ignore attaching and detaching the keybord cover to avoid
3724 * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
3725 * to userspace.
3726 *
3727 * Please refer to the following thread for more information and a preliminary
3728 * implementation using the GTOP ("Get Tablet OPtions") interface that could be
3729 * extended to other attachment options of the ThinkPad X1 Tablet series, such as
3730 * the Pico cartridge dock module:
3731 * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
3732 */
3733 case TP_HKEY_EV_KBD_COVER_ATTACH:
3734 case TP_HKEY_EV_KBD_COVER_DETACH:
3735 *send_acpi_ev = false;
3736 return true;
3737
3738 default:
3739 return false;
3740 }
3741 }
3742
3743 /* 0x5000-0x5FFF: human interface helpers */
hotkey_notify_usrevent(const u32 hkey,bool * send_acpi_ev)3744 static bool hotkey_notify_usrevent(const u32 hkey, bool *send_acpi_ev)
3745 {
3746 switch (hkey) {
3747 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3748 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3749 return true;
3750
3751 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3752 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3753 tpacpi_input_send_tabletsw();
3754 hotkey_tablet_mode_notify_change();
3755 *send_acpi_ev = false;
3756 return true;
3757
3758 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3759 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3760 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3761 /* do not propagate these events */
3762 *send_acpi_ev = false;
3763 return true;
3764
3765 default:
3766 return false;
3767 }
3768 }
3769
3770 static void thermal_dump_all_sensors(void);
3771 static void palmsensor_refresh(void);
3772
3773 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
hotkey_notify_6xxx(const u32 hkey,bool * send_acpi_ev)3774 static bool hotkey_notify_6xxx(const u32 hkey, bool *send_acpi_ev)
3775 {
3776 switch (hkey) {
3777 case TP_HKEY_EV_THM_TABLE_CHANGED:
3778 pr_debug("EC reports: Thermal Table has changed\n");
3779 /* recommended action: do nothing, we don't have
3780 * Lenovo ATM information */
3781 return true;
3782 case TP_HKEY_EV_THM_CSM_COMPLETED:
3783 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
3784 /* Thermal event - pass on to event handler */
3785 tpacpi_driver_event(hkey);
3786 return true;
3787 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
3788 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
3789 /* recommended action: do nothing, we don't have
3790 * Lenovo ATM information */
3791 return true;
3792 case TP_HKEY_EV_ALARM_BAT_HOT:
3793 pr_crit("THERMAL ALARM: battery is too hot!\n");
3794 /* recommended action: warn user through gui */
3795 break;
3796 case TP_HKEY_EV_ALARM_BAT_XHOT:
3797 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3798 /* recommended action: immediate sleep/hibernate */
3799 break;
3800 case TP_HKEY_EV_ALARM_BAT_LIM_CHANGE:
3801 pr_debug("Battery Info: battery charge threshold changed\n");
3802 /* User changed charging threshold. No action needed */
3803 return true;
3804 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3805 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3806 /* recommended action: warn user through gui, that */
3807 /* some internal component is too hot */
3808 break;
3809 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3810 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3811 /* recommended action: immediate sleep/hibernate */
3812 break;
3813 case TP_HKEY_EV_AC_CHANGED:
3814 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3815 * AC status changed; can be triggered by plugging or
3816 * unplugging AC adapter, docking or undocking. */
3817
3818 fallthrough;
3819
3820 case TP_HKEY_EV_KEY_NUMLOCK:
3821 case TP_HKEY_EV_KEY_FN:
3822 /* key press events, we just ignore them as long as the EC
3823 * is still reporting them in the normal keyboard stream */
3824 *send_acpi_ev = false;
3825 return true;
3826
3827 case TP_HKEY_EV_KEY_FN_ESC:
3828 /* Get the media key status to force the status LED to update */
3829 acpi_evalf(hkey_handle, NULL, "GMKS", "v");
3830 *send_acpi_ev = false;
3831 return true;
3832
3833 case TP_HKEY_EV_TABLET_CHANGED:
3834 tpacpi_input_send_tabletsw();
3835 hotkey_tablet_mode_notify_change();
3836 *send_acpi_ev = false;
3837 return true;
3838
3839 case TP_HKEY_EV_PALM_DETECTED:
3840 case TP_HKEY_EV_PALM_UNDETECTED:
3841 /* palm detected - pass on to event handler */
3842 palmsensor_refresh();
3843 return true;
3844
3845 default:
3846 /* report simply as unknown, no sensor dump */
3847 return false;
3848 }
3849
3850 thermal_dump_all_sensors();
3851 return true;
3852 }
3853
hotkey_notify_8xxx(const u32 hkey,bool * send_acpi_ev)3854 static bool hotkey_notify_8xxx(const u32 hkey, bool *send_acpi_ev)
3855 {
3856 switch (hkey) {
3857 case TP_HKEY_EV_TRACK_DOUBLETAP:
3858 /* Only send event if doubletap is enabled */
3859 if (!tp_features.trackpoint_doubletap_enable)
3860 *send_acpi_ev = false;
3861 return true;
3862 default:
3863 return false;
3864 }
3865 }
3866
hotkey_notify(struct ibm_struct * ibm,u32 event)3867 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3868 {
3869 u32 hkey;
3870 bool send_acpi_ev;
3871 bool known_ev;
3872
3873 if (event != 0x80) {
3874 pr_err("unknown HKEY notification event %d\n", event);
3875 /* forward it to userspace, maybe it knows how to handle it */
3876 acpi_bus_generate_netlink_event(
3877 ibm->device_class,
3878 dev_name(&ibm->acpi->device->dev),
3879 event, 0);
3880 return;
3881 }
3882
3883 while (1) {
3884 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3885 pr_err("failed to retrieve HKEY event\n");
3886 return;
3887 }
3888
3889 if (hkey == 0) {
3890 /* queue empty */
3891 return;
3892 }
3893
3894 send_acpi_ev = true;
3895 known_ev = false;
3896
3897 switch (hkey >> 12) {
3898 case 1:
3899 /* 0x1000-0x1FFF: key presses */
3900 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev);
3901 break;
3902 case 2:
3903 /* 0x2000-0x2FFF: Wakeup reason */
3904 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev);
3905 break;
3906 case 3:
3907 /* 0x3000-0x3FFF: bay-related wakeups */
3908 switch (hkey) {
3909 case TP_HKEY_EV_BAYEJ_ACK:
3910 hotkey_autosleep_ack = 1;
3911 pr_info("bay ejected\n");
3912 hotkey_wakeup_hotunplug_complete_notify_change();
3913 known_ev = true;
3914 break;
3915 case TP_HKEY_EV_OPTDRV_EJ:
3916 /* FIXME: kick libata if SATA link offline */
3917 known_ev = true;
3918 break;
3919 }
3920 break;
3921 case 4:
3922 /* 0x4000-0x4FFF: dock-related events */
3923 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev);
3924 break;
3925 case 5:
3926 /* 0x5000-0x5FFF: human interface helpers */
3927 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev);
3928 break;
3929 case 6:
3930 /* 0x6000-0x6FFF: thermal alarms/notices and
3931 * keyboard events */
3932 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev);
3933 break;
3934 case 7:
3935 /* 0x7000-0x7FFF: misc */
3936 if (tp_features.hotkey_wlsw &&
3937 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3938 tpacpi_send_radiosw_update();
3939 send_acpi_ev = false;
3940 known_ev = true;
3941 }
3942 break;
3943 case 8:
3944 /* 0x8000-0x8FFF: misc2 */
3945 known_ev = hotkey_notify_8xxx(hkey, &send_acpi_ev);
3946 break;
3947 }
3948 if (!known_ev) {
3949 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3950 pr_notice("please report the conditions when this event happened to %s\n",
3951 TPACPI_MAIL);
3952 }
3953
3954 /* netlink events */
3955 if (send_acpi_ev) {
3956 acpi_bus_generate_netlink_event(
3957 ibm->device_class,
3958 dev_name(&ibm->acpi->device->dev),
3959 event, hkey);
3960 }
3961 }
3962 }
3963
hotkey_suspend(void)3964 static void hotkey_suspend(void)
3965 {
3966 /* Do these on suspend, we get the events on early resume! */
3967 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3968 hotkey_autosleep_ack = 0;
3969
3970 /* save previous mode of adaptive keyboard of X1 Carbon */
3971 if (tp_features.has_adaptive_kbd) {
3972 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
3973 "GTRW", "dd", 0)) {
3974 pr_err("Cannot read adaptive keyboard mode.\n");
3975 }
3976 }
3977 }
3978
hotkey_resume(void)3979 static void hotkey_resume(void)
3980 {
3981 tpacpi_disable_brightness_delay();
3982
3983 mutex_lock(&hotkey_mutex);
3984 if (hotkey_status_set(true) < 0 ||
3985 hotkey_mask_set(hotkey_acpi_mask) < 0)
3986 pr_err("error while attempting to reset the event firmware interface\n");
3987 mutex_unlock(&hotkey_mutex);
3988
3989 tpacpi_send_radiosw_update();
3990 tpacpi_input_send_tabletsw();
3991 hotkey_tablet_mode_notify_change();
3992 hotkey_wakeup_reason_notify_change();
3993 hotkey_wakeup_hotunplug_complete_notify_change();
3994 hotkey_poll_setup_safe(false);
3995
3996 /* restore previous mode of adapive keyboard of X1 Carbon */
3997 if (tp_features.has_adaptive_kbd) {
3998 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
3999 adaptive_keyboard_prev_mode)) {
4000 pr_err("Cannot set adaptive keyboard mode.\n");
4001 }
4002 }
4003 }
4004
4005 /* procfs -------------------------------------------------------------- */
hotkey_read(struct seq_file * m)4006 static int hotkey_read(struct seq_file *m)
4007 {
4008 int res, status;
4009
4010 if (!tp_features.hotkey) {
4011 seq_puts(m, "status:\t\tnot supported\n");
4012 return 0;
4013 }
4014
4015 if (mutex_lock_killable(&hotkey_mutex))
4016 return -ERESTARTSYS;
4017 res = hotkey_status_get(&status);
4018 if (!res)
4019 res = hotkey_mask_get();
4020 mutex_unlock(&hotkey_mutex);
4021 if (res)
4022 return res;
4023
4024 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
4025 if (hotkey_all_mask) {
4026 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4027 seq_puts(m, "commands:\tenable, disable, reset, <mask>\n");
4028 } else {
4029 seq_puts(m, "mask:\t\tnot supported\n");
4030 seq_puts(m, "commands:\tenable, disable, reset\n");
4031 }
4032
4033 return 0;
4034 }
4035
hotkey_enabledisable_warn(bool enable)4036 static void hotkey_enabledisable_warn(bool enable)
4037 {
4038 tpacpi_log_usertask("procfs hotkey enable/disable");
4039 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4040 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4041 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4042 }
4043
hotkey_write(char * buf)4044 static int hotkey_write(char *buf)
4045 {
4046 int res;
4047 u32 mask;
4048 char *cmd;
4049
4050 if (!tp_features.hotkey)
4051 return -ENODEV;
4052
4053 if (mutex_lock_killable(&hotkey_mutex))
4054 return -ERESTARTSYS;
4055
4056 mask = hotkey_user_mask;
4057
4058 res = 0;
4059 while ((cmd = strsep(&buf, ","))) {
4060 if (strstarts(cmd, "enable")) {
4061 hotkey_enabledisable_warn(1);
4062 } else if (strstarts(cmd, "disable")) {
4063 hotkey_enabledisable_warn(0);
4064 res = -EPERM;
4065 } else if (strstarts(cmd, "reset")) {
4066 mask = (hotkey_all_mask | hotkey_source_mask)
4067 & ~hotkey_reserved_mask;
4068 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4069 /* mask set */
4070 } else if (sscanf(cmd, "%x", &mask) == 1) {
4071 /* mask set */
4072 } else {
4073 res = -EINVAL;
4074 goto errexit;
4075 }
4076 }
4077
4078 if (!res) {
4079 tpacpi_disclose_usertask("procfs hotkey",
4080 "set mask to 0x%08x\n", mask);
4081 res = hotkey_user_mask_set(mask);
4082 }
4083
4084 errexit:
4085 mutex_unlock(&hotkey_mutex);
4086 return res;
4087 }
4088
4089 static const struct acpi_device_id ibm_htk_device_ids[] = {
4090 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4091 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4092 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4093 {"", 0},
4094 };
4095
4096 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4097 .hid = ibm_htk_device_ids,
4098 .notify = hotkey_notify,
4099 .handle = &hkey_handle,
4100 .type = ACPI_DEVICE_NOTIFY,
4101 };
4102
4103 static struct ibm_struct hotkey_driver_data = {
4104 .name = "hotkey",
4105 .read = hotkey_read,
4106 .write = hotkey_write,
4107 .exit = hotkey_exit,
4108 .resume = hotkey_resume,
4109 .suspend = hotkey_suspend,
4110 .acpi = &ibm_hotkey_acpidriver,
4111 };
4112
4113 /*************************************************************************
4114 * Bluetooth subdriver
4115 */
4116
4117 enum {
4118 /* ACPI GBDC/SBDC bits */
4119 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4120 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4121 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4122 0 = disable, 1 = enable */
4123 };
4124
4125 enum {
4126 /* ACPI \BLTH commands */
4127 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4128 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4129 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4130 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4131 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4132 };
4133
4134 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4135
bluetooth_get_status(void)4136 static int bluetooth_get_status(void)
4137 {
4138 int status;
4139
4140 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4141 if (dbg_bluetoothemul)
4142 return (tpacpi_bluetooth_emulstate) ?
4143 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4144 #endif
4145
4146 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4147 return -EIO;
4148
4149 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4150 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4151 }
4152
bluetooth_set_status(enum tpacpi_rfkill_state state)4153 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4154 {
4155 int status;
4156
4157 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s bluetooth\n",
4158 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4159
4160 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4161 if (dbg_bluetoothemul) {
4162 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4163 return 0;
4164 }
4165 #endif
4166
4167 if (state == TPACPI_RFK_RADIO_ON)
4168 status = TP_ACPI_BLUETOOTH_RADIOSSW
4169 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4170 else
4171 status = 0;
4172
4173 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4174 return -EIO;
4175
4176 return 0;
4177 }
4178
4179 /* sysfs bluetooth enable ---------------------------------------------- */
bluetooth_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4180 static ssize_t bluetooth_enable_show(struct device *dev,
4181 struct device_attribute *attr,
4182 char *buf)
4183 {
4184 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4185 attr, buf);
4186 }
4187
bluetooth_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4188 static ssize_t bluetooth_enable_store(struct device *dev,
4189 struct device_attribute *attr,
4190 const char *buf, size_t count)
4191 {
4192 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4193 attr, buf, count);
4194 }
4195
4196 static DEVICE_ATTR_RW(bluetooth_enable);
4197
4198 /* --------------------------------------------------------------------- */
4199
4200 static struct attribute *bluetooth_attributes[] = {
4201 &dev_attr_bluetooth_enable.attr,
4202 NULL
4203 };
4204
bluetooth_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)4205 static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
4206 struct attribute *attr, int n)
4207 {
4208 return tp_features.bluetooth ? attr->mode : 0;
4209 }
4210
4211 static const struct attribute_group bluetooth_attr_group = {
4212 .is_visible = bluetooth_attr_is_visible,
4213 .attrs = bluetooth_attributes,
4214 };
4215
4216 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4217 .get_status = bluetooth_get_status,
4218 .set_status = bluetooth_set_status,
4219 };
4220
bluetooth_shutdown(void)4221 static void bluetooth_shutdown(void)
4222 {
4223 /* Order firmware to save current state to NVRAM */
4224 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4225 TP_ACPI_BLTH_SAVE_STATE))
4226 pr_notice("failed to save bluetooth state to NVRAM\n");
4227 else
4228 vdbg_printk(TPACPI_DBG_RFKILL,
4229 "bluetooth state saved to NVRAM\n");
4230 }
4231
bluetooth_exit(void)4232 static void bluetooth_exit(void)
4233 {
4234 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4235 bluetooth_shutdown();
4236 }
4237
4238 static const struct dmi_system_id fwbug_list[] __initconst = {
4239 {
4240 .ident = "ThinkPad E485",
4241 .driver_data = &quirk_btusb_bug,
4242 .matches = {
4243 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4244 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4245 },
4246 },
4247 {
4248 .ident = "ThinkPad E585",
4249 .driver_data = &quirk_btusb_bug,
4250 .matches = {
4251 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4252 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4253 },
4254 },
4255 {
4256 .ident = "ThinkPad A285 - 20MW",
4257 .driver_data = &quirk_btusb_bug,
4258 .matches = {
4259 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4260 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4261 },
4262 },
4263 {
4264 .ident = "ThinkPad A285 - 20MX",
4265 .driver_data = &quirk_btusb_bug,
4266 .matches = {
4267 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4268 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4269 },
4270 },
4271 {
4272 .ident = "ThinkPad A485 - 20MU",
4273 .driver_data = &quirk_btusb_bug,
4274 .matches = {
4275 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4276 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4277 },
4278 },
4279 {
4280 .ident = "ThinkPad A485 - 20MV",
4281 .driver_data = &quirk_btusb_bug,
4282 .matches = {
4283 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4284 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4285 },
4286 },
4287 {}
4288 };
4289
4290 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4291 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4292 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4293 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4294 {}
4295 };
4296
4297
have_bt_fwbug(void)4298 static int __init have_bt_fwbug(void)
4299 {
4300 /*
4301 * Some AMD based ThinkPads have a firmware bug that calling
4302 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4303 */
4304 if (tp_features.quirks && tp_features.quirks->btusb_bug &&
4305 pci_dev_present(fwbug_cards_ids)) {
4306 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4307 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4308 return 1;
4309 } else
4310 return 0;
4311 }
4312
bluetooth_init(struct ibm_init_struct * iibm)4313 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4314 {
4315 int res;
4316 int status = 0;
4317
4318 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4319 "initializing bluetooth subdriver\n");
4320
4321 TPACPI_ACPIHANDLE_INIT(hkey);
4322
4323 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4324 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4325 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4326 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4327
4328 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4329 "bluetooth is %s, status 0x%02x\n",
4330 str_supported(tp_features.bluetooth),
4331 status);
4332
4333 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4334 if (dbg_bluetoothemul) {
4335 tp_features.bluetooth = 1;
4336 pr_info("bluetooth switch emulation enabled\n");
4337 } else
4338 #endif
4339 if (tp_features.bluetooth &&
4340 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4341 /* no bluetooth hardware present in system */
4342 tp_features.bluetooth = 0;
4343 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4344 "bluetooth hardware not installed\n");
4345 }
4346
4347 if (!tp_features.bluetooth)
4348 return -ENODEV;
4349
4350 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4351 &bluetooth_tprfk_ops,
4352 RFKILL_TYPE_BLUETOOTH,
4353 TPACPI_RFK_BLUETOOTH_SW_NAME,
4354 true);
4355 return res;
4356 }
4357
4358 /* procfs -------------------------------------------------------------- */
bluetooth_read(struct seq_file * m)4359 static int bluetooth_read(struct seq_file *m)
4360 {
4361 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4362 }
4363
bluetooth_write(char * buf)4364 static int bluetooth_write(char *buf)
4365 {
4366 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4367 }
4368
4369 static struct ibm_struct bluetooth_driver_data = {
4370 .name = "bluetooth",
4371 .read = bluetooth_read,
4372 .write = bluetooth_write,
4373 .exit = bluetooth_exit,
4374 .shutdown = bluetooth_shutdown,
4375 };
4376
4377 /*************************************************************************
4378 * Wan subdriver
4379 */
4380
4381 enum {
4382 /* ACPI GWAN/SWAN bits */
4383 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4384 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4385 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4386 0 = disable, 1 = enable */
4387 };
4388
4389 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4390
wan_get_status(void)4391 static int wan_get_status(void)
4392 {
4393 int status;
4394
4395 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4396 if (dbg_wwanemul)
4397 return (tpacpi_wwan_emulstate) ?
4398 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4399 #endif
4400
4401 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4402 return -EIO;
4403
4404 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4405 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4406 }
4407
wan_set_status(enum tpacpi_rfkill_state state)4408 static int wan_set_status(enum tpacpi_rfkill_state state)
4409 {
4410 int status;
4411
4412 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
4413 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4414
4415 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4416 if (dbg_wwanemul) {
4417 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4418 return 0;
4419 }
4420 #endif
4421
4422 if (state == TPACPI_RFK_RADIO_ON)
4423 status = TP_ACPI_WANCARD_RADIOSSW
4424 | TP_ACPI_WANCARD_RESUMECTRL;
4425 else
4426 status = 0;
4427
4428 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4429 return -EIO;
4430
4431 return 0;
4432 }
4433
4434 /* sysfs wan enable ---------------------------------------------------- */
wan_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4435 static ssize_t wan_enable_show(struct device *dev,
4436 struct device_attribute *attr,
4437 char *buf)
4438 {
4439 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4440 attr, buf);
4441 }
4442
wan_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4443 static ssize_t wan_enable_store(struct device *dev,
4444 struct device_attribute *attr,
4445 const char *buf, size_t count)
4446 {
4447 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4448 attr, buf, count);
4449 }
4450
4451 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4452 wan_enable_show, wan_enable_store);
4453
4454 /* --------------------------------------------------------------------- */
4455
4456 static struct attribute *wan_attributes[] = {
4457 &dev_attr_wwan_enable.attr,
4458 NULL
4459 };
4460
wan_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)4461 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
4462 int n)
4463 {
4464 return tp_features.wan ? attr->mode : 0;
4465 }
4466
4467 static const struct attribute_group wan_attr_group = {
4468 .is_visible = wan_attr_is_visible,
4469 .attrs = wan_attributes,
4470 };
4471
4472 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4473 .get_status = wan_get_status,
4474 .set_status = wan_set_status,
4475 };
4476
wan_shutdown(void)4477 static void wan_shutdown(void)
4478 {
4479 /* Order firmware to save current state to NVRAM */
4480 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4481 TP_ACPI_WGSV_SAVE_STATE))
4482 pr_notice("failed to save WWAN state to NVRAM\n");
4483 else
4484 vdbg_printk(TPACPI_DBG_RFKILL,
4485 "WWAN state saved to NVRAM\n");
4486 }
4487
wan_exit(void)4488 static void wan_exit(void)
4489 {
4490 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4491 wan_shutdown();
4492 }
4493
wan_init(struct ibm_init_struct * iibm)4494 static int __init wan_init(struct ibm_init_struct *iibm)
4495 {
4496 int res;
4497 int status = 0;
4498
4499 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4500 "initializing wan subdriver\n");
4501
4502 TPACPI_ACPIHANDLE_INIT(hkey);
4503
4504 tp_features.wan = hkey_handle &&
4505 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4506
4507 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4508 "wan is %s, status 0x%02x\n",
4509 str_supported(tp_features.wan),
4510 status);
4511
4512 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4513 if (dbg_wwanemul) {
4514 tp_features.wan = 1;
4515 pr_info("wwan switch emulation enabled\n");
4516 } else
4517 #endif
4518 if (tp_features.wan &&
4519 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4520 /* no wan hardware present in system */
4521 tp_features.wan = 0;
4522 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4523 "wan hardware not installed\n");
4524 }
4525
4526 if (!tp_features.wan)
4527 return -ENODEV;
4528
4529 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4530 &wan_tprfk_ops,
4531 RFKILL_TYPE_WWAN,
4532 TPACPI_RFK_WWAN_SW_NAME,
4533 true);
4534 return res;
4535 }
4536
4537 /* procfs -------------------------------------------------------------- */
wan_read(struct seq_file * m)4538 static int wan_read(struct seq_file *m)
4539 {
4540 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4541 }
4542
wan_write(char * buf)4543 static int wan_write(char *buf)
4544 {
4545 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4546 }
4547
4548 static struct ibm_struct wan_driver_data = {
4549 .name = "wan",
4550 .read = wan_read,
4551 .write = wan_write,
4552 .exit = wan_exit,
4553 .shutdown = wan_shutdown,
4554 };
4555
4556 /*************************************************************************
4557 * UWB subdriver
4558 */
4559
4560 enum {
4561 /* ACPI GUWB/SUWB bits */
4562 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4563 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4564 };
4565
4566 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4567
uwb_get_status(void)4568 static int uwb_get_status(void)
4569 {
4570 int status;
4571
4572 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4573 if (dbg_uwbemul)
4574 return (tpacpi_uwb_emulstate) ?
4575 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4576 #endif
4577
4578 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4579 return -EIO;
4580
4581 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4582 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4583 }
4584
uwb_set_status(enum tpacpi_rfkill_state state)4585 static int uwb_set_status(enum tpacpi_rfkill_state state)
4586 {
4587 int status;
4588
4589 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
4590 str_enable_disable(state == TPACPI_RFK_RADIO_ON));
4591
4592 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4593 if (dbg_uwbemul) {
4594 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4595 return 0;
4596 }
4597 #endif
4598
4599 if (state == TPACPI_RFK_RADIO_ON)
4600 status = TP_ACPI_UWB_RADIOSSW;
4601 else
4602 status = 0;
4603
4604 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4605 return -EIO;
4606
4607 return 0;
4608 }
4609
4610 /* --------------------------------------------------------------------- */
4611
4612 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4613 .get_status = uwb_get_status,
4614 .set_status = uwb_set_status,
4615 };
4616
uwb_exit(void)4617 static void uwb_exit(void)
4618 {
4619 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4620 }
4621
uwb_init(struct ibm_init_struct * iibm)4622 static int __init uwb_init(struct ibm_init_struct *iibm)
4623 {
4624 int res;
4625 int status = 0;
4626
4627 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4628 "initializing uwb subdriver\n");
4629
4630 TPACPI_ACPIHANDLE_INIT(hkey);
4631
4632 tp_features.uwb = hkey_handle &&
4633 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4634
4635 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4636 "uwb is %s, status 0x%02x\n",
4637 str_supported(tp_features.uwb),
4638 status);
4639
4640 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4641 if (dbg_uwbemul) {
4642 tp_features.uwb = 1;
4643 pr_info("uwb switch emulation enabled\n");
4644 } else
4645 #endif
4646 if (tp_features.uwb &&
4647 !(status & TP_ACPI_UWB_HWPRESENT)) {
4648 /* no uwb hardware present in system */
4649 tp_features.uwb = 0;
4650 dbg_printk(TPACPI_DBG_INIT,
4651 "uwb hardware not installed\n");
4652 }
4653
4654 if (!tp_features.uwb)
4655 return -ENODEV;
4656
4657 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4658 &uwb_tprfk_ops,
4659 RFKILL_TYPE_UWB,
4660 TPACPI_RFK_UWB_SW_NAME,
4661 false);
4662 return res;
4663 }
4664
4665 static struct ibm_struct uwb_driver_data = {
4666 .name = "uwb",
4667 .exit = uwb_exit,
4668 .flags.experimental = 1,
4669 };
4670
4671 /*************************************************************************
4672 * Video subdriver
4673 */
4674
4675 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4676
4677 enum video_access_mode {
4678 TPACPI_VIDEO_NONE = 0,
4679 TPACPI_VIDEO_570, /* 570 */
4680 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4681 TPACPI_VIDEO_NEW, /* all others */
4682 };
4683
4684 enum { /* video status flags, based on VIDEO_570 */
4685 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4686 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4687 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4688 };
4689
4690 enum { /* TPACPI_VIDEO_570 constants */
4691 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4692 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4693 * video_status_flags */
4694 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4695 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4696 };
4697
4698 static enum video_access_mode video_supported;
4699 static int video_orig_autosw;
4700
4701 static int video_autosw_get(void);
4702 static int video_autosw_set(int enable);
4703
4704 TPACPI_HANDLE(vid, root,
4705 "\\_SB.PCI.AGP.VGA", /* 570 */
4706 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4707 "\\_SB.PCI0.VID0", /* 770e */
4708 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4709 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4710 "\\_SB.PCI0.AGP.VID", /* all others */
4711 ); /* R30, R31 */
4712
4713 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4714
video_init(struct ibm_init_struct * iibm)4715 static int __init video_init(struct ibm_init_struct *iibm)
4716 {
4717 int ivga;
4718
4719 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4720
4721 TPACPI_ACPIHANDLE_INIT(vid);
4722 if (tpacpi_is_ibm())
4723 TPACPI_ACPIHANDLE_INIT(vid2);
4724
4725 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4726 /* G41, assume IVGA doesn't change */
4727 vid_handle = vid2_handle;
4728
4729 if (!vid_handle)
4730 /* video switching not supported on R30, R31 */
4731 video_supported = TPACPI_VIDEO_NONE;
4732 else if (tpacpi_is_ibm() &&
4733 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4734 /* 570 */
4735 video_supported = TPACPI_VIDEO_570;
4736 else if (tpacpi_is_ibm() &&
4737 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4738 /* 600e/x, 770e, 770x */
4739 video_supported = TPACPI_VIDEO_770;
4740 else
4741 /* all others */
4742 video_supported = TPACPI_VIDEO_NEW;
4743
4744 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4745 str_supported(video_supported != TPACPI_VIDEO_NONE),
4746 video_supported);
4747
4748 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
4749 }
4750
video_exit(void)4751 static void video_exit(void)
4752 {
4753 dbg_printk(TPACPI_DBG_EXIT,
4754 "restoring original video autoswitch mode\n");
4755 if (video_autosw_set(video_orig_autosw))
4756 pr_err("error while trying to restore original video autoswitch mode\n");
4757 }
4758
video_outputsw_get(void)4759 static int video_outputsw_get(void)
4760 {
4761 int status = 0;
4762 int i;
4763
4764 switch (video_supported) {
4765 case TPACPI_VIDEO_570:
4766 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4767 TP_ACPI_VIDEO_570_PHSCMD))
4768 return -EIO;
4769 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4770 break;
4771 case TPACPI_VIDEO_770:
4772 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4773 return -EIO;
4774 if (i)
4775 status |= TP_ACPI_VIDEO_S_LCD;
4776 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4777 return -EIO;
4778 if (i)
4779 status |= TP_ACPI_VIDEO_S_CRT;
4780 break;
4781 case TPACPI_VIDEO_NEW:
4782 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4783 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4784 return -EIO;
4785 if (i)
4786 status |= TP_ACPI_VIDEO_S_CRT;
4787
4788 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4789 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4790 return -EIO;
4791 if (i)
4792 status |= TP_ACPI_VIDEO_S_LCD;
4793 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4794 return -EIO;
4795 if (i)
4796 status |= TP_ACPI_VIDEO_S_DVI;
4797 break;
4798 default:
4799 return -ENOSYS;
4800 }
4801
4802 return status;
4803 }
4804
video_outputsw_set(int status)4805 static int video_outputsw_set(int status)
4806 {
4807 int autosw;
4808 int res = 0;
4809
4810 switch (video_supported) {
4811 case TPACPI_VIDEO_570:
4812 res = acpi_evalf(NULL, NULL,
4813 "\\_SB.PHS2", "vdd",
4814 TP_ACPI_VIDEO_570_PHS2CMD,
4815 status | TP_ACPI_VIDEO_570_PHS2SET);
4816 break;
4817 case TPACPI_VIDEO_770:
4818 autosw = video_autosw_get();
4819 if (autosw < 0)
4820 return autosw;
4821
4822 res = video_autosw_set(1);
4823 if (res)
4824 return res;
4825 res = acpi_evalf(vid_handle, NULL,
4826 "ASWT", "vdd", status * 0x100, 0);
4827 if (!autosw && video_autosw_set(autosw)) {
4828 pr_err("video auto-switch left enabled due to error\n");
4829 return -EIO;
4830 }
4831 break;
4832 case TPACPI_VIDEO_NEW:
4833 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4834 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4835 break;
4836 default:
4837 return -ENOSYS;
4838 }
4839
4840 return (res) ? 0 : -EIO;
4841 }
4842
video_autosw_get(void)4843 static int video_autosw_get(void)
4844 {
4845 int autosw = 0;
4846
4847 switch (video_supported) {
4848 case TPACPI_VIDEO_570:
4849 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4850 return -EIO;
4851 break;
4852 case TPACPI_VIDEO_770:
4853 case TPACPI_VIDEO_NEW:
4854 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4855 return -EIO;
4856 break;
4857 default:
4858 return -ENOSYS;
4859 }
4860
4861 return autosw & 1;
4862 }
4863
video_autosw_set(int enable)4864 static int video_autosw_set(int enable)
4865 {
4866 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4867 return -EIO;
4868 return 0;
4869 }
4870
video_outputsw_cycle(void)4871 static int video_outputsw_cycle(void)
4872 {
4873 int autosw = video_autosw_get();
4874 int res;
4875
4876 if (autosw < 0)
4877 return autosw;
4878
4879 switch (video_supported) {
4880 case TPACPI_VIDEO_570:
4881 res = video_autosw_set(1);
4882 if (res)
4883 return res;
4884 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4885 break;
4886 case TPACPI_VIDEO_770:
4887 case TPACPI_VIDEO_NEW:
4888 res = video_autosw_set(1);
4889 if (res)
4890 return res;
4891 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4892 break;
4893 default:
4894 return -ENOSYS;
4895 }
4896 if (!autosw && video_autosw_set(autosw)) {
4897 pr_err("video auto-switch left enabled due to error\n");
4898 return -EIO;
4899 }
4900
4901 return (res) ? 0 : -EIO;
4902 }
4903
video_expand_toggle(void)4904 static int video_expand_toggle(void)
4905 {
4906 switch (video_supported) {
4907 case TPACPI_VIDEO_570:
4908 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
4909 0 : -EIO;
4910 case TPACPI_VIDEO_770:
4911 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
4912 0 : -EIO;
4913 case TPACPI_VIDEO_NEW:
4914 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
4915 0 : -EIO;
4916 default:
4917 return -ENOSYS;
4918 }
4919 /* not reached */
4920 }
4921
video_read(struct seq_file * m)4922 static int video_read(struct seq_file *m)
4923 {
4924 int status, autosw;
4925
4926 if (video_supported == TPACPI_VIDEO_NONE) {
4927 seq_puts(m, "status:\t\tnot supported\n");
4928 return 0;
4929 }
4930
4931 /* Even reads can crash X.org, so... */
4932 if (!capable(CAP_SYS_ADMIN))
4933 return -EPERM;
4934
4935 status = video_outputsw_get();
4936 if (status < 0)
4937 return status;
4938
4939 autosw = video_autosw_get();
4940 if (autosw < 0)
4941 return autosw;
4942
4943 seq_puts(m, "status:\t\tsupported\n");
4944 seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
4945 seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
4946 if (video_supported == TPACPI_VIDEO_NEW)
4947 seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
4948 seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
4949 seq_puts(m, "commands:\tlcd_enable, lcd_disable\n");
4950 seq_puts(m, "commands:\tcrt_enable, crt_disable\n");
4951 if (video_supported == TPACPI_VIDEO_NEW)
4952 seq_puts(m, "commands:\tdvi_enable, dvi_disable\n");
4953 seq_puts(m, "commands:\tauto_enable, auto_disable\n");
4954 seq_puts(m, "commands:\tvideo_switch, expand_toggle\n");
4955
4956 return 0;
4957 }
4958
video_write(char * buf)4959 static int video_write(char *buf)
4960 {
4961 char *cmd;
4962 int enable, disable, status;
4963 int res;
4964
4965 if (video_supported == TPACPI_VIDEO_NONE)
4966 return -ENODEV;
4967
4968 /* Even reads can crash X.org, let alone writes... */
4969 if (!capable(CAP_SYS_ADMIN))
4970 return -EPERM;
4971
4972 enable = 0;
4973 disable = 0;
4974
4975 while ((cmd = strsep(&buf, ","))) {
4976 if (strstarts(cmd, "lcd_enable")) {
4977 enable |= TP_ACPI_VIDEO_S_LCD;
4978 } else if (strstarts(cmd, "lcd_disable")) {
4979 disable |= TP_ACPI_VIDEO_S_LCD;
4980 } else if (strstarts(cmd, "crt_enable")) {
4981 enable |= TP_ACPI_VIDEO_S_CRT;
4982 } else if (strstarts(cmd, "crt_disable")) {
4983 disable |= TP_ACPI_VIDEO_S_CRT;
4984 } else if (video_supported == TPACPI_VIDEO_NEW &&
4985 strstarts(cmd, "dvi_enable")) {
4986 enable |= TP_ACPI_VIDEO_S_DVI;
4987 } else if (video_supported == TPACPI_VIDEO_NEW &&
4988 strstarts(cmd, "dvi_disable")) {
4989 disable |= TP_ACPI_VIDEO_S_DVI;
4990 } else if (strstarts(cmd, "auto_enable")) {
4991 res = video_autosw_set(1);
4992 if (res)
4993 return res;
4994 } else if (strstarts(cmd, "auto_disable")) {
4995 res = video_autosw_set(0);
4996 if (res)
4997 return res;
4998 } else if (strstarts(cmd, "video_switch")) {
4999 res = video_outputsw_cycle();
5000 if (res)
5001 return res;
5002 } else if (strstarts(cmd, "expand_toggle")) {
5003 res = video_expand_toggle();
5004 if (res)
5005 return res;
5006 } else
5007 return -EINVAL;
5008 }
5009
5010 if (enable || disable) {
5011 status = video_outputsw_get();
5012 if (status < 0)
5013 return status;
5014 res = video_outputsw_set((status & ~disable) | enable);
5015 if (res)
5016 return res;
5017 }
5018
5019 return 0;
5020 }
5021
5022 static struct ibm_struct video_driver_data = {
5023 .name = "video",
5024 .read = video_read,
5025 .write = video_write,
5026 .exit = video_exit,
5027 };
5028
5029 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5030
5031 /*************************************************************************
5032 * Keyboard backlight subdriver
5033 */
5034
5035 static enum led_brightness kbdlight_brightness;
5036 static DEFINE_MUTEX(kbdlight_mutex);
5037
kbdlight_set_level(int level)5038 static int kbdlight_set_level(int level)
5039 {
5040 int ret = 0;
5041
5042 if (!hkey_handle)
5043 return -ENXIO;
5044
5045 mutex_lock(&kbdlight_mutex);
5046
5047 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5048 ret = -EIO;
5049 else
5050 kbdlight_brightness = level;
5051
5052 mutex_unlock(&kbdlight_mutex);
5053
5054 return ret;
5055 }
5056
kbdlight_get_level(void)5057 static int kbdlight_get_level(void)
5058 {
5059 int status = 0;
5060
5061 if (!hkey_handle)
5062 return -ENXIO;
5063
5064 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5065 return -EIO;
5066
5067 if (status < 0)
5068 return status;
5069
5070 return status & 0x3;
5071 }
5072
kbdlight_is_supported(void)5073 static bool kbdlight_is_supported(void)
5074 {
5075 int status = 0;
5076
5077 if (!hkey_handle)
5078 return false;
5079
5080 if (!acpi_has_method(hkey_handle, "MLCG")) {
5081 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5082 return false;
5083 }
5084
5085 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5086 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5087 return false;
5088 }
5089
5090 if (status < 0) {
5091 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5092 return false;
5093 }
5094
5095 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5096 /*
5097 * Guessed test for keyboard backlight:
5098 *
5099 * Machines with backlight keyboard return:
5100 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5101 * b110100010010000000XX - ThinkPad x230
5102 * b010100000010000000XX - ThinkPad x240
5103 * b010100000010000000XX - ThinkPad W541
5104 * (XX is current backlight level)
5105 *
5106 * Machines without backlight keyboard return:
5107 * b10100001000000000000 - ThinkPad x230
5108 * b10110001000000000000 - ThinkPad E430
5109 * b00000000000000000000 - ThinkPad E450
5110 *
5111 * Candidate BITs for detection test (XOR):
5112 * b01000000001000000000
5113 * ^
5114 */
5115 return status & BIT(9);
5116 }
5117
kbdlight_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5118 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5119 enum led_brightness brightness)
5120 {
5121 return kbdlight_set_level(brightness);
5122 }
5123
kbdlight_sysfs_get(struct led_classdev * led_cdev)5124 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5125 {
5126 int level;
5127
5128 level = kbdlight_get_level();
5129 if (level < 0)
5130 return 0;
5131
5132 return level;
5133 }
5134
5135 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5136 .led_classdev = {
5137 .name = "tpacpi::kbd_backlight",
5138 .max_brightness = 2,
5139 .flags = LED_BRIGHT_HW_CHANGED,
5140 .brightness_set_blocking = &kbdlight_sysfs_set,
5141 .brightness_get = &kbdlight_sysfs_get,
5142 }
5143 };
5144
kbdlight_init(struct ibm_init_struct * iibm)5145 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5146 {
5147 int rc;
5148
5149 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5150
5151 TPACPI_ACPIHANDLE_INIT(hkey);
5152
5153 if (!kbdlight_is_supported()) {
5154 tp_features.kbdlight = 0;
5155 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5156 return -ENODEV;
5157 }
5158
5159 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5160 tp_features.kbdlight = 1;
5161
5162 rc = led_classdev_register(&tpacpi_pdev->dev,
5163 &tpacpi_led_kbdlight.led_classdev);
5164 if (rc < 0) {
5165 tp_features.kbdlight = 0;
5166 return rc;
5167 }
5168
5169 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5170 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5171 return 0;
5172 }
5173
kbdlight_exit(void)5174 static void kbdlight_exit(void)
5175 {
5176 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5177 }
5178
kbdlight_set_level_and_update(int level)5179 static int kbdlight_set_level_and_update(int level)
5180 {
5181 int ret;
5182 struct led_classdev *led_cdev;
5183
5184 ret = kbdlight_set_level(level);
5185 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5186
5187 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5188 led_cdev->brightness = level;
5189
5190 return ret;
5191 }
5192
kbdlight_read(struct seq_file * m)5193 static int kbdlight_read(struct seq_file *m)
5194 {
5195 int level;
5196
5197 if (!tp_features.kbdlight) {
5198 seq_puts(m, "status:\t\tnot supported\n");
5199 } else {
5200 level = kbdlight_get_level();
5201 if (level < 0)
5202 seq_printf(m, "status:\t\terror %d\n", level);
5203 else
5204 seq_printf(m, "status:\t\t%d\n", level);
5205 seq_puts(m, "commands:\t0, 1, 2\n");
5206 }
5207
5208 return 0;
5209 }
5210
kbdlight_write(char * buf)5211 static int kbdlight_write(char *buf)
5212 {
5213 char *cmd;
5214 int res, level = -EINVAL;
5215
5216 if (!tp_features.kbdlight)
5217 return -ENODEV;
5218
5219 while ((cmd = strsep(&buf, ","))) {
5220 res = kstrtoint(cmd, 10, &level);
5221 if (res < 0)
5222 return res;
5223 }
5224
5225 if (level >= 3 || level < 0)
5226 return -EINVAL;
5227
5228 return kbdlight_set_level_and_update(level);
5229 }
5230
kbdlight_suspend(void)5231 static void kbdlight_suspend(void)
5232 {
5233 struct led_classdev *led_cdev;
5234
5235 if (!tp_features.kbdlight)
5236 return;
5237
5238 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5239 led_update_brightness(led_cdev);
5240 led_classdev_suspend(led_cdev);
5241 }
5242
kbdlight_resume(void)5243 static void kbdlight_resume(void)
5244 {
5245 if (!tp_features.kbdlight)
5246 return;
5247
5248 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5249 }
5250
5251 static struct ibm_struct kbdlight_driver_data = {
5252 .name = "kbdlight",
5253 .read = kbdlight_read,
5254 .write = kbdlight_write,
5255 .suspend = kbdlight_suspend,
5256 .resume = kbdlight_resume,
5257 .exit = kbdlight_exit,
5258 };
5259
5260 /*************************************************************************
5261 * Light (thinklight) subdriver
5262 */
5263
5264 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5265 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5266
light_get_status(void)5267 static int light_get_status(void)
5268 {
5269 int status = 0;
5270
5271 if (tp_features.light_status) {
5272 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5273 return -EIO;
5274 return (!!status);
5275 }
5276
5277 return -ENXIO;
5278 }
5279
light_set_status(int status)5280 static int light_set_status(int status)
5281 {
5282 int rc;
5283
5284 if (tp_features.light) {
5285 if (cmos_handle) {
5286 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5287 (status) ?
5288 TP_CMOS_THINKLIGHT_ON :
5289 TP_CMOS_THINKLIGHT_OFF);
5290 } else {
5291 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5292 (status) ? 1 : 0);
5293 }
5294 return (rc) ? 0 : -EIO;
5295 }
5296
5297 return -ENXIO;
5298 }
5299
light_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5300 static int light_sysfs_set(struct led_classdev *led_cdev,
5301 enum led_brightness brightness)
5302 {
5303 return light_set_status((brightness != LED_OFF) ?
5304 TPACPI_LED_ON : TPACPI_LED_OFF);
5305 }
5306
light_sysfs_get(struct led_classdev * led_cdev)5307 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5308 {
5309 return (light_get_status() == 1) ? LED_ON : LED_OFF;
5310 }
5311
5312 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5313 .led_classdev = {
5314 .name = "tpacpi::thinklight",
5315 .max_brightness = 1,
5316 .brightness_set_blocking = &light_sysfs_set,
5317 .brightness_get = &light_sysfs_get,
5318 }
5319 };
5320
light_init(struct ibm_init_struct * iibm)5321 static int __init light_init(struct ibm_init_struct *iibm)
5322 {
5323 int rc;
5324
5325 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5326
5327 if (tpacpi_is_ibm()) {
5328 TPACPI_ACPIHANDLE_INIT(ledb);
5329 TPACPI_ACPIHANDLE_INIT(lght);
5330 }
5331 TPACPI_ACPIHANDLE_INIT(cmos);
5332
5333 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5334 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5335
5336 if (tp_features.light)
5337 /* light status not supported on
5338 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5339 tp_features.light_status =
5340 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5341
5342 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5343 str_supported(tp_features.light),
5344 str_supported(tp_features.light_status));
5345
5346 if (!tp_features.light)
5347 return -ENODEV;
5348
5349 rc = led_classdev_register(&tpacpi_pdev->dev,
5350 &tpacpi_led_thinklight.led_classdev);
5351
5352 if (rc < 0) {
5353 tp_features.light = 0;
5354 tp_features.light_status = 0;
5355 } else {
5356 rc = 0;
5357 }
5358
5359 return rc;
5360 }
5361
light_exit(void)5362 static void light_exit(void)
5363 {
5364 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5365 }
5366
light_read(struct seq_file * m)5367 static int light_read(struct seq_file *m)
5368 {
5369 int status;
5370
5371 if (!tp_features.light) {
5372 seq_puts(m, "status:\t\tnot supported\n");
5373 } else if (!tp_features.light_status) {
5374 seq_puts(m, "status:\t\tunknown\n");
5375 seq_puts(m, "commands:\ton, off\n");
5376 } else {
5377 status = light_get_status();
5378 if (status < 0)
5379 return status;
5380 seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
5381 seq_puts(m, "commands:\ton, off\n");
5382 }
5383
5384 return 0;
5385 }
5386
light_write(char * buf)5387 static int light_write(char *buf)
5388 {
5389 char *cmd;
5390 int newstatus = 0;
5391
5392 if (!tp_features.light)
5393 return -ENODEV;
5394
5395 while ((cmd = strsep(&buf, ","))) {
5396 if (strstarts(cmd, "on")) {
5397 newstatus = 1;
5398 } else if (strstarts(cmd, "off")) {
5399 newstatus = 0;
5400 } else
5401 return -EINVAL;
5402 }
5403
5404 return light_set_status(newstatus);
5405 }
5406
5407 static struct ibm_struct light_driver_data = {
5408 .name = "light",
5409 .read = light_read,
5410 .write = light_write,
5411 .exit = light_exit,
5412 };
5413
5414 /*************************************************************************
5415 * CMOS subdriver
5416 */
5417
5418 /* sysfs cmos_command -------------------------------------------------- */
cmos_command_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5419 static ssize_t cmos_command_store(struct device *dev,
5420 struct device_attribute *attr,
5421 const char *buf, size_t count)
5422 {
5423 unsigned long cmos_cmd;
5424 int res;
5425
5426 if (parse_strtoul(buf, 21, &cmos_cmd))
5427 return -EINVAL;
5428
5429 res = issue_thinkpad_cmos_command(cmos_cmd);
5430 return (res) ? res : count;
5431 }
5432
5433 static DEVICE_ATTR_WO(cmos_command);
5434
5435 static struct attribute *cmos_attributes[] = {
5436 &dev_attr_cmos_command.attr,
5437 NULL
5438 };
5439
cmos_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)5440 static umode_t cmos_attr_is_visible(struct kobject *kobj,
5441 struct attribute *attr, int n)
5442 {
5443 return cmos_handle ? attr->mode : 0;
5444 }
5445
5446 static const struct attribute_group cmos_attr_group = {
5447 .is_visible = cmos_attr_is_visible,
5448 .attrs = cmos_attributes,
5449 };
5450
5451 /* --------------------------------------------------------------------- */
5452
cmos_init(struct ibm_init_struct * iibm)5453 static int __init cmos_init(struct ibm_init_struct *iibm)
5454 {
5455 vdbg_printk(TPACPI_DBG_INIT,
5456 "initializing cmos commands subdriver\n");
5457
5458 TPACPI_ACPIHANDLE_INIT(cmos);
5459
5460 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5461 str_supported(cmos_handle != NULL));
5462
5463 return cmos_handle ? 0 : -ENODEV;
5464 }
5465
cmos_read(struct seq_file * m)5466 static int cmos_read(struct seq_file *m)
5467 {
5468 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5469 R30, R31, T20-22, X20-21 */
5470 if (!cmos_handle)
5471 seq_puts(m, "status:\t\tnot supported\n");
5472 else {
5473 seq_puts(m, "status:\t\tsupported\n");
5474 seq_puts(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5475 }
5476
5477 return 0;
5478 }
5479
cmos_write(char * buf)5480 static int cmos_write(char *buf)
5481 {
5482 char *cmd;
5483 int cmos_cmd, res;
5484
5485 while ((cmd = strsep(&buf, ","))) {
5486 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5487 cmos_cmd >= 0 && cmos_cmd <= 21) {
5488 /* cmos_cmd set */
5489 } else
5490 return -EINVAL;
5491
5492 res = issue_thinkpad_cmos_command(cmos_cmd);
5493 if (res)
5494 return res;
5495 }
5496
5497 return 0;
5498 }
5499
5500 static struct ibm_struct cmos_driver_data = {
5501 .name = "cmos",
5502 .read = cmos_read,
5503 .write = cmos_write,
5504 };
5505
5506 /*************************************************************************
5507 * LED subdriver
5508 */
5509
5510 enum led_access_mode {
5511 TPACPI_LED_NONE = 0,
5512 TPACPI_LED_570, /* 570 */
5513 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5514 TPACPI_LED_NEW, /* all others */
5515 };
5516
5517 enum { /* For TPACPI_LED_OLD */
5518 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5519 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5520 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5521 };
5522
5523 static enum led_access_mode led_supported;
5524
5525 static acpi_handle led_handle;
5526
5527 #define TPACPI_LED_NUMLEDS 16
5528 static struct tpacpi_led_classdev *tpacpi_leds;
5529 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5530 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5531 /* there's a limit of 19 chars + NULL before 2.6.26 */
5532 "tpacpi::power",
5533 "tpacpi:orange:batt",
5534 "tpacpi:green:batt",
5535 "tpacpi::dock_active",
5536 "tpacpi::bay_active",
5537 "tpacpi::dock_batt",
5538 "tpacpi::unknown_led",
5539 "tpacpi::standby",
5540 "tpacpi::dock_status1",
5541 "tpacpi::dock_status2",
5542 "tpacpi::lid_logo_dot",
5543 "tpacpi::unknown_led3",
5544 "tpacpi::thinkvantage",
5545 };
5546 #define TPACPI_SAFE_LEDS 0x1481U
5547
tpacpi_is_led_restricted(const unsigned int led)5548 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5549 {
5550 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5551 return false;
5552 #else
5553 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5554 #endif
5555 }
5556
led_get_status(const unsigned int led)5557 static int led_get_status(const unsigned int led)
5558 {
5559 int status;
5560 enum led_status_t led_s;
5561
5562 switch (led_supported) {
5563 case TPACPI_LED_570:
5564 if (!acpi_evalf(ec_handle,
5565 &status, "GLED", "dd", 1 << led))
5566 return -EIO;
5567 led_s = (status == 0) ?
5568 TPACPI_LED_OFF :
5569 ((status == 1) ?
5570 TPACPI_LED_ON :
5571 TPACPI_LED_BLINK);
5572 tpacpi_led_state_cache[led] = led_s;
5573 return led_s;
5574 default:
5575 return -ENXIO;
5576 }
5577
5578 /* not reached */
5579 }
5580
led_set_status(const unsigned int led,const enum led_status_t ledstatus)5581 static int led_set_status(const unsigned int led,
5582 const enum led_status_t ledstatus)
5583 {
5584 /* off, on, blink. Index is led_status_t */
5585 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5586 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5587
5588 int rc = 0;
5589
5590 switch (led_supported) {
5591 case TPACPI_LED_570:
5592 /* 570 */
5593 if (unlikely(led > 7))
5594 return -EINVAL;
5595 if (unlikely(tpacpi_is_led_restricted(led)))
5596 return -EPERM;
5597 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5598 (1 << led), led_sled_arg1[ledstatus]))
5599 return -EIO;
5600 break;
5601 case TPACPI_LED_OLD:
5602 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5603 if (unlikely(led > 7))
5604 return -EINVAL;
5605 if (unlikely(tpacpi_is_led_restricted(led)))
5606 return -EPERM;
5607 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5608 if (rc >= 0)
5609 rc = ec_write(TPACPI_LED_EC_HLBL,
5610 (ledstatus == TPACPI_LED_BLINK) << led);
5611 if (rc >= 0)
5612 rc = ec_write(TPACPI_LED_EC_HLCL,
5613 (ledstatus != TPACPI_LED_OFF) << led);
5614 break;
5615 case TPACPI_LED_NEW:
5616 /* all others */
5617 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5618 return -EINVAL;
5619 if (unlikely(tpacpi_is_led_restricted(led)))
5620 return -EPERM;
5621 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5622 led, led_led_arg1[ledstatus]))
5623 return -EIO;
5624 break;
5625 default:
5626 return -ENXIO;
5627 }
5628
5629 if (!rc)
5630 tpacpi_led_state_cache[led] = ledstatus;
5631
5632 return rc;
5633 }
5634
led_sysfs_set(struct led_classdev * led_cdev,enum led_brightness brightness)5635 static int led_sysfs_set(struct led_classdev *led_cdev,
5636 enum led_brightness brightness)
5637 {
5638 struct tpacpi_led_classdev *data = container_of(led_cdev,
5639 struct tpacpi_led_classdev, led_classdev);
5640 enum led_status_t new_state;
5641
5642 if (brightness == LED_OFF)
5643 new_state = TPACPI_LED_OFF;
5644 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5645 new_state = TPACPI_LED_ON;
5646 else
5647 new_state = TPACPI_LED_BLINK;
5648
5649 return led_set_status(data->led, new_state);
5650 }
5651
led_sysfs_blink_set(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)5652 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5653 unsigned long *delay_on, unsigned long *delay_off)
5654 {
5655 struct tpacpi_led_classdev *data = container_of(led_cdev,
5656 struct tpacpi_led_classdev, led_classdev);
5657
5658 /* Can we choose the flash rate? */
5659 if (*delay_on == 0 && *delay_off == 0) {
5660 /* yes. set them to the hardware blink rate (1 Hz) */
5661 *delay_on = 500; /* ms */
5662 *delay_off = 500; /* ms */
5663 } else if ((*delay_on != 500) || (*delay_off != 500))
5664 return -EINVAL;
5665
5666 return led_set_status(data->led, TPACPI_LED_BLINK);
5667 }
5668
led_sysfs_get(struct led_classdev * led_cdev)5669 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5670 {
5671 int rc;
5672
5673 struct tpacpi_led_classdev *data = container_of(led_cdev,
5674 struct tpacpi_led_classdev, led_classdev);
5675
5676 rc = led_get_status(data->led);
5677
5678 if (rc == TPACPI_LED_OFF || rc < 0)
5679 rc = LED_OFF; /* no error handling in led class :( */
5680 else
5681 rc = LED_FULL;
5682
5683 return rc;
5684 }
5685
led_exit(void)5686 static void led_exit(void)
5687 {
5688 unsigned int i;
5689
5690 for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
5691 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5692
5693 kfree(tpacpi_leds);
5694 }
5695
tpacpi_init_led(unsigned int led)5696 static int __init tpacpi_init_led(unsigned int led)
5697 {
5698 /* LEDs with no name don't get registered */
5699 if (!tpacpi_led_names[led])
5700 return 0;
5701
5702 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5703 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5704 if (led_supported == TPACPI_LED_570)
5705 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
5706
5707 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5708 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
5709 tpacpi_leds[led].led = led;
5710
5711 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
5712 }
5713
5714 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5715 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5716 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5717 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5718
5719 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5720 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5721 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5722 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5723 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5724 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5725 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5726 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5727
5728 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5729 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5730 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5731 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5732 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5733
5734 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5735 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5736 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5737 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5738
5739 /* (1) - may have excess leds enabled on MSB */
5740
5741 /* Defaults (order matters, keep last, don't reorder!) */
5742 { /* Lenovo */
5743 .vendor = PCI_VENDOR_ID_LENOVO,
5744 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5745 .quirks = 0x1fffU,
5746 },
5747 { /* IBM ThinkPads with no EC version string */
5748 .vendor = PCI_VENDOR_ID_IBM,
5749 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5750 .quirks = 0x00ffU,
5751 },
5752 { /* IBM ThinkPads with EC version string */
5753 .vendor = PCI_VENDOR_ID_IBM,
5754 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5755 .quirks = 0x00bfU,
5756 },
5757 };
5758
led_init_detect_mode(void)5759 static enum led_access_mode __init led_init_detect_mode(void)
5760 {
5761 acpi_status status;
5762
5763 if (tpacpi_is_ibm()) {
5764 /* 570 */
5765 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5766 if (ACPI_SUCCESS(status))
5767 return TPACPI_LED_570;
5768
5769 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5770 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5771 if (ACPI_SUCCESS(status))
5772 return TPACPI_LED_OLD;
5773 }
5774
5775 /* most others */
5776 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5777 if (ACPI_SUCCESS(status))
5778 return TPACPI_LED_NEW;
5779
5780 /* R30, R31, and unknown firmwares */
5781 led_handle = NULL;
5782 return TPACPI_LED_NONE;
5783 }
5784
led_init(struct ibm_init_struct * iibm)5785 static int __init led_init(struct ibm_init_struct *iibm)
5786 {
5787 unsigned int i;
5788 int rc;
5789 unsigned long useful_leds;
5790
5791 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5792
5793 led_supported = led_init_detect_mode();
5794
5795 if (led_supported != TPACPI_LED_NONE) {
5796 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5797 ARRAY_SIZE(led_useful_qtable));
5798
5799 if (!useful_leds) {
5800 led_handle = NULL;
5801 led_supported = TPACPI_LED_NONE;
5802 }
5803 }
5804
5805 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5806 str_supported(led_supported), led_supported);
5807
5808 if (led_supported == TPACPI_LED_NONE)
5809 return -ENODEV;
5810
5811 tpacpi_leds = kzalloc_objs(*tpacpi_leds, TPACPI_LED_NUMLEDS);
5812 if (!tpacpi_leds) {
5813 pr_err("Out of memory for LED data\n");
5814 return -ENOMEM;
5815 }
5816
5817 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5818 tpacpi_leds[i].led = -1;
5819
5820 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
5821 rc = tpacpi_init_led(i);
5822 if (rc < 0) {
5823 led_exit();
5824 return rc;
5825 }
5826 }
5827 }
5828
5829 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5830 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5831 #endif
5832 return 0;
5833 }
5834
5835 #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
5836
led_read(struct seq_file * m)5837 static int led_read(struct seq_file *m)
5838 {
5839 if (!led_supported) {
5840 seq_puts(m, "status:\t\tnot supported\n");
5841 return 0;
5842 }
5843 seq_puts(m, "status:\t\tsupported\n");
5844
5845 if (led_supported == TPACPI_LED_570) {
5846 /* 570 */
5847 int i, status;
5848 for (i = 0; i < 8; i++) {
5849 status = led_get_status(i);
5850 if (status < 0)
5851 return -EIO;
5852 seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
5853 }
5854 }
5855
5856 seq_puts(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5857
5858 return 0;
5859 }
5860
led_write(char * buf)5861 static int led_write(char *buf)
5862 {
5863 char *cmd;
5864 int led, rc;
5865 enum led_status_t s;
5866
5867 if (!led_supported)
5868 return -ENODEV;
5869
5870 while ((cmd = strsep(&buf, ","))) {
5871 if (sscanf(cmd, "%d", &led) != 1)
5872 return -EINVAL;
5873
5874 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
5875 return -ENODEV;
5876
5877 if (tpacpi_leds[led].led < 0)
5878 return -ENODEV;
5879
5880 if (strstr(cmd, "off")) {
5881 s = TPACPI_LED_OFF;
5882 } else if (strstr(cmd, "on")) {
5883 s = TPACPI_LED_ON;
5884 } else if (strstr(cmd, "blink")) {
5885 s = TPACPI_LED_BLINK;
5886 } else {
5887 return -EINVAL;
5888 }
5889
5890 rc = led_set_status(led, s);
5891 if (rc < 0)
5892 return rc;
5893 }
5894
5895 return 0;
5896 }
5897
5898 static struct ibm_struct led_driver_data = {
5899 .name = "led",
5900 .read = led_read,
5901 .write = led_write,
5902 .exit = led_exit,
5903 };
5904
5905 /*************************************************************************
5906 * Beep subdriver
5907 */
5908
5909 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5910
5911 #define TPACPI_BEEP_Q1 0x0001
5912
5913 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5914 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5915 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5916 };
5917
beep_init(struct ibm_init_struct * iibm)5918 static int __init beep_init(struct ibm_init_struct *iibm)
5919 {
5920 unsigned long quirks;
5921
5922 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5923
5924 TPACPI_ACPIHANDLE_INIT(beep);
5925
5926 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5927 str_supported(beep_handle != NULL));
5928
5929 quirks = tpacpi_check_quirks(beep_quirk_table,
5930 ARRAY_SIZE(beep_quirk_table));
5931
5932 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5933
5934 return (beep_handle) ? 0 : -ENODEV;
5935 }
5936
beep_read(struct seq_file * m)5937 static int beep_read(struct seq_file *m)
5938 {
5939 if (!beep_handle)
5940 seq_puts(m, "status:\t\tnot supported\n");
5941 else {
5942 seq_puts(m, "status:\t\tsupported\n");
5943 seq_puts(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5944 }
5945
5946 return 0;
5947 }
5948
beep_write(char * buf)5949 static int beep_write(char *buf)
5950 {
5951 char *cmd;
5952 int beep_cmd;
5953
5954 if (!beep_handle)
5955 return -ENODEV;
5956
5957 while ((cmd = strsep(&buf, ","))) {
5958 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5959 beep_cmd >= 0 && beep_cmd <= 17) {
5960 /* beep_cmd set */
5961 } else
5962 return -EINVAL;
5963 if (tp_features.beep_needs_two_args) {
5964 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5965 beep_cmd, 0))
5966 return -EIO;
5967 } else {
5968 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5969 beep_cmd))
5970 return -EIO;
5971 }
5972 }
5973
5974 return 0;
5975 }
5976
5977 static struct ibm_struct beep_driver_data = {
5978 .name = "beep",
5979 .read = beep_read,
5980 .write = beep_write,
5981 };
5982
5983 /*************************************************************************
5984 * Thermal subdriver
5985 */
5986
5987 enum thermal_access_mode {
5988 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5989 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5990 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5991 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5992 TPACPI_THERMAL_TPEC_12, /* Use ACPI EC regs, 12 sensors */
5993 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5994 };
5995
5996 enum { /* TPACPI_THERMAL_TPEC_* */
5997 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5998 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5999 TP_EC_THERMAL_TMP0_NS = 0xA8, /* ACPI EC Non-Standard regs TMP 0..7 */
6000 TP_EC_THERMAL_TMP8_NS = 0xB8, /* ACPI EC Non-standard regs TMP 8..11 */
6001 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6002 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6003
6004 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6005 };
6006
6007
6008 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6009 struct ibm_thermal_sensors_struct {
6010 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6011 };
6012
6013 static const struct tpacpi_quirk thermal_quirk_table[] __initconst = {
6014 /* Non-standard address for thermal registers on some ThinkPads */
6015 TPACPI_Q_LNV3('R', '1', 'F', true), /* L13 Yoga Gen 2 */
6016 TPACPI_Q_LNV3('N', '2', 'U', true), /* X13 Yoga Gen 2*/
6017 TPACPI_Q_LNV3('R', '0', 'R', true), /* L380 */
6018 TPACPI_Q_LNV3('R', '1', '5', true), /* L13 Yoga Gen 1*/
6019 TPACPI_Q_LNV3('R', '1', '0', true), /* L390 */
6020 TPACPI_Q_LNV3('N', '2', 'L', true), /* X13 Yoga Gen 1*/
6021 TPACPI_Q_LNV3('R', '0', 'T', true), /* 11e Gen5 GL*/
6022 TPACPI_Q_LNV3('R', '1', 'D', true), /* 11e Gen5 GL-R*/
6023 TPACPI_Q_LNV3('R', '0', 'V', true), /* 11e Gen5 KL-Y*/
6024 };
6025
6026 static enum thermal_access_mode thermal_read_mode;
6027 static bool thermal_use_labels;
6028 static bool thermal_with_ns_address; /* Non-standard thermal reg address */
6029
6030 /* Function to check thermal read mode */
thermal_read_mode_check(void)6031 static enum thermal_access_mode __init thermal_read_mode_check(void)
6032 {
6033 u8 t, ta1, ta2, ver = 0;
6034 int i;
6035 int acpi_tmp7;
6036
6037 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6038
6039 if (thinkpad_id.ec_model) {
6040 /*
6041 * Direct EC access mode: sensors at registers 0x78-0x7F,
6042 * 0xC0-0xC7. Registers return 0x00 for non-implemented,
6043 * thermal sensors return 0x80 when not available.
6044 *
6045 * In some special cases (when Power Supply ID is 0xC2)
6046 * above rule causes thermal control issues. Offset 0xEF
6047 * determines EC version. 0xC0-0xC7 are not thermal registers
6048 * in Ver 3.
6049 */
6050 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6051 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6052
6053 /* Quirks to check non-standard EC */
6054 thermal_with_ns_address = tpacpi_check_quirks(thermal_quirk_table,
6055 ARRAY_SIZE(thermal_quirk_table));
6056
6057 /* Support for Thinkpads with non-standard address */
6058 if (thermal_with_ns_address) {
6059 pr_info("ECFW with non-standard thermal registers found\n");
6060 return TPACPI_THERMAL_TPEC_12;
6061 }
6062
6063 ta1 = ta2 = 0;
6064 for (i = 0; i < 8; i++) {
6065 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6066 ta1 |= t;
6067 } else {
6068 ta1 = 0;
6069 break;
6070 }
6071 if (ver < 3) {
6072 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6073 ta2 |= t;
6074 } else {
6075 ta1 = 0;
6076 break;
6077 }
6078 }
6079 }
6080
6081 if (ta1 == 0) {
6082 /* This is sheer paranoia, but we handle it anyway */
6083 if (acpi_tmp7) {
6084 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6085 return TPACPI_THERMAL_ACPI_TMP07;
6086 }
6087 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6088 return TPACPI_THERMAL_NONE;
6089 }
6090
6091 if (ver >= 3) {
6092 thermal_use_labels = true;
6093 return TPACPI_THERMAL_TPEC_8;
6094 }
6095
6096 return (ta2 != 0) ? TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6097 }
6098
6099 if (acpi_tmp7) {
6100 if (tpacpi_is_ibm() && acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6101 /* 600e/x, 770e, 770x */
6102 return TPACPI_THERMAL_ACPI_UPDT;
6103 }
6104 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6105 return TPACPI_THERMAL_ACPI_TMP07;
6106 }
6107
6108 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6109 return TPACPI_THERMAL_NONE;
6110 }
6111
6112 /* idx is zero-based */
thermal_get_sensor(int idx,s32 * value)6113 static int thermal_get_sensor(int idx, s32 *value)
6114 {
6115 int t;
6116 s8 tmp;
6117 char tmpi[5];
6118
6119 t = TP_EC_THERMAL_TMP0;
6120
6121 switch (thermal_read_mode) {
6122 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6123 case TPACPI_THERMAL_TPEC_16:
6124 if (idx >= 8 && idx <= 15) {
6125 t = TP_EC_THERMAL_TMP8;
6126 idx -= 8;
6127 }
6128 #endif
6129 fallthrough;
6130 case TPACPI_THERMAL_TPEC_8:
6131 if (idx <= 7) {
6132 if (!acpi_ec_read(t + idx, &tmp))
6133 return -EIO;
6134 *value = tmp * 1000;
6135 return 0;
6136 }
6137 break;
6138
6139 /* The Non-standard EC uses 12 Thermal areas */
6140 case TPACPI_THERMAL_TPEC_12:
6141 if (idx >= 12)
6142 return -EINVAL;
6143
6144 t = idx < 8 ? TP_EC_THERMAL_TMP0_NS + idx :
6145 TP_EC_THERMAL_TMP8_NS + (idx - 8);
6146
6147 if (!acpi_ec_read(t, &tmp))
6148 return -EIO;
6149
6150 *value = tmp * MILLIDEGREE_PER_DEGREE;
6151 return 0;
6152
6153 case TPACPI_THERMAL_ACPI_UPDT:
6154 if (idx <= 7) {
6155 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6156 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6157 return -EIO;
6158 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6159 return -EIO;
6160 *value = (t - 2732) * 100;
6161 return 0;
6162 }
6163 break;
6164
6165 case TPACPI_THERMAL_ACPI_TMP07:
6166 if (idx <= 7) {
6167 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6168 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6169 return -EIO;
6170 if (t > 127 || t < -127)
6171 t = TP_EC_THERMAL_TMP_NA;
6172 *value = t * 1000;
6173 return 0;
6174 }
6175 break;
6176
6177 case TPACPI_THERMAL_NONE:
6178 default:
6179 return -ENOSYS;
6180 }
6181
6182 return -EINVAL;
6183 }
6184
thermal_get_sensors(struct ibm_thermal_sensors_struct * s)6185 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6186 {
6187 int res, i, n;
6188
6189 if (!s)
6190 return -EINVAL;
6191
6192 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6193 n = 16;
6194 else if (thermal_read_mode == TPACPI_THERMAL_TPEC_12)
6195 n = 12;
6196 else
6197 n = 8;
6198
6199 for (i = 0 ; i < n; i++) {
6200 res = thermal_get_sensor(i, &s->temp[i]);
6201 if (res)
6202 return res;
6203 }
6204
6205 return n;
6206 }
6207
thermal_dump_all_sensors(void)6208 static void thermal_dump_all_sensors(void)
6209 {
6210 int n, i;
6211 struct ibm_thermal_sensors_struct t;
6212
6213 n = thermal_get_sensors(&t);
6214 if (n <= 0)
6215 return;
6216
6217 pr_notice("temperatures (Celsius):");
6218
6219 for (i = 0; i < n; i++) {
6220 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6221 pr_cont(" %d", (int)(t.temp[i] / 1000));
6222 else
6223 pr_cont(" N/A");
6224 }
6225
6226 pr_cont("\n");
6227 }
6228
6229 /* sysfs temp##_input -------------------------------------------------- */
6230
thermal_temp_input_show(struct device * dev,struct device_attribute * attr,char * buf)6231 static ssize_t thermal_temp_input_show(struct device *dev,
6232 struct device_attribute *attr,
6233 char *buf)
6234 {
6235 struct sensor_device_attribute *sensor_attr =
6236 to_sensor_dev_attr(attr);
6237 int idx = sensor_attr->index;
6238 s32 value;
6239 int res;
6240
6241 res = thermal_get_sensor(idx, &value);
6242 if (res)
6243 return res;
6244 if (value == TPACPI_THERMAL_SENSOR_NA)
6245 return -ENXIO;
6246
6247 return sysfs_emit(buf, "%d\n", value);
6248 }
6249
6250 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6251 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6252 thermal_temp_input_show, NULL, _idxB)
6253
6254 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6255 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6256 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6257 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6258 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6259 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6260 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6261 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6262 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6263 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6264 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6265 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6266 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6267 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6268 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6269 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6270 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6271 };
6272
6273 #define THERMAL_ATTRS(X) \
6274 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6275
6276 static struct attribute *thermal_temp_input_attr[] = {
6277 THERMAL_ATTRS(0),
6278 THERMAL_ATTRS(1),
6279 THERMAL_ATTRS(2),
6280 THERMAL_ATTRS(3),
6281 THERMAL_ATTRS(4),
6282 THERMAL_ATTRS(5),
6283 THERMAL_ATTRS(6),
6284 THERMAL_ATTRS(7),
6285 THERMAL_ATTRS(8),
6286 THERMAL_ATTRS(9),
6287 THERMAL_ATTRS(10),
6288 THERMAL_ATTRS(11),
6289 THERMAL_ATTRS(12),
6290 THERMAL_ATTRS(13),
6291 THERMAL_ATTRS(14),
6292 THERMAL_ATTRS(15),
6293 NULL
6294 };
6295
6296 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
6297
thermal_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)6298 static umode_t thermal_attr_is_visible(struct kobject *kobj,
6299 struct attribute *attr, int n)
6300 {
6301 struct device_attribute *dev_attr = to_dev_attr(attr);
6302 struct sensor_device_attribute *sensor_attr =
6303 to_sensor_dev_attr(dev_attr);
6304
6305 int idx = sensor_attr->index;
6306
6307 switch (thermal_read_mode) {
6308 case TPACPI_THERMAL_NONE:
6309 return 0;
6310
6311 case TPACPI_THERMAL_ACPI_TMP07:
6312 case TPACPI_THERMAL_ACPI_UPDT:
6313 case TPACPI_THERMAL_TPEC_8:
6314 if (idx >= 8)
6315 return 0;
6316 break;
6317
6318 case TPACPI_THERMAL_TPEC_12:
6319 if (idx >= 12)
6320 return 0;
6321 break;
6322
6323 default:
6324 break;
6325
6326 }
6327
6328 return attr->mode;
6329 }
6330
6331 static const struct attribute_group thermal_attr_group = {
6332 .is_visible = thermal_attr_is_visible,
6333 .attrs = thermal_temp_input_attr,
6334 };
6335
6336 #undef THERMAL_SENSOR_ATTR_TEMP
6337 #undef THERMAL_ATTRS
6338
temp1_label_show(struct device * dev,struct device_attribute * attr,char * buf)6339 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6340 {
6341 return sysfs_emit(buf, "CPU\n");
6342 }
6343 static DEVICE_ATTR_RO(temp1_label);
6344
temp2_label_show(struct device * dev,struct device_attribute * attr,char * buf)6345 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
6346 {
6347 return sysfs_emit(buf, "GPU\n");
6348 }
6349 static DEVICE_ATTR_RO(temp2_label);
6350
6351 static struct attribute *temp_label_attributes[] = {
6352 &dev_attr_temp1_label.attr,
6353 &dev_attr_temp2_label.attr,
6354 NULL
6355 };
6356
temp_label_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)6357 static umode_t temp_label_attr_is_visible(struct kobject *kobj,
6358 struct attribute *attr, int n)
6359 {
6360 return thermal_use_labels ? attr->mode : 0;
6361 }
6362
6363 static const struct attribute_group temp_label_attr_group = {
6364 .is_visible = temp_label_attr_is_visible,
6365 .attrs = temp_label_attributes,
6366 };
6367
6368 /* --------------------------------------------------------------------- */
6369
thermal_init(struct ibm_init_struct * iibm)6370 static int __init thermal_init(struct ibm_init_struct *iibm)
6371 {
6372 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6373
6374 thermal_read_mode = thermal_read_mode_check();
6375
6376 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6377 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6378 thermal_read_mode);
6379
6380 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
6381 }
6382
thermal_read(struct seq_file * m)6383 static int thermal_read(struct seq_file *m)
6384 {
6385 int n, i;
6386 struct ibm_thermal_sensors_struct t;
6387
6388 n = thermal_get_sensors(&t);
6389 if (unlikely(n < 0))
6390 return n;
6391
6392 seq_puts(m, "temperatures:\t");
6393
6394 if (n > 0) {
6395 for (i = 0; i < (n - 1); i++)
6396 seq_printf(m, "%d ", t.temp[i] / 1000);
6397 seq_printf(m, "%d\n", t.temp[i] / 1000);
6398 } else
6399 seq_puts(m, "not supported\n");
6400
6401 return 0;
6402 }
6403
6404 static struct ibm_struct thermal_driver_data = {
6405 .name = "thermal",
6406 .read = thermal_read,
6407 };
6408
6409 /*************************************************************************
6410 * Backlight/brightness subdriver
6411 */
6412
6413 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6414
6415 /*
6416 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6417 * CMOS NVRAM byte 0x5E, bits 0-3.
6418 *
6419 * EC HBRV (0x31) has the following layout
6420 * Bit 7: unknown function
6421 * Bit 6: unknown function
6422 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6423 * Bit 4: must be set to zero to avoid problems
6424 * Bit 3-0: backlight brightness level
6425 *
6426 * brightness_get_raw returns status data in the HBRV layout
6427 *
6428 * WARNING: The X61 has been verified to use HBRV for something else, so
6429 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6430 * testing on the very early *60 Lenovo models...
6431 */
6432
6433 enum {
6434 TP_EC_BACKLIGHT = 0x31,
6435
6436 /* TP_EC_BACKLIGHT bitmasks */
6437 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6438 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6439 TP_EC_BACKLIGHT_MAPSW = 0x20,
6440 };
6441
6442 enum tpacpi_brightness_access_mode {
6443 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6444 TPACPI_BRGHT_MODE_EC, /* EC control */
6445 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6446 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6447 TPACPI_BRGHT_MODE_MAX
6448 };
6449
6450 static struct backlight_device *ibm_backlight_device;
6451
6452 static enum tpacpi_brightness_access_mode brightness_mode =
6453 TPACPI_BRGHT_MODE_MAX;
6454
6455 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6456
6457 static struct mutex brightness_mutex;
6458
6459 /* NVRAM brightness access */
tpacpi_brightness_nvram_get(void)6460 static unsigned int tpacpi_brightness_nvram_get(void)
6461 {
6462 u8 lnvram;
6463
6464 lockdep_assert_held(&brightness_mutex);
6465
6466 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6467 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6468 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6469 lnvram &= bright_maxlvl;
6470
6471 return lnvram;
6472 }
6473
tpacpi_brightness_checkpoint_nvram(void)6474 static void tpacpi_brightness_checkpoint_nvram(void)
6475 {
6476 u8 lec = 0;
6477 u8 b_nvram;
6478
6479 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6480 return;
6481
6482 vdbg_printk(TPACPI_DBG_BRGHT,
6483 "trying to checkpoint backlight level to NVRAM...\n");
6484
6485 if (mutex_lock_killable(&brightness_mutex) < 0)
6486 return;
6487
6488 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6489 goto unlock;
6490 lec &= TP_EC_BACKLIGHT_LVLMSK;
6491 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6492
6493 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6494 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6495 /* NVRAM needs update */
6496 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6497 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6498 b_nvram |= lec;
6499 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6500 dbg_printk(TPACPI_DBG_BRGHT,
6501 "updated NVRAM backlight level to %u (0x%02x)\n",
6502 (unsigned int) lec, (unsigned int) b_nvram);
6503 } else
6504 vdbg_printk(TPACPI_DBG_BRGHT,
6505 "NVRAM backlight level already is %u (0x%02x)\n",
6506 (unsigned int) lec, (unsigned int) b_nvram);
6507
6508 unlock:
6509 mutex_unlock(&brightness_mutex);
6510 }
6511
6512
tpacpi_brightness_get_raw(int * status)6513 static int tpacpi_brightness_get_raw(int *status)
6514 {
6515 u8 lec = 0;
6516
6517 lockdep_assert_held(&brightness_mutex);
6518
6519 switch (brightness_mode) {
6520 case TPACPI_BRGHT_MODE_UCMS_STEP:
6521 *status = tpacpi_brightness_nvram_get();
6522 return 0;
6523 case TPACPI_BRGHT_MODE_EC:
6524 case TPACPI_BRGHT_MODE_ECNVRAM:
6525 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6526 return -EIO;
6527 *status = lec;
6528 return 0;
6529 default:
6530 return -ENXIO;
6531 }
6532 }
6533
6534 /* do NOT call with illegal backlight level value */
tpacpi_brightness_set_ec(unsigned int value)6535 static int tpacpi_brightness_set_ec(unsigned int value)
6536 {
6537 u8 lec = 0;
6538
6539 lockdep_assert_held(&brightness_mutex);
6540
6541 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6542 return -EIO;
6543
6544 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6545 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6546 (value & TP_EC_BACKLIGHT_LVLMSK))))
6547 return -EIO;
6548
6549 return 0;
6550 }
6551
tpacpi_brightness_set_ucmsstep(unsigned int value)6552 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6553 {
6554 int cmos_cmd, inc;
6555 unsigned int current_value, i;
6556
6557 lockdep_assert_held(&brightness_mutex);
6558
6559 current_value = tpacpi_brightness_nvram_get();
6560
6561 if (value == current_value)
6562 return 0;
6563
6564 cmos_cmd = (value > current_value) ?
6565 TP_CMOS_BRIGHTNESS_UP :
6566 TP_CMOS_BRIGHTNESS_DOWN;
6567 inc = (value > current_value) ? 1 : -1;
6568
6569 for (i = current_value; i != value; i += inc)
6570 if (issue_thinkpad_cmos_command(cmos_cmd))
6571 return -EIO;
6572
6573 return 0;
6574 }
6575
6576 /* May return EINTR which can always be mapped to ERESTARTSYS */
brightness_set(unsigned int value)6577 static int brightness_set(unsigned int value)
6578 {
6579 int res;
6580
6581 if (value > bright_maxlvl)
6582 return -EINVAL;
6583
6584 vdbg_printk(TPACPI_DBG_BRGHT,
6585 "set backlight level to %d\n", value);
6586
6587 res = mutex_lock_killable(&brightness_mutex);
6588 if (res < 0)
6589 return res;
6590
6591 switch (brightness_mode) {
6592 case TPACPI_BRGHT_MODE_EC:
6593 case TPACPI_BRGHT_MODE_ECNVRAM:
6594 res = tpacpi_brightness_set_ec(value);
6595 break;
6596 case TPACPI_BRGHT_MODE_UCMS_STEP:
6597 res = tpacpi_brightness_set_ucmsstep(value);
6598 break;
6599 default:
6600 res = -ENXIO;
6601 }
6602
6603 mutex_unlock(&brightness_mutex);
6604 return res;
6605 }
6606
6607 /* sysfs backlight class ----------------------------------------------- */
6608
brightness_update_status(struct backlight_device * bd)6609 static int brightness_update_status(struct backlight_device *bd)
6610 {
6611 int level = backlight_get_brightness(bd);
6612
6613 dbg_printk(TPACPI_DBG_BRGHT,
6614 "backlight: attempt to set level to %d\n",
6615 level);
6616
6617 /* it is the backlight class's job (caller) to handle
6618 * EINTR and other errors properly */
6619 return brightness_set(level);
6620 }
6621
brightness_get(struct backlight_device * bd)6622 static int brightness_get(struct backlight_device *bd)
6623 {
6624 int status, res;
6625
6626 res = mutex_lock_killable(&brightness_mutex);
6627 if (res < 0)
6628 return 0;
6629
6630 res = tpacpi_brightness_get_raw(&status);
6631
6632 mutex_unlock(&brightness_mutex);
6633
6634 if (res < 0)
6635 return 0;
6636
6637 return status & TP_EC_BACKLIGHT_LVLMSK;
6638 }
6639
tpacpi_brightness_notify_change(void)6640 static void tpacpi_brightness_notify_change(void)
6641 {
6642 backlight_force_update(ibm_backlight_device,
6643 BACKLIGHT_UPDATE_HOTKEY);
6644 }
6645
6646 static const struct backlight_ops ibm_backlight_data = {
6647 .get_brightness = brightness_get,
6648 .update_status = brightness_update_status,
6649 };
6650
6651 /* --------------------------------------------------------------------- */
6652
tpacpi_evaluate_bcl(struct acpi_device * adev,void * not_used)6653 static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
6654 {
6655 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6656 union acpi_object *obj;
6657 acpi_status status;
6658 int rc;
6659
6660 status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
6661 if (ACPI_FAILURE(status))
6662 return 0;
6663
6664 obj = buffer.pointer;
6665 if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
6666 acpi_handle_info(adev->handle,
6667 "Unknown _BCL data, please report this to %s\n",
6668 TPACPI_MAIL);
6669 rc = 0;
6670 } else {
6671 rc = obj->package.count;
6672 }
6673 kfree(obj);
6674
6675 return rc;
6676 }
6677
6678 /*
6679 * Call _BCL method of video device. On some ThinkPads this will
6680 * switch the firmware to the ACPI brightness control mode.
6681 */
6682
tpacpi_query_bcl_levels(acpi_handle handle)6683 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6684 {
6685 struct acpi_device *device;
6686
6687 device = acpi_fetch_acpi_dev(handle);
6688 if (!device)
6689 return 0;
6690
6691 return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
6692 }
6693
6694
6695 /*
6696 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6697 */
tpacpi_check_std_acpi_brightness_support(void)6698 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6699 {
6700 acpi_handle video_device;
6701 int bcl_levels = 0;
6702
6703 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6704 if (video_device)
6705 bcl_levels = tpacpi_query_bcl_levels(video_device);
6706
6707 tp_features.bright_acpimode = (bcl_levels > 0);
6708
6709 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6710 }
6711
6712 /*
6713 * These are only useful for models that have only one possibility
6714 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6715 * these quirks.
6716 */
6717 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6718 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6719 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6720
6721 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6722 /* Models with ATI GPUs known to require ECNVRAM mode */
6723 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6724
6725 /* Models with ATI GPUs that can use ECNVRAM */
6726 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6727 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6728 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6729 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6730
6731 /* Models with Intel Extreme Graphics 2 */
6732 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6733 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6734 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6735
6736 /* Models with Intel GMA900 */
6737 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6738 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6739 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6740 };
6741
6742 /*
6743 * Returns < 0 for error, otherwise sets tp_features.bright_*
6744 * and bright_maxlvl.
6745 */
tpacpi_detect_brightness_capabilities(void)6746 static void __init tpacpi_detect_brightness_capabilities(void)
6747 {
6748 unsigned int b;
6749
6750 vdbg_printk(TPACPI_DBG_INIT,
6751 "detecting firmware brightness interface capabilities\n");
6752
6753 /* we could run a quirks check here (same table used by
6754 * brightness_init) if needed */
6755
6756 /*
6757 * We always attempt to detect acpi support, so as to switch
6758 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6759 * going to publish a backlight interface
6760 */
6761 b = tpacpi_check_std_acpi_brightness_support();
6762 switch (b) {
6763 case 16:
6764 bright_maxlvl = 15;
6765 break;
6766 case 8:
6767 case 0:
6768 bright_maxlvl = 7;
6769 break;
6770 default:
6771 tp_features.bright_unkfw = 1;
6772 bright_maxlvl = b - 1;
6773 }
6774 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6775 }
6776
brightness_init(struct ibm_init_struct * iibm)6777 static int __init brightness_init(struct ibm_init_struct *iibm)
6778 {
6779 struct backlight_properties props;
6780 int b;
6781 unsigned long quirks;
6782
6783 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6784
6785 mutex_init(&brightness_mutex);
6786
6787 quirks = tpacpi_check_quirks(brightness_quirk_table,
6788 ARRAY_SIZE(brightness_quirk_table));
6789
6790 /* tpacpi_detect_brightness_capabilities() must have run already */
6791
6792 /* if it is unknown, we don't handle it: it wouldn't be safe */
6793 if (tp_features.bright_unkfw)
6794 return -ENODEV;
6795
6796 if (!brightness_enable) {
6797 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6798 "brightness support disabled by module parameter\n");
6799 return -ENODEV;
6800 }
6801
6802 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6803 if (brightness_enable > 1) {
6804 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6805 return -ENODEV;
6806 } else if (brightness_enable == 1) {
6807 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6808 return -ENODEV;
6809 }
6810 } else if (!tp_features.bright_acpimode) {
6811 pr_notice("ACPI backlight interface not available\n");
6812 return -ENODEV;
6813 }
6814
6815 pr_notice("ACPI native brightness control enabled\n");
6816
6817 /*
6818 * Check for module parameter bogosity, note that we
6819 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6820 * able to detect "unspecified"
6821 */
6822 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6823 return -EINVAL;
6824
6825 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6826 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6827 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6828 if (quirks & TPACPI_BRGHT_Q_EC)
6829 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6830 else
6831 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6832
6833 dbg_printk(TPACPI_DBG_BRGHT,
6834 "driver auto-selected brightness_mode=%d\n",
6835 brightness_mode);
6836 }
6837
6838 /* Safety */
6839 if (!tpacpi_is_ibm() &&
6840 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6841 brightness_mode == TPACPI_BRGHT_MODE_EC))
6842 return -EINVAL;
6843
6844 if (tpacpi_brightness_get_raw(&b) < 0)
6845 return -ENODEV;
6846
6847 memset(&props, 0, sizeof(struct backlight_properties));
6848 props.type = BACKLIGHT_PLATFORM;
6849 props.max_brightness = bright_maxlvl;
6850 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6851 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6852 NULL, NULL,
6853 &ibm_backlight_data,
6854 &props);
6855 if (IS_ERR(ibm_backlight_device)) {
6856 int rc = PTR_ERR(ibm_backlight_device);
6857 ibm_backlight_device = NULL;
6858 pr_err("Could not register backlight device\n");
6859 return rc;
6860 }
6861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6862 "brightness is supported\n");
6863
6864 if (quirks & TPACPI_BRGHT_Q_ASK) {
6865 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6866 brightness_mode);
6867 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6868 TPACPI_MAIL);
6869 }
6870
6871 /* Added by mistake in early 2007. Probably useless, but it could
6872 * be working around some unknown firmware problem where the value
6873 * read at startup doesn't match the real hardware state... so leave
6874 * it in place just in case */
6875 backlight_update_status(ibm_backlight_device);
6876
6877 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6878 "brightness: registering brightness hotkeys as change notification\n");
6879 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6880 | TP_ACPI_HKEY_BRGHTUP_MASK
6881 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6882 return 0;
6883 }
6884
brightness_suspend(void)6885 static void brightness_suspend(void)
6886 {
6887 tpacpi_brightness_checkpoint_nvram();
6888 }
6889
brightness_shutdown(void)6890 static void brightness_shutdown(void)
6891 {
6892 tpacpi_brightness_checkpoint_nvram();
6893 }
6894
brightness_exit(void)6895 static void brightness_exit(void)
6896 {
6897 if (ibm_backlight_device) {
6898 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6899 "calling backlight_device_unregister()\n");
6900 backlight_device_unregister(ibm_backlight_device);
6901 }
6902
6903 tpacpi_brightness_checkpoint_nvram();
6904 }
6905
brightness_read(struct seq_file * m)6906 static int brightness_read(struct seq_file *m)
6907 {
6908 int level;
6909
6910 level = brightness_get(NULL);
6911 if (level < 0) {
6912 seq_puts(m, "level:\t\tunreadable\n");
6913 } else {
6914 seq_printf(m, "level:\t\t%d\n", level);
6915 seq_puts(m, "commands:\tup, down\n");
6916 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6917 bright_maxlvl);
6918 }
6919
6920 return 0;
6921 }
6922
brightness_write(char * buf)6923 static int brightness_write(char *buf)
6924 {
6925 int level;
6926 int rc;
6927 char *cmd;
6928
6929 level = brightness_get(NULL);
6930 if (level < 0)
6931 return level;
6932
6933 while ((cmd = strsep(&buf, ","))) {
6934 if (strstarts(cmd, "up")) {
6935 if (level < bright_maxlvl)
6936 level++;
6937 } else if (strstarts(cmd, "down")) {
6938 if (level > 0)
6939 level--;
6940 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6941 level >= 0 && level <= bright_maxlvl) {
6942 /* new level set */
6943 } else
6944 return -EINVAL;
6945 }
6946
6947 tpacpi_disclose_usertask("procfs brightness",
6948 "set level to %d\n", level);
6949
6950 /*
6951 * Now we know what the final level should be, so we try to set it.
6952 * Doing it this way makes the syscall restartable in case of EINTR
6953 */
6954 rc = brightness_set(level);
6955 if (!rc && ibm_backlight_device)
6956 backlight_force_update(ibm_backlight_device,
6957 BACKLIGHT_UPDATE_SYSFS);
6958 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6959 }
6960
6961 static struct ibm_struct brightness_driver_data = {
6962 .name = "brightness",
6963 .read = brightness_read,
6964 .write = brightness_write,
6965 .exit = brightness_exit,
6966 .suspend = brightness_suspend,
6967 .shutdown = brightness_shutdown,
6968 };
6969
6970 /*************************************************************************
6971 * Volume subdriver
6972 */
6973
6974 /*
6975 * IBM ThinkPads have a simple volume controller with MUTE gating.
6976 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6977 *
6978 * Since the *61 series (and probably also the later *60 series), Lenovo
6979 * ThinkPads only implement the MUTE gate.
6980 *
6981 * EC register 0x30
6982 * Bit 6: MUTE (1 mutes sound)
6983 * Bit 3-0: Volume
6984 * Other bits should be zero as far as we know.
6985 *
6986 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6987 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6988 * such as bit 7 which is used to detect repeated presses of MUTE,
6989 * and we leave them unchanged.
6990 *
6991 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6992 * in response to user input. Unfortunately, this rarely works well.
6993 * The laptop changes the state of its internal MUTE gate and, on some
6994 * models, sends KEY_MUTE, causing any user code that responds to the
6995 * mute button to get confused. The hardware MUTE gate is also
6996 * unnecessary, since user code can handle the mute button without
6997 * kernel or EC help.
6998 *
6999 * To avoid confusing userspace, we simply disable all EC-based mute
7000 * and volume controls when possible.
7001 */
7002
7003 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7004
7005 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7006 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7007 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7008
7009 #if SNDRV_CARDS <= 32
7010 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7011 #else
7012 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7013 #endif
7014 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7015 static char *alsa_id = "ThinkPadEC";
7016 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7017
7018 struct tpacpi_alsa_data {
7019 struct snd_card *card;
7020 struct snd_ctl_elem_id *ctl_mute_id;
7021 struct snd_ctl_elem_id *ctl_vol_id;
7022 };
7023
7024 static struct snd_card *alsa_card;
7025
7026 enum {
7027 TP_EC_AUDIO = 0x30,
7028
7029 /* TP_EC_AUDIO bits */
7030 TP_EC_AUDIO_MUTESW = 6,
7031
7032 /* TP_EC_AUDIO bitmasks */
7033 TP_EC_AUDIO_LVL_MSK = 0x0F,
7034 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7035
7036 /* Maximum volume */
7037 TP_EC_VOLUME_MAX = 14,
7038 };
7039
7040 enum tpacpi_volume_access_mode {
7041 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7042 TPACPI_VOL_MODE_EC, /* Pure EC control */
7043 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7044 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7045 TPACPI_VOL_MODE_MAX
7046 };
7047
7048 enum tpacpi_volume_capabilities {
7049 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7050 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7051 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7052 TPACPI_VOL_CAP_MAX
7053 };
7054
7055 enum tpacpi_mute_btn_mode {
7056 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7057 /* We don't know what mode 1 is. */
7058 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7059 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7060 };
7061
7062 static enum tpacpi_volume_access_mode volume_mode =
7063 TPACPI_VOL_MODE_MAX;
7064
7065 static enum tpacpi_volume_capabilities volume_capabilities;
7066 static bool volume_control_allowed;
7067 static bool software_mute_requested = true;
7068 static bool software_mute_active;
7069 static int software_mute_orig_mode;
7070
7071 /*
7072 * Used to syncronize writers to TP_EC_AUDIO and
7073 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7074 */
7075 static struct mutex volume_mutex;
7076
tpacpi_volume_checkpoint_nvram(void)7077 static void tpacpi_volume_checkpoint_nvram(void)
7078 {
7079 u8 lec = 0;
7080 u8 b_nvram;
7081 u8 ec_mask;
7082
7083 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7084 return;
7085 if (!volume_control_allowed)
7086 return;
7087 if (software_mute_active)
7088 return;
7089
7090 vdbg_printk(TPACPI_DBG_MIXER,
7091 "trying to checkpoint mixer state to NVRAM...\n");
7092
7093 if (tp_features.mixer_no_level_control)
7094 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7095 else
7096 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7097
7098 if (mutex_lock_killable(&volume_mutex) < 0)
7099 return;
7100
7101 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7102 goto unlock;
7103 lec &= ec_mask;
7104 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7105
7106 if (lec != (b_nvram & ec_mask)) {
7107 /* NVRAM needs update */
7108 b_nvram &= ~ec_mask;
7109 b_nvram |= lec;
7110 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7111 dbg_printk(TPACPI_DBG_MIXER,
7112 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7113 (unsigned int) lec, (unsigned int) b_nvram);
7114 } else {
7115 vdbg_printk(TPACPI_DBG_MIXER,
7116 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7117 (unsigned int) lec, (unsigned int) b_nvram);
7118 }
7119
7120 unlock:
7121 mutex_unlock(&volume_mutex);
7122 }
7123
volume_get_status_ec(u8 * status)7124 static int volume_get_status_ec(u8 *status)
7125 {
7126 u8 s;
7127
7128 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7129 return -EIO;
7130
7131 *status = s;
7132
7133 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7134
7135 return 0;
7136 }
7137
volume_get_status(u8 * status)7138 static int volume_get_status(u8 *status)
7139 {
7140 return volume_get_status_ec(status);
7141 }
7142
volume_set_status_ec(const u8 status)7143 static int volume_set_status_ec(const u8 status)
7144 {
7145 if (!acpi_ec_write(TP_EC_AUDIO, status))
7146 return -EIO;
7147
7148 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7149
7150 /*
7151 * On X200s, and possibly on others, it can take a while for
7152 * reads to become correct.
7153 */
7154 msleep(1);
7155
7156 return 0;
7157 }
7158
volume_set_status(const u8 status)7159 static int volume_set_status(const u8 status)
7160 {
7161 return volume_set_status_ec(status);
7162 }
7163
7164 /* returns < 0 on error, 0 on no change, 1 on change */
__volume_set_mute_ec(const bool mute)7165 static int __volume_set_mute_ec(const bool mute)
7166 {
7167 int rc;
7168 u8 s, n;
7169
7170 if (mutex_lock_killable(&volume_mutex) < 0)
7171 return -EINTR;
7172
7173 rc = volume_get_status_ec(&s);
7174 if (rc)
7175 goto unlock;
7176
7177 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7178 s & ~TP_EC_AUDIO_MUTESW_MSK;
7179
7180 if (n != s) {
7181 rc = volume_set_status_ec(n);
7182 if (!rc)
7183 rc = 1;
7184 }
7185
7186 unlock:
7187 mutex_unlock(&volume_mutex);
7188 return rc;
7189 }
7190
volume_alsa_set_mute(const bool mute)7191 static int volume_alsa_set_mute(const bool mute)
7192 {
7193 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7194 (mute) ? "" : "un");
7195 return __volume_set_mute_ec(mute);
7196 }
7197
volume_set_mute(const bool mute)7198 static int volume_set_mute(const bool mute)
7199 {
7200 int rc;
7201
7202 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7203 (mute) ? "" : "un");
7204
7205 rc = __volume_set_mute_ec(mute);
7206 return (rc < 0) ? rc : 0;
7207 }
7208
7209 /* returns < 0 on error, 0 on no change, 1 on change */
__volume_set_volume_ec(const u8 vol)7210 static int __volume_set_volume_ec(const u8 vol)
7211 {
7212 int rc;
7213 u8 s, n;
7214
7215 if (vol > TP_EC_VOLUME_MAX)
7216 return -EINVAL;
7217
7218 if (mutex_lock_killable(&volume_mutex) < 0)
7219 return -EINTR;
7220
7221 rc = volume_get_status_ec(&s);
7222 if (rc)
7223 goto unlock;
7224
7225 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7226
7227 if (n != s) {
7228 rc = volume_set_status_ec(n);
7229 if (!rc)
7230 rc = 1;
7231 }
7232
7233 unlock:
7234 mutex_unlock(&volume_mutex);
7235 return rc;
7236 }
7237
volume_set_software_mute(bool startup)7238 static int volume_set_software_mute(bool startup)
7239 {
7240 int result;
7241
7242 if (!tpacpi_is_lenovo())
7243 return -ENODEV;
7244
7245 if (startup) {
7246 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7247 "HAUM", "qd"))
7248 return -EIO;
7249
7250 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7251 "Initial HAUM setting was %d\n",
7252 software_mute_orig_mode);
7253 }
7254
7255 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7256 (int)TP_EC_MUTE_BTN_NONE))
7257 return -EIO;
7258
7259 if (result != TP_EC_MUTE_BTN_NONE)
7260 pr_warn("Unexpected SAUM result %d\n",
7261 result);
7262
7263 /*
7264 * In software mute mode, the standard codec controls take
7265 * precendence, so we unmute the ThinkPad HW switch at
7266 * startup. Just on case there are SAUM-capable ThinkPads
7267 * with level controls, set max HW volume as well.
7268 */
7269 if (tp_features.mixer_no_level_control)
7270 result = volume_set_mute(false);
7271 else
7272 result = volume_set_status(TP_EC_VOLUME_MAX);
7273
7274 if (result != 0)
7275 pr_warn("Failed to unmute the HW mute switch\n");
7276
7277 return 0;
7278 }
7279
volume_exit_software_mute(void)7280 static void volume_exit_software_mute(void)
7281 {
7282 int r;
7283
7284 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7285 || r != software_mute_orig_mode)
7286 pr_warn("Failed to restore mute mode\n");
7287 }
7288
volume_alsa_set_volume(const u8 vol)7289 static int volume_alsa_set_volume(const u8 vol)
7290 {
7291 dbg_printk(TPACPI_DBG_MIXER,
7292 "ALSA: trying to set volume level to %hu\n", vol);
7293 return __volume_set_volume_ec(vol);
7294 }
7295
volume_alsa_notify_change(void)7296 static void volume_alsa_notify_change(void)
7297 {
7298 struct tpacpi_alsa_data *d;
7299
7300 if (alsa_card && alsa_card->private_data) {
7301 d = alsa_card->private_data;
7302 if (d->ctl_mute_id)
7303 snd_ctl_notify(alsa_card,
7304 SNDRV_CTL_EVENT_MASK_VALUE,
7305 d->ctl_mute_id);
7306 if (d->ctl_vol_id)
7307 snd_ctl_notify(alsa_card,
7308 SNDRV_CTL_EVENT_MASK_VALUE,
7309 d->ctl_vol_id);
7310 }
7311 }
7312
volume_alsa_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)7313 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7314 struct snd_ctl_elem_info *uinfo)
7315 {
7316 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7317 uinfo->count = 1;
7318 uinfo->value.integer.min = 0;
7319 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7320 return 0;
7321 }
7322
volume_alsa_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7323 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7324 struct snd_ctl_elem_value *ucontrol)
7325 {
7326 u8 s;
7327 int rc;
7328
7329 rc = volume_get_status(&s);
7330 if (rc < 0)
7331 return rc;
7332
7333 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7334 return 0;
7335 }
7336
volume_alsa_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7337 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7338 struct snd_ctl_elem_value *ucontrol)
7339 {
7340 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7341 ucontrol->value.integer.value[0]);
7342 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7343 }
7344
7345 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7346
volume_alsa_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7347 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7348 struct snd_ctl_elem_value *ucontrol)
7349 {
7350 u8 s;
7351 int rc;
7352
7353 rc = volume_get_status(&s);
7354 if (rc < 0)
7355 return rc;
7356
7357 ucontrol->value.integer.value[0] =
7358 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7359 return 0;
7360 }
7361
volume_alsa_mute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)7362 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7363 struct snd_ctl_elem_value *ucontrol)
7364 {
7365 tpacpi_disclose_usertask("ALSA", "%smute\n",
7366 ucontrol->value.integer.value[0] ?
7367 "un" : "");
7368 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7369 }
7370
7371 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7373 .name = "Console Playback Volume",
7374 .index = 0,
7375 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7376 .info = volume_alsa_vol_info,
7377 .get = volume_alsa_vol_get,
7378 };
7379
7380 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7381 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7382 .name = "Console Playback Switch",
7383 .index = 0,
7384 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7385 .info = volume_alsa_mute_info,
7386 .get = volume_alsa_mute_get,
7387 };
7388
volume_suspend(void)7389 static void volume_suspend(void)
7390 {
7391 tpacpi_volume_checkpoint_nvram();
7392 }
7393
volume_resume(void)7394 static void volume_resume(void)
7395 {
7396 if (software_mute_active) {
7397 if (volume_set_software_mute(false) < 0)
7398 pr_warn("Failed to restore software mute\n");
7399 } else {
7400 volume_alsa_notify_change();
7401 }
7402 }
7403
volume_shutdown(void)7404 static void volume_shutdown(void)
7405 {
7406 tpacpi_volume_checkpoint_nvram();
7407 }
7408
volume_exit(void)7409 static void volume_exit(void)
7410 {
7411 if (alsa_card) {
7412 snd_card_free(alsa_card);
7413 alsa_card = NULL;
7414 }
7415
7416 tpacpi_volume_checkpoint_nvram();
7417
7418 if (software_mute_active)
7419 volume_exit_software_mute();
7420 }
7421
volume_create_alsa_mixer(void)7422 static int __init volume_create_alsa_mixer(void)
7423 {
7424 struct snd_card *card;
7425 struct tpacpi_alsa_data *data;
7426 struct snd_kcontrol *ctl_vol;
7427 struct snd_kcontrol *ctl_mute;
7428 int rc;
7429
7430 rc = snd_card_new(&tpacpi_pdev->dev,
7431 alsa_index, alsa_id, THIS_MODULE,
7432 sizeof(struct tpacpi_alsa_data), &card);
7433 if (rc < 0 || !card) {
7434 pr_err("Failed to create ALSA card structures: %d\n", rc);
7435 return -ENODEV;
7436 }
7437
7438 BUG_ON(!card->private_data);
7439 data = card->private_data;
7440 data->card = card;
7441
7442 strscpy(card->driver, TPACPI_ALSA_DRVNAME);
7443 strscpy(card->shortname, TPACPI_ALSA_SHRTNAME);
7444 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7445 (thinkpad_id.ec_version_str) ?
7446 thinkpad_id.ec_version_str : "(unknown)");
7447 snprintf(card->longname, sizeof(card->longname),
7448 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7449 (thinkpad_id.ec_version_str) ?
7450 thinkpad_id.ec_version_str : "unknown");
7451
7452 if (volume_control_allowed) {
7453 volume_alsa_control_vol.put = volume_alsa_vol_put;
7454 volume_alsa_control_vol.access =
7455 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7456
7457 volume_alsa_control_mute.put = volume_alsa_mute_put;
7458 volume_alsa_control_mute.access =
7459 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7460 }
7461
7462 if (!tp_features.mixer_no_level_control) {
7463 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7464 rc = snd_ctl_add(card, ctl_vol);
7465 if (rc < 0) {
7466 pr_err("Failed to create ALSA volume control: %d\n",
7467 rc);
7468 goto err_exit;
7469 }
7470 data->ctl_vol_id = &ctl_vol->id;
7471 }
7472
7473 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7474 rc = snd_ctl_add(card, ctl_mute);
7475 if (rc < 0) {
7476 pr_err("Failed to create ALSA mute control: %d\n", rc);
7477 goto err_exit;
7478 }
7479 data->ctl_mute_id = &ctl_mute->id;
7480
7481 rc = snd_card_register(card);
7482 if (rc < 0) {
7483 pr_err("Failed to register ALSA card: %d\n", rc);
7484 goto err_exit;
7485 }
7486
7487 alsa_card = card;
7488 return 0;
7489
7490 err_exit:
7491 snd_card_free(card);
7492 return -ENODEV;
7493 }
7494
7495 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7496 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7497
7498 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7499 /* Whitelist volume level on all IBM by default */
7500 { .vendor = PCI_VENDOR_ID_IBM,
7501 .bios = TPACPI_MATCH_ANY,
7502 .ec = TPACPI_MATCH_ANY,
7503 .quirks = TPACPI_VOL_Q_LEVEL },
7504
7505 /* Lenovo models with volume control (needs confirmation) */
7506 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7507 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7508 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7509 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7510 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7511 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7512 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7513
7514 /* Whitelist mute-only on all Lenovo by default */
7515 { .vendor = PCI_VENDOR_ID_LENOVO,
7516 .bios = TPACPI_MATCH_ANY,
7517 .ec = TPACPI_MATCH_ANY,
7518 .quirks = TPACPI_VOL_Q_MUTEONLY }
7519 };
7520
volume_init(struct ibm_init_struct * iibm)7521 static int __init volume_init(struct ibm_init_struct *iibm)
7522 {
7523 unsigned long quirks;
7524 int rc;
7525
7526 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7527
7528 mutex_init(&volume_mutex);
7529
7530 /*
7531 * Check for module parameter bogosity, note that we
7532 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7533 * able to detect "unspecified"
7534 */
7535 if (volume_mode > TPACPI_VOL_MODE_MAX)
7536 return -EINVAL;
7537
7538 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7539 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7540 TPACPI_MAIL);
7541 return -ENODEV;
7542 }
7543
7544 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7545 return -EINVAL;
7546
7547 /*
7548 * The ALSA mixer is our primary interface.
7549 * When disabled, don't install the subdriver at all
7550 */
7551 if (!alsa_enable) {
7552 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7553 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7554 return -ENODEV;
7555 }
7556
7557 quirks = tpacpi_check_quirks(volume_quirk_table,
7558 ARRAY_SIZE(volume_quirk_table));
7559
7560 switch (volume_capabilities) {
7561 case TPACPI_VOL_CAP_AUTO:
7562 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7563 tp_features.mixer_no_level_control = 1;
7564 else if (quirks & TPACPI_VOL_Q_LEVEL)
7565 tp_features.mixer_no_level_control = 0;
7566 else
7567 return -ENODEV; /* no mixer */
7568 break;
7569 case TPACPI_VOL_CAP_VOLMUTE:
7570 tp_features.mixer_no_level_control = 0;
7571 break;
7572 case TPACPI_VOL_CAP_MUTEONLY:
7573 tp_features.mixer_no_level_control = 1;
7574 break;
7575 default:
7576 return -ENODEV;
7577 }
7578
7579 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7580 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7581 "using user-supplied volume_capabilities=%d\n",
7582 volume_capabilities);
7583
7584 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7585 volume_mode == TPACPI_VOL_MODE_MAX) {
7586 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7587
7588 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7589 "driver auto-selected volume_mode=%d\n",
7590 volume_mode);
7591 } else {
7592 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7593 "using user-supplied volume_mode=%d\n",
7594 volume_mode);
7595 }
7596
7597 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7598 "mute is supported, volume control is %s\n",
7599 str_supported(!tp_features.mixer_no_level_control));
7600
7601 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7602 software_mute_active = true;
7603 } else {
7604 rc = volume_create_alsa_mixer();
7605 if (rc) {
7606 pr_err("Could not create the ALSA mixer interface\n");
7607 return rc;
7608 }
7609
7610 pr_info("Console audio control enabled, mode: %s\n",
7611 (volume_control_allowed) ?
7612 "override (read/write)" :
7613 "monitor (read only)");
7614 }
7615
7616 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7617 "registering volume hotkeys as change notification\n");
7618 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7619 | TP_ACPI_HKEY_VOLUP_MASK
7620 | TP_ACPI_HKEY_VOLDWN_MASK
7621 | TP_ACPI_HKEY_MUTE_MASK);
7622
7623 return 0;
7624 }
7625
volume_read(struct seq_file * m)7626 static int volume_read(struct seq_file *m)
7627 {
7628 u8 status;
7629
7630 if (volume_get_status(&status) < 0) {
7631 seq_puts(m, "level:\t\tunreadable\n");
7632 } else {
7633 if (tp_features.mixer_no_level_control)
7634 seq_puts(m, "level:\t\tunsupported\n");
7635 else
7636 seq_printf(m, "level:\t\t%d\n",
7637 status & TP_EC_AUDIO_LVL_MSK);
7638
7639 seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
7640
7641 if (volume_control_allowed) {
7642 seq_puts(m, "commands:\tunmute, mute\n");
7643 if (!tp_features.mixer_no_level_control) {
7644 seq_puts(m, "commands:\tup, down\n");
7645 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7646 TP_EC_VOLUME_MAX);
7647 }
7648 }
7649 }
7650
7651 return 0;
7652 }
7653
volume_write(char * buf)7654 static int volume_write(char *buf)
7655 {
7656 u8 s;
7657 u8 new_level, new_mute;
7658 int l;
7659 char *cmd;
7660 int rc;
7661
7662 /*
7663 * We do allow volume control at driver startup, so that the
7664 * user can set initial state through the volume=... parameter hack.
7665 */
7666 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7667 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7668 tp_warned.volume_ctrl_forbidden = 1;
7669 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7670 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7671 }
7672 return -EPERM;
7673 }
7674
7675 rc = volume_get_status(&s);
7676 if (rc < 0)
7677 return rc;
7678
7679 new_level = s & TP_EC_AUDIO_LVL_MSK;
7680 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7681
7682 while ((cmd = strsep(&buf, ","))) {
7683 if (!tp_features.mixer_no_level_control) {
7684 if (strstarts(cmd, "up")) {
7685 if (new_mute)
7686 new_mute = 0;
7687 else if (new_level < TP_EC_VOLUME_MAX)
7688 new_level++;
7689 continue;
7690 } else if (strstarts(cmd, "down")) {
7691 if (new_mute)
7692 new_mute = 0;
7693 else if (new_level > 0)
7694 new_level--;
7695 continue;
7696 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7697 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7698 new_level = l;
7699 continue;
7700 }
7701 }
7702 if (strstarts(cmd, "mute"))
7703 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7704 else if (strstarts(cmd, "unmute"))
7705 new_mute = 0;
7706 else
7707 return -EINVAL;
7708 }
7709
7710 if (tp_features.mixer_no_level_control) {
7711 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7712 new_mute ? "" : "un");
7713 rc = volume_set_mute(!!new_mute);
7714 } else {
7715 tpacpi_disclose_usertask("procfs volume",
7716 "%smute and set level to %d\n",
7717 new_mute ? "" : "un", new_level);
7718 rc = volume_set_status(new_mute | new_level);
7719 }
7720 volume_alsa_notify_change();
7721
7722 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7723 }
7724
7725 static struct ibm_struct volume_driver_data = {
7726 .name = "volume",
7727 .read = volume_read,
7728 .write = volume_write,
7729 .exit = volume_exit,
7730 .suspend = volume_suspend,
7731 .resume = volume_resume,
7732 .shutdown = volume_shutdown,
7733 };
7734
7735 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7736
7737 #define alsa_card NULL
7738
volume_alsa_notify_change(void)7739 static inline void volume_alsa_notify_change(void)
7740 {
7741 }
7742
volume_init(struct ibm_init_struct * iibm)7743 static int __init volume_init(struct ibm_init_struct *iibm)
7744 {
7745 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7746
7747 return -ENODEV;
7748 }
7749
7750 static struct ibm_struct volume_driver_data = {
7751 .name = "volume",
7752 };
7753
7754 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7755
7756 /*************************************************************************
7757 * Fan subdriver
7758 */
7759
7760 /*
7761 * FAN ACCESS MODES
7762 *
7763 * TPACPI_FAN_RD_ACPI_GFAN:
7764 * ACPI GFAN method: returns fan level
7765 *
7766 * see TPACPI_FAN_WR_ACPI_SFAN
7767 * EC 0x2f (HFSP) not available if GFAN exists
7768 *
7769 * TPACPI_FAN_WR_ACPI_SFAN:
7770 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7771 *
7772 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7773 * it for writing.
7774 *
7775 * TPACPI_FAN_RD_ACPI_FANG:
7776 * ACPI FANG method: returns fan control register
7777 *
7778 * Takes one parameter which is 0x8100 plus the offset to EC memory
7779 * address 0xf500 and returns the byte at this address.
7780 *
7781 * 0xf500:
7782 * When the value is less than 9 automatic mode is enabled
7783 * 0xf502:
7784 * Contains the current fan speed from 0-100%
7785 * 0xf506:
7786 * Bit 7 has to be set in order to enable manual control by
7787 * writing a value >= 9 to 0xf500
7788 *
7789 * TPACPI_FAN_WR_ACPI_FANW:
7790 * ACPI FANW method: sets fan control registers
7791 *
7792 * Takes 0x8100 plus the offset to EC memory address 0xf500 and the
7793 * value to be written there as parameters.
7794 *
7795 * see TPACPI_FAN_RD_ACPI_FANG
7796 *
7797 * TPACPI_FAN_WR_TPEC:
7798 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7799 * Supported on almost all ThinkPads
7800 *
7801 * Fan speed changes of any sort (including those caused by the
7802 * disengaged mode) are usually done slowly by the firmware as the
7803 * maximum amount of fan duty cycle change per second seems to be
7804 * limited.
7805 *
7806 * Reading is not available if GFAN exists.
7807 * Writing is not available if SFAN exists.
7808 *
7809 * Bits
7810 * 7 automatic mode engaged;
7811 * (default operation mode of the ThinkPad)
7812 * fan level is ignored in this mode.
7813 * 6 full speed mode (takes precedence over bit 7);
7814 * not available on all thinkpads. May disable
7815 * the tachometer while the fan controller ramps up
7816 * the speed (which can take up to a few *minutes*).
7817 * Speeds up fan to 100% duty-cycle, which is far above
7818 * the standard RPM levels. It is not impossible that
7819 * it could cause hardware damage.
7820 * 5-3 unused in some models. Extra bits for fan level
7821 * in others, but still useless as all values above
7822 * 7 map to the same speed as level 7 in these models.
7823 * 2-0 fan level (0..7 usually)
7824 * 0x00 = stop
7825 * 0x07 = max (set when temperatures critical)
7826 * Some ThinkPads may have other levels, see
7827 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7828 *
7829 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7830 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7831 * does so, its initial value is meaningless (0x07).
7832 *
7833 * For firmware bugs, refer to:
7834 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7835 *
7836 * ----
7837 *
7838 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7839 * Main fan tachometer reading (in RPM)
7840 *
7841 * This register is present on all ThinkPads with a new-style EC, and
7842 * it is known not to be present on the A21m/e, and T22, as there is
7843 * something else in offset 0x84 according to the ACPI DSDT. Other
7844 * ThinkPads from this same time period (and earlier) probably lack the
7845 * tachometer as well.
7846 *
7847 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7848 * was never fixed by IBM to report the EC firmware version string
7849 * probably support the tachometer (like the early X models), so
7850 * detecting it is quite hard. We need more data to know for sure.
7851 *
7852 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7853 * might result.
7854 *
7855 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7856 * mode.
7857 *
7858 * For firmware bugs, refer to:
7859 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7860 *
7861 * ----
7862 *
7863 * ThinkPad EC register 0x31 bit 0 (only on select models)
7864 *
7865 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7866 * show the speed of the main fan. When bit 0 of EC register 0x31
7867 * is one, the tachometer registers show the speed of the auxiliary
7868 * fan.
7869 *
7870 * Fan control seems to affect both fans, regardless of the state
7871 * of this bit.
7872 *
7873 * So far, only the firmware for the X60/X61 non-tablet versions
7874 * seem to support this (firmware TP-7M).
7875 *
7876 * TPACPI_FAN_WR_ACPI_FANS:
7877 * ThinkPad X31, X40, X41. Not available in the X60.
7878 *
7879 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7880 * high speed. ACPI DSDT seems to map these three speeds to levels
7881 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7882 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7883 *
7884 * The speeds are stored on handles
7885 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7886 *
7887 * There are three default speed sets, accessible as handles:
7888 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7889 *
7890 * ACPI DSDT switches which set is in use depending on various
7891 * factors.
7892 *
7893 * TPACPI_FAN_WR_TPEC is also available and should be used to
7894 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7895 * but the ACPI tables just mention level 7.
7896 *
7897 * TPACPI_FAN_RD_TPEC_NS:
7898 * This mode is used for a few ThinkPads (L13 Yoga Gen2, X13 Yoga Gen2 etc.)
7899 * that are using non-standard EC locations for reporting fan speeds.
7900 * Currently these platforms only provide fan rpm reporting.
7901 *
7902 */
7903
7904 #define FAN_RPM_CAL_CONST 491520 /* FAN RPM calculation offset for some non-standard ECFW */
7905
7906 #define FAN_NS_CTRL_STATUS BIT(2) /* Bit which determines control is enabled or not */
7907 #define FAN_NS_CTRL BIT(4) /* Bit which determines control is by host or EC */
7908 #define FAN_CLOCK_TPM (22500*60) /* Ticks per minute for a 22.5 kHz clock */
7909
7910 enum { /* Fan control constants */
7911 fan_status_offset = 0x2f, /* EC register 0x2f */
7912 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7913 * 0x84 must be read before 0x85 */
7914 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7915 bit 0 selects which fan is active */
7916
7917 fan_status_offset_ns = 0x93, /* Special status/control offset for non-standard EC Fan1 */
7918 fan2_status_offset_ns = 0x96, /* Special status/control offset for non-standard EC Fan2 */
7919 fan_rpm_status_ns = 0x95, /* Special offset for Fan1 RPM status for non-standard EC */
7920 fan2_rpm_status_ns = 0x98, /* Special offset for Fan2 RPM status for non-standard EC */
7921
7922 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7923 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7924
7925 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7926 };
7927
7928 enum fan_status_access_mode {
7929 TPACPI_FAN_NONE = 0, /* No fan status or control */
7930 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7931 TPACPI_FAN_RD_ACPI_FANG, /* Use ACPI FANG */
7932 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7933 TPACPI_FAN_RD_TPEC_NS, /* Use non-standard ACPI EC regs (eg: L13 Yoga gen2 etc.) */
7934 };
7935
7936 enum fan_control_access_mode {
7937 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7938 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7939 TPACPI_FAN_WR_ACPI_FANW, /* Use ACPI FANW */
7940 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7941 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7942 };
7943
7944 enum fan_control_commands {
7945 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7946 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7947 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7948 * and also watchdog cmd */
7949 };
7950
7951 static bool fan_control_allowed;
7952
7953 static enum fan_status_access_mode fan_status_access_mode;
7954 static enum fan_control_access_mode fan_control_access_mode;
7955 static enum fan_control_commands fan_control_commands;
7956
7957 static u8 fan_control_initial_status;
7958 static u8 fan_control_desired_level;
7959 static u8 fan_control_resume_level;
7960 static int fan_watchdog_maxinterval;
7961
7962 static bool fan_with_ns_addr;
7963 static bool ecfw_with_fan_dec_rpm;
7964 static bool fan_speed_in_tpr;
7965
7966 static struct mutex fan_mutex;
7967
7968 static void fan_watchdog_fire(struct work_struct *ignored);
7969 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7970
7971 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7972 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7973 "\\FSPD", /* 600e/x, 770e, 770x */
7974 ); /* all others */
7975 TPACPI_HANDLE(fang, ec, "FANG", /* E531 */
7976 ); /* all others */
7977 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7978 "JFNS", /* 770x-JL */
7979 ); /* all others */
7980 TPACPI_HANDLE(fanw, ec, "FANW", /* E531 */
7981 ); /* all others */
7982
7983 /*
7984 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7985 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7986 * be in auto mode (0x80).
7987 *
7988 * This is corrected by any write to HFSP either by the driver, or
7989 * by the firmware.
7990 *
7991 * We assume 0x07 really means auto mode while this quirk is active,
7992 * as this is far more likely than the ThinkPad being in level 7,
7993 * which is only used by the firmware during thermal emergencies.
7994 *
7995 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7996 * TP-70 (T43, R52), which are known to be buggy.
7997 */
7998
fan_quirk1_setup(void)7999 static void fan_quirk1_setup(void)
8000 {
8001 if (fan_control_initial_status == 0x07) {
8002 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8003 tp_features.fan_ctrl_status_undef = 1;
8004 }
8005 }
8006
fan_quirk1_handle(u8 * fan_status)8007 static void fan_quirk1_handle(u8 *fan_status)
8008 {
8009 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8010 if (*fan_status != fan_control_initial_status) {
8011 /* something changed the HFSP regisnter since
8012 * driver init time, so it is not undefined
8013 * anymore */
8014 tp_features.fan_ctrl_status_undef = 0;
8015 } else {
8016 /* Return most likely status. In fact, it
8017 * might be the only possible status */
8018 *fan_status = TP_EC_FAN_AUTO;
8019 }
8020 }
8021 }
8022
8023 /* Select main fan on X60/X61, NOOP on others */
fan_select_fan1(void)8024 static bool fan_select_fan1(void)
8025 {
8026 if (tp_features.second_fan) {
8027 u8 val;
8028
8029 if (ec_read(fan_select_offset, &val) < 0)
8030 return false;
8031 val &= 0xFEU;
8032 if (ec_write(fan_select_offset, val) < 0)
8033 return false;
8034 }
8035 return true;
8036 }
8037
8038 /* Select secondary fan on X60/X61 */
fan_select_fan2(void)8039 static bool fan_select_fan2(void)
8040 {
8041 u8 val;
8042
8043 if (!tp_features.second_fan)
8044 return false;
8045
8046 if (ec_read(fan_select_offset, &val) < 0)
8047 return false;
8048 val |= 0x01U;
8049 if (ec_write(fan_select_offset, val) < 0)
8050 return false;
8051
8052 return true;
8053 }
8054
fan_update_desired_level(u8 status)8055 static void fan_update_desired_level(u8 status)
8056 {
8057 lockdep_assert_held(&fan_mutex);
8058
8059 if ((status &
8060 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8061 if (status > 7)
8062 fan_control_desired_level = 7;
8063 else
8064 fan_control_desired_level = status;
8065 }
8066 }
8067
fan_get_status(u8 * status)8068 static int fan_get_status(u8 *status)
8069 {
8070 u8 s;
8071
8072 /* TODO:
8073 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8074
8075 switch (fan_status_access_mode) {
8076 case TPACPI_FAN_RD_ACPI_GFAN: {
8077 /* 570, 600e/x, 770e, 770x */
8078 int res;
8079
8080 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8081 return -EIO;
8082
8083 if (likely(status))
8084 *status = res & 0x07;
8085
8086 break;
8087 }
8088 case TPACPI_FAN_RD_ACPI_FANG: {
8089 /* E531 */
8090 int mode, speed;
8091
8092 if (unlikely(!acpi_evalf(fang_handle, &mode, NULL, "dd", 0x8100)))
8093 return -EIO;
8094 if (unlikely(!acpi_evalf(fang_handle, &speed, NULL, "dd", 0x8102)))
8095 return -EIO;
8096
8097 if (likely(status)) {
8098 *status = speed * 7 / 100;
8099 if (mode < 9)
8100 *status |= TP_EC_FAN_AUTO;
8101 }
8102
8103 break;
8104 }
8105 case TPACPI_FAN_RD_TPEC:
8106 /* all except 570, 600e/x, 770e, 770x */
8107 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8108 return -EIO;
8109
8110 if (likely(status)) {
8111 *status = s;
8112 fan_quirk1_handle(status);
8113 }
8114
8115 break;
8116 case TPACPI_FAN_RD_TPEC_NS:
8117 /* Default mode is AUTO which means controlled by EC */
8118 if (!acpi_ec_read(fan_status_offset_ns, &s))
8119 return -EIO;
8120
8121 if (status)
8122 *status = s;
8123
8124 break;
8125
8126 default:
8127 return -ENXIO;
8128 }
8129
8130 return 0;
8131 }
8132
fan_get_status_safe(u8 * status)8133 static int fan_get_status_safe(u8 *status)
8134 {
8135 int rc;
8136 u8 s;
8137
8138 if (mutex_lock_killable(&fan_mutex))
8139 return -ERESTARTSYS;
8140 rc = fan_get_status(&s);
8141 /* NS EC doesn't have register with level settings */
8142 if (!rc && !fan_with_ns_addr)
8143 fan_update_desired_level(s);
8144 mutex_unlock(&fan_mutex);
8145
8146 if (rc)
8147 return rc;
8148 if (status)
8149 *status = s;
8150
8151 return 0;
8152 }
8153
fan_get_speed(unsigned int * speed)8154 static int fan_get_speed(unsigned int *speed)
8155 {
8156 u8 hi, lo;
8157
8158 switch (fan_status_access_mode) {
8159 case TPACPI_FAN_RD_TPEC:
8160 /* all except 570, 600e/x, 770e, 770x */
8161 if (unlikely(!fan_select_fan1()))
8162 return -EIO;
8163 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8164 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8165 return -EIO;
8166
8167 if (likely(speed)) {
8168 *speed = (hi << 8) | lo;
8169 if (fan_speed_in_tpr && *speed != 0)
8170 *speed = FAN_CLOCK_TPM / *speed;
8171 }
8172 break;
8173 case TPACPI_FAN_RD_TPEC_NS:
8174 if (!acpi_ec_read(fan_rpm_status_ns, &lo))
8175 return -EIO;
8176
8177 if (speed)
8178 *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
8179 break;
8180
8181 default:
8182 return -ENXIO;
8183 }
8184
8185 return 0;
8186 }
8187
fan2_get_speed(unsigned int * speed)8188 static int fan2_get_speed(unsigned int *speed)
8189 {
8190 u8 hi, lo, status;
8191 bool rc;
8192
8193 switch (fan_status_access_mode) {
8194 case TPACPI_FAN_RD_TPEC:
8195 /* all except 570, 600e/x, 770e, 770x */
8196 if (unlikely(!fan_select_fan2()))
8197 return -EIO;
8198 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8199 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8200 fan_select_fan1(); /* play it safe */
8201 if (rc)
8202 return -EIO;
8203
8204 if (likely(speed)) {
8205 *speed = (hi << 8) | lo;
8206 if (fan_speed_in_tpr && *speed != 0)
8207 *speed = FAN_CLOCK_TPM / *speed;
8208 }
8209 break;
8210
8211 case TPACPI_FAN_RD_TPEC_NS:
8212 rc = !acpi_ec_read(fan2_status_offset_ns, &status);
8213 if (rc)
8214 return -EIO;
8215 if (!(status & FAN_NS_CTRL_STATUS)) {
8216 pr_info("secondary fan control not supported\n");
8217 return -EIO;
8218 }
8219 rc = !acpi_ec_read(fan2_rpm_status_ns, &lo);
8220 if (rc)
8221 return -EIO;
8222 if (speed)
8223 *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
8224 break;
8225 case TPACPI_FAN_RD_ACPI_FANG: {
8226 /* E531 */
8227 int speed_tmp;
8228
8229 if (unlikely(!acpi_evalf(fang_handle, &speed_tmp, NULL, "dd", 0x8102)))
8230 return -EIO;
8231
8232 if (likely(speed))
8233 *speed = speed_tmp * 65535 / 100;
8234 break;
8235 }
8236
8237 default:
8238 return -ENXIO;
8239 }
8240
8241 return 0;
8242 }
8243
fan_set_level(int level)8244 static int fan_set_level(int level)
8245 {
8246 if (!fan_control_allowed)
8247 return -EPERM;
8248
8249 switch (fan_control_access_mode) {
8250 case TPACPI_FAN_WR_ACPI_SFAN:
8251 if ((level < 0) || (level > 7))
8252 return -EINVAL;
8253
8254 if (tp_features.second_fan_ctl) {
8255 if (!fan_select_fan2() ||
8256 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
8257 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8258 tp_features.second_fan_ctl = 0;
8259 }
8260 fan_select_fan1();
8261 }
8262 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8263 return -EIO;
8264 break;
8265
8266 case TPACPI_FAN_WR_ACPI_FANS:
8267 case TPACPI_FAN_WR_TPEC:
8268 if (!(level & TP_EC_FAN_AUTO) &&
8269 !(level & TP_EC_FAN_FULLSPEED) &&
8270 ((level < 0) || (level > 7)))
8271 return -EINVAL;
8272
8273 /* safety net should the EC not support AUTO
8274 * or FULLSPEED mode bits and just ignore them */
8275 if (level & TP_EC_FAN_FULLSPEED)
8276 level |= 7; /* safety min speed 7 */
8277 else if (level & TP_EC_FAN_AUTO)
8278 level |= 4; /* safety min speed 4 */
8279
8280 if (tp_features.second_fan_ctl) {
8281 if (!fan_select_fan2() ||
8282 !acpi_ec_write(fan_status_offset, level)) {
8283 pr_warn("Couldn't set 2nd fan level, disabling support\n");
8284 tp_features.second_fan_ctl = 0;
8285 }
8286 fan_select_fan1();
8287
8288 }
8289 if (!acpi_ec_write(fan_status_offset, level))
8290 return -EIO;
8291 else
8292 tp_features.fan_ctrl_status_undef = 0;
8293 break;
8294
8295 case TPACPI_FAN_WR_ACPI_FANW:
8296 if (!(level & TP_EC_FAN_AUTO) && (level < 0 || level > 7))
8297 return -EINVAL;
8298 if (level & TP_EC_FAN_FULLSPEED)
8299 return -EINVAL;
8300
8301 if (level & TP_EC_FAN_AUTO) {
8302 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x05)) {
8303 return -EIO;
8304 }
8305 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0x00)) {
8306 return -EIO;
8307 }
8308 } else {
8309 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
8310 return -EIO;
8311 }
8312 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
8313 return -EIO;
8314 }
8315 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8102, level * 100 / 7)) {
8316 return -EIO;
8317 }
8318 }
8319 break;
8320
8321 default:
8322 return -ENXIO;
8323 }
8324
8325 vdbg_printk(TPACPI_DBG_FAN,
8326 "fan control: set fan control register to 0x%02x\n", level);
8327 return 0;
8328 }
8329
fan_set_level_safe(int level)8330 static int fan_set_level_safe(int level)
8331 {
8332 int rc;
8333
8334 if (!fan_control_allowed)
8335 return -EPERM;
8336
8337 if (mutex_lock_killable(&fan_mutex))
8338 return -ERESTARTSYS;
8339
8340 if (level == TPACPI_FAN_LAST_LEVEL)
8341 level = fan_control_desired_level;
8342
8343 rc = fan_set_level(level);
8344 if (!rc)
8345 fan_update_desired_level(level);
8346
8347 mutex_unlock(&fan_mutex);
8348 return rc;
8349 }
8350
fan_set_enable(void)8351 static int fan_set_enable(void)
8352 {
8353 u8 s = 0;
8354 int rc;
8355
8356 if (!fan_control_allowed)
8357 return -EPERM;
8358
8359 if (mutex_lock_killable(&fan_mutex))
8360 return -ERESTARTSYS;
8361
8362 switch (fan_control_access_mode) {
8363 case TPACPI_FAN_WR_ACPI_FANS:
8364 case TPACPI_FAN_WR_TPEC:
8365 rc = fan_get_status(&s);
8366 if (rc)
8367 break;
8368
8369 /* Don't go out of emergency fan mode */
8370 if (s != 7) {
8371 s &= 0x07;
8372 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8373 }
8374
8375 if (!acpi_ec_write(fan_status_offset, s))
8376 rc = -EIO;
8377 else {
8378 tp_features.fan_ctrl_status_undef = 0;
8379 rc = 0;
8380 }
8381 break;
8382
8383 case TPACPI_FAN_WR_ACPI_SFAN:
8384 rc = fan_get_status(&s);
8385 if (rc)
8386 break;
8387
8388 s &= 0x07;
8389
8390 /* Set fan to at least level 4 */
8391 s |= 4;
8392
8393 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8394 rc = -EIO;
8395 else
8396 rc = 0;
8397 break;
8398
8399 case TPACPI_FAN_WR_ACPI_FANW:
8400 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x05)) {
8401 rc = -EIO;
8402 break;
8403 }
8404 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0x00)) {
8405 rc = -EIO;
8406 break;
8407 }
8408
8409 rc = 0;
8410 break;
8411
8412 default:
8413 rc = -ENXIO;
8414 }
8415
8416 mutex_unlock(&fan_mutex);
8417
8418 if (!rc)
8419 vdbg_printk(TPACPI_DBG_FAN,
8420 "fan control: set fan control register to 0x%02x\n",
8421 s);
8422 return rc;
8423 }
8424
fan_set_disable(void)8425 static int fan_set_disable(void)
8426 {
8427 int rc;
8428
8429 if (!fan_control_allowed)
8430 return -EPERM;
8431
8432 if (mutex_lock_killable(&fan_mutex))
8433 return -ERESTARTSYS;
8434
8435 rc = 0;
8436 switch (fan_control_access_mode) {
8437 case TPACPI_FAN_WR_ACPI_FANS:
8438 case TPACPI_FAN_WR_TPEC:
8439 if (!acpi_ec_write(fan_status_offset, 0x00))
8440 rc = -EIO;
8441 else {
8442 fan_control_desired_level = 0;
8443 tp_features.fan_ctrl_status_undef = 0;
8444 }
8445 break;
8446
8447 case TPACPI_FAN_WR_ACPI_SFAN:
8448 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8449 rc = -EIO;
8450 else
8451 fan_control_desired_level = 0;
8452 break;
8453
8454 case TPACPI_FAN_WR_ACPI_FANW:
8455 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
8456 rc = -EIO;
8457 break;
8458 }
8459 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
8460 rc = -EIO;
8461 break;
8462 }
8463 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8102, 0x00)) {
8464 rc = -EIO;
8465 break;
8466 }
8467 rc = 0;
8468 break;
8469
8470 default:
8471 rc = -ENXIO;
8472 }
8473
8474 if (!rc)
8475 vdbg_printk(TPACPI_DBG_FAN,
8476 "fan control: set fan control register to 0\n");
8477
8478 mutex_unlock(&fan_mutex);
8479 return rc;
8480 }
8481
fan_set_speed(int speed)8482 static int fan_set_speed(int speed)
8483 {
8484 int rc;
8485
8486 if (!fan_control_allowed)
8487 return -EPERM;
8488
8489 if (mutex_lock_killable(&fan_mutex))
8490 return -ERESTARTSYS;
8491
8492 rc = 0;
8493 switch (fan_control_access_mode) {
8494 case TPACPI_FAN_WR_ACPI_FANS:
8495 if (speed >= 0 && speed <= 65535) {
8496 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8497 speed, speed, speed))
8498 rc = -EIO;
8499 } else
8500 rc = -EINVAL;
8501 break;
8502
8503 case TPACPI_FAN_WR_ACPI_FANW:
8504 if (speed >= 0 && speed <= 65535) {
8505 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
8506 rc = -EIO;
8507 break;
8508 }
8509 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
8510 rc = -EIO;
8511 break;
8512 }
8513 if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd",
8514 0x8102, speed * 100 / 65535))
8515 rc = -EIO;
8516 } else
8517 rc = -EINVAL;
8518 break;
8519
8520 default:
8521 rc = -ENXIO;
8522 }
8523
8524 mutex_unlock(&fan_mutex);
8525 return rc;
8526 }
8527
fan_watchdog_reset(void)8528 static void fan_watchdog_reset(void)
8529 {
8530 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8531 return;
8532
8533 if (fan_watchdog_maxinterval > 0 &&
8534 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8535 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8536 secs_to_jiffies(fan_watchdog_maxinterval));
8537 else
8538 cancel_delayed_work(&fan_watchdog_task);
8539 }
8540
fan_watchdog_fire(struct work_struct * ignored)8541 static void fan_watchdog_fire(struct work_struct *ignored)
8542 {
8543 int rc;
8544
8545 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8546 return;
8547
8548 pr_notice("fan watchdog: enabling fan\n");
8549 rc = fan_set_enable();
8550 if (rc < 0) {
8551 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8552 rc);
8553 /* reschedule for later */
8554 fan_watchdog_reset();
8555 }
8556 }
8557
8558 /*
8559 * SYSFS fan layout: hwmon compatible (device)
8560 *
8561 * pwm*_enable:
8562 * 0: "disengaged" mode
8563 * 1: manual mode
8564 * 2: native EC "auto" mode (recommended, hardware default)
8565 *
8566 * pwm*: set speed in manual mode, ignored otherwise.
8567 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8568 * interpolation.
8569 *
8570 * fan*_input: tachometer reading, RPM
8571 *
8572 *
8573 * SYSFS fan layout: extensions
8574 *
8575 * fan_watchdog (driver):
8576 * fan watchdog interval in seconds, 0 disables (default), max 120
8577 */
8578
8579 /* sysfs fan pwm1_enable ----------------------------------------------- */
fan_pwm1_enable_show(struct device * dev,struct device_attribute * attr,char * buf)8580 static ssize_t fan_pwm1_enable_show(struct device *dev,
8581 struct device_attribute *attr,
8582 char *buf)
8583 {
8584 int res, mode;
8585 u8 status;
8586
8587 res = fan_get_status_safe(&status);
8588 if (res)
8589 return res;
8590
8591 if (status & TP_EC_FAN_FULLSPEED) {
8592 mode = 0;
8593 } else if (status & TP_EC_FAN_AUTO) {
8594 mode = 2;
8595 } else
8596 mode = 1;
8597
8598 return sysfs_emit(buf, "%d\n", mode);
8599 }
8600
fan_pwm1_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)8601 static ssize_t fan_pwm1_enable_store(struct device *dev,
8602 struct device_attribute *attr,
8603 const char *buf, size_t count)
8604 {
8605 unsigned long t;
8606 int res, level;
8607
8608 if (parse_strtoul(buf, 2, &t))
8609 return -EINVAL;
8610
8611 tpacpi_disclose_usertask("hwmon pwm1_enable",
8612 "set fan mode to %lu\n", t);
8613
8614 switch (t) {
8615 case 0:
8616 level = TP_EC_FAN_FULLSPEED;
8617 break;
8618 case 1:
8619 level = TPACPI_FAN_LAST_LEVEL;
8620 break;
8621 case 2:
8622 level = TP_EC_FAN_AUTO;
8623 break;
8624 case 3:
8625 /* reserved for software-controlled auto mode */
8626 return -ENOSYS;
8627 default:
8628 return -EINVAL;
8629 }
8630
8631 res = fan_set_level_safe(level);
8632 if (res == -ENXIO)
8633 return -EINVAL;
8634 else if (res < 0)
8635 return res;
8636
8637 fan_watchdog_reset();
8638
8639 return count;
8640 }
8641
8642 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8643 fan_pwm1_enable_show, fan_pwm1_enable_store);
8644
8645 /* sysfs fan pwm1 ------------------------------------------------------ */
fan_pwm1_show(struct device * dev,struct device_attribute * attr,char * buf)8646 static ssize_t fan_pwm1_show(struct device *dev,
8647 struct device_attribute *attr,
8648 char *buf)
8649 {
8650 int res;
8651 u8 status;
8652
8653 res = fan_get_status_safe(&status);
8654 if (res)
8655 return res;
8656
8657 if ((status &
8658 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8659 status = fan_control_desired_level;
8660
8661 if (status > 7)
8662 status = 7;
8663
8664 return sysfs_emit(buf, "%u\n", (status * 255) / 7);
8665 }
8666
fan_pwm1_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)8667 static ssize_t fan_pwm1_store(struct device *dev,
8668 struct device_attribute *attr,
8669 const char *buf, size_t count)
8670 {
8671 unsigned long s;
8672 int rc;
8673 u8 status, newlevel;
8674
8675 if (parse_strtoul(buf, 255, &s))
8676 return -EINVAL;
8677
8678 tpacpi_disclose_usertask("hwmon pwm1",
8679 "set fan speed to %lu\n", s);
8680
8681 /* scale down from 0-255 to 0-7 */
8682 newlevel = (s >> 5) & 0x07;
8683
8684 if (mutex_lock_killable(&fan_mutex))
8685 return -ERESTARTSYS;
8686
8687 rc = fan_get_status(&status);
8688 if (!rc && (status &
8689 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8690 rc = fan_set_level(newlevel);
8691 if (rc == -ENXIO)
8692 rc = -EINVAL;
8693 else if (!rc) {
8694 fan_update_desired_level(newlevel);
8695 fan_watchdog_reset();
8696 }
8697 }
8698
8699 mutex_unlock(&fan_mutex);
8700 return (rc) ? rc : count;
8701 }
8702
8703 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8704
8705 /* sysfs fan fan1_input ------------------------------------------------ */
fan_fan1_input_show(struct device * dev,struct device_attribute * attr,char * buf)8706 static ssize_t fan_fan1_input_show(struct device *dev,
8707 struct device_attribute *attr,
8708 char *buf)
8709 {
8710 int res;
8711 unsigned int speed;
8712
8713 res = fan_get_speed(&speed);
8714 if (res < 0)
8715 return res;
8716
8717 /* Check for fan speeds displayed in hexadecimal */
8718 if (!ecfw_with_fan_dec_rpm)
8719 return sysfs_emit(buf, "%u\n", speed);
8720 else
8721 return sysfs_emit(buf, "%x\n", speed);
8722 }
8723
8724 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8725
8726 /* sysfs fan fan2_input ------------------------------------------------ */
fan_fan2_input_show(struct device * dev,struct device_attribute * attr,char * buf)8727 static ssize_t fan_fan2_input_show(struct device *dev,
8728 struct device_attribute *attr,
8729 char *buf)
8730 {
8731 int res;
8732 unsigned int speed;
8733
8734 res = fan2_get_speed(&speed);
8735 if (res < 0)
8736 return res;
8737
8738 /* Check for fan speeds displayed in hexadecimal */
8739 if (!ecfw_with_fan_dec_rpm)
8740 return sysfs_emit(buf, "%u\n", speed);
8741 else
8742 return sysfs_emit(buf, "%x\n", speed);
8743 }
8744
8745 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8746
8747 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
fan_watchdog_show(struct device_driver * drv,char * buf)8748 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8749 {
8750 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
8751 }
8752
fan_watchdog_store(struct device_driver * drv,const char * buf,size_t count)8753 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8754 size_t count)
8755 {
8756 unsigned long t;
8757
8758 if (parse_strtoul(buf, 120, &t))
8759 return -EINVAL;
8760
8761 if (!fan_control_allowed)
8762 return -EPERM;
8763
8764 fan_watchdog_maxinterval = t;
8765 fan_watchdog_reset();
8766
8767 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8768
8769 return count;
8770 }
8771 static DRIVER_ATTR_RW(fan_watchdog);
8772
8773 /* --------------------------------------------------------------------- */
8774
8775 static struct attribute *fan_attributes[] = {
8776 &dev_attr_pwm1_enable.attr,
8777 &dev_attr_pwm1.attr,
8778 &dev_attr_fan1_input.attr,
8779 &dev_attr_fan2_input.attr,
8780 NULL
8781 };
8782
fan_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)8783 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
8784 int n)
8785 {
8786 if (fan_status_access_mode == TPACPI_FAN_NONE &&
8787 fan_control_access_mode == TPACPI_FAN_WR_NONE)
8788 return 0;
8789
8790 if (attr == &dev_attr_fan2_input.attr) {
8791 if (!tp_features.second_fan)
8792 return 0;
8793 }
8794
8795 return attr->mode;
8796 }
8797
8798 static const struct attribute_group fan_attr_group = {
8799 .is_visible = fan_attr_is_visible,
8800 .attrs = fan_attributes,
8801 };
8802
8803 static struct attribute *fan_driver_attributes[] = {
8804 &driver_attr_fan_watchdog.attr,
8805 NULL
8806 };
8807
8808 static const struct attribute_group fan_driver_attr_group = {
8809 .is_visible = fan_attr_is_visible,
8810 .attrs = fan_driver_attributes,
8811 };
8812
8813 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
8814 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8815 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
8816 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
8817 #define TPACPI_FAN_NS 0x0010 /* For EC with non-Standard register addresses */
8818 #define TPACPI_FAN_DECRPM 0x0020 /* For ECFW's with RPM in register as decimal */
8819 #define TPACPI_FAN_TPR 0x0040 /* Fan speed is in Ticks Per Revolution */
8820 #define TPACPI_FAN_NOACPI 0x0080 /* Don't use ACPI methods even if detected */
8821
8822 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8823 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8824 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8825 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8826 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8827 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8828 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8829 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
8830 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
8831 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
8832 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
8833 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
8834 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
8835 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
8836 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
8837 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
8838 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
8839 TPACPI_Q_LNV3('R', '1', 'F', TPACPI_FAN_NS), /* L13 Yoga Gen 2 */
8840 TPACPI_Q_LNV3('N', '2', 'U', TPACPI_FAN_NS), /* X13 Yoga Gen 2*/
8841 TPACPI_Q_LNV3('R', '0', 'R', TPACPI_FAN_NS), /* L380 */
8842 TPACPI_Q_LNV3('R', '1', '5', TPACPI_FAN_NS), /* L13 Yoga Gen 1 */
8843 TPACPI_Q_LNV3('R', '1', '0', TPACPI_FAN_NS), /* L390 */
8844 TPACPI_Q_LNV3('N', '2', 'L', TPACPI_FAN_NS), /* X13 Yoga Gen 1 */
8845 TPACPI_Q_LNV3('R', '0', 'T', TPACPI_FAN_NS), /* 11e Gen5 GL */
8846 TPACPI_Q_LNV3('R', '1', 'D', TPACPI_FAN_NS), /* 11e Gen5 GL-R */
8847 TPACPI_Q_LNV3('R', '0', 'V', TPACPI_FAN_NS), /* 11e Gen5 KL-Y */
8848 TPACPI_Q_LNV('H', '3', TPACPI_FAN_NS), /* Edge E330 */
8849 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
8850 TPACPI_Q_LNV3('R', '0', 'Q', TPACPI_FAN_DECRPM),/* L480 */
8851 TPACPI_Q_LNV('8', 'F', TPACPI_FAN_TPR), /* ThinkPad x120e */
8852 TPACPI_Q_LNV3('R', '0', '0', TPACPI_FAN_NOACPI),/* E560 */
8853 TPACPI_Q_LNV3('R', '1', '2', TPACPI_FAN_NOACPI),/* T495 */
8854 TPACPI_Q_LNV3('R', '1', '3', TPACPI_FAN_NOACPI),/* T495s */
8855 };
8856
fan_init(struct ibm_init_struct * iibm)8857 static int __init fan_init(struct ibm_init_struct *iibm)
8858 {
8859 unsigned long quirks;
8860
8861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8862 "initializing fan subdriver\n");
8863
8864 mutex_init(&fan_mutex);
8865 fan_status_access_mode = TPACPI_FAN_NONE;
8866 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8867 fan_control_commands = 0;
8868 fan_watchdog_maxinterval = 0;
8869 tp_features.fan_ctrl_status_undef = 0;
8870 tp_features.second_fan = 0;
8871 tp_features.second_fan_ctl = 0;
8872 fan_control_desired_level = 7;
8873
8874 if (tpacpi_is_ibm()) {
8875 TPACPI_ACPIHANDLE_INIT(fans);
8876 TPACPI_ACPIHANDLE_INIT(gfan);
8877 TPACPI_ACPIHANDLE_INIT(sfan);
8878 }
8879 if (tpacpi_is_lenovo()) {
8880 TPACPI_ACPIHANDLE_INIT(fang);
8881 TPACPI_ACPIHANDLE_INIT(fanw);
8882 }
8883
8884 quirks = tpacpi_check_quirks(fan_quirk_table,
8885 ARRAY_SIZE(fan_quirk_table));
8886
8887 if (quirks & TPACPI_FAN_NOFAN) {
8888 pr_info("No integrated ThinkPad fan available\n");
8889 return -ENODEV;
8890 }
8891
8892 if (quirks & TPACPI_FAN_NS) {
8893 pr_info("ECFW with non-standard fan reg control found\n");
8894 fan_with_ns_addr = 1;
8895 /* Fan ctrl support from host is undefined for now */
8896 tp_features.fan_ctrl_status_undef = 1;
8897 }
8898
8899 /* Check for the EC/BIOS with RPM reported in decimal*/
8900 if (quirks & TPACPI_FAN_DECRPM) {
8901 pr_info("ECFW with fan RPM as decimal in EC register\n");
8902 ecfw_with_fan_dec_rpm = 1;
8903 tp_features.fan_ctrl_status_undef = 1;
8904 }
8905
8906 if (quirks & TPACPI_FAN_NOACPI) {
8907 /* E560, T495, T495s */
8908 pr_info("Ignoring buggy ACPI fan access method\n");
8909 fang_handle = NULL;
8910 fanw_handle = NULL;
8911 }
8912
8913 if (gfan_handle) {
8914 /* 570, 600e/x, 770e, 770x */
8915 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8916 } else if (fang_handle) {
8917 /* E531 */
8918 fan_status_access_mode = TPACPI_FAN_RD_ACPI_FANG;
8919 } else {
8920 /* all other ThinkPads: note that even old-style
8921 * ThinkPad ECs supports the fan control register */
8922 if (fan_with_ns_addr ||
8923 likely(acpi_ec_read(fan_status_offset, &fan_control_initial_status))) {
8924 int res;
8925 unsigned int speed;
8926
8927 fan_status_access_mode = fan_with_ns_addr ?
8928 TPACPI_FAN_RD_TPEC_NS : TPACPI_FAN_RD_TPEC;
8929
8930 if (quirks & TPACPI_FAN_Q1)
8931 fan_quirk1_setup();
8932 if (quirks & TPACPI_FAN_TPR)
8933 fan_speed_in_tpr = true;
8934 /* Try and probe the 2nd fan */
8935 tp_features.second_fan = 1; /* needed for get_speed to work */
8936 res = fan2_get_speed(&speed);
8937 if (res >= 0 && speed != FAN_NOT_PRESENT) {
8938 /* It responded - so let's assume it's there */
8939 tp_features.second_fan = 1;
8940 /* fan control not currently available for ns ECFW */
8941 tp_features.second_fan_ctl = !fan_with_ns_addr;
8942 pr_info("secondary fan control detected & enabled\n");
8943 } else {
8944 /* Fan not auto-detected */
8945 tp_features.second_fan = 0;
8946 if (quirks & TPACPI_FAN_2FAN) {
8947 tp_features.second_fan = 1;
8948 pr_info("secondary fan support enabled\n");
8949 }
8950 if (quirks & TPACPI_FAN_2CTL) {
8951 tp_features.second_fan = 1;
8952 tp_features.second_fan_ctl = 1;
8953 pr_info("secondary fan control enabled\n");
8954 }
8955 }
8956 } else {
8957 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8958 return -ENODEV;
8959 }
8960 }
8961
8962 if (sfan_handle) {
8963 /* 570, 770x-JL */
8964 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8965 fan_control_commands |=
8966 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8967 } else if (fanw_handle) {
8968 /* E531 */
8969 fan_control_access_mode = TPACPI_FAN_WR_ACPI_FANW;
8970 fan_control_commands |=
8971 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_SPEED | TPACPI_FAN_CMD_ENABLE;
8972 } else {
8973 if (!gfan_handle) {
8974 /* gfan without sfan means no fan control */
8975 /* all other models implement TP EC 0x2f control */
8976
8977 if (fans_handle) {
8978 /* X31, X40, X41 */
8979 fan_control_access_mode =
8980 TPACPI_FAN_WR_ACPI_FANS;
8981 fan_control_commands |=
8982 TPACPI_FAN_CMD_SPEED |
8983 TPACPI_FAN_CMD_LEVEL |
8984 TPACPI_FAN_CMD_ENABLE;
8985 } else {
8986 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8987 fan_control_commands |=
8988 TPACPI_FAN_CMD_LEVEL |
8989 TPACPI_FAN_CMD_ENABLE;
8990 }
8991 }
8992 }
8993
8994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8995 "fan is %s, modes %d, %d\n",
8996 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8997 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8998 fan_status_access_mode, fan_control_access_mode);
8999
9000 /* fan control master switch */
9001 if (!fan_control_allowed) {
9002 fan_control_access_mode = TPACPI_FAN_WR_NONE;
9003 fan_control_commands = 0;
9004 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
9005 "fan control features disabled by parameter\n");
9006 }
9007
9008 /* update fan_control_desired_level */
9009 if (fan_status_access_mode != TPACPI_FAN_NONE)
9010 fan_get_status_safe(NULL);
9011
9012 if (fan_status_access_mode == TPACPI_FAN_NONE &&
9013 fan_control_access_mode == TPACPI_FAN_WR_NONE)
9014 return -ENODEV;
9015
9016 return 0;
9017 }
9018
fan_exit(void)9019 static void fan_exit(void)
9020 {
9021 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
9022 "cancelling any pending fan watchdog tasks\n");
9023
9024 cancel_delayed_work(&fan_watchdog_task);
9025 flush_workqueue(tpacpi_wq);
9026 }
9027
fan_suspend(void)9028 static void fan_suspend(void)
9029 {
9030 int rc;
9031
9032 if (!fan_control_allowed)
9033 return;
9034
9035 /* Store fan status in cache */
9036 fan_control_resume_level = 0;
9037 rc = fan_get_status_safe(&fan_control_resume_level);
9038 if (rc)
9039 pr_notice("failed to read fan level for later restore during resume: %d\n",
9040 rc);
9041
9042 /* if it is undefined, don't attempt to restore it.
9043 * KEEP THIS LAST */
9044 if (tp_features.fan_ctrl_status_undef)
9045 fan_control_resume_level = 0;
9046 }
9047
fan_resume(void)9048 static void fan_resume(void)
9049 {
9050 u8 current_level = 7;
9051 bool do_set = false;
9052 int rc;
9053
9054 /* DSDT *always* updates status on resume */
9055 tp_features.fan_ctrl_status_undef = 0;
9056
9057 if (!fan_control_allowed ||
9058 !fan_control_resume_level ||
9059 fan_get_status_safe(¤t_level))
9060 return;
9061
9062 switch (fan_control_access_mode) {
9063 case TPACPI_FAN_WR_ACPI_SFAN:
9064 /* never decrease fan level */
9065 do_set = (fan_control_resume_level > current_level);
9066 break;
9067 case TPACPI_FAN_WR_ACPI_FANS:
9068 case TPACPI_FAN_WR_TPEC:
9069 /* never decrease fan level, scale is:
9070 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
9071 *
9072 * We expect the firmware to set either 7 or AUTO, but we
9073 * handle FULLSPEED out of paranoia.
9074 *
9075 * So, we can safely only restore FULLSPEED or 7, anything
9076 * else could slow the fan. Restoring AUTO is useless, at
9077 * best that's exactly what the DSDT already set (it is the
9078 * slower it uses).
9079 *
9080 * Always keep in mind that the DSDT *will* have set the
9081 * fans to what the vendor supposes is the best level. We
9082 * muck with it only to speed the fan up.
9083 */
9084 if (fan_control_resume_level != 7 &&
9085 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
9086 return;
9087 else
9088 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
9089 (current_level != fan_control_resume_level);
9090 break;
9091 default:
9092 return;
9093 }
9094 if (do_set) {
9095 pr_notice("restoring fan level to 0x%02x\n",
9096 fan_control_resume_level);
9097 rc = fan_set_level_safe(fan_control_resume_level);
9098 if (rc < 0)
9099 pr_notice("failed to restore fan level: %d\n", rc);
9100 }
9101 }
9102
fan_read(struct seq_file * m)9103 static int fan_read(struct seq_file *m)
9104 {
9105 int rc;
9106 u8 status;
9107 unsigned int speed = 0;
9108
9109 switch (fan_status_access_mode) {
9110 case TPACPI_FAN_RD_ACPI_GFAN:
9111 /* 570, 600e/x, 770e, 770x */
9112 rc = fan_get_status_safe(&status);
9113 if (rc)
9114 return rc;
9115
9116 seq_printf(m, "status:\t\t%s\n"
9117 "level:\t\t%d\n",
9118 str_enabled_disabled(status), status);
9119 break;
9120
9121 case TPACPI_FAN_RD_TPEC_NS:
9122 case TPACPI_FAN_RD_TPEC:
9123 case TPACPI_FAN_RD_ACPI_FANG:
9124 /* all except 570, 600e/x, 770e, 770x */
9125 rc = fan_get_status_safe(&status);
9126 if (rc)
9127 return rc;
9128
9129 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
9130
9131 rc = fan_get_speed(&speed);
9132 if (rc < 0)
9133 return rc;
9134
9135 /* Check for fan speeds displayed in hexadecimal */
9136 if (!ecfw_with_fan_dec_rpm)
9137 seq_printf(m, "speed:\t\t%d\n", speed);
9138 else
9139 seq_printf(m, "speed:\t\t%x\n", speed);
9140
9141 if (fan_status_access_mode == TPACPI_FAN_RD_TPEC_NS) {
9142 /*
9143 * No full speed bit in NS EC
9144 * EC Auto mode is set by default.
9145 * No other levels settings available
9146 */
9147 seq_printf(m, "level:\t\t%s\n", status & FAN_NS_CTRL ? "unknown" : "auto");
9148 } else if (fan_status_access_mode == TPACPI_FAN_RD_TPEC) {
9149 if (status & TP_EC_FAN_FULLSPEED)
9150 /* Disengaged mode takes precedence */
9151 seq_puts(m, "level:\t\tdisengaged\n");
9152 else if (status & TP_EC_FAN_AUTO)
9153 seq_puts(m, "level:\t\tauto\n");
9154 else
9155 seq_printf(m, "level:\t\t%d\n", status);
9156 }
9157 break;
9158
9159 case TPACPI_FAN_NONE:
9160 default:
9161 seq_puts(m, "status:\t\tnot supported\n");
9162 }
9163
9164 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9165 seq_puts(m, "commands:\tlevel <level>");
9166
9167 switch (fan_control_access_mode) {
9168 case TPACPI_FAN_WR_ACPI_SFAN:
9169 seq_puts(m, " (<level> is 0-7)\n");
9170 break;
9171
9172 default:
9173 seq_puts(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9174 break;
9175 }
9176 }
9177
9178 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9179 seq_printf(m, "commands:\tenable, disable\n"
9180 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9181
9182 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9183 seq_puts(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9184
9185 return 0;
9186 }
9187
fan_write_cmd_level(const char * cmd,int * rc)9188 static int fan_write_cmd_level(const char *cmd, int *rc)
9189 {
9190 int level;
9191
9192 if (strstarts(cmd, "level auto"))
9193 level = TP_EC_FAN_AUTO;
9194 else if (strstarts(cmd, "level disengaged") || strstarts(cmd, "level full-speed"))
9195 level = TP_EC_FAN_FULLSPEED;
9196 else if (sscanf(cmd, "level %d", &level) != 1)
9197 return 0;
9198
9199 *rc = fan_set_level_safe(level);
9200 if (*rc == -ENXIO)
9201 pr_err("level command accepted for unsupported access mode %d\n",
9202 fan_control_access_mode);
9203 else if (!*rc)
9204 tpacpi_disclose_usertask("procfs fan",
9205 "set level to %d\n", level);
9206
9207 return 1;
9208 }
9209
fan_write_cmd_enable(const char * cmd,int * rc)9210 static int fan_write_cmd_enable(const char *cmd, int *rc)
9211 {
9212 if (!strstarts(cmd, "enable"))
9213 return 0;
9214
9215 *rc = fan_set_enable();
9216 if (*rc == -ENXIO)
9217 pr_err("enable command accepted for unsupported access mode %d\n",
9218 fan_control_access_mode);
9219 else if (!*rc)
9220 tpacpi_disclose_usertask("procfs fan", "enable\n");
9221
9222 return 1;
9223 }
9224
fan_write_cmd_disable(const char * cmd,int * rc)9225 static int fan_write_cmd_disable(const char *cmd, int *rc)
9226 {
9227 if (!strstarts(cmd, "disable"))
9228 return 0;
9229
9230 *rc = fan_set_disable();
9231 if (*rc == -ENXIO)
9232 pr_err("disable command accepted for unsupported access mode %d\n",
9233 fan_control_access_mode);
9234 else if (!*rc)
9235 tpacpi_disclose_usertask("procfs fan", "disable\n");
9236
9237 return 1;
9238 }
9239
fan_write_cmd_speed(const char * cmd,int * rc)9240 static int fan_write_cmd_speed(const char *cmd, int *rc)
9241 {
9242 int speed;
9243
9244 if (sscanf(cmd, "speed %d", &speed) != 1)
9245 return 0;
9246
9247 *rc = fan_set_speed(speed);
9248 if (*rc == -ENXIO)
9249 pr_err("speed command accepted for unsupported access mode %d\n",
9250 fan_control_access_mode);
9251 else if (!*rc)
9252 tpacpi_disclose_usertask("procfs fan",
9253 "set speed to %d\n", speed);
9254
9255 return 1;
9256 }
9257
fan_write_cmd_watchdog(const char * cmd,int * rc)9258 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9259 {
9260 int interval;
9261
9262 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9263 return 0;
9264
9265 if (interval < 0 || interval > 120)
9266 *rc = -EINVAL;
9267 else {
9268 fan_watchdog_maxinterval = interval;
9269 tpacpi_disclose_usertask("procfs fan",
9270 "set watchdog timer to %d\n",
9271 interval);
9272 }
9273
9274 return 1;
9275 }
9276
fan_write(char * buf)9277 static int fan_write(char *buf)
9278 {
9279 char *cmd;
9280 int rc = 0;
9281
9282 while (!rc && (cmd = strsep(&buf, ","))) {
9283 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9284 fan_write_cmd_level(cmd, &rc)) &&
9285 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9286 (fan_write_cmd_enable(cmd, &rc) ||
9287 fan_write_cmd_disable(cmd, &rc) ||
9288 fan_write_cmd_watchdog(cmd, &rc))) &&
9289 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9290 fan_write_cmd_speed(cmd, &rc))
9291 )
9292 rc = -EINVAL;
9293 else if (!rc)
9294 fan_watchdog_reset();
9295 }
9296
9297 return rc;
9298 }
9299
9300 static struct ibm_struct fan_driver_data = {
9301 .name = "fan",
9302 .read = fan_read,
9303 .write = fan_write,
9304 .exit = fan_exit,
9305 .suspend = fan_suspend,
9306 .resume = fan_resume,
9307 };
9308
9309 /*************************************************************************
9310 * Mute LED subdriver
9311 */
9312
9313 #define TPACPI_LED_MAX 2
9314
9315 struct tp_led_table {
9316 acpi_string name;
9317 int on_value;
9318 int off_value;
9319 int state;
9320 };
9321
9322 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9323 [LED_AUDIO_MUTE] = {
9324 .name = "SSMS",
9325 .on_value = 1,
9326 .off_value = 0,
9327 },
9328 [LED_AUDIO_MICMUTE] = {
9329 .name = "MMTS",
9330 .on_value = 2,
9331 .off_value = 0,
9332 },
9333 };
9334
mute_led_on_off(struct tp_led_table * t,bool state)9335 static int mute_led_on_off(struct tp_led_table *t, bool state)
9336 {
9337 acpi_handle temp;
9338 int output;
9339
9340 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9341 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9342 return -EIO;
9343 }
9344
9345 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9346 state ? t->on_value : t->off_value))
9347 return -EIO;
9348
9349 t->state = state;
9350 return state;
9351 }
9352
tpacpi_led_set(int whichled,bool on)9353 static int tpacpi_led_set(int whichled, bool on)
9354 {
9355 struct tp_led_table *t;
9356
9357 t = &led_tables[whichled];
9358 if (t->state < 0 || t->state == on)
9359 return t->state;
9360 return mute_led_on_off(t, on);
9361 }
9362
tpacpi_led_mute_set(struct led_classdev * led_cdev,enum led_brightness brightness)9363 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9364 enum led_brightness brightness)
9365 {
9366 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9367 }
9368
tpacpi_led_micmute_set(struct led_classdev * led_cdev,enum led_brightness brightness)9369 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9370 enum led_brightness brightness)
9371 {
9372 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9373 }
9374
9375 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9376 [LED_AUDIO_MUTE] = {
9377 .name = "platform::mute",
9378 .max_brightness = 1,
9379 .brightness_set_blocking = tpacpi_led_mute_set,
9380 .default_trigger = "audio-mute",
9381 },
9382 [LED_AUDIO_MICMUTE] = {
9383 .name = "platform::micmute",
9384 .max_brightness = 1,
9385 .brightness_set_blocking = tpacpi_led_micmute_set,
9386 .default_trigger = "audio-micmute",
9387 },
9388 };
9389
mute_led_init(struct ibm_init_struct * iibm)9390 static int mute_led_init(struct ibm_init_struct *iibm)
9391 {
9392 acpi_handle temp;
9393 int i, err;
9394
9395 for (i = 0; i < TPACPI_LED_MAX; i++) {
9396 struct tp_led_table *t = &led_tables[i];
9397 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9398 t->state = -ENODEV;
9399 continue;
9400 }
9401
9402 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9403 if (err < 0) {
9404 while (i--)
9405 led_classdev_unregister(&mute_led_cdev[i]);
9406 return err;
9407 }
9408 }
9409 return 0;
9410 }
9411
mute_led_exit(void)9412 static void mute_led_exit(void)
9413 {
9414 int i;
9415
9416 for (i = 0; i < TPACPI_LED_MAX; i++) {
9417 led_classdev_unregister(&mute_led_cdev[i]);
9418 tpacpi_led_set(i, false);
9419 }
9420 }
9421
mute_led_resume(void)9422 static void mute_led_resume(void)
9423 {
9424 int i;
9425
9426 for (i = 0; i < TPACPI_LED_MAX; i++) {
9427 struct tp_led_table *t = &led_tables[i];
9428 if (t->state >= 0)
9429 mute_led_on_off(t, t->state);
9430 }
9431 }
9432
9433 static struct ibm_struct mute_led_driver_data = {
9434 .name = "mute_led",
9435 .exit = mute_led_exit,
9436 .resume = mute_led_resume,
9437 };
9438
9439 /*
9440 * Battery Wear Control Driver
9441 * Contact: Ognjen Galic <smclt30p@gmail.com>
9442 */
9443
9444 /* Metadata */
9445
9446 #define GET_START "BCTG"
9447 #define SET_START "BCCS"
9448 #define GET_STOP "BCSG"
9449 #define SET_STOP "BCSS"
9450 #define GET_DISCHARGE "BDSG"
9451 #define SET_DISCHARGE "BDSS"
9452 #define GET_INHIBIT "BICG"
9453 #define SET_INHIBIT "BICS"
9454
9455 enum {
9456 BAT_ANY = 0,
9457 BAT_PRIMARY = 1,
9458 BAT_SECONDARY = 2
9459 };
9460
9461 enum {
9462 /* Error condition bit */
9463 METHOD_ERR = BIT(31),
9464 };
9465
9466 enum {
9467 /* This is used in the get/set helpers */
9468 THRESHOLD_START,
9469 THRESHOLD_STOP,
9470 FORCE_DISCHARGE,
9471 INHIBIT_CHARGE,
9472 };
9473
9474 struct tpacpi_battery_data {
9475 int charge_start;
9476 int start_support;
9477 int charge_stop;
9478 int stop_support;
9479 unsigned int charge_behaviours;
9480 };
9481
9482 struct tpacpi_battery_driver_data {
9483 struct tpacpi_battery_data batteries[3];
9484 int individual_addressing;
9485 };
9486
9487 static struct tpacpi_battery_driver_data battery_info;
9488
9489 /* ACPI helpers/functions/probes */
9490
9491 /*
9492 * This evaluates a ACPI method call specific to the battery
9493 * ACPI extension. The specifics are that an error is marked
9494 * in the 32rd bit of the response, so we just check that here.
9495 */
tpacpi_battery_acpi_eval(char * method,int * ret,int param)9496 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9497 {
9498 int response;
9499
9500 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9501 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9502 return AE_ERROR;
9503 }
9504 if (response & METHOD_ERR) {
9505 acpi_handle_err(hkey_handle,
9506 "%s evaluated but flagged as error", method);
9507 return AE_ERROR;
9508 }
9509 *ret = response;
9510 return AE_OK;
9511 }
9512
tpacpi_battery_get(int what,int battery,int * ret)9513 static int tpacpi_battery_get(int what, int battery, int *ret)
9514 {
9515 switch (what) {
9516 case THRESHOLD_START:
9517 if (!battery_info.batteries[battery].start_support ||
9518 ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery)))
9519 return -ENODEV;
9520
9521 /* The value is in the low 8 bits of the response */
9522 *ret = *ret & 0xFF;
9523 return 0;
9524 case THRESHOLD_STOP:
9525 if (!battery_info.batteries[battery].stop_support ||
9526 ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery)))
9527 return -ENODEV;
9528 /* Value is in lower 8 bits */
9529 *ret = *ret & 0xFF;
9530 /*
9531 * On the stop value, if we return 0 that
9532 * does not make any sense. 0 means Default, which
9533 * means that charging stops at 100%, so we return
9534 * that.
9535 */
9536 if (*ret == 0)
9537 *ret = 100;
9538 return 0;
9539 case FORCE_DISCHARGE:
9540 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
9541 return -ENODEV;
9542 /* The force discharge status is in bit 0 */
9543 *ret = *ret & 0x01;
9544 return 0;
9545 case INHIBIT_CHARGE:
9546 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
9547 return -ENODEV;
9548 /* The inhibit charge status is in bit 0 */
9549 *ret = *ret & 0x01;
9550 return 0;
9551 default:
9552 pr_crit("wrong parameter: %d", what);
9553 return -EINVAL;
9554 }
9555 }
9556
tpacpi_battery_set(int what,int battery,int value)9557 static int tpacpi_battery_set(int what, int battery, int value)
9558 {
9559 int param, ret;
9560 /* The first 8 bits are the value of the threshold */
9561 param = value;
9562 /* The battery ID is in bits 8-9, 2 bits */
9563 param |= battery << 8;
9564
9565 switch (what) {
9566 case THRESHOLD_START:
9567 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9568 pr_err("failed to set charge threshold on battery %d",
9569 battery);
9570 return -ENODEV;
9571 }
9572 return 0;
9573 case THRESHOLD_STOP:
9574 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9575 pr_err("failed to set stop threshold: %d", battery);
9576 return -ENODEV;
9577 }
9578 return 0;
9579 case FORCE_DISCHARGE:
9580 /* Force discharge is in bit 0,
9581 * break on AC attach is in bit 1 (won't work on some ThinkPads),
9582 * battery ID is in bits 8-9, 2 bits.
9583 */
9584 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
9585 pr_err("failed to set force discharge on %d", battery);
9586 return -ENODEV;
9587 }
9588 return 0;
9589 case INHIBIT_CHARGE:
9590 /* When setting inhibit charge, we set a default value of
9591 * always breaking on AC detach and the effective time is set to
9592 * be permanent.
9593 * The battery ID is in bits 4-5, 2 bits,
9594 * the effective time is in bits 8-23, 2 bytes.
9595 * A time of FFFF indicates forever.
9596 */
9597 param = value;
9598 param |= battery << 4;
9599 param |= 0xFFFF << 8;
9600 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
9601 pr_err("failed to set inhibit charge on %d", battery);
9602 return -ENODEV;
9603 }
9604 return 0;
9605 default:
9606 pr_crit("wrong parameter: %d", what);
9607 return -EINVAL;
9608 }
9609 }
9610
tpacpi_battery_set_validate(int what,int battery,int value)9611 static int tpacpi_battery_set_validate(int what, int battery, int value)
9612 {
9613 int ret, v;
9614
9615 ret = tpacpi_battery_set(what, battery, value);
9616 if (ret < 0)
9617 return ret;
9618
9619 ret = tpacpi_battery_get(what, battery, &v);
9620 if (ret < 0)
9621 return ret;
9622
9623 if (v == value)
9624 return 0;
9625
9626 msleep(500);
9627
9628 ret = tpacpi_battery_get(what, battery, &v);
9629 if (ret < 0)
9630 return ret;
9631
9632 if (v == value)
9633 return 0;
9634
9635 return -EIO;
9636 }
9637
tpacpi_battery_probe(int battery)9638 static int tpacpi_battery_probe(int battery)
9639 {
9640 int ret = 0;
9641
9642 memset(&battery_info.batteries[battery], 0,
9643 sizeof(battery_info.batteries[battery]));
9644
9645 /*
9646 * 1) Get the current start threshold
9647 * 2) Check for support
9648 * 3) Get the current stop threshold
9649 * 4) Check for support
9650 * 5) Get the current force discharge status
9651 * 6) Check for support
9652 * 7) Get the current inhibit charge status
9653 * 8) Check for support
9654 */
9655 if (acpi_has_method(hkey_handle, GET_START)) {
9656 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9657 pr_err("Error probing battery %d\n", battery);
9658 return -ENODEV;
9659 }
9660 /* Individual addressing is in bit 9 */
9661 if (ret & BIT(9))
9662 battery_info.individual_addressing = true;
9663 /* Support is marked in bit 8 */
9664 if (ret & BIT(8))
9665 battery_info.batteries[battery].start_support = 1;
9666 else
9667 return -ENODEV;
9668 if (tpacpi_battery_get(THRESHOLD_START, battery,
9669 &battery_info.batteries[battery].charge_start)) {
9670 pr_err("Error probing battery %d\n", battery);
9671 return -ENODEV;
9672 }
9673 }
9674 if (acpi_has_method(hkey_handle, GET_STOP)) {
9675 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9676 pr_err("Error probing battery stop; %d\n", battery);
9677 return -ENODEV;
9678 }
9679 /* Support is marked in bit 8 */
9680 if (ret & BIT(8))
9681 battery_info.batteries[battery].stop_support = 1;
9682 else
9683 return -ENODEV;
9684 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9685 &battery_info.batteries[battery].charge_stop)) {
9686 pr_err("Error probing battery stop: %d\n", battery);
9687 return -ENODEV;
9688 }
9689 }
9690 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
9691 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
9692 pr_err("Error probing battery discharge; %d\n", battery);
9693 return -ENODEV;
9694 }
9695 /* Support is marked in bit 8 */
9696 if (ret & BIT(8))
9697 battery_info.batteries[battery].charge_behaviours |=
9698 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
9699 }
9700 if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
9701 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
9702 pr_err("Error probing battery inhibit charge; %d\n", battery);
9703 return -ENODEV;
9704 }
9705 /* Support is marked in bit 5 */
9706 if (ret & BIT(5))
9707 battery_info.batteries[battery].charge_behaviours |=
9708 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
9709 }
9710
9711 battery_info.batteries[battery].charge_behaviours |=
9712 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
9713
9714 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
9715 battery,
9716 battery_info.batteries[battery].charge_start,
9717 battery_info.batteries[battery].charge_stop,
9718 battery_info.batteries[battery].charge_behaviours);
9719
9720 return 0;
9721 }
9722
9723 /* General helper functions */
9724
tpacpi_battery_get_id(const char * battery_name)9725 static int tpacpi_battery_get_id(const char *battery_name)
9726 {
9727
9728 if (strcmp(battery_name, "BAT0") == 0 ||
9729 tp_features.battery_force_primary)
9730 return BAT_PRIMARY;
9731 if (strcmp(battery_name, "BAT1") == 0)
9732 return BAT_SECONDARY;
9733 /*
9734 * If for some reason the battery is not BAT0 nor is it
9735 * BAT1, we will assume it's the default, first battery,
9736 * AKA primary.
9737 */
9738 pr_warn("unknown battery %s, assuming primary", battery_name);
9739 return BAT_PRIMARY;
9740 }
9741
9742 /* sysfs interface */
9743
tpacpi_battery_store(int what,struct device * dev,const char * buf,size_t count)9744 static ssize_t tpacpi_battery_store(int what,
9745 struct device *dev,
9746 const char *buf, size_t count)
9747 {
9748 struct power_supply *supply = to_power_supply(dev);
9749 unsigned long value;
9750 int battery, rval;
9751 /*
9752 * Some systems have support for more than
9753 * one battery. If that is the case,
9754 * tpacpi_battery_probe marked that addressing
9755 * them individually is supported, so we do that
9756 * based on the device struct.
9757 *
9758 * On systems that are not supported, we assume
9759 * the primary as most of the ACPI calls fail
9760 * with "Any Battery" as the parameter.
9761 */
9762 if (battery_info.individual_addressing)
9763 /* BAT_PRIMARY or BAT_SECONDARY */
9764 battery = tpacpi_battery_get_id(supply->desc->name);
9765 else
9766 battery = BAT_PRIMARY;
9767
9768 rval = kstrtoul(buf, 10, &value);
9769 if (rval)
9770 return rval;
9771
9772 switch (what) {
9773 case THRESHOLD_START:
9774 if (!battery_info.batteries[battery].start_support)
9775 return -ENODEV;
9776 /* valid values are [0, 99] */
9777 if (value > 99)
9778 return -EINVAL;
9779 if (value > battery_info.batteries[battery].charge_stop)
9780 return -EINVAL;
9781 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9782 return -ENODEV;
9783 battery_info.batteries[battery].charge_start = value;
9784 return count;
9785
9786 case THRESHOLD_STOP:
9787 if (!battery_info.batteries[battery].stop_support)
9788 return -ENODEV;
9789 /* valid values are [1, 100] */
9790 if (value < 1 || value > 100)
9791 return -EINVAL;
9792 if (value < battery_info.batteries[battery].charge_start)
9793 return -EINVAL;
9794 battery_info.batteries[battery].charge_stop = value;
9795 /*
9796 * When 100 is passed to stop, we need to flip
9797 * it to 0 as that the EC understands that as
9798 * "Default", which will charge to 100%
9799 */
9800 if (value == 100)
9801 value = 0;
9802 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9803 return -EINVAL;
9804 return count;
9805 default:
9806 pr_crit("Wrong parameter: %d", what);
9807 return -EINVAL;
9808 }
9809 return count;
9810 }
9811
tpacpi_battery_show(int what,struct device * dev,char * buf)9812 static ssize_t tpacpi_battery_show(int what,
9813 struct device *dev,
9814 char *buf)
9815 {
9816 struct power_supply *supply = to_power_supply(dev);
9817 int ret, battery;
9818 /*
9819 * Some systems have support for more than
9820 * one battery. If that is the case,
9821 * tpacpi_battery_probe marked that addressing
9822 * them individually is supported, so we;
9823 * based on the device struct.
9824 *
9825 * On systems that are not supported, we assume
9826 * the primary as most of the ACPI calls fail
9827 * with "Any Battery" as the parameter.
9828 */
9829 if (battery_info.individual_addressing)
9830 /* BAT_PRIMARY or BAT_SECONDARY */
9831 battery = tpacpi_battery_get_id(supply->desc->name);
9832 else
9833 battery = BAT_PRIMARY;
9834 if (tpacpi_battery_get(what, battery, &ret))
9835 return -ENODEV;
9836 return sysfs_emit(buf, "%d\n", ret);
9837 }
9838
charge_control_start_threshold_show(struct device * device,struct device_attribute * attr,char * buf)9839 static ssize_t charge_control_start_threshold_show(struct device *device,
9840 struct device_attribute *attr,
9841 char *buf)
9842 {
9843 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9844 }
9845
charge_control_end_threshold_show(struct device * device,struct device_attribute * attr,char * buf)9846 static ssize_t charge_control_end_threshold_show(struct device *device,
9847 struct device_attribute *attr,
9848 char *buf)
9849 {
9850 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9851 }
9852
charge_behaviour_show(struct device * dev,struct device_attribute * attr,char * buf)9853 static ssize_t charge_behaviour_show(struct device *dev,
9854 struct device_attribute *attr,
9855 char *buf)
9856 {
9857 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
9858 struct power_supply *supply = to_power_supply(dev);
9859 unsigned int available;
9860 int ret, battery;
9861
9862 battery = tpacpi_battery_get_id(supply->desc->name);
9863 available = battery_info.batteries[battery].charge_behaviours;
9864
9865 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
9866 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
9867 return -ENODEV;
9868 if (ret) {
9869 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
9870 goto out;
9871 }
9872 }
9873
9874 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
9875 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
9876 return -ENODEV;
9877 if (ret) {
9878 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
9879 goto out;
9880 }
9881 }
9882
9883 out:
9884 return power_supply_charge_behaviour_show(dev, available, active, buf);
9885 }
9886
charge_control_start_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9887 static ssize_t charge_control_start_threshold_store(struct device *dev,
9888 struct device_attribute *attr,
9889 const char *buf, size_t count)
9890 {
9891 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9892 }
9893
charge_control_end_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9894 static ssize_t charge_control_end_threshold_store(struct device *dev,
9895 struct device_attribute *attr,
9896 const char *buf, size_t count)
9897 {
9898 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9899 }
9900
charge_behaviour_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)9901 static ssize_t charge_behaviour_store(struct device *dev,
9902 struct device_attribute *attr,
9903 const char *buf, size_t count)
9904 {
9905 struct power_supply *supply = to_power_supply(dev);
9906 int selected, battery, ret = 0;
9907 unsigned int available;
9908
9909 battery = tpacpi_battery_get_id(supply->desc->name);
9910 available = battery_info.batteries[battery].charge_behaviours;
9911 selected = power_supply_charge_behaviour_parse(available, buf);
9912
9913 if (selected < 0)
9914 return selected;
9915
9916 switch (selected) {
9917 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
9918 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9919 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9920 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9921 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
9922 if (ret < 0)
9923 return ret;
9924 break;
9925 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
9926 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
9927 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
9928 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
9929 if (ret < 0)
9930 return ret;
9931 break;
9932 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
9933 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
9934 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
9935 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
9936 if (ret < 0)
9937 return ret;
9938 break;
9939 default:
9940 dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
9941 return -EINVAL;
9942 }
9943
9944 return count;
9945 }
9946
9947 static DEVICE_ATTR_RW(charge_control_start_threshold);
9948 static DEVICE_ATTR_RW(charge_control_end_threshold);
9949 static DEVICE_ATTR_RW(charge_behaviour);
9950 static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
9951 charge_start_threshold,
9952 0644,
9953 charge_control_start_threshold_show,
9954 charge_control_start_threshold_store
9955 );
9956 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
9957 charge_stop_threshold,
9958 0644,
9959 charge_control_end_threshold_show,
9960 charge_control_end_threshold_store
9961 );
9962
9963 static struct attribute *tpacpi_battery_attrs[] = {
9964 &dev_attr_charge_control_start_threshold.attr,
9965 &dev_attr_charge_control_end_threshold.attr,
9966 &dev_attr_charge_start_threshold.attr,
9967 &dev_attr_charge_stop_threshold.attr,
9968 &dev_attr_charge_behaviour.attr,
9969 NULL,
9970 };
9971
9972 ATTRIBUTE_GROUPS(tpacpi_battery);
9973
9974 /* ACPI battery hooking */
9975
tpacpi_battery_add(struct power_supply * battery,struct acpi_battery_hook * hook)9976 static int tpacpi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
9977 {
9978 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9979
9980 if (tpacpi_battery_probe(batteryid))
9981 return -ENODEV;
9982 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9983 return -ENODEV;
9984 return 0;
9985 }
9986
tpacpi_battery_remove(struct power_supply * battery,struct acpi_battery_hook * hook)9987 static int tpacpi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
9988 {
9989 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9990 return 0;
9991 }
9992
9993 static struct acpi_battery_hook battery_hook = {
9994 .add_battery = tpacpi_battery_add,
9995 .remove_battery = tpacpi_battery_remove,
9996 .name = "ThinkPad Battery Extension",
9997 };
9998
9999 /* Subdriver init/exit */
10000
10001 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
10002 /*
10003 * Individual addressing is broken on models that expose the
10004 * primary battery as BAT1.
10005 */
10006 TPACPI_Q_LNV('G', '8', true), /* ThinkPad X131e */
10007 TPACPI_Q_LNV('8', 'F', true), /* Thinkpad X120e */
10008 TPACPI_Q_LNV('J', '7', true), /* B5400 */
10009 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
10010 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
10011 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
10012 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
10013 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
10014 };
10015
tpacpi_battery_init(struct ibm_init_struct * ibm)10016 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
10017 {
10018 memset(&battery_info, 0, sizeof(battery_info));
10019
10020 tp_features.battery_force_primary = tpacpi_check_quirks(
10021 battery_quirk_table,
10022 ARRAY_SIZE(battery_quirk_table));
10023
10024 battery_hook_register(&battery_hook);
10025 return 0;
10026 }
10027
tpacpi_battery_exit(void)10028 static void tpacpi_battery_exit(void)
10029 {
10030 battery_hook_unregister(&battery_hook);
10031 }
10032
10033 static struct ibm_struct battery_driver_data = {
10034 .name = "battery",
10035 .exit = tpacpi_battery_exit,
10036 };
10037
10038 /*************************************************************************
10039 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
10040 */
10041
10042 static struct drm_privacy_screen *lcdshadow_dev;
10043 static acpi_handle lcdshadow_get_handle;
10044 static acpi_handle lcdshadow_set_handle;
10045
lcdshadow_set_sw_state(struct drm_privacy_screen * priv,enum drm_privacy_screen_status state)10046 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
10047 enum drm_privacy_screen_status state)
10048 {
10049 int output;
10050
10051 if (WARN_ON(!mutex_is_locked(&priv->lock)))
10052 return -EIO;
10053
10054 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
10055 return -EIO;
10056
10057 priv->hw_state = priv->sw_state = state;
10058 return 0;
10059 }
10060
lcdshadow_get_hw_state(struct drm_privacy_screen * priv)10061 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
10062 {
10063 int output;
10064
10065 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10066 return;
10067
10068 priv->hw_state = priv->sw_state = output & 0x1;
10069 }
10070
10071 static const struct drm_privacy_screen_ops lcdshadow_ops = {
10072 .set_sw_state = lcdshadow_set_sw_state,
10073 .get_hw_state = lcdshadow_get_hw_state,
10074 };
10075
tpacpi_lcdshadow_init(struct ibm_init_struct * iibm)10076 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
10077 {
10078 acpi_status status1, status2;
10079 int output;
10080
10081 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
10082 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
10083 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
10084 return 0;
10085
10086 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
10087 return -EIO;
10088
10089 if (!(output & 0x10000))
10090 return 0;
10091
10092 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
10093 &lcdshadow_ops, NULL);
10094 if (IS_ERR(lcdshadow_dev))
10095 return PTR_ERR(lcdshadow_dev);
10096
10097 return 0;
10098 }
10099
lcdshadow_exit(void)10100 static void lcdshadow_exit(void)
10101 {
10102 drm_privacy_screen_unregister(lcdshadow_dev);
10103 }
10104
lcdshadow_resume(void)10105 static void lcdshadow_resume(void)
10106 {
10107 if (!lcdshadow_dev)
10108 return;
10109
10110 mutex_lock(&lcdshadow_dev->lock);
10111 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
10112 mutex_unlock(&lcdshadow_dev->lock);
10113 }
10114
lcdshadow_read(struct seq_file * m)10115 static int lcdshadow_read(struct seq_file *m)
10116 {
10117 if (!lcdshadow_dev) {
10118 seq_puts(m, "status:\t\tnot supported\n");
10119 } else {
10120 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
10121 seq_puts(m, "commands:\t0, 1\n");
10122 }
10123
10124 return 0;
10125 }
10126
lcdshadow_write(char * buf)10127 static int lcdshadow_write(char *buf)
10128 {
10129 char *cmd;
10130 int res, state = -EINVAL;
10131
10132 if (!lcdshadow_dev)
10133 return -ENODEV;
10134
10135 while ((cmd = strsep(&buf, ","))) {
10136 res = kstrtoint(cmd, 10, &state);
10137 if (res < 0)
10138 return res;
10139 }
10140
10141 if (state >= 2 || state < 0)
10142 return -EINVAL;
10143
10144 mutex_lock(&lcdshadow_dev->lock);
10145 res = lcdshadow_set_sw_state(lcdshadow_dev, state);
10146 mutex_unlock(&lcdshadow_dev->lock);
10147
10148 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
10149
10150 return res;
10151 }
10152
10153 static struct ibm_struct lcdshadow_driver_data = {
10154 .name = "lcdshadow",
10155 .exit = lcdshadow_exit,
10156 .resume = lcdshadow_resume,
10157 .read = lcdshadow_read,
10158 .write = lcdshadow_write,
10159 };
10160
10161 /*************************************************************************
10162 * Thinkpad sensor interfaces
10163 */
10164
10165 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
10166 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
10167 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
10168 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
10169
10170 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
10171 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
10172
10173 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
10174 #define PALMSENSOR_ON_BIT 1 /* psensor status */
10175
10176 static bool has_palmsensor;
10177 static bool has_lapsensor;
10178 static bool palm_state;
10179 static bool lap_state;
10180 static int dytc_version;
10181
dytc_command(int command,int * output)10182 static int dytc_command(int command, int *output)
10183 {
10184 acpi_handle dytc_handle;
10185
10186 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
10187 /* Platform doesn't support DYTC */
10188 return -ENODEV;
10189 }
10190 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
10191 return -EIO;
10192 return 0;
10193 }
10194
lapsensor_get(bool * present,bool * state)10195 static int lapsensor_get(bool *present, bool *state)
10196 {
10197 int output, err;
10198
10199 *present = false;
10200 err = dytc_command(DYTC_CMD_GET, &output);
10201 if (err)
10202 return err;
10203
10204 *present = true; /*If we get his far, we have lapmode support*/
10205 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
10206 return 0;
10207 }
10208
palmsensor_get(bool * present,bool * state)10209 static int palmsensor_get(bool *present, bool *state)
10210 {
10211 acpi_handle psensor_handle;
10212 int output;
10213
10214 *present = false;
10215 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
10216 return -ENODEV;
10217 if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
10218 return -EIO;
10219
10220 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
10221 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
10222 return 0;
10223 }
10224
lapsensor_refresh(void)10225 static void lapsensor_refresh(void)
10226 {
10227 bool state;
10228 int err;
10229
10230 if (has_lapsensor) {
10231 err = lapsensor_get(&has_lapsensor, &state);
10232 if (err)
10233 return;
10234 if (lap_state != state) {
10235 lap_state = state;
10236 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
10237 }
10238 }
10239 }
10240
palmsensor_refresh(void)10241 static void palmsensor_refresh(void)
10242 {
10243 bool state;
10244 int err;
10245
10246 if (has_palmsensor) {
10247 err = palmsensor_get(&has_palmsensor, &state);
10248 if (err)
10249 return;
10250 if (palm_state != state) {
10251 palm_state = state;
10252 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
10253 }
10254 }
10255 }
10256
dytc_lapmode_show(struct device * dev,struct device_attribute * attr,char * buf)10257 static ssize_t dytc_lapmode_show(struct device *dev,
10258 struct device_attribute *attr,
10259 char *buf)
10260 {
10261 if (has_lapsensor)
10262 return sysfs_emit(buf, "%d\n", lap_state);
10263 return sysfs_emit(buf, "\n");
10264 }
10265 static DEVICE_ATTR_RO(dytc_lapmode);
10266
palmsensor_show(struct device * dev,struct device_attribute * attr,char * buf)10267 static ssize_t palmsensor_show(struct device *dev,
10268 struct device_attribute *attr,
10269 char *buf)
10270 {
10271 if (has_palmsensor)
10272 return sysfs_emit(buf, "%d\n", palm_state);
10273 return sysfs_emit(buf, "\n");
10274 }
10275 static DEVICE_ATTR_RO(palmsensor);
10276
10277 static struct attribute *proxsensor_attributes[] = {
10278 &dev_attr_dytc_lapmode.attr,
10279 &dev_attr_palmsensor.attr,
10280 NULL
10281 };
10282
proxsensor_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)10283 static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
10284 struct attribute *attr, int n)
10285 {
10286 if (attr == &dev_attr_dytc_lapmode.attr) {
10287 /*
10288 * Platforms before DYTC version 5 claim to have a lap sensor,
10289 * but it doesn't work, so we ignore them.
10290 */
10291 if (!has_lapsensor || dytc_version < 5)
10292 return 0;
10293 } else if (attr == &dev_attr_palmsensor.attr) {
10294 if (!has_palmsensor)
10295 return 0;
10296 }
10297
10298 return attr->mode;
10299 }
10300
10301 static const struct attribute_group proxsensor_attr_group = {
10302 .is_visible = proxsensor_attr_is_visible,
10303 .attrs = proxsensor_attributes,
10304 };
10305
tpacpi_proxsensor_init(struct ibm_init_struct * iibm)10306 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
10307 {
10308 int palm_err, lap_err;
10309
10310 palm_err = palmsensor_get(&has_palmsensor, &palm_state);
10311 lap_err = lapsensor_get(&has_lapsensor, &lap_state);
10312 /* If support isn't available for both devices return -ENODEV */
10313 if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
10314 return -ENODEV;
10315 /* Otherwise, if there was an error return it */
10316 if (palm_err && (palm_err != -ENODEV))
10317 return palm_err;
10318 if (lap_err && (lap_err != -ENODEV))
10319 return lap_err;
10320
10321 return 0;
10322 }
10323
10324 static struct ibm_struct proxsensor_driver_data = {
10325 .name = "proximity-sensor",
10326 };
10327
10328 /*************************************************************************
10329 * DYTC Platform Profile interface
10330 */
10331
10332 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
10333 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
10334 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
10335
10336 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
10337 #define DYTC_FC_MMC 27 /* MMC Mode supported */
10338 #define DYTC_FC_PSC 29 /* PSC Mode supported */
10339 #define DYTC_FC_AMT 31 /* AMT mode supported */
10340
10341 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
10342 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
10343
10344 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
10345 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
10346 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
10347
10348 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
10349 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
10350 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
10351 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
10352 #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
10353
10354 #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
10355 #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
10356
10357 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
10358 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
10359 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
10360 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
10361
10362 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
10363 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
10364 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
10365
10366 #define DYTC_MODE_PSCV9_LOWPOWER 1 /* Low power mode */
10367 #define DYTC_MODE_PSCV9_BALANCE 3 /* Default mode aka balanced */
10368 #define DYTC_MODE_PSCV9_PERFORM 4 /* High power mode aka performance */
10369
10370 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
10371 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
10372
10373 #define DYTC_SET_COMMAND(function, mode, on) \
10374 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
10375 (mode) << DYTC_SET_MODE_BIT | \
10376 (on) << DYTC_SET_VALID_BIT)
10377
10378 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
10379 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
10380 static int dytc_control_amt(bool enable);
10381 static bool dytc_amt_active;
10382
10383 static enum platform_profile_option dytc_current_profile;
10384 static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
10385 static DEFINE_MUTEX(dytc_mutex);
10386 static int dytc_capabilities;
10387 static bool dytc_mmc_get_available;
10388 static int profile_force;
10389
10390 static int platform_psc_profile_lowpower = DYTC_MODE_PSC_LOWPOWER;
10391 static int platform_psc_profile_balanced = DYTC_MODE_PSC_BALANCE;
10392 static int platform_psc_profile_performance = DYTC_MODE_PSC_PERFORM;
10393
convert_dytc_to_profile(int funcmode,int dytcmode,enum platform_profile_option * profile)10394 static int convert_dytc_to_profile(int funcmode, int dytcmode,
10395 enum platform_profile_option *profile)
10396 {
10397 switch (funcmode) {
10398 case DYTC_FUNCTION_MMC:
10399 switch (dytcmode) {
10400 case DYTC_MODE_MMC_LOWPOWER:
10401 *profile = PLATFORM_PROFILE_LOW_POWER;
10402 break;
10403 case DYTC_MODE_MMC_DEFAULT:
10404 case DYTC_MODE_MMC_BALANCE:
10405 *profile = PLATFORM_PROFILE_BALANCED;
10406 break;
10407 case DYTC_MODE_MMC_PERFORM:
10408 *profile = PLATFORM_PROFILE_PERFORMANCE;
10409 break;
10410 default: /* Unknown mode */
10411 return -EINVAL;
10412 }
10413 return 0;
10414 case DYTC_FUNCTION_PSC:
10415 if (dytcmode == platform_psc_profile_lowpower)
10416 *profile = PLATFORM_PROFILE_LOW_POWER;
10417 else if (dytcmode == platform_psc_profile_balanced)
10418 *profile = PLATFORM_PROFILE_BALANCED;
10419 else if (dytcmode == platform_psc_profile_performance)
10420 *profile = PLATFORM_PROFILE_PERFORMANCE;
10421 else
10422 return -EINVAL;
10423
10424 return 0;
10425 case DYTC_FUNCTION_AMT:
10426 /* For now return balanced. It's the closest we have to 'auto' */
10427 *profile = PLATFORM_PROFILE_BALANCED;
10428 return 0;
10429 default:
10430 /* Unknown function */
10431 pr_debug("unknown function 0x%x\n", funcmode);
10432 return -EOPNOTSUPP;
10433 }
10434 return 0;
10435 }
10436
convert_profile_to_dytc(enum platform_profile_option profile,int * perfmode)10437 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
10438 {
10439 switch (profile) {
10440 case PLATFORM_PROFILE_LOW_POWER:
10441 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10442 *perfmode = DYTC_MODE_MMC_LOWPOWER;
10443 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10444 *perfmode = platform_psc_profile_lowpower;
10445 break;
10446 case PLATFORM_PROFILE_BALANCED:
10447 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10448 *perfmode = DYTC_MODE_MMC_BALANCE;
10449 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10450 *perfmode = platform_psc_profile_balanced;
10451 break;
10452 case PLATFORM_PROFILE_PERFORMANCE:
10453 if (dytc_capabilities & BIT(DYTC_FC_MMC))
10454 *perfmode = DYTC_MODE_MMC_PERFORM;
10455 else if (dytc_capabilities & BIT(DYTC_FC_PSC))
10456 *perfmode = platform_psc_profile_performance;
10457 break;
10458 default: /* Unknown profile */
10459 return -EOPNOTSUPP;
10460 }
10461 return 0;
10462 }
10463
10464 /*
10465 * dytc_profile_get: Function to register with platform_profile
10466 * handler. Returns current platform profile.
10467 */
dytc_profile_get(struct device * dev,enum platform_profile_option * profile)10468 static int dytc_profile_get(struct device *dev,
10469 enum platform_profile_option *profile)
10470 {
10471 *profile = dytc_current_profile;
10472 return 0;
10473 }
10474
dytc_control_amt(bool enable)10475 static int dytc_control_amt(bool enable)
10476 {
10477 int dummy;
10478 int err;
10479 int cmd;
10480
10481 if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
10482 pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
10483 return -ENODEV;
10484 }
10485
10486 if (enable)
10487 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
10488 else
10489 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
10490
10491 pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
10492 err = dytc_command(cmd, &dummy);
10493 if (err)
10494 return err;
10495 dytc_amt_active = enable;
10496 return 0;
10497 }
10498
10499 /*
10500 * Helper function - check if we are in CQL mode and if we are
10501 * - disable CQL,
10502 * - run the command
10503 * - enable CQL
10504 * If not in CQL mode, just run the command
10505 */
dytc_cql_command(int command,int * output)10506 static int dytc_cql_command(int command, int *output)
10507 {
10508 int err, cmd_err, dummy;
10509 int cur_funcmode;
10510
10511 /* Determine if we are in CQL mode. This alters the commands we do */
10512 err = dytc_command(DYTC_CMD_GET, output);
10513 if (err)
10514 return err;
10515
10516 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10517 /* Check if we're OK to return immediately */
10518 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
10519 return 0;
10520
10521 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10522 atomic_inc(&dytc_ignore_event);
10523 err = dytc_command(DYTC_DISABLE_CQL, &dummy);
10524 if (err)
10525 return err;
10526 }
10527
10528 cmd_err = dytc_command(command, output);
10529 /* Check return condition after we've restored CQL state */
10530
10531 if (cur_funcmode == DYTC_FUNCTION_CQL) {
10532 err = dytc_command(DYTC_ENABLE_CQL, &dummy);
10533 if (err)
10534 return err;
10535 }
10536 return cmd_err;
10537 }
10538
10539 /*
10540 * dytc_profile_set: Function to register with platform_profile
10541 * handler. Sets current platform profile.
10542 */
dytc_profile_set(struct device * dev,enum platform_profile_option profile)10543 static int dytc_profile_set(struct device *dev,
10544 enum platform_profile_option profile)
10545 {
10546 int perfmode;
10547 int output;
10548 int err;
10549
10550 err = mutex_lock_interruptible(&dytc_mutex);
10551 if (err)
10552 return err;
10553
10554 err = convert_profile_to_dytc(profile, &perfmode);
10555 if (err)
10556 goto unlock;
10557
10558 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10559 if (profile == PLATFORM_PROFILE_BALANCED) {
10560 /*
10561 * To get back to balanced mode we need to issue a reset command.
10562 * Note we still need to disable CQL mode before hand and re-enable
10563 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
10564 * stuck at 0 for aprox. 30 minutes.
10565 */
10566 err = dytc_cql_command(DYTC_CMD_RESET, &output);
10567 if (err)
10568 goto unlock;
10569 } else {
10570 /* Determine if we are in CQL mode. This alters the commands we do */
10571 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
10572 &output);
10573 if (err)
10574 goto unlock;
10575 }
10576 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10577 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
10578 if (err)
10579 goto unlock;
10580
10581 /* system supports AMT, activate it when on balanced */
10582 if (dytc_capabilities & BIT(DYTC_FC_AMT))
10583 dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
10584 }
10585 /* Success - update current profile */
10586 dytc_current_profile = profile;
10587 unlock:
10588 mutex_unlock(&dytc_mutex);
10589 return err;
10590 }
10591
dytc_profile_probe(void * drvdata,unsigned long * choices)10592 static int dytc_profile_probe(void *drvdata, unsigned long *choices)
10593 {
10594 set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
10595 set_bit(PLATFORM_PROFILE_BALANCED, choices);
10596 set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
10597
10598 return 0;
10599 }
10600
10601 static const struct platform_profile_ops dytc_profile_ops = {
10602 .probe = dytc_profile_probe,
10603 .profile_get = dytc_profile_get,
10604 .profile_set = dytc_profile_set,
10605 };
10606
dytc_profile_refresh(void)10607 static void dytc_profile_refresh(void)
10608 {
10609 enum platform_profile_option profile;
10610 int output = 0, err = 0;
10611 int perfmode, funcmode = 0;
10612
10613 mutex_lock(&dytc_mutex);
10614 if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
10615 if (dytc_mmc_get_available)
10616 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10617 else
10618 err = dytc_cql_command(DYTC_CMD_GET, &output);
10619 funcmode = DYTC_FUNCTION_MMC;
10620 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
10621 err = dytc_command(DYTC_CMD_GET, &output);
10622 /* Check if we are PSC mode, or have AMT enabled */
10623 funcmode = (output >> DYTC_GET_FUNCTION_BIT) & 0xF;
10624 } else { /* Unknown profile mode */
10625 err = -ENODEV;
10626 }
10627 mutex_unlock(&dytc_mutex);
10628 if (err)
10629 return;
10630
10631 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
10632 err = convert_dytc_to_profile(funcmode, perfmode, &profile);
10633 if (!err && profile != dytc_current_profile) {
10634 dytc_current_profile = profile;
10635 platform_profile_notify(tpacpi_pprof);
10636 }
10637 }
10638
tpacpi_dytc_profile_init(struct ibm_init_struct * iibm)10639 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
10640 {
10641 int err, output;
10642
10643 err = dytc_command(DYTC_CMD_QUERY, &output);
10644 if (err)
10645 return err;
10646
10647 if (output & BIT(DYTC_QUERY_ENABLE_BIT))
10648 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
10649
10650 dbg_printk(TPACPI_DBG_INIT, "DYTC version %d\n", dytc_version);
10651 /* Check DYTC is enabled and supports mode setting */
10652 if (dytc_version < 5)
10653 return -ENODEV;
10654
10655 /* Check what capabilities are supported */
10656 err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
10657 if (err)
10658 return err;
10659
10660 /* Check if user wants to override the profile selection */
10661 if (profile_force) {
10662 switch (profile_force) {
10663 case -1:
10664 dytc_capabilities = 0;
10665 break;
10666 case 1:
10667 dytc_capabilities = BIT(DYTC_FC_MMC);
10668 break;
10669 case 2:
10670 dytc_capabilities = BIT(DYTC_FC_PSC);
10671 break;
10672 }
10673 pr_debug("Profile selection forced: 0x%x\n", dytc_capabilities);
10674 }
10675 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
10676 pr_debug("MMC is supported\n");
10677 /*
10678 * Check if MMC_GET functionality available
10679 * Version > 6 and return success from MMC_GET command
10680 */
10681 dytc_mmc_get_available = false;
10682 if (dytc_version >= 6) {
10683 err = dytc_command(DYTC_CMD_MMC_GET, &output);
10684 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
10685 dytc_mmc_get_available = true;
10686 }
10687 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
10688 pr_debug("PSC is supported\n");
10689 if (dytc_version >= 9) { /* update profiles for DYTC 9 and up */
10690 platform_psc_profile_lowpower = DYTC_MODE_PSCV9_LOWPOWER;
10691 platform_psc_profile_balanced = DYTC_MODE_PSCV9_BALANCE;
10692 platform_psc_profile_performance = DYTC_MODE_PSCV9_PERFORM;
10693 }
10694 } else {
10695 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
10696 return -ENODEV;
10697 }
10698
10699 dbg_printk(TPACPI_DBG_INIT,
10700 "DYTC version %d: thermal mode available\n", dytc_version);
10701
10702 /* Create platform_profile structure and register */
10703 tpacpi_pprof = platform_profile_register(&tpacpi_pdev->dev, "thinkpad-acpi-profile",
10704 NULL, &dytc_profile_ops);
10705 /*
10706 * If for some reason platform_profiles aren't enabled
10707 * don't quit terminally.
10708 */
10709 if (IS_ERR(tpacpi_pprof))
10710 return -ENODEV;
10711
10712 /* Ensure initial values are correct */
10713 dytc_profile_refresh();
10714
10715 /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
10716 if (dytc_capabilities & BIT(DYTC_FC_PSC))
10717 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
10718
10719 return 0;
10720 }
10721
dytc_profile_exit(void)10722 static void dytc_profile_exit(void)
10723 {
10724 if (!IS_ERR_OR_NULL(tpacpi_pprof))
10725 platform_profile_remove(tpacpi_pprof);
10726 }
10727
10728 static struct ibm_struct dytc_profile_driver_data = {
10729 .name = "dytc-profile",
10730 .exit = dytc_profile_exit,
10731 };
10732
10733 /*************************************************************************
10734 * Keyboard language interface
10735 */
10736
10737 struct keyboard_lang_data {
10738 const char *lang_str;
10739 int lang_code;
10740 };
10741
10742 static const struct keyboard_lang_data keyboard_lang_data[] = {
10743 {"be", 0x080c},
10744 {"cz", 0x0405},
10745 {"da", 0x0406},
10746 {"de", 0x0c07},
10747 {"en", 0x0000},
10748 {"es", 0x2c0a},
10749 {"et", 0x0425},
10750 {"fr", 0x040c},
10751 {"fr-ch", 0x100c},
10752 {"hu", 0x040e},
10753 {"it", 0x0410},
10754 {"jp", 0x0411},
10755 {"nl", 0x0413},
10756 {"nn", 0x0414},
10757 {"pl", 0x0415},
10758 {"pt", 0x0816},
10759 {"sl", 0x041b},
10760 {"sv", 0x081d},
10761 {"tr", 0x041f},
10762 };
10763
set_keyboard_lang_command(int command)10764 static int set_keyboard_lang_command(int command)
10765 {
10766 acpi_handle sskl_handle;
10767 int output;
10768
10769 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
10770 /* Platform doesn't support SSKL */
10771 return -ENODEV;
10772 }
10773
10774 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
10775 return -EIO;
10776
10777 return 0;
10778 }
10779
get_keyboard_lang(int * output)10780 static int get_keyboard_lang(int *output)
10781 {
10782 acpi_handle gskl_handle;
10783 int kbd_lang;
10784
10785 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
10786 /* Platform doesn't support GSKL */
10787 return -ENODEV;
10788 }
10789
10790 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
10791 return -EIO;
10792
10793 /*
10794 * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
10795 * '(' and ')') keys which use layout dependent key-press emulation.
10796 */
10797 if (kbd_lang & METHOD_ERR)
10798 return -ENODEV;
10799
10800 *output = kbd_lang;
10801
10802 return 0;
10803 }
10804
10805 /* sysfs keyboard language entry */
keyboard_lang_show(struct device * dev,struct device_attribute * attr,char * buf)10806 static ssize_t keyboard_lang_show(struct device *dev,
10807 struct device_attribute *attr,
10808 char *buf)
10809 {
10810 int output, err, i, len = 0;
10811
10812 err = get_keyboard_lang(&output);
10813 if (err)
10814 return err;
10815
10816 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10817 if (i)
10818 len += sysfs_emit_at(buf, len, "%s", " ");
10819
10820 if (output == keyboard_lang_data[i].lang_code) {
10821 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
10822 } else {
10823 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
10824 }
10825 }
10826 len += sysfs_emit_at(buf, len, "\n");
10827
10828 return len;
10829 }
10830
keyboard_lang_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)10831 static ssize_t keyboard_lang_store(struct device *dev,
10832 struct device_attribute *attr,
10833 const char *buf, size_t count)
10834 {
10835 int err, i;
10836 bool lang_found = false;
10837 int lang_code = 0;
10838
10839 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
10840 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
10841 lang_code = keyboard_lang_data[i].lang_code;
10842 lang_found = true;
10843 break;
10844 }
10845 }
10846
10847 if (lang_found) {
10848 lang_code = lang_code | 1 << 24;
10849
10850 /* Set language code */
10851 err = set_keyboard_lang_command(lang_code);
10852 if (err)
10853 return err;
10854 } else {
10855 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
10856 return -EINVAL;
10857 }
10858
10859 tpacpi_disclose_usertask(attr->attr.name,
10860 "keyboard language is set to %s\n", buf);
10861
10862 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
10863
10864 return count;
10865 }
10866 static DEVICE_ATTR_RW(keyboard_lang);
10867
10868 static struct attribute *kbdlang_attributes[] = {
10869 &dev_attr_keyboard_lang.attr,
10870 NULL
10871 };
10872
kbdlang_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)10873 static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
10874 struct attribute *attr, int n)
10875 {
10876 return tp_features.kbd_lang ? attr->mode : 0;
10877 }
10878
10879 static const struct attribute_group kbdlang_attr_group = {
10880 .is_visible = kbdlang_attr_is_visible,
10881 .attrs = kbdlang_attributes,
10882 };
10883
tpacpi_kbdlang_init(struct ibm_init_struct * iibm)10884 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
10885 {
10886 int err, output;
10887
10888 err = get_keyboard_lang(&output);
10889 tp_features.kbd_lang = !err;
10890 return err;
10891 }
10892
10893 static struct ibm_struct kbdlang_driver_data = {
10894 .name = "kbdlang",
10895 };
10896
10897 /*************************************************************************
10898 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
10899 * and WLAN feature.
10900 */
10901 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
10902 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
10903 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
10904 static bool has_antennatype;
10905 static int wwan_antennatype;
10906
dprc_command(int command,int * output)10907 static int dprc_command(int command, int *output)
10908 {
10909 acpi_handle dprc_handle;
10910
10911 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
10912 /* Platform doesn't support DPRC */
10913 return -ENODEV;
10914 }
10915
10916 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
10917 return -EIO;
10918
10919 /*
10920 * METHOD_ERR gets returned on devices where few commands are not supported
10921 * for example command to get WWAN Antenna type command is not supported on
10922 * some devices.
10923 */
10924 if (*output & METHOD_ERR)
10925 return -ENODEV;
10926
10927 return 0;
10928 }
10929
get_wwan_antenna(int * wwan_antennatype)10930 static int get_wwan_antenna(int *wwan_antennatype)
10931 {
10932 int output, err;
10933
10934 /* Get current Antenna type */
10935 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
10936 if (err)
10937 return err;
10938
10939 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
10940 *wwan_antennatype = 1;
10941 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
10942 *wwan_antennatype = 2;
10943 else
10944 return -ENODEV;
10945
10946 return 0;
10947 }
10948
10949 /* sysfs wwan antenna type entry */
wwan_antenna_type_show(struct device * dev,struct device_attribute * attr,char * buf)10950 static ssize_t wwan_antenna_type_show(struct device *dev,
10951 struct device_attribute *attr,
10952 char *buf)
10953 {
10954 switch (wwan_antennatype) {
10955 case 1:
10956 return sysfs_emit(buf, "type a\n");
10957 case 2:
10958 return sysfs_emit(buf, "type b\n");
10959 default:
10960 return -ENODATA;
10961 }
10962 }
10963 static DEVICE_ATTR_RO(wwan_antenna_type);
10964
10965 static struct attribute *dprc_attributes[] = {
10966 &dev_attr_wwan_antenna_type.attr,
10967 NULL
10968 };
10969
dprc_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)10970 static umode_t dprc_attr_is_visible(struct kobject *kobj,
10971 struct attribute *attr, int n)
10972 {
10973 return has_antennatype ? attr->mode : 0;
10974 }
10975
10976 static const struct attribute_group dprc_attr_group = {
10977 .is_visible = dprc_attr_is_visible,
10978 .attrs = dprc_attributes,
10979 };
10980
tpacpi_dprc_init(struct ibm_init_struct * iibm)10981 static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
10982 {
10983 int err;
10984
10985 err = get_wwan_antenna(&wwan_antennatype);
10986 if (err)
10987 return err;
10988
10989 has_antennatype = true;
10990 return 0;
10991 }
10992
10993 static struct ibm_struct dprc_driver_data = {
10994 .name = "dprc",
10995 };
10996
10997 /*
10998 * Auxmac
10999 *
11000 * This auxiliary mac address is enabled in the bios through the
11001 * MAC Address Pass-through feature. In most cases, there are three
11002 * possibilities: Internal Mac, Second Mac, and disabled.
11003 *
11004 */
11005
11006 #define AUXMAC_LEN 12
11007 #define AUXMAC_START 9
11008 #define AUXMAC_STRLEN 22
11009 #define AUXMAC_BEGIN_MARKER 8
11010 #define AUXMAC_END_MARKER 21
11011
11012 static char auxmac[AUXMAC_LEN + 1];
11013
auxmac_init(struct ibm_init_struct * iibm)11014 static int auxmac_init(struct ibm_init_struct *iibm)
11015 {
11016 acpi_status status;
11017 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
11018 union acpi_object *obj;
11019
11020 status = acpi_evaluate_object(NULL, "\\MACA", NULL, &buffer);
11021
11022 if (ACPI_FAILURE(status))
11023 return -ENODEV;
11024
11025 obj = buffer.pointer;
11026
11027 if (obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
11028 pr_info("Invalid buffer for MAC address pass-through.\n");
11029 goto auxmacinvalid;
11030 }
11031
11032 if (obj->string.pointer[AUXMAC_BEGIN_MARKER] != '#' ||
11033 obj->string.pointer[AUXMAC_END_MARKER] != '#') {
11034 pr_info("Invalid header for MAC address pass-through.\n");
11035 goto auxmacinvalid;
11036 }
11037
11038 if (strncmp(obj->string.pointer + AUXMAC_START, "XXXXXXXXXXXX", AUXMAC_LEN) != 0)
11039 strscpy(auxmac, obj->string.pointer + AUXMAC_START, sizeof(auxmac));
11040 else
11041 strscpy(auxmac, "disabled", sizeof(auxmac));
11042
11043 free:
11044 kfree(obj);
11045 return 0;
11046
11047 auxmacinvalid:
11048 strscpy(auxmac, "unavailable", sizeof(auxmac));
11049 goto free;
11050 }
11051
11052 static struct ibm_struct auxmac_data = {
11053 .name = "auxmac",
11054 };
11055
11056 static DEVICE_STRING_ATTR_RO(auxmac, 0444, auxmac);
11057
auxmac_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)11058 static umode_t auxmac_attr_is_visible(struct kobject *kobj,
11059 struct attribute *attr, int n)
11060 {
11061 return auxmac[0] == 0 ? 0 : attr->mode;
11062 }
11063
11064 static struct attribute *auxmac_attributes[] = {
11065 &dev_attr_auxmac.attr.attr,
11066 NULL
11067 };
11068
11069 static const struct attribute_group auxmac_attr_group = {
11070 .is_visible = auxmac_attr_is_visible,
11071 .attrs = auxmac_attributes,
11072 };
11073
11074 /*************************************************************************
11075 * HWDD subdriver, for the Lenovo Hardware Damage Detection feature.
11076 */
11077
11078 #define HWDD_GET_DMG_USBC 0x80000001
11079 #define HWDD_GET_CAP 0
11080 #define HWDD_NOT_SUPPORTED BIT(31)
11081 #define HWDD_SUPPORT_USBC BIT(0)
11082
11083 #define PORT_STATUS GENMASK(7, 4)
11084 #define LID_STATUS GENMASK(11, 8)
11085 #define BASE_STATUS GENMASK(15, 12)
11086 #define POS_STATUS GENMASK(3, 2)
11087 #define PANEL_STATUS GENMASK(1, 0)
11088
11089 #define PORT_DETAIL_OFFSET 16
11090
11091 #define PANEL_TOP 0
11092 #define PANEL_BASE 1
11093 #define PANEL_LEFT 2
11094 #define PANEL_RIGHT 3
11095
11096 #define POS_LEFT 0
11097 #define POS_CENTER 1
11098 #define POS_RIGHT 2
11099
11100 #define NUM_PORTS 4
11101
11102 static bool hwdd_support_available;
11103 static bool ucdd_supported;
11104
hwdd_command(int command,int * output)11105 static int hwdd_command(int command, int *output)
11106 {
11107 acpi_handle hwdd_handle;
11108
11109 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "HWDD", &hwdd_handle)))
11110 return -ENODEV;
11111
11112 if (!acpi_evalf(hwdd_handle, output, NULL, "dd", command))
11113 return -EIO;
11114
11115 return 0;
11116 }
11117
display_damage(char * buf,int * count,char * type,unsigned int dmg_status)11118 static bool display_damage(char *buf, int *count, char *type, unsigned int dmg_status)
11119 {
11120 unsigned char lid_status, base_status, port_status;
11121 unsigned char loc_status, pos_status, panel_status;
11122 bool damage_detected = false;
11123 int i;
11124
11125 port_status = FIELD_GET(PORT_STATUS, dmg_status);
11126 lid_status = FIELD_GET(LID_STATUS, dmg_status);
11127 base_status = FIELD_GET(BASE_STATUS, dmg_status);
11128 for (i = 0; i < NUM_PORTS; i++) {
11129 if (!(dmg_status & BIT(i)) || !(port_status & BIT(i)))
11130 continue;
11131
11132 *count += sysfs_emit_at(buf, *count, "%s: ", type);
11133 loc_status = (dmg_status >> (PORT_DETAIL_OFFSET + (4 * i))) & 0xF;
11134 pos_status = FIELD_GET(POS_STATUS, loc_status);
11135 panel_status = FIELD_GET(PANEL_STATUS, loc_status);
11136
11137 if (lid_status & BIT(i))
11138 *count += sysfs_emit_at(buf, *count, "Lid, ");
11139 if (base_status & BIT(i))
11140 *count += sysfs_emit_at(buf, *count, "Base, ");
11141
11142 switch (pos_status) {
11143 case PANEL_TOP:
11144 *count += sysfs_emit_at(buf, *count, "Top, ");
11145 break;
11146 case PANEL_BASE:
11147 *count += sysfs_emit_at(buf, *count, "Bottom, ");
11148 break;
11149 case PANEL_LEFT:
11150 *count += sysfs_emit_at(buf, *count, "Left, ");
11151 break;
11152 case PANEL_RIGHT:
11153 *count += sysfs_emit_at(buf, *count, "Right, ");
11154 break;
11155 default:
11156 pr_err("Unexpected value %d in switch statement\n", pos_status);
11157 }
11158
11159 switch (panel_status) {
11160 case POS_LEFT:
11161 *count += sysfs_emit_at(buf, *count, "Left port\n");
11162 break;
11163 case POS_CENTER:
11164 *count += sysfs_emit_at(buf, *count, "Center port\n");
11165 break;
11166 case POS_RIGHT:
11167 *count += sysfs_emit_at(buf, *count, "Right port\n");
11168 break;
11169 default:
11170 *count += sysfs_emit_at(buf, *count, "Undefined\n");
11171 break;
11172 }
11173 damage_detected = true;
11174 }
11175 return damage_detected;
11176 }
11177
11178 /* sysfs type-c damage detection detail */
hwdd_detail_show(struct device * dev,struct device_attribute * attr,char * buf)11179 static ssize_t hwdd_detail_show(struct device *dev,
11180 struct device_attribute *attr,
11181 char *buf)
11182 {
11183 unsigned int damage_status;
11184 int err, count = 0;
11185
11186 if (!ucdd_supported)
11187 return -ENODEV;
11188
11189 /* Get USB TYPE-C damage status */
11190 err = hwdd_command(HWDD_GET_DMG_USBC, &damage_status);
11191 if (err)
11192 return err;
11193
11194 if (!display_damage(buf, &count, "Type-C", damage_status))
11195 count += sysfs_emit_at(buf, count, "No damage detected\n");
11196
11197 return count;
11198 }
11199
11200 /* sysfs type-c damage detection capability */
hwdd_status_show(struct device * dev,struct device_attribute * attr,char * buf)11201 static ssize_t hwdd_status_show(struct device *dev,
11202 struct device_attribute *attr,
11203 char *buf)
11204 {
11205 unsigned int damage_status, port_status;
11206 int err, i;
11207
11208 if (!ucdd_supported)
11209 return -ENODEV;
11210
11211 /* Get USB TYPE-C damage status */
11212 err = hwdd_command(HWDD_GET_DMG_USBC, &damage_status);
11213 if (err)
11214 return err;
11215
11216 port_status = FIELD_GET(PORT_STATUS, damage_status);
11217 for (i = 0; i < NUM_PORTS; i++) {
11218 if (!(damage_status & BIT(i)))
11219 continue;
11220 if (port_status & BIT(i))
11221 return sysfs_emit(buf, "1\n");
11222 }
11223
11224 return sysfs_emit(buf, "0\n");
11225 }
11226
11227 /* sysfs type-c damage detection raw data - accessed via debugfs*/
hwdd_raw_show(struct seq_file * m,void * v)11228 static int hwdd_raw_show(struct seq_file *m, void *v)
11229 {
11230 unsigned int damage_status;
11231 int err;
11232
11233 if (!ucdd_supported)
11234 return -ENODEV;
11235
11236 /* Get USB TYPE-C damage status */
11237 err = hwdd_command(HWDD_GET_DMG_USBC, &damage_status);
11238 if (err)
11239 return err;
11240
11241 seq_printf(m, "HWDD: 0x%x\n", damage_status);
11242 return 0;
11243 }
11244 DEFINE_SHOW_ATTRIBUTE(hwdd_raw);
11245
11246 static DEVICE_ATTR_RO(hwdd_status);
11247 static DEVICE_ATTR_RO(hwdd_detail);
11248
11249 static struct attribute *hwdd_attributes[] = {
11250 &dev_attr_hwdd_status.attr,
11251 &dev_attr_hwdd_detail.attr,
11252 NULL
11253 };
11254
hwdd_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)11255 static umode_t hwdd_attr_is_visible(struct kobject *kobj,
11256 struct attribute *attr, int n)
11257 {
11258 return hwdd_support_available ? attr->mode : 0;
11259 }
11260
11261 static const struct attribute_group hwdd_attr_group = {
11262 .is_visible = hwdd_attr_is_visible,
11263 .attrs = hwdd_attributes,
11264 };
11265
tpacpi_hwdd_init(struct ibm_init_struct * iibm)11266 static int tpacpi_hwdd_init(struct ibm_init_struct *iibm)
11267 {
11268 int err, output;
11269
11270 /* Below command checks the HWDD damage capability */
11271 err = hwdd_command(HWDD_GET_CAP, &output);
11272 if (err)
11273 return err;
11274
11275 if (!(output & HWDD_NOT_SUPPORTED))
11276 return -ENODEV;
11277
11278 hwdd_support_available = true;
11279
11280 /*
11281 * BIT(0) is assigned to check capability of damage detection is
11282 * supported for USB Type-C port or not.
11283 */
11284 if (output & HWDD_SUPPORT_USBC)
11285 ucdd_supported = true;
11286
11287 return err;
11288 }
11289
11290 static struct ibm_struct hwdd_driver_data = {
11291 .name = "hwdd",
11292 };
11293
11294 /*************************************************************************
11295 * USB-C Security subdriver
11296 *
11297 * HKEY.USCS(0) is a read-only ACPI method; its argument is ignored.
11298 * It always returns:
11299 * bit 16 - USB-C security capability present on this SKU or not
11300 * bit 0 - USB-C Security state (enable or disable)
11301 *
11302 * Hotkey
11303 * ------
11304 * 0x131e (Fn+U, Fn+S): firmware toggles USBS before firing the event.
11305 * The driver reads back the new state and notifies the sysfs attribute.
11306 */
11307
11308 /* USCS() return word bit layout */
11309 #define USCS_CAP_BIT BIT(16) /* capability: feature present on SKU */
11310 #define USCS_STATUS_BIT BIT(0) /* current security state */
11311
11312 /* Protects USCS() ACPI method calls in usbc_security_query() */
11313 static DEFINE_MUTEX(usbc_security_mutex);
11314
11315 /**
11316 * usbc_security_query - read current USB-C security state via USCS()
11317 * @enabled: out - true when security is ON (data connections blocked)
11318 *
11319 * Returns:
11320 * 0 success, @enabled contains the current state
11321 * -EIO ACPI evaluation failed
11322 * -ENODEV capability bit absent; feature not present on this SKU*
11323 */
usbc_security_query(bool * enabled)11324 static int usbc_security_query(bool *enabled)
11325 {
11326 int status;
11327
11328 guard(mutex)(&usbc_security_mutex);
11329 if (!acpi_evalf(hkey_handle, &status, "USCS", "dd", 0))
11330 return -EIO;
11331
11332 if (!(status & USCS_CAP_BIT)) {
11333 pr_debug("USCS cap bit absent (raw=0x%x)\n", status);
11334 return -ENODEV;
11335 }
11336
11337 *enabled = status & USCS_STATUS_BIT;
11338 return 0;
11339 }
11340
11341 /* sysfs: /sys/devices/platform/thinkpad_acpi/usb_c_security ---------- */
usb_c_security_show(struct device * dev,struct device_attribute * attr,char * buf)11342 static ssize_t usb_c_security_show(struct device *dev,
11343 struct device_attribute *attr,
11344 char *buf)
11345 {
11346 return sysfs_emit(buf, "%s\n",
11347 str_enabled_disabled(tp_features.usbc_security_enabled));
11348 }
11349
11350 static DEVICE_ATTR_RO(usb_c_security);
11351
11352 static struct attribute *usbc_security_attributes[] = {
11353 &dev_attr_usb_c_security.attr,
11354 NULL,
11355 };
11356
usbc_security_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)11357 static umode_t usbc_security_attr_is_visible(struct kobject *kobj,
11358 struct attribute *attr, int n)
11359 {
11360 return tp_features.usbc_security_supported ? attr->mode : 0;
11361 }
11362
11363 static const struct attribute_group usbc_security_attr_group = {
11364 .is_visible = usbc_security_attr_is_visible,
11365 .attrs = usbc_security_attributes,
11366 };
11367
tpacpi_usbc_security_init(struct ibm_init_struct * iibm)11368 static int tpacpi_usbc_security_init(struct ibm_init_struct *iibm)
11369 {
11370 int err;
11371
11372 if (!acpi_has_method(hkey_handle, "USCS"))
11373 return -ENODEV;
11374
11375 err = usbc_security_query(&tp_features.usbc_security_enabled);
11376 if (err == -ENODEV)
11377 return 0;
11378 if (err)
11379 return err;
11380
11381 tp_features.usbc_security_supported = true;
11382 return 0;
11383 }
11384
11385 /* tpacpi_usbc_security_hotkey - handle Fn+U Fn+S hotkey (0x131e) */
tpacpi_usbc_security_hotkey(void)11386 static bool tpacpi_usbc_security_hotkey(void)
11387 {
11388 if (!tp_features.usbc_security_supported)
11389 return false;
11390
11391 if (usbc_security_query(&tp_features.usbc_security_enabled))
11392 return false;
11393
11394 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "usb_c_security");
11395 return true;
11396 }
11397
11398 static struct ibm_struct usbc_security_driver_data = {
11399 .name = "usbc_security",
11400 };
11401
11402 /* --------------------------------------------------------------------- */
11403
11404 static struct attribute *tpacpi_driver_attributes[] = {
11405 &driver_attr_debug_level.attr,
11406 &driver_attr_version.attr,
11407 &driver_attr_interface_version.attr,
11408 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11409 &driver_attr_wlsw_emulstate.attr,
11410 &driver_attr_bluetooth_emulstate.attr,
11411 &driver_attr_wwan_emulstate.attr,
11412 &driver_attr_uwb_emulstate.attr,
11413 #endif
11414 NULL
11415 };
11416
11417 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
tpacpi_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)11418 static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
11419 struct attribute *attr, int n)
11420 {
11421 if (attr == &driver_attr_wlsw_emulstate.attr) {
11422 if (!dbg_wlswemul)
11423 return 0;
11424 } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
11425 if (!dbg_bluetoothemul)
11426 return 0;
11427 } else if (attr == &driver_attr_wwan_emulstate.attr) {
11428 if (!dbg_wwanemul)
11429 return 0;
11430 } else if (attr == &driver_attr_uwb_emulstate.attr) {
11431 if (!dbg_uwbemul)
11432 return 0;
11433 }
11434
11435 return attr->mode;
11436 }
11437 #endif
11438
11439 static const struct attribute_group tpacpi_driver_attr_group = {
11440 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
11441 .is_visible = tpacpi_attr_is_visible,
11442 #endif
11443 .attrs = tpacpi_driver_attributes,
11444 };
11445
11446 static const struct attribute_group *tpacpi_driver_groups[] = {
11447 &tpacpi_driver_attr_group,
11448 NULL,
11449 };
11450
11451 static const struct attribute_group *tpacpi_groups[] = {
11452 &adaptive_kbd_attr_group,
11453 &hotkey_attr_group,
11454 &bluetooth_attr_group,
11455 &wan_attr_group,
11456 &cmos_attr_group,
11457 &proxsensor_attr_group,
11458 &kbdlang_attr_group,
11459 &dprc_attr_group,
11460 &auxmac_attr_group,
11461 &hwdd_attr_group,
11462 &usbc_security_attr_group,
11463 NULL,
11464 };
11465
11466 static const struct attribute_group *tpacpi_hwmon_groups[] = {
11467 &thermal_attr_group,
11468 &temp_label_attr_group,
11469 &fan_attr_group,
11470 NULL,
11471 };
11472
11473 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
11474 &fan_driver_attr_group,
11475 NULL,
11476 };
11477
11478 /****************************************************************************
11479 ****************************************************************************
11480 *
11481 * Platform drivers
11482 *
11483 ****************************************************************************
11484 ****************************************************************************/
11485
11486 static struct platform_driver tpacpi_pdriver = {
11487 .driver = {
11488 .name = TPACPI_DRVR_NAME,
11489 .pm = &tpacpi_pm,
11490 .groups = tpacpi_driver_groups,
11491 .dev_groups = tpacpi_groups,
11492 },
11493 .shutdown = tpacpi_shutdown_handler,
11494 };
11495
11496 static struct platform_driver tpacpi_hwmon_pdriver = {
11497 .driver = {
11498 .name = TPACPI_HWMON_DRVR_NAME,
11499 .groups = tpacpi_hwmon_driver_groups,
11500 },
11501 };
11502
11503 /****************************************************************************
11504 ****************************************************************************
11505 *
11506 * Infrastructure
11507 *
11508 ****************************************************************************
11509 ****************************************************************************/
11510
11511 /*
11512 * HKEY event callout for other subdrivers go here
11513 * (yes, it is ugly, but it is quick, safe, and gets the job done
11514 */
tpacpi_driver_event(const unsigned int hkey_event)11515 static bool tpacpi_driver_event(const unsigned int hkey_event)
11516 {
11517 int camera_shutter_state;
11518
11519 switch (hkey_event) {
11520 case TP_HKEY_EV_BRGHT_UP:
11521 case TP_HKEY_EV_BRGHT_DOWN:
11522 if (ibm_backlight_device)
11523 tpacpi_brightness_notify_change();
11524 /*
11525 * Key press events are suppressed by default hotkey_user_mask
11526 * and should still be reported if explicitly requested.
11527 */
11528 return false;
11529 case TP_HKEY_EV_VOL_UP:
11530 case TP_HKEY_EV_VOL_DOWN:
11531 case TP_HKEY_EV_VOL_MUTE:
11532 if (alsa_card)
11533 volume_alsa_notify_change();
11534
11535 /* Key events are suppressed by default hotkey_user_mask */
11536 return false;
11537 case TP_HKEY_EV_KBD_LIGHT:
11538 if (tp_features.kbdlight) {
11539 enum led_brightness brightness;
11540
11541 mutex_lock(&kbdlight_mutex);
11542
11543 /*
11544 * Check the brightness actually changed, setting the brightness
11545 * through kbdlight_set_level() also triggers this event.
11546 */
11547 brightness = kbdlight_sysfs_get(NULL);
11548 if (kbdlight_brightness != brightness) {
11549 kbdlight_brightness = brightness;
11550 led_classdev_notify_brightness_hw_changed(
11551 &tpacpi_led_kbdlight.led_classdev, brightness);
11552 }
11553
11554 mutex_unlock(&kbdlight_mutex);
11555 }
11556 /* Key events are suppressed by default hotkey_user_mask */
11557 return false;
11558 case TP_HKEY_EV_DFR_CHANGE_ROW:
11559 adaptive_keyboard_change_row();
11560 return true;
11561 case TP_HKEY_EV_DFR_S_QUICKVIEW_ROW:
11562 adaptive_keyboard_s_quickview_row();
11563 return true;
11564 case TP_HKEY_EV_THM_CSM_COMPLETED:
11565 lapsensor_refresh();
11566 /* If we are already accessing DYTC then skip dytc update */
11567 if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
11568 dytc_profile_refresh();
11569
11570 return true;
11571 case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
11572 if (lcdshadow_dev) {
11573 enum drm_privacy_screen_status old_hw_state;
11574 bool changed;
11575
11576 mutex_lock(&lcdshadow_dev->lock);
11577 old_hw_state = lcdshadow_dev->hw_state;
11578 lcdshadow_get_hw_state(lcdshadow_dev);
11579 changed = lcdshadow_dev->hw_state != old_hw_state;
11580 mutex_unlock(&lcdshadow_dev->lock);
11581
11582 if (changed)
11583 drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
11584 }
11585 return true;
11586 case TP_HKEY_EV_AMT_TOGGLE:
11587 /* If we're enabling AMT we need to force balanced mode */
11588 if (!dytc_amt_active)
11589 /* This will also set AMT mode enabled */
11590 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
11591 else
11592 dytc_control_amt(!dytc_amt_active);
11593
11594 return true;
11595 case TP_HKEY_EV_CAMERASHUTTER_TOGGLE:
11596 camera_shutter_state = get_camera_shutter();
11597 if (camera_shutter_state < 0) {
11598 pr_err("Error retrieving camera shutter state after shutter event\n");
11599 return true;
11600 }
11601 mutex_lock(&tpacpi_inputdev_send_mutex);
11602
11603 input_report_switch(tpacpi_inputdev, SW_CAMERA_LENS_COVER, camera_shutter_state);
11604 input_sync(tpacpi_inputdev);
11605
11606 mutex_unlock(&tpacpi_inputdev_send_mutex);
11607 return true;
11608 case TP_HKEY_EV_DOUBLETAP_TOGGLE:
11609 /* Toggle kernel-level doubletap event filtering */
11610 tp_features.trackpoint_doubletap_enable =
11611 !tp_features.trackpoint_doubletap_enable;
11612 return true;
11613 case TP_HKEY_EV_PROFILE_TOGGLE:
11614 case TP_HKEY_EV_PROFILE_TOGGLE2:
11615 platform_profile_cycle();
11616 return true;
11617 case TP_HKEY_EV_USB_C_SECURITY:
11618 return tpacpi_usbc_security_hotkey();
11619 }
11620
11621 return false;
11622 }
11623
11624 /* --------------------------------------------------------------------- */
11625
11626 /* /proc support */
11627 static struct proc_dir_entry *proc_dir;
11628
11629 /*
11630 * Module and infrastructure proble, init and exit handling
11631 */
11632
11633 static bool force_load;
11634
11635 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
str_supported(int is_supported)11636 static const char * __init str_supported(int is_supported)
11637 {
11638 static char text_unsupported[] __initdata = "not supported";
11639
11640 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
11641 }
11642 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
11643
11644 static struct dentry *tpacpi_dbg;
tpacpi_debugfs_init(void)11645 static void tpacpi_debugfs_init(void)
11646 {
11647 tpacpi_dbg = debugfs_create_dir("tpacpi", NULL);
11648
11649 /* HWDD raw data */
11650 debugfs_create_file("hwdd-raw", 0444, tpacpi_dbg, NULL, &hwdd_raw_fops);
11651 }
11652
tpacpi_debugfs_remove(void)11653 static void tpacpi_debugfs_remove(void)
11654 {
11655 debugfs_remove_recursive(tpacpi_dbg);
11656 }
11657
ibm_exit(struct ibm_struct * ibm)11658 static void ibm_exit(struct ibm_struct *ibm)
11659 {
11660 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
11661
11662 list_del_init(&ibm->all_drivers);
11663
11664 if (ibm->flags.acpi_notify_installed) {
11665 dbg_printk(TPACPI_DBG_EXIT,
11666 "%s: acpi_remove_notify_handler\n", ibm->name);
11667 BUG_ON(!ibm->acpi);
11668 acpi_remove_notify_handler(*ibm->acpi->handle,
11669 ibm->acpi->type,
11670 dispatch_acpi_notify);
11671 ibm->acpi->device->driver_data = NULL;
11672 acpi_dev_put(ibm->acpi->device);
11673 ibm->flags.acpi_notify_installed = 0;
11674 }
11675
11676 if (ibm->flags.proc_created) {
11677 dbg_printk(TPACPI_DBG_EXIT,
11678 "%s: remove_proc_entry\n", ibm->name);
11679 remove_proc_entry(ibm->name, proc_dir);
11680 ibm->flags.proc_created = 0;
11681 }
11682
11683 if (ibm->flags.init_called && ibm->exit) {
11684 ibm->exit();
11685 ibm->flags.init_called = 0;
11686 }
11687
11688 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
11689 }
11690
ibm_init(struct ibm_init_struct * iibm)11691 static int __init ibm_init(struct ibm_init_struct *iibm)
11692 {
11693 int ret;
11694 struct ibm_struct *ibm = iibm->data;
11695 struct proc_dir_entry *entry;
11696
11697 BUG_ON(ibm == NULL);
11698
11699 INIT_LIST_HEAD(&ibm->all_drivers);
11700
11701 if (ibm->flags.experimental && !experimental)
11702 return 0;
11703
11704 dbg_printk(TPACPI_DBG_INIT,
11705 "probing for %s\n", ibm->name);
11706
11707 if (iibm->init) {
11708 ret = iibm->init(iibm);
11709 if (ret > 0 || ret == -ENODEV)
11710 return 0; /* subdriver functionality not available */
11711 if (ret)
11712 return ret;
11713
11714 ibm->flags.init_called = 1;
11715 }
11716
11717 if (ibm->acpi) {
11718 if (ibm->acpi->notify) {
11719 ret = setup_acpi_notify(ibm);
11720 if (ret == -ENODEV) {
11721 pr_notice("disabling subdriver %s\n",
11722 ibm->name);
11723 ret = 0;
11724 goto err_out;
11725 }
11726 if (ret < 0)
11727 goto err_out;
11728 }
11729 }
11730
11731 dbg_printk(TPACPI_DBG_INIT,
11732 "%s installed\n", ibm->name);
11733
11734 if (ibm->read) {
11735 umode_t mode = iibm->base_procfs_mode;
11736
11737 if (!mode)
11738 mode = S_IRUGO;
11739 if (ibm->write)
11740 mode |= S_IWUSR;
11741 entry = proc_create_data(ibm->name, mode, proc_dir,
11742 &dispatch_proc_ops, ibm);
11743 if (!entry) {
11744 pr_err("unable to create proc entry %s\n", ibm->name);
11745 ret = -ENODEV;
11746 goto err_out;
11747 }
11748 ibm->flags.proc_created = 1;
11749 }
11750
11751 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
11752
11753 return 0;
11754
11755 err_out:
11756 dbg_printk(TPACPI_DBG_INIT,
11757 "%s: at error exit path with result %d\n",
11758 ibm->name, ret);
11759
11760 ibm_exit(ibm);
11761 return (ret < 0) ? ret : 0;
11762 }
11763
11764 /* Probing */
11765
tpacpi_parse_fw_id(const char * const s,u32 * model,u16 * release)11766 static char __init tpacpi_parse_fw_id(const char * const s,
11767 u32 *model, u16 *release)
11768 {
11769 int i;
11770
11771 if (!s || strlen(s) < 8)
11772 goto invalid;
11773
11774 for (i = 0; i < 8; i++)
11775 if (!((s[i] >= '0' && s[i] <= '9') ||
11776 (s[i] >= 'A' && s[i] <= 'Z')))
11777 goto invalid;
11778
11779 /*
11780 * Most models: xxyTkkWW (#.##c)
11781 * Ancient 570/600 and -SL lacks (#.##c)
11782 */
11783 if (s[3] == 'T' || s[3] == 'N') {
11784 *model = TPID(s[0], s[1]);
11785 *release = TPVER(s[4], s[5]);
11786 return s[2];
11787
11788 /* New models: xxxyTkkW (#.##c); T550 and some others */
11789 } else if (s[4] == 'T' || s[4] == 'N') {
11790 *model = TPID3(s[0], s[1], s[2]);
11791 *release = TPVER(s[5], s[6]);
11792 return s[3];
11793 }
11794
11795 invalid:
11796 return '\0';
11797 }
11798
11799 #define EC_FW_STRING_LEN 18
11800
find_new_ec_fwstr(const struct dmi_header * dm,void * private)11801 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
11802 {
11803 char *ec_fw_string = (char *) private;
11804 const char *dmi_data = (const char *)dm;
11805 /*
11806 * ThinkPad Embedded Controller Program Table on newer models
11807 *
11808 * Offset | Name | Width | Description
11809 * ----------------------------------------------------
11810 * 0x00 | Type | BYTE | 0x8C
11811 * 0x01 | Length | BYTE |
11812 * 0x02 | Handle | WORD | Varies
11813 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
11814 * 0x0A | OEM struct offset | BYTE | 0x0B
11815 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
11816 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
11817 * 0x0D | ECP version ID | STR ID |
11818 * 0x0E | ECP release date | STR ID |
11819 */
11820
11821 /* Return if data structure not match */
11822 if (dm->type != 140 || dm->length < 0x0F ||
11823 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
11824 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
11825 dmi_data[0x0C] != 0x01)
11826 return;
11827
11828 /* fwstr is the first 8byte string */
11829 BUILD_BUG_ON(EC_FW_STRING_LEN <= 8);
11830 memcpy(ec_fw_string, dmi_data + 0x0F, 8);
11831 }
11832
11833 /* returns 0 - probe ok, or < 0 - probe error.
11834 * Probe ok doesn't mean thinkpad found.
11835 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
get_thinkpad_model_data(struct thinkpad_id_data * tp)11836 static int __must_check __init get_thinkpad_model_data(
11837 struct thinkpad_id_data *tp)
11838 {
11839 const struct dmi_device *dev = NULL;
11840 char ec_fw_string[EC_FW_STRING_LEN] = {0};
11841 char const *s;
11842 char t;
11843
11844 if (!tp)
11845 return -EINVAL;
11846
11847 memset(tp, 0, sizeof(*tp));
11848
11849 if (dmi_name_in_vendors("IBM"))
11850 tp->vendor = PCI_VENDOR_ID_IBM;
11851 else if (dmi_name_in_vendors("LENOVO"))
11852 tp->vendor = PCI_VENDOR_ID_LENOVO;
11853 else if (dmi_name_in_vendors("NEC"))
11854 tp->vendor = PCI_VENDOR_ID_LENOVO;
11855 else
11856 return 0;
11857
11858 s = dmi_get_system_info(DMI_BIOS_VERSION);
11859 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
11860 if (s && !tp->bios_version_str)
11861 return -ENOMEM;
11862
11863 /* Really ancient ThinkPad 240X will fail this, which is fine */
11864 t = tpacpi_parse_fw_id(tp->bios_version_str,
11865 &tp->bios_model, &tp->bios_release);
11866 if (t != 'E' && t != 'C')
11867 return 0;
11868
11869 /*
11870 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
11871 * X32 or newer, all Z series; Some models must have an
11872 * up-to-date BIOS or they will not be detected.
11873 *
11874 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
11875 */
11876 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
11877 if (sscanf(dev->name,
11878 "IBM ThinkPad Embedded Controller -[%17c",
11879 ec_fw_string) == 1) {
11880 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
11881 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
11882 break;
11883 }
11884 }
11885
11886 /* Newer ThinkPads have different EC program info table */
11887 if (!ec_fw_string[0])
11888 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
11889
11890 if (ec_fw_string[0]) {
11891 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
11892 if (!tp->ec_version_str)
11893 return -ENOMEM;
11894
11895 t = tpacpi_parse_fw_id(ec_fw_string,
11896 &tp->ec_model, &tp->ec_release);
11897 if (t != 'H') {
11898 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
11899 ec_fw_string);
11900 pr_notice("please report this to %s\n", TPACPI_MAIL);
11901 }
11902 }
11903
11904 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
11905 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
11906 tp->model_str = kstrdup(s, GFP_KERNEL);
11907 if (!tp->model_str)
11908 return -ENOMEM;
11909 } else {
11910 s = dmi_get_system_info(DMI_BIOS_VENDOR);
11911 if (s && !(strncasecmp(s, "Lenovo", 6))) {
11912 tp->model_str = kstrdup(s, GFP_KERNEL);
11913 if (!tp->model_str)
11914 return -ENOMEM;
11915 }
11916 }
11917
11918 s = dmi_get_system_info(DMI_PRODUCT_NAME);
11919 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
11920 if (s && !tp->nummodel_str)
11921 return -ENOMEM;
11922
11923 return 0;
11924 }
11925
probe_for_thinkpad(void)11926 static int __init probe_for_thinkpad(void)
11927 {
11928 int is_thinkpad;
11929
11930 if (acpi_disabled)
11931 return -ENODEV;
11932
11933 /* It would be dangerous to run the driver in this case */
11934 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
11935 return -ENODEV;
11936
11937 /*
11938 * Non-ancient models have better DMI tagging, but very old models
11939 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
11940 */
11941 is_thinkpad = (thinkpad_id.model_str != NULL) ||
11942 (thinkpad_id.ec_model != 0) ||
11943 tpacpi_is_fw_known();
11944
11945 /* The EC handler is required */
11946 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
11947 if (!ec_handle) {
11948 if (is_thinkpad)
11949 pr_err("Not yet supported ThinkPad detected!\n");
11950 return -ENODEV;
11951 }
11952
11953 if (!is_thinkpad && !force_load)
11954 return -ENODEV;
11955
11956 return 0;
11957 }
11958
thinkpad_acpi_init_banner(void)11959 static void __init thinkpad_acpi_init_banner(void)
11960 {
11961 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
11962 pr_info("%s\n", TPACPI_URL);
11963
11964 pr_info("ThinkPad BIOS %s, EC %s\n",
11965 (thinkpad_id.bios_version_str) ?
11966 thinkpad_id.bios_version_str : "unknown",
11967 (thinkpad_id.ec_version_str) ?
11968 thinkpad_id.ec_version_str : "unknown");
11969
11970 BUG_ON(!thinkpad_id.vendor);
11971
11972 if (thinkpad_id.model_str)
11973 pr_info("%s %s, model %s\n",
11974 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
11975 "IBM" : ((thinkpad_id.vendor ==
11976 PCI_VENDOR_ID_LENOVO) ?
11977 "Lenovo" : "Unknown vendor"),
11978 thinkpad_id.model_str,
11979 (thinkpad_id.nummodel_str) ?
11980 thinkpad_id.nummodel_str : "unknown");
11981 }
11982
11983 /* Module init, exit, parameters */
11984
11985 static struct ibm_init_struct ibms_init[] __initdata = {
11986 {
11987 .data = &thinkpad_acpi_driver_data,
11988 },
11989 {
11990 .init = hotkey_init,
11991 .data = &hotkey_driver_data,
11992 },
11993 {
11994 .init = bluetooth_init,
11995 .data = &bluetooth_driver_data,
11996 },
11997 {
11998 .init = wan_init,
11999 .data = &wan_driver_data,
12000 },
12001 {
12002 .init = uwb_init,
12003 .data = &uwb_driver_data,
12004 },
12005 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
12006 {
12007 .init = video_init,
12008 .base_procfs_mode = S_IRUSR,
12009 .data = &video_driver_data,
12010 },
12011 #endif
12012 {
12013 .init = kbdlight_init,
12014 .data = &kbdlight_driver_data,
12015 },
12016 {
12017 .init = light_init,
12018 .data = &light_driver_data,
12019 },
12020 {
12021 .init = cmos_init,
12022 .data = &cmos_driver_data,
12023 },
12024 {
12025 .init = led_init,
12026 .data = &led_driver_data,
12027 },
12028 {
12029 .init = beep_init,
12030 .data = &beep_driver_data,
12031 },
12032 {
12033 .init = thermal_init,
12034 .data = &thermal_driver_data,
12035 },
12036 {
12037 .init = brightness_init,
12038 .data = &brightness_driver_data,
12039 },
12040 {
12041 .init = volume_init,
12042 .data = &volume_driver_data,
12043 },
12044 {
12045 .init = fan_init,
12046 .data = &fan_driver_data,
12047 },
12048 {
12049 .init = mute_led_init,
12050 .data = &mute_led_driver_data,
12051 },
12052 {
12053 .init = tpacpi_battery_init,
12054 .data = &battery_driver_data,
12055 },
12056 {
12057 .init = tpacpi_lcdshadow_init,
12058 .data = &lcdshadow_driver_data,
12059 },
12060 {
12061 .init = tpacpi_proxsensor_init,
12062 .data = &proxsensor_driver_data,
12063 },
12064 {
12065 .init = tpacpi_dytc_profile_init,
12066 .data = &dytc_profile_driver_data,
12067 },
12068 {
12069 .init = tpacpi_kbdlang_init,
12070 .data = &kbdlang_driver_data,
12071 },
12072 {
12073 .init = tpacpi_dprc_init,
12074 .data = &dprc_driver_data,
12075 },
12076 {
12077 .init = auxmac_init,
12078 .data = &auxmac_data,
12079 },
12080 {
12081 .init = tpacpi_hwdd_init,
12082 .data = &hwdd_driver_data,
12083 },
12084 {
12085 .init = tpacpi_usbc_security_init,
12086 .data = &usbc_security_driver_data,
12087 },
12088 };
12089
set_ibm_param(const char * val,const struct kernel_param * kp)12090 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
12091 {
12092 unsigned int i;
12093 struct ibm_struct *ibm;
12094
12095 if (!kp || !kp->name || !val)
12096 return -EINVAL;
12097
12098 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
12099 ibm = ibms_init[i].data;
12100 if (!ibm || !ibm->name)
12101 continue;
12102
12103 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
12104 if (strlen(val) > sizeof(ibms_init[i].param) - 1)
12105 return -ENOSPC;
12106 strscpy(ibms_init[i].param, val);
12107 return 0;
12108 }
12109 }
12110
12111 return -EINVAL;
12112 }
12113
12114 module_param(experimental, int, 0444);
12115 MODULE_PARM_DESC(experimental,
12116 "Enables experimental features when non-zero");
12117
12118 module_param_named(debug, dbg_level, uint, 0);
12119 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
12120
12121 module_param(force_load, bool, 0444);
12122 MODULE_PARM_DESC(force_load,
12123 "Attempts to load the driver even on a mis-identified ThinkPad when true");
12124
12125 module_param_named(fan_control, fan_control_allowed, bool, 0444);
12126 MODULE_PARM_DESC(fan_control,
12127 "Enables setting fan parameters features when true");
12128
12129 module_param_named(brightness_mode, brightness_mode, uint, 0444);
12130 MODULE_PARM_DESC(brightness_mode,
12131 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
12132
12133 module_param(brightness_enable, uint, 0444);
12134 MODULE_PARM_DESC(brightness_enable,
12135 "Enables backlight control when 1, disables when 0");
12136
12137 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
12138 module_param_named(volume_mode, volume_mode, uint, 0444);
12139 MODULE_PARM_DESC(volume_mode,
12140 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
12141
12142 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
12143 MODULE_PARM_DESC(volume_capabilities,
12144 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
12145
12146 module_param_named(volume_control, volume_control_allowed, bool, 0444);
12147 MODULE_PARM_DESC(volume_control,
12148 "Enables software override for the console audio control when true");
12149
12150 module_param_named(software_mute, software_mute_requested, bool, 0444);
12151 MODULE_PARM_DESC(software_mute,
12152 "Request full software mute control");
12153
12154 /* ALSA module API parameters */
12155 module_param_named(index, alsa_index, int, 0444);
12156 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
12157 module_param_named(id, alsa_id, charp, 0444);
12158 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
12159 module_param_named(enable, alsa_enable, bool, 0444);
12160 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
12161 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
12162
12163 /* The module parameter can't be read back, that's why 0 is used here */
12164 #define TPACPI_PARAM(feature) \
12165 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
12166 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
12167
12168 TPACPI_PARAM(hotkey);
12169 TPACPI_PARAM(bluetooth);
12170 TPACPI_PARAM(video);
12171 TPACPI_PARAM(light);
12172 TPACPI_PARAM(cmos);
12173 TPACPI_PARAM(led);
12174 TPACPI_PARAM(beep);
12175 TPACPI_PARAM(brightness);
12176 TPACPI_PARAM(volume);
12177 TPACPI_PARAM(fan);
12178
12179 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
12180 module_param(dbg_wlswemul, uint, 0444);
12181 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
12182 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
12183 MODULE_PARM_DESC(wlsw_state,
12184 "Initial state of the emulated WLSW switch");
12185
12186 module_param(dbg_bluetoothemul, uint, 0444);
12187 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
12188 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
12189 MODULE_PARM_DESC(bluetooth_state,
12190 "Initial state of the emulated bluetooth switch");
12191
12192 module_param(dbg_wwanemul, uint, 0444);
12193 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
12194 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
12195 MODULE_PARM_DESC(wwan_state,
12196 "Initial state of the emulated WWAN switch");
12197
12198 module_param(dbg_uwbemul, uint, 0444);
12199 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
12200 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
12201 MODULE_PARM_DESC(uwb_state,
12202 "Initial state of the emulated UWB switch");
12203 #endif
12204
12205 module_param(profile_force, int, 0444);
12206 MODULE_PARM_DESC(profile_force, "Force profile mode. -1=off, 1=MMC, 2=PSC");
12207
thinkpad_acpi_module_exit(void)12208 static void thinkpad_acpi_module_exit(void)
12209 {
12210 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
12211
12212 if (tpacpi_sensors_pdev) {
12213 platform_driver_unregister(&tpacpi_hwmon_pdriver);
12214 platform_device_unregister(tpacpi_sensors_pdev);
12215 }
12216
12217 if (tp_features.platform_drv_registered)
12218 platform_driver_unregister(&tpacpi_pdriver);
12219 if (tpacpi_pdev)
12220 platform_device_unregister(tpacpi_pdev);
12221
12222 if (proc_dir)
12223 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
12224 if (tpacpi_wq)
12225 destroy_workqueue(tpacpi_wq);
12226 if (tpacpi_dbg)
12227 tpacpi_debugfs_remove();
12228
12229 kfree(thinkpad_id.bios_version_str);
12230 kfree(thinkpad_id.ec_version_str);
12231 kfree(thinkpad_id.model_str);
12232 kfree(thinkpad_id.nummodel_str);
12233 }
12234
tpacpi_subdrivers_release(void * data)12235 static void tpacpi_subdrivers_release(void *data)
12236 {
12237 struct ibm_struct *ibm, *itmp;
12238
12239 list_for_each_entry_safe_reverse(ibm, itmp, &tpacpi_all_drivers, all_drivers)
12240 ibm_exit(ibm);
12241
12242 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
12243 }
12244
tpacpi_pdriver_probe(struct platform_device * pdev)12245 static int __init tpacpi_pdriver_probe(struct platform_device *pdev)
12246 {
12247 int ret;
12248
12249 ret = devm_mutex_init(&pdev->dev, &tpacpi_inputdev_send_mutex);
12250 if (ret)
12251 return ret;
12252
12253 tpacpi_inputdev = devm_input_allocate_device(&pdev->dev);
12254 if (!tpacpi_inputdev)
12255 return -ENOMEM;
12256
12257 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
12258 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
12259 tpacpi_inputdev->id.bustype = BUS_HOST;
12260 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
12261 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
12262 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
12263 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
12264
12265 /* Init subdriver dependencies */
12266 tpacpi_detect_brightness_capabilities();
12267
12268 /* Init subdrivers */
12269 for (unsigned int i = 0; i < ARRAY_SIZE(ibms_init); i++) {
12270 ret = ibm_init(&ibms_init[i]);
12271 if (ret >= 0 && *ibms_init[i].param)
12272 ret = ibms_init[i].data->write(ibms_init[i].param);
12273 if (ret < 0) {
12274 tpacpi_subdrivers_release(NULL);
12275 return ret;
12276 }
12277 }
12278
12279 ret = devm_add_action_or_reset(&pdev->dev, tpacpi_subdrivers_release, NULL);
12280 if (ret)
12281 return ret;
12282
12283 ret = input_register_device(tpacpi_inputdev);
12284 if (ret < 0)
12285 pr_err("unable to register input device\n");
12286
12287 return ret;
12288 }
12289
tpacpi_hwmon_pdriver_probe(struct platform_device * pdev)12290 static int __init tpacpi_hwmon_pdriver_probe(struct platform_device *pdev)
12291 {
12292 tpacpi_hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, TPACPI_NAME,
12293 NULL, tpacpi_hwmon_groups);
12294 if (IS_ERR(tpacpi_hwmon))
12295 pr_err("unable to register hwmon device\n");
12296
12297 return PTR_ERR_OR_ZERO(tpacpi_hwmon);
12298 }
12299
thinkpad_acpi_module_init(void)12300 static int __init thinkpad_acpi_module_init(void)
12301 {
12302 const struct dmi_system_id *dmi_id;
12303 int ret;
12304 acpi_object_type obj_type;
12305
12306 tpacpi_lifecycle = TPACPI_LIFE_INIT;
12307
12308 /* Driver-level probe */
12309
12310 ret = get_thinkpad_model_data(&thinkpad_id);
12311 if (ret) {
12312 pr_err("unable to get DMI data: %d\n", ret);
12313 thinkpad_acpi_module_exit();
12314 return ret;
12315 }
12316 ret = probe_for_thinkpad();
12317 if (ret) {
12318 thinkpad_acpi_module_exit();
12319 return ret;
12320 }
12321
12322 /* Driver initialization */
12323
12324 thinkpad_acpi_init_banner();
12325 tpacpi_check_outdated_fw();
12326
12327 TPACPI_ACPIHANDLE_INIT(ecrd);
12328 TPACPI_ACPIHANDLE_INIT(ecwr);
12329
12330 /*
12331 * Quirk: in some models (e.g. X380 Yoga), an object named ECRD
12332 * exists, but it is a register, not a method.
12333 */
12334 if (ecrd_handle) {
12335 acpi_get_type(ecrd_handle, &obj_type);
12336 if (obj_type != ACPI_TYPE_METHOD)
12337 ecrd_handle = NULL;
12338 }
12339 if (ecwr_handle) {
12340 acpi_get_type(ecwr_handle, &obj_type);
12341 if (obj_type != ACPI_TYPE_METHOD)
12342 ecwr_handle = NULL;
12343 }
12344
12345 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
12346 if (!tpacpi_wq) {
12347 thinkpad_acpi_module_exit();
12348 return -ENOMEM;
12349 }
12350
12351 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
12352 if (!proc_dir) {
12353 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
12354 thinkpad_acpi_module_exit();
12355 return -ENODEV;
12356 }
12357
12358 tpacpi_debugfs_init();
12359
12360 dmi_id = dmi_first_match(fwbug_list);
12361 if (dmi_id)
12362 tp_features.quirks = dmi_id->driver_data;
12363
12364 /* Device initialization */
12365 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
12366 NULL, 0);
12367 if (IS_ERR(tpacpi_pdev)) {
12368 ret = PTR_ERR(tpacpi_pdev);
12369 tpacpi_pdev = NULL;
12370 pr_err("unable to register platform device\n");
12371 thinkpad_acpi_module_exit();
12372 return ret;
12373 }
12374
12375 ret = platform_driver_probe(&tpacpi_pdriver, tpacpi_pdriver_probe);
12376 if (ret) {
12377 pr_err("unable to register main platform driver\n");
12378 thinkpad_acpi_module_exit();
12379 return ret;
12380 }
12381 tp_features.platform_drv_registered = 1;
12382
12383 tpacpi_sensors_pdev = platform_create_bundle(&tpacpi_hwmon_pdriver,
12384 tpacpi_hwmon_pdriver_probe,
12385 NULL, 0, NULL, 0);
12386 if (IS_ERR(tpacpi_sensors_pdev)) {
12387 ret = PTR_ERR(tpacpi_sensors_pdev);
12388 tpacpi_sensors_pdev = NULL;
12389 pr_err("unable to register hwmon platform device/driver bundle\n");
12390 thinkpad_acpi_module_exit();
12391 return ret;
12392 }
12393
12394 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
12395
12396 return 0;
12397 }
12398
12399 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
12400
12401 /*
12402 * This will autoload the driver in almost every ThinkPad
12403 * in widespread use.
12404 *
12405 * Only _VERY_ old models, like the 240, 240x and 570 lack
12406 * the HKEY event interface.
12407 */
12408 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
12409
12410 /*
12411 * DMI matching for module autoloading
12412 *
12413 * See https://thinkwiki.org/wiki/List_of_DMI_IDs
12414 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
12415 *
12416 * Only models listed in thinkwiki will be supported, so add yours
12417 * if it is not there yet.
12418 */
12419 #define IBM_BIOS_MODULE_ALIAS(__type) \
12420 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
12421
12422 /* Ancient thinkpad BIOSes have to be identified by
12423 * BIOS type or model number, and there are far less
12424 * BIOS types than model numbers... */
12425 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
12426
12427 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
12428 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
12429 MODULE_DESCRIPTION(TPACPI_DESC);
12430 MODULE_VERSION(TPACPI_VERSION);
12431 MODULE_LICENSE("GPL");
12432
12433 module_init(thinkpad_acpi_module_init);
12434 module_exit(thinkpad_acpi_module_exit);
12435