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