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