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