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