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